Commit 442333d9 authored by Mike Greiling's avatar Mike Greiling

simplify and reorganize webpack config

parent 636aed1f
...@@ -13,7 +13,7 @@ const IS_PRODUCTION = process.env.NODE_ENV === 'production'; ...@@ -13,7 +13,7 @@ const IS_PRODUCTION = process.env.NODE_ENV === 'production';
const IS_DEV_SERVER = process.argv.join(' ').indexOf('webpack-dev-server') !== -1; const IS_DEV_SERVER = process.argv.join(' ').indexOf('webpack-dev-server') !== -1;
const DEV_SERVER_HOST = process.env.DEV_SERVER_HOST || 'localhost'; const DEV_SERVER_HOST = process.env.DEV_SERVER_HOST || 'localhost';
const DEV_SERVER_PORT = parseInt(process.env.DEV_SERVER_PORT, 10) || 3808; const DEV_SERVER_PORT = parseInt(process.env.DEV_SERVER_PORT, 10) || 3808;
const DEV_SERVER_LIVERELOAD = process.env.DEV_SERVER_LIVERELOAD !== 'false'; const DEV_SERVER_LIVERELOAD = IS_DEV_SERVER && process.env.DEV_SERVER_LIVERELOAD !== 'false';
const WEBPACK_REPORT = process.env.WEBPACK_REPORT; const WEBPACK_REPORT = process.env.WEBPACK_REPORT;
const NO_COMPRESSION = process.env.NO_COMPRESSION; const NO_COMPRESSION = process.env.NO_COMPRESSION;
...@@ -62,7 +62,7 @@ function generateEntries() { ...@@ -62,7 +62,7 @@ function generateEntries() {
return Object.assign(manualEntries, autoEntries); return Object.assign(manualEntries, autoEntries);
} }
const config = { module.exports = {
mode: IS_PRODUCTION ? 'production' : 'development', mode: IS_PRODUCTION ? 'production' : 'development',
context: path.join(ROOT_PATH, 'app/assets/javascripts'), context: path.join(ROOT_PATH, 'app/assets/javascripts'),
...@@ -77,36 +77,22 @@ const config = { ...@@ -77,36 +77,22 @@ const config = {
globalObject: 'this', // allow HMR and web workers to play nice globalObject: 'this', // allow HMR and web workers to play nice
}, },
devtool: IS_PRODUCTION ? 'source-map' : 'cheap-module-eval-source-map', resolve: {
extensions: ['.js'],
optimization: { alias: {
nodeEnv: false, '~': path.join(ROOT_PATH, 'app/assets/javascripts'),
runtimeChunk: 'single', emojis: path.join(ROOT_PATH, 'fixtures/emojis'),
splitChunks: { empty_states: path.join(ROOT_PATH, 'app/views/shared/empty_states'),
maxInitialRequests: 4, icons: path.join(ROOT_PATH, 'app/views/shared/icons'),
cacheGroups: { images: path.join(ROOT_PATH, 'app/assets/images'),
default: false, vendor: path.join(ROOT_PATH, 'vendor/assets/javascripts'),
common: () => ({ vue$: 'vue/dist/vue.esm.js',
priority: 20, spec: path.join(ROOT_PATH, 'spec/javascripts'),
name: 'main',
chunks: 'initial',
minChunks: autoEntriesCount * 0.9,
}),
vendors: {
priority: 10,
chunks: 'async',
test: /[\\/](node_modules|vendor[\\/]assets[\\/]javascripts)[\\/]/,
},
commons: {
chunks: 'all',
minChunks: 2,
reuseExistingChunk: true,
},
},
}, },
}, },
module: { module: {
strictExportPresence: true,
rules: [ rules: [
{ {
test: /\.js$/, test: /\.js$/,
...@@ -177,9 +163,34 @@ const config = { ...@@ -177,9 +163,34 @@ const config = {
], ],
}, },
], ],
noParse: [/monaco-editor\/\w+\/vs\//], noParse: [/monaco-editor\/\w+\/vs\//],
strictExportPresence: true, },
optimization: {
nodeEnv: false,
runtimeChunk: 'single',
splitChunks: {
maxInitialRequests: 4,
cacheGroups: {
default: false,
common: () => ({
priority: 20,
name: 'main',
chunks: 'initial',
minChunks: autoEntriesCount * 0.9,
}),
vendors: {
priority: 10,
chunks: 'async',
test: /[\\/](node_modules|vendor[\\/]assets[\\/]javascripts)[\\/]/,
},
commons: {
chunks: 'all',
minChunks: 2,
reuseExistingChunk: true,
},
},
},
}, },
plugins: [ plugins: [
...@@ -233,37 +244,52 @@ const config = { ...@@ -233,37 +244,52 @@ const config = {
}, },
}, },
]), ]),
],
resolve: { // compression can require a lot of compute time and is disabled in CI
extensions: ['.js'], IS_PRODUCTION && !NO_COMPRESSION && new CompressionPlugin(),
alias: {
'~': path.join(ROOT_PATH, 'app/assets/javascripts'),
emojis: path.join(ROOT_PATH, 'fixtures/emojis'),
empty_states: path.join(ROOT_PATH, 'app/views/shared/empty_states'),
icons: path.join(ROOT_PATH, 'app/views/shared/icons'),
images: path.join(ROOT_PATH, 'app/assets/images'),
vendor: path.join(ROOT_PATH, 'vendor/assets/javascripts'),
vue$: 'vue/dist/vue.esm.js',
spec: path.join(ROOT_PATH, 'spec/javascripts'),
},
},
// sqljs requires fs // WatchForChangesPlugin
node: { // TODO: publish this as a separate plugin
fs: 'empty', IS_DEV_SERVER && {
}, apply(compiler) {
}; compiler.hooks.emit.tapAsync('WatchForChangesPlugin', (compilation, callback) => {
const missingDeps = Array.from(compilation.missingDependencies);
const nodeModulesPath = path.join(ROOT_PATH, 'node_modules');
const hasMissingNodeModules = missingDeps.some(
file => file.indexOf(nodeModulesPath) !== -1
);
if (IS_PRODUCTION) { // watch for changes to missing node_modules
// compression can require a lot of compute time and is disabled in CI if (hasMissingNodeModules) compilation.contextDependencies.add(nodeModulesPath);
if (!NO_COMPRESSION) {
config.plugins.push(new CompressionPlugin()); // watch for changes to automatic entrypoints
} watchAutoEntries.forEach(watchPath => compilation.contextDependencies.add(watchPath));
}
if (IS_DEV_SERVER) { // report our auto-generated bundle count
config.devServer = { console.log(
`${autoEntriesCount} entries from '/pages' automatically added to webpack output.`
);
callback();
});
},
},
// enable HMR only in webpack-dev-server
DEV_SERVER_LIVERELOAD && new webpack.HotModuleReplacementPlugin(),
// optionally generate webpack bundle analysis
WEBPACK_REPORT &&
new BundleAnalyzerPlugin({
analyzerMode: 'static',
generateStatsFile: true,
openAnalyzer: false,
reportFilename: path.join(ROOT_PATH, 'webpack-report/index.html'),
statsFilename: path.join(ROOT_PATH, 'webpack-report/stats.json'),
}),
].filter(Boolean),
devServer: {
host: DEV_SERVER_HOST, host: DEV_SERVER_HOST,
port: DEV_SERVER_PORT, port: DEV_SERVER_PORT,
disableHostCheck: true, disableHostCheck: true,
...@@ -271,46 +297,10 @@ if (IS_DEV_SERVER) { ...@@ -271,46 +297,10 @@ if (IS_DEV_SERVER) {
stats: 'errors-only', stats: 'errors-only',
hot: DEV_SERVER_LIVERELOAD, hot: DEV_SERVER_LIVERELOAD,
inline: DEV_SERVER_LIVERELOAD, inline: DEV_SERVER_LIVERELOAD,
}; },
config.plugins.push({
apply(compiler) {
compiler.hooks.emit.tapAsync('WatchForChangesPlugin', (compilation, callback) => {
const missingDeps = Array.from(compilation.missingDependencies);
const nodeModulesPath = path.join(ROOT_PATH, 'node_modules');
const hasMissingNodeModules = missingDeps.some(
file => file.indexOf(nodeModulesPath) !== -1
);
// watch for changes to missing node_modules
if (hasMissingNodeModules) compilation.contextDependencies.add(nodeModulesPath);
// watch for changes to automatic entrypoints
watchAutoEntries.forEach(watchPath => compilation.contextDependencies.add(watchPath));
// report our auto-generated bundle count
console.log(
`${autoEntriesCount} entries from '/pages' automatically added to webpack output.`
);
callback();
});
},
});
if (DEV_SERVER_LIVERELOAD) {
config.plugins.push(new webpack.HotModuleReplacementPlugin());
}
}
if (WEBPACK_REPORT) { devtool: IS_PRODUCTION ? 'source-map' : 'cheap-module-eval-source-map',
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
generateStatsFile: true,
openAnalyzer: false,
reportFilename: path.join(ROOT_PATH, 'webpack-report/index.html'),
statsFilename: path.join(ROOT_PATH, 'webpack-report/stats.json'),
})
);
}
module.exports = config; // sqljs requires fs
node: { fs: 'empty' },
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment