Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update webpack to v5.xx #1258

Merged
merged 4 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 27 additions & 40 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const path = require('path');

const OptimizeCssnanoPlugin = require('@intervolga/optimize-cssnano-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const webpack = require('webpack');
const nodeModules = require('webpack-node-externals');

const enableServer = require('./server.config');

const NODE_ENV = process.env.NODE_ENV || 'development';
const assetName = ext => `assets/[hash:base32].[name].${ext || '[ext]'}`;
const assetName = ext => `assets/[contenthash:base32].[name]${ext || '[ext]'}`;

let cfg = {

Expand All @@ -18,18 +18,17 @@ let cfg = {

entry: {
main: ['./src/main.ts'],
'assets/init': ['./src/frontend/javascript/init.js'],
'assets/sankey': ['./src/frontend/javascript/sankey.js'],
init: {import: './src/frontend/javascript/init.js', filename: 'assets/init.js'},
sankey: {import: './src/frontend/javascript/sankey.js', filename: 'assets/sankey.js'}
},

output: {
path: path.resolve(__dirname, '..', 'dist'),
filename: '[name].js',
publicPath: '/',
},

devtool: 'source-map',

externalsPresets: { node: true },
externals: ['chokidar'],

stats: {
Expand All @@ -41,6 +40,9 @@ let cfg = {
},

optimization: {
minimizer: [
new CssMinimizerPlugin(),
],
splitChunks: false,
runtimeChunk: false,
},
Expand All @@ -57,36 +59,18 @@ let cfg = {
rules: [
{
test: /\.(png|jpg|ico|svg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: assetName(),
esModule: false,
},
},
],
type: 'asset/resource',
generator: {
filename: assetName()
}
},
{
test: /\.(scss)$/,
use: [
{
loader: 'file-loader',
options: {
name: assetName('css'),
esModule: false,
},
},
{
loader: 'extract-loader',
options: {
publicPath: '/',
},
},
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: NODE_ENV === 'production' ? false : true,
url: false,
},
},
Expand All @@ -98,7 +82,6 @@ let cfg = {
path.resolve(__dirname, '../node_modules'),
],
},
sourceMap: true,
},
},
],
Expand Down Expand Up @@ -130,28 +113,32 @@ let cfg = {
new CompressionPlugin({
test: /\.(js|svg|css)$/,
include: 'assets/',
deleteOriginalAssets: true,
deleteOriginalAssets: 'keep-source-map',
}),
new MiniCssExtractPlugin({
filename: 'assets/govuk.screen.css'
})
],
};

if (process.env.ENABLE_WATCH === 'true') {
cfg.watch = true;
cfg.watch = true; //possibly depricated by webpack 5 https://webpack.js.org/migrate/5/#clean-up-configuration
}

if (process.env.ENABLE_SERVER === 'true') {
cfg = enableServer(cfg);
}

if (NODE_ENV === 'production') {
cfg.plugins.push(new OptimizeCssnanoPlugin({
sourceMap: false,
cssnanoOptions: {
preset: ['default', {
discardComments: {
removeAll: true,
cfg.devtool = false;
cfg.plugins.push(new CssMinimizerPlugin({
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: true },
},
}],
],
},
}));
}
Expand Down
Loading