diff options
Diffstat (limited to 'language-web/webpack.config.js')
-rw-r--r-- | language-web/webpack.config.js | 49 |
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 @@ | |||
1 | const fs = require('fs'); | ||
1 | const path = require('path'); | 2 | const path = require('path'); |
2 | 3 | ||
4 | const WebpackBeforeBuildPlugin = require('before-build-webpack'); | ||
3 | const HtmlWebpackPlugin = require('html-webpack-plugin'); | 5 | const HtmlWebpackPlugin = require('html-webpack-plugin'); |
4 | const MiniCssExtractPlugin = require("mini-css-extract-plugin"); | 6 | const MiniCssExtractPlugin = require("mini-css-extract-plugin"); |
5 | 7 | ||
6 | const devMode = process.env.NODE_ENV !== 'production'; | 8 | const currentNodeEnv = process.env.NODE_ENV || 'development'; |
7 | const outputPath = path.resolve(__dirname, 'build/webpack', devMode ? 'development' : 'production'); | 9 | const devMode = currentNodeEnv !== 'production'; |
10 | const outputPath = path.resolve(__dirname, 'build/webpack', currentNodeEnv); | ||
11 | |||
12 | const portNumberOrElse = (envName, fallback) => { | ||
13 | const value = process.env[envName]; | ||
14 | return value ? parseInt(value) : fallback; | ||
15 | } | ||
16 | const listenHost = process.env['LISTEN_HOST'] || 'localhost'; | ||
17 | const listenPort = portNumberOrElse('LISTEN_PORT', 1313); | ||
18 | const apiHost = process.env['API_HOST'] || listenHost; | ||
19 | const apiPort = portNumberOrElse('API_PORT', 1312); | ||
20 | const publicHost = process.env['PUBLIC_HOST'] || listenHost; | ||
21 | const publicPort = portNumberOrElse('PUBLIC_PORT', listenPort); | ||
8 | 22 | ||
9 | module.exports = { | 23 | module.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 | }; |