aboutsummaryrefslogtreecommitdiffstats
path: root/language-web/webpack.config.js
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <marussy@mit.bme.hu>2021-06-29 19:48:24 +0200
committerLibravatar Kristóf Marussy <marussy@mit.bme.hu>2021-06-29 19:48:24 +0200
commit95a90faf2d2e099a2f4afe976c0dceb7ad2b08b9 (patch)
treeae67dc45389b1c04e90baf67bef8b4db849f0a99 /language-web/webpack.config.js
parentGenerate all files on build (diff)
downloadrefinery-95a90faf2d2e099a2f4afe976c0dceb7ad2b08b9.tar.gz
refinery-95a90faf2d2e099a2f4afe976c0dceb7ad2b08b9.tar.zst
refinery-95a90faf2d2e099a2f4afe976c0dceb7ad2b08b9.zip
Add hot module replacement for development
Diffstat (limited to 'language-web/webpack.config.js')
-rw-r--r--language-web/webpack.config.js49
1 files changed, 46 insertions, 3 deletions
diff --git a/language-web/webpack.config.js b/language-web/webpack.config.js
index ec07d4cf..f047f6c6 100644
--- a/language-web/webpack.config.js
+++ b/language-web/webpack.config.js
@@ -1,10 +1,24 @@
1const fs = require('fs');
1const path = require('path'); 2const path = require('path');
2 3
4const WebpackBeforeBuildPlugin = require('before-build-webpack');
3const HtmlWebpackPlugin = require('html-webpack-plugin'); 5const HtmlWebpackPlugin = require('html-webpack-plugin');
4const MiniCssExtractPlugin = require("mini-css-extract-plugin"); 6const MiniCssExtractPlugin = require("mini-css-extract-plugin");
5 7
6const devMode = process.env.NODE_ENV !== 'production'; 8const currentNodeEnv = process.env.NODE_ENV || 'development';
7const outputPath = path.resolve(__dirname, 'build/webpack', devMode ? 'development' : 'production'); 9const devMode = currentNodeEnv !== 'production';
10const outputPath = path.resolve(__dirname, 'build/webpack', currentNodeEnv);
11
12const portNumberOrElse = (envName, fallback) => {
13 const value = process.env[envName];
14 return value ? parseInt(value) : fallback;
15}
16const listenHost = process.env['LISTEN_HOST'] || 'localhost';
17const listenPort = portNumberOrElse('LISTEN_PORT', 1313);
18const apiHost = process.env['API_HOST'] || listenHost;
19const apiPort = portNumberOrElse('API_PORT', 1312);
20const publicHost = process.env['PUBLIC_HOST'] || listenHost;
21const publicPort = portNumberOrElse('PUBLIC_PORT', listenPort);
8 22
9module.exports = { 23module.exports = {
10 mode: devMode ? 'development' : 'production', 24 mode: devMode ? 'development' : 'production',
@@ -66,12 +80,24 @@ module.exports = {
66 images: path.resolve(__dirname, 'src/main/images'), 80 images: path.resolve(__dirname, 'src/main/images'),
67 }, 81 },
68 }, 82 },
69 devtool: devMode ? 'eval' : 'source-map', 83 devtool: devMode ? 'inline-source-map' : 'source-map',
70 optimization: { 84 optimization: {
71 splitChunks: { 85 splitChunks: {
72 chunks: 'all', 86 chunks: 'all',
73 }, 87 },
74 }, 88 },
89 devServer: {
90 contentBase: outputPath,
91 compress: true,
92 host: listenHost,
93 port: listenPort,
94 proxy: {
95 '/xtext-service': `${apiPort === 443 ? 'https' : 'http'}://${apiHost}:${apiPort}`,
96 },
97 public: `${publicHost}:${publicPort}`,
98 sockHost: publicHost,
99 sockPort: publicPort,
100 },
75 plugins: [ 101 plugins: [
76 new MiniCssExtractPlugin({ 102 new MiniCssExtractPlugin({
77 filename: '[contenthash].css', 103 filename: '[contenthash].css',
@@ -89,5 +115,22 @@ module.exports = {
89 useShortDoctype: true, 115 useShortDoctype: true,
90 }, 116 },
91 }), 117 }),
118 new WebpackBeforeBuildPlugin((stats, callback) => {
119 // https://stackoverflow.com/a/40370750
120 const newlyCreatedAssets = stats.compilation.assets;
121 const unlinked = [];
122 fs.readdir(outputPath, (err, files) => {
123 files.forEach(file => {
124 if (!newlyCreatedAssets[file]) {
125 fs.unlinkSync(path.resolve(outputPath, file));
126 unlinked.push(file);
127 }
128 });
129 if (unlinked.length > 0) {
130 console.log('Removed old assets: ', unlinked);
131 }
132 });
133 callback();
134 }, ['done']),
92 ], 135 ],
93}; 136};