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'
},
};
@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