Skip to content

Instantly share code, notes, and snippets.

@tschaub
Last active January 20, 2023 14:23
Show Gist options
  • Save tschaub/79025aef325cd2837364400a105405b8 to your computer and use it in GitHub Desktop.
Save tschaub/79025aef325cd2837364400a105405b8 to your computer and use it in GitHub Desktop.
OpenLayers + Webpack
/node_modules/
bundle.js

OpenLayers + Webpack

This example demonstrates how the ol package can be used with webpack 2.

Clone the project.

git clone [email protected]:79025aef325cd2837364400a105405b8.git ol-webpack

Install the project dependencies.

cd ol-webpack
npm install

Create a bundle for the browser.

npm run build

Open index.html to see the result.

open index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Using OpenLayers with Webpack</title>
<link rel="stylesheet" href="https://openlayers.org/en/latest/css/ol.css" type="text/css">
<style>
html, body {
margin: 0;
height: 100%;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="./bundle.js"></script>
</body>
</html>
import {Map, View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
new Map({
target: 'map',
layers: [
new TileLayer({
source: new XYZ({
url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
})
})
],
view: new View({
center: [0, 0],
zoom: 2
})
});
{
"name": "ol-webpack",
"version": "1.0.0",
"description": "Example using OpenLayers with Webpack",
"scripts": {
"build": "webpack --config webpack.config.js --mode production"
},
"devDependencies": {
"webpack": "^4.12.1",
"webpack-command": "^0.3.0"
},
"dependencies": {
"ol": "^5.0.0"
}
}
const webpack = require('webpack');
module.exports = {
entry: './main.js',
output: {
path: __dirname,
filename: 'bundle.js'
},
};
@hutzelknecht
Copy link

hutzelknecht commented Jan 18, 2023

works for me on node 16.11 and npm 8

(base) micha@thinkpad:~/dev$ cd ol-webpack/
(base) micha@thinkpad:~/dev/ol-webpack$ ls
index.html  package.json       readme.md
main.js     package-lock.json  webpack.config.js
(base) micha@thinkpad:~/dev/ol-webpack$ npm i

added 133 packages, and audited 134 packages in 4s

18 packages are looking for funding
  run `npm fund` for details

2 vulnerabilities (1 high, 1 critical)

To address all issues, run:
  npm audit fix

Run `npm audit` for details.
npm notice 
npm notice New major version of npm available! 8.0.0 -> 9.3.1
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.3.1
npm notice Run npm install -g [email protected] to update!
npm notice 
(base) micha@thinkpad:~/dev/ol-webpack$ npm run build

> [email protected] build
> wp --config webpack.config.js

⬡ webpack: Build Finished
⬡ webpack: asset bundle.js 182 KiB [emitted] [minimized] (name: main)
  orphan modules 1.7 MiB [orphan] 211 modules
  ./main.js + 137 modules 899 KiB [built] [code generated]
  webpack 5.58.1 compiled successfully in 5252 ms 

(base) micha@thinkpad:~/dev/ol-webpack$ ls
bundle.js   main.js       package.json       readme.md
index.html  node_modules  package-lock.json  webpack.config.js
(base) micha@thinkpad:~/dev/ol-webpack$ node -v
v16.11.0
(base) micha@thinkpad:~/dev/ol-webpack$ npm -v
8.0.0

@eflowbeach
Copy link

It may be because I don't have admin rights for node/npm and have them set up locally.
For context:
$ npm -v
8.19.3

$ node -v
v18.13.0

@tschaub
Copy link
Author

tschaub commented Jan 18, 2023

For a more updated setup, see https://github.com/openlayers/ol-webpack.

You can also get a project set up using the template above with this:

npx create-ol-app my-app --template webpack

See https://github.com/openlayers/create-ol-app for more detail.

@eflowbeach
Copy link

@tschaub Oh, thanks! I tried that version too and got:
$ npm run build

[email protected] build
wp --config webpack.config.js

Error: error:0308010C:digital envelope routines::unsupported
at new Hash (node:internal/crypto/hash:71:19)
at Object.createHash (node:crypto:133:10)

It may be something with my versions of Node and npm...
https://stackoverflow.com/questions/73144960/error-error0308010cdigital-envelope-routinesunsupported-at-new-hash-nodei

Not a big deal as I'm just trying to see how easy it is to build an OpenLayers project with WebPack.

@tschaub
Copy link
Author

tschaub commented Jan 18, 2023

@eflowbeach - Give it a try again now. It looks like webpack-nano no longer works (openlayers/ol-webpack#61 uses webpack-cli instead). The setup in that repo is very bare bones. You can use a dev server, configure loaders, etc. just as you would with another webpack project.

If you aren't already attached to webpack, I'd suggest vite as an alternative (see https://github.com/openlayers/ol-vite).

@eflowbeach
Copy link

@tschaub thanks! I tried again with no luck. It's probably because of my lack of administrative priviledges with my particular machine with regards to npm libraries. I tried Vite and it worked great! Thanks for the suggestion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment