Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using openlayers with browserify #8497

Closed
BasieP opened this issue Aug 8, 2018 · 6 comments
Closed

using openlayers with browserify #8497

BasieP opened this issue Aug 8, 2018 · 6 comments

Comments

@BasieP
Copy link

BasieP commented Aug 8, 2018

i always used the pre-compiled version of openlayers due to the fact that the entire closure compiler was way to complicated a way to build a simple app.

Now openlayers 5 is here, so i am rebuilding my 'simple app' (a npm package) to 'the new way of life'

I removed all the local files, did an npm install ol and started with this simple piece of code:

import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import OSM from 'ol/source/OSM.js';

module.exports = function () {
	var map = new Map({
		layers: [
			new TileLayer({
				visible: false,
				preload: Infinity,
				source: new OSM()
			})
		],
		target: 'map',
		view: new View({
			center: [-6655.5402445057125, 6709968.258934638],
			zoom: 13
		})
	});
};

now as you can see i use module.exports because we use browserify. Since the new openlayers uses the ES6 style import and export functions, we need to use babel to pre-compile our javascript (including dependencies like openlayers) to Ecmascript 5.

I use this buildscript in my package.json

"build": "\"browserify src/js/test.js -t [ babelify --presets [ \"@babel/preset-env\" ] ]",

and i have the necessary dependencies for babel installed.
Still i get this error:

d:\mySimpleProject\node_modules\ol\View.js:4
import {DEFAULT_TILE_SIZE} from './tilegrid/common.js';
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'

I'm sure i'm doing something wrong but i can't get my finger on it. If i goolgle, i only find bugreports on git for other projects... not really helping.

@BasieP
Copy link
Author

BasieP commented Aug 8, 2018 via email

@ahocevar
Copy link
Member

ahocevar commented Aug 8, 2018

You need to use babelify as global transform, i.e. with the -g option instead of -t:

"build": "\"browserify src/js/test.js -g [ babelify --presets [ \"@babel/preset-env\" ] ]",

If that breaks other dependencies, configure babelify with the only option.

@aprehot
Copy link

aprehot commented Aug 8, 2018

@BasieP Can i get a copy of your repo that you are using for this? im having a similar problem as you and want to deduce it.

@BasieP
Copy link
Author

BasieP commented Aug 8, 2018 via email

@ahocevar
Copy link
Member

ahocevar commented Aug 9, 2018

I have set up an example repository that shows how to use the ol package with Browserify: https://github.com/openlayers/ol-browserify.

@BasieP
Copy link
Author

BasieP commented Aug 9, 2018

thanks man 👍
i keep going on my webpack migration, but this sure helps others a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants