This section shows how to bundle the HERE Maps API for JavaScript with the help of several most common bundlers such as Webpack and Rollup. The sections below provide the simple boilerplate code that produces the output for the bundler of choice.
-
Add development dependencies to Rollup and a Rollup module resolution plugin to the project:
npm install rollup @rollup/plugin-node-resolve --save-dev
-
Create Rollup configuration file rollup.config.js
in the root directory of the project with the following configuration:
import resolve from '@rollup/plugin-node-resolve';
export default {
input: 'index.js',
output: {
dir: './out/',
format: 'iife'
},
onwarn: function (message) {
if (/mapsjs.bundle.js/.test(message) && /Use of eval/.test(message)) return;
console.error(message);
},
plugins: [resolve()]
};
The configuration above defines the input source and output directory for the bundled script, as well as the format of the output.
-
The Rollup was installed locally - update the script
section of the package.json
by adding the bundle
target:
"bundle": "rollup --config rollup.config.js"
-
Create an index.js
file that is the entry point for Rollup:
import H from '@here/maps-api-for-javascript';
var platform = new H.service.Platform({
'apikey': '{YOUR_API_KEY}'
});
var maptypes = platform.createDefaultLayers();
var map = new H.Map(
document.getElementById('mapContainer'),
maptypes.vector.normal.map,
{
zoom: 10,
center: { lng: 13.4, lat: 52.51 }
});
The code above repeats the steps from the Get Started chapter.
-
Add development dependencies to the Webpack
npm install webpack webpack-cli --save-dev
-
Create Webpack configuration file in the root directory of the project with the following configuration:
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
mode: 'production',
entry: {
index: './index.js'
},
output: {
publicPath: './out/',
path: path.resolve(__dirname, 'out'),
filename: '[name].js',
chunkFilename: '[name].bundle.js'
},
optimization: {
minimizer: [new TerserPlugin({
chunkFilter: (chunk) => {
if (/mapsjs/.test(chunk.name)) return false;
return true;
}
})],
}
};
In the configuration above the mapsjs.bundle.js
is explicitly excluded from the minification process. That is done in order to prevent adverse effects of the double minification. The entry module itself lazy-loads the HERE Maps API for JavaScript in order to take advantage of the Webpack's code splitting.
-
Webpack was installed locally - update the script
section of the package.json
by adding the bundle
target:
"bundle": "webpack"
-
Create an index.js
file that is the entry point for the bundler:
import( '@here/maps-api-for-javascript').then(
({ default: H }) => {
var platform = new H.service.Platform({
'apikey': '{YOUR_API_KEY}'
});
var maptypes = platform.createDefaultLayers();
var map = new H.Map(
document.getElementById('mapContainer'),
maptypes.vector.normal.map,
{
zoom: 10,
center: { lng: 13.4, lat: 52.51 }
});
}
).catch(error => console.log(error));
The code above makes use of the dynamic imports
which is the recommended way of the dynamic code splitting for the Webpack. The body of the function remains without changes.