The minimal recommended setup for an application using Cesium with Webpack.
npm install
npm start
var Cesium = require('cesium/Cesium');
var viewer = new Cesium.Viewer('cesiumContainer');
import Cesium from 'cesium/Cesium';
var viewer = new Cesium.Viewer('cesiumContainer');
require('cesium/Widgets/widgets.css');
var Color = require('cesium/Core/Color');
var white = new Color.WHITE;
We've set the cesium Source location to be what's included with the cesium npm module.
var cesiumSource = path.resolve(__dirname, 'node_modules/cesium/Source');
However, you may want to use a different version of cesium, like if you've cloned the cesium source code directly. Just set the cesium location to the appropriate path.
var cesiumSource = path.resolve(__dirname, '../path/to/cesium/Source');
You could even point the cesium to a branch in github or another url in your package.json
file.
The following optimizations are recommended for building for production and will increase performance and result in smaller bundle sizes.
Since Cesium is such a large library, it's recommended to ignore unused parts of the Cesium library by using the IgnorePlugin
included with Webpack.
For example, if you are not using default Assets, prevent them from being included in the bundle:
plugins: [
// Ignore default Cesium Assets
new webpack.IgnorePlugin(/Assets/, /cesium$/),
],
You can still include ignored files in your app by requiring them explicitly:
require('cesium/Source/Assets/approximateTerrainHeights.json');
To remove pragmas like a traditional cesium release build, use the webpack-strip-block
plugin.
rules: [{
test: /\.js$/,
enforce: 'pre',
include: cesiumSource,
use: [{
loader: 'webpack-strip-block',
options: {
start: '>>includeStart(\'debug\', pragmas.debug);',
end: '>>includeEnd(\'debug\')'
}
}]
}]
Compress the final size of the bundle by minifying included JavaScript using UglifyJS with the uglifyjs-webpack-plugin
included with Webpack.
plugins: [
new webpack.optimize.UglifyJsPlugin()
]
Additionally, minify the CSS files when loaded with the css-loader
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
minimize: true
}
}
]
},
}