forked from oskariorg/oskari-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
133 lines (119 loc) · 5.38 KB
/
webpack.config.js
File metadata and controls
133 lines (119 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');
const OskariConfig = require('./webpack/config.js');
const parseParams = require('./webpack/parseParams.js');
const { lstatSync, readdirSync, existsSync } = require('fs');
const generateEntries = require('./webpack/generateEntries.js');
const { DefinePlugin, NormalModuleReplacementPlugin } = require('webpack');
const CopywebpackPlugin = require('copy-webpack-plugin');
const proxyPort = 8081;
// helpers
const isDirectory = source => lstatSync(source).isDirectory();
const getDirectories = source => readdirSync(source).map(name => path.join(source, name)).filter(isDirectory);
// The path to the CesiumJS source code (normal and dev mode)
const cesiumSourceOptions = ['../@cesium/engine/Build', 'node_modules/@cesium/engine/Build'];
const cesiumTarget = 'cesium';
module.exports = (env, argv) => {
const isProd = argv.mode === 'production';
const { version, pathParam, publicPathPrefix } = parseParams(env);
// assumes applications are directories under pathParam
const appsetupPaths = getDirectories(path.resolve(pathParam));
// entries are configs for each app, plugins accumulate resource copying etc from all the apps
const { entries, plugins } = generateEntries(appsetupPaths, isProd, __dirname);
const devPath = path.resolve(process.cwd(), './node_modules/oskari-frontend/node_modules/olcs/css/olcs.css'); // works with build:dev & start:dev
const prodPath = path.resolve(process.cwd(), './node_modules/olcs/css/olcs.css'); // works with build & start
const devModeEnabled = lstatSync('./node_modules/oskari-frontend').isSymbolicLink();
const olcsPath = devModeEnabled ? devPath : prodPath;
plugins.push(new NormalModuleReplacementPlugin(/^olcs\/css\/olcs\.css$/, olcsPath));
plugins.push(new MiniCssExtractPlugin({
filename: '[name]/oskari.min.css'
}));
// Copy Cesium Assets, Widgets, and Workers to a static directory
cesiumSourceOptions
.filter(possiblePath => existsSync(path.join(__dirname, possiblePath)))
.forEach(possibleSrcPath => {
plugins.push(new CopywebpackPlugin({
patterns: [
{ from: path.join(__dirname, possibleSrcPath, '../Source/Assets'), to: cesiumTarget + '/Assets' },
// This is not available under Build folder:
// - oskari-frontend/node_modules/@cesium/engine/Source/ThirdParty/draco_decoder.wasm
{ from: path.join(__dirname, possibleSrcPath, '../Source/ThirdParty'), to: cesiumTarget + '/ThirdParty' },
{ from: path.join(__dirname, possibleSrcPath, 'Workers'), to: cesiumTarget + '/Workers' },
// { from: path.join(__dirname, possibleSrcPath, 'Widgets'), to: cesiumTarget + '/Widgets' },
// copy Cesium's minified third-party scripts
{ from: path.join(__dirname, possibleSrcPath, 'ThirdParty'), to: cesiumTarget + '/ThirdParty' }
]
}));
});
// Define relative base path in Cesium for loading assets
plugins.push(new DefinePlugin({
CESIUM_BASE_URL: JSON.stringify(`${publicPathPrefix}Oskari/dist/${version}/${cesiumTarget}`)
}));
// Common config for both prod & dev
const config = {
node: {
},
amd: {
// Enable webpack-friendly use of require in Cesium
toUrlUndefined: true
},
cache: {
type: 'filesystem'
},
mode: isProd ? 'production' : 'development',
entry: entries,
devtool: isProd ? 'source-map' : 'eval-source-map',
output: {
path: path.resolve(`dist/${version}/`),
publicPath: `${publicPathPrefix}Oskari/dist/${version}/`,
filename: '[name]/oskari.min.js',
assetModuleFilename: 'assets/[hash][ext][query]',
// Needed to compile multiline strings in Cesium
sourcePrefix: ''
},
module: {
rules: OskariConfig.getModuleRules(isProd)
},
plugins,
resolveLoader: OskariConfig.RESOLVE_LOADER,
resolve: OskariConfig.RESOLVE
};
// Mode specific config
if (isProd) {
config.optimization = {
minimizer: [
new TerserPlugin({
parallel: true,
extractComments: false,
terserOptions: {
format: { comments: false },
compress: { passes: 2 }
}
}),
new CssMinimizerWebpackPlugin({})
]
};
} else {
config.devServer = {
port: proxyPort,
client: {
logging: 'info',
overlay: false,
progress: true
},
proxy: [{
context: ['**', `!/Oskari/dist/${version}/**`, '!/Oskari/bundles/bundle.js'],
target: 'http://localhost:8080',
secure: false,
changeOrigin: true,
headers: {
'X-Forwarded-Host': 'localhost:' + proxyPort,
'X-Forwarded-Proto': 'http'
}
}]
};
}
return config;
};