gpt4 book ai didi

javascript - 汇总 - 导入外部库

转载 作者:行者123 更新时间:2023-12-05 06:13:41 26 4
gpt4 key购买 nike

之前我是通过脚本来使用库的:

现在我使用 yarn 和 rollup 来构建带有下一个文件的项目。包.json:

{
"name": "maplib",
"version": "1.0.0",
"main": "src/main.js",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.10.5",
"@babel/node": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"@rollup/plugin-node-resolve": "^8.4.0",
"rollup": "^2.22.2"
},
"dependencies": {
"mapbox-gl": "^1.11.1"
},
"scripts": {
"build": "rollup -c"
}
}

rollup.config.js:

import resolve from '@rollup/plugin-node-resolve';

export default {
input: 'src/main.js',
external: ['mapbox-gl-dev'],
output: {
file: 'src/blazorMap/dist/MapBoxBlazor.js',
format: 'iife',
globals: {
mapboxgl: 'mapboxgl'
},
plugins: [
resolve(), // so Rollup can find `mapbox-gl`
]
}
};

和 main.js:

import * as mapboxgl from 'mapbox-gl'

export function init(o) {

this.assign(mapboxgl, "accessToken", o.accessToken);
this.mapblazor = new mapboxgl.Map(
container = o.container,
style = o.mapStyle,
center = o.mapCenter,
zoom = o.mapZoom
);
}

我收到错误信息: https://rollupjs.org/guide/en/.. .依赖mapbox-gl(由 src\main.js 导入)(!) 缺少全局变量名称使用output.globals指定外部模块对应的浏览器全局变量名mapbox-gl(猜测'mapboxgl')在 37 毫秒内创建了 src/blazorMap/dist/MapBoxBlazor.js如何解决 importin mapbox-gl 的问题?

最佳答案

您可能需要“@rollup/plugin-commonjs”,将“插件”的位置移到输出之外,并将“名称”属性设置为“输出”:

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs'

export default {
input: 'src/main.js',
external: ['mapbox-gl-dev'],
output: {
file: 'src/blazorMap/dist/MapBoxBlazor.js',
format: 'iife',
globals: {
mapboxgl: 'mapboxgl',
},
name: 'xxx'
},
plugins: [
resolve(), // so Rollup can find `mapbox-gl`
commonjs(),
],
};

关于javascript - 汇总 - 导入外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63213389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com