简体   繁体   English

当与browserify捆绑在一起时,mapbox-gl-js不会呈现

[英]mapbox-gl-js won't render when bundled with browserify

I have an odd issue with mapbox-gl-js. 我对mapbox-gl-js有一个奇怪的问题。 When I add the mapbox-gl.js file as CDN to html doc head, the map renders fine. 当我将mapbox-gl.js文件作为CDN添加到html doc head时,地图呈现良好。 When I require it and bundle it with browserify as documented, the map doesn't render and I get a bundling error like this: Error: Cannot find module './feature' from '/home/.../mbtst/node_modules/mapbox-gl/dist' 当我需要它并按照记录将其与browserify捆绑在一起时,地图不会呈现,并且出现如下捆绑错误: Error: Cannot find module './feature' from '/home/.../mbtst/node_modules/mapbox-gl/dist'

The docs describe how to use a module bundler: https://www.mapbox.com/mapbox-gl-js/api/ . 该文档描述了如何使用模块捆绑器: https ://www.mapbox.com/mapbox-gl-js/api/。 At the time of posting this issue that was (it has since been changed due to this issue): 在发布此问题时(此问题由于此问题已更改):

npm install --save mapbox-gl
import mapboxgl from 'mapbox-gl/dist/mapbox-gl';
// or "const mapboxgl = require('mapbox-gl/dist/mapbox-gl');"

My map is initiated like this: 我的地图是这样启动的:

mapboxgl.accessToken = 'pk.eyJ1IjoiZWxsdnRyemVnIiwiYSI6ImNpejl4M2M0NDAxbWoycXRlanZnc283dnYifQ.sPFCSTsdlCOp1hk6afDvJg';
this.map = new mapboxgl.Map({
    container: 'map-container', // container id
    style: 'mapbox://styles/mapbox/streets-v9', 
    center: [6.16342, 62.47126], // aalesund 
    zoom: 11
});

If I just require the module as such require('mapbox-gl'); 如果我只需要这样的模块,则require('mapbox-gl'); - same error. -同样的错误。

I even tried to save the file from the CDN source and require that file - which complains about multiple dependency modules missing. 我什至尝试从CDN源保存文件并要求该文件-抱怨缺少多个依赖模块。

The code is in this repo: https://github.com/awesomemaptools/mbtst 该代码在此仓库中: https : //github.com/awesomemaptools/mbtst

PS: I need to bundle the script for offline use in a Cordova app, ie using cdn is not an option. PS:我需要捆绑脚本以在Cordova应用程序中脱机使用,即,不能使用cdn。

It turns out that it is a bug and the folks at mapbox are working to fix it, see issue here: https://github.com/mapbox/mapbox-gl-js/issues/4453 原来,这是一个错误,mapbox的工作人员正在努力修复它,请参见此处的问题: https : //github.com/mapbox/mapbox-gl-js/issues/4453

Meanwhile I upgraded from mapbox-gl version "^0.33.1" to "^0.34.0" and now it works with a workaround when including src instead of dist like this: 同时,我从mapbox-gl版本“ ^ 0.33.1”升级到“ ^ 0.34.0”,现在当包含src而不是dist时,它可以解决此问题:

const mapboxgl = require('mapbox-gl/src/index.js');

I tried that with the previous version but it didn't work then. 我在以前的版本中尝试过,但后来没有用。

The code you provided seems basically OK. 您提供的代码基本上可以。 Try to use require instead of import : 尝试使用require而不是import

const mapboxgl = require('mapbox-gl/dist/mapbox-gl');

Then call the following in the terminal: 然后在终端中调用以下命令:

browserify your-main-js-file.js > bundle.js

This adds your code and the required modules to the created JavaScript file bundle.js , so everything should be available even if your're offline. 这会将您的代码和所需的模块添加到创建的JavaScript文件bundle.js ,因此即使您处于离线状态,所有内容也应该可用。

Afterwards add the output bundle to your HTML page via script-tag: 然后通过脚本标记将输出包添加到您的HTML页面:

<script src="bundle.js"></script>

See https://github.com/substack/browserify-handbook#bundling-for-the-browser for a bit more details and check the options browserify takes to customize your bundle. 有关更多详细信息,请参见https://github.com/substack/browserify-handbook#bundling-for-the-browser ,并检查browserify用来定制软件包的选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM