Skip to content

Commit 2f5eb81

Browse files
authored
chore(build): upgrade webpack 4 to webpack 5 (WEBAPP-57855) (#742)
* chore(build): upgrade webpack 4 to webpack 5 (WEBAPP-57855) Upgrade webpack and related loaders/plugins to webpack 5: - webpack 4 -> 5, webpack-cli 3 -> 5, webpack-dev-server 3 -> 5 - mini-css-extract-plugin 0.9 -> 2, css-loader 3 -> 7 - postcss-loader 3 -> 8, sass-loader 8 -> 16, style-loader 1 -> 4 - babel-loader 8 -> 9 - Replace optimize-css-assets-webpack-plugin with css-minimizer-webpack-plugin - Replace raw-loader/file-loader with webpack 5 asset modules - Remove terser-webpack-plugin (webpack 5 built-in) - Update devServer config for webpack-dev-server 5 API - Remove deprecated --colors CLI flag This unblocks WEBAPP-49218 by enabling native support for package.json exports field, required by @box/blueprint-web-assets and other modern @box/* packages. * fix(build): resolve DefinePlugin NODE_ENV conflict Webpack 5 sets process.env.NODE_ENV via mode option. Remove duplicate definition from DefinePlugin to fix conflicting values warning.
1 parent 9e16437 commit 2f5eb81

5 files changed

Lines changed: 2249 additions & 2843 deletions

File tree

package.json

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"axios": "^0.24.0",
5555
"babel-eslint": "^10.1.0",
5656
"babel-jest": "^24.9.0",
57-
"babel-loader": "^8.3.0",
57+
"babel-loader": "^9.2.1",
5858
"babel-plugin-istanbul": "^6.0.0",
5959
"babel-plugin-react-intl": "^7.5.1",
6060
"babel-plugin-transform-require-ignore": "^0.1.1",
@@ -64,7 +64,8 @@
6464
"classnames": "^2.2.5",
6565
"conventional-github-releaser": "^3.1.3",
6666
"core-js": "^3.38.1",
67-
"css-loader": "^3.5.2",
67+
"css-loader": "^7.1.2",
68+
"css-minimizer-webpack-plugin": "^7.0.4",
6869
"cypress": "^4.4.1",
6970
"draft-js": "0.10.5",
7071
"enzyme": "^3.11.0",
@@ -90,12 +91,10 @@
9091
"jest-environment-jsdom-sixteen": "^1.0.3",
9192
"lint-staged": "^9.5.0",
9293
"lodash": "^4.17.15",
93-
"mini-css-extract-plugin": "^0.9.0",
94+
"mini-css-extract-plugin": "^2.10.1",
9495
"npm-run-all": "^4.1.5",
95-
"optimize-css-assets-webpack-plugin": "^5.0.3",
96-
"postcss-loader": "^3.0.0",
96+
"postcss-loader": "^8.1.1",
9797
"prettier": "^1.19.1",
98-
"raw-loader": "^4.0.1",
9998
"react": "18.3.1",
10099
"react-dom": "18.3.1",
101100
"react-intl": "6.4.2",
@@ -105,27 +104,26 @@
105104
"redux": "^4.0.5",
106105
"regenerator-runtime": "^0.13.9",
107106
"sass": "^1.64.1",
108-
"sass-loader": "^8.0.2",
107+
"sass-loader": "^16.0.4",
109108
"scroll-into-view-if-needed": "^2.2.24",
110-
"style-loader": "^1.1.4",
109+
"style-loader": "^4.0.0",
111110
"stylelint": "^12.0.0",
112111
"stylelint-config-rational-order": "^0.1.2",
113112
"stylelint-config-standard": "^19.0.0",
114113
"stylelint-order": "^3.1.1",
115114
"tabbable": "^1.1.3",
116-
"terser-webpack-plugin": "^4.2.3",
117115
"typescript": "4.9.5",
118116
"uuid": "^8.3.1",
119117
"wait-on": "^4.0.2",
120-
"webpack": "^4.47.0",
121-
"webpack-cli": "^3.3.11",
122-
"webpack-dev-server": "^3.10.3",
118+
"webpack": "^5.105.4",
119+
"webpack-cli": "^5.1.4",
120+
"webpack-dev-server": "^5.2.3",
123121
"worker-farm": "^1.7.0"
124122
},
125123
"scripts": {
126124
"build": "yarn setup && yarn build:prod:dist",
127125
"build:i18n": "props2es",
128-
"build:dev:dist": "LANGUAGE=en-US BABEL_ENV=dev NODE_ENV=dev webpack --config scripts/webpack.config.js --mode development --progress --colors",
126+
"build:dev:dist": "LANGUAGE=en-US BABEL_ENV=dev NODE_ENV=dev webpack --config scripts/webpack.config.js --mode development --progress",
129127
"build:prod:dist": "LANGUAGE=en-US BABEL_ENV=production NODE_ENV=production webpack --config scripts/webpack.config.js --mode production",
130128
"clean": "rm -rf dist reports/* i18n/json i18n/*.js",
131129
"cy:open": "yarn cy:wait; yarn cypress open",

scripts/webpack.common.config.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ module.exports = () => {
2828
},
2929
{
3030
test: /\.(svg|html)$/,
31-
loader: 'raw-loader',
31+
type: 'asset/source',
3232
exclude: [path.resolve('node_modules')],
3333
},
3434
{
3535
test: /\.(jpe?g|png|gif|woff2|woff)$/,
36-
loader: 'file-loader',
37-
options: {
38-
name: '[name].[ext]',
36+
type: 'asset/resource',
37+
generator: {
38+
filename: '[name][ext]',
3939
},
4040
exclude: [path.resolve('node_modules')],
4141
},
@@ -50,10 +50,7 @@ module.exports = () => {
5050
__LANGUAGE__: JSON.stringify(language),
5151
__NAME__: JSON.stringify(packageJSON.name),
5252
__VERSION__: JSON.stringify(version),
53-
'process.env': {
54-
BABEL_ENV: JSON.stringify(process.env.BABEL_ENV),
55-
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
56-
},
53+
'process.env.BABEL_ENV': JSON.stringify(process.env.BABEL_ENV),
5754
}),
5855
new MiniCssExtractPlugin({
5956
filename: '[name].css',

scripts/webpack.config.js

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const fs = require('fs');
22
const path = require('path');
33
const CircularDependencyPlugin = require('circular-dependency-plugin');
4-
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
4+
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
55
const RsyncPlugin = require('@box/frontend/webpack/RsyncPlugin');
66
const TranslationsPlugin = require('@box/frontend/webpack/TranslationsPlugin');
7-
const TerserPlugin = require('terser-webpack-plugin');
87

98
const { BannerPlugin } = require('webpack');
109

@@ -31,7 +30,7 @@ const config = Object.assign(commonConfig(), {
3130
annotations: ['./src/BoxAnnotations.ts'],
3231
},
3332
output: {
34-
filename: '[Name].js',
33+
filename: '[name].js',
3534
path: path.resolve('dist'),
3635
},
3736
resolve: {
@@ -49,10 +48,9 @@ const config = Object.assign(commonConfig(), {
4948
modules: ['src', 'node_modules'],
5049
},
5150
devServer: {
52-
contentBase: './test',
53-
disableHostCheck: true,
51+
static: './test',
52+
allowedHosts: 'all',
5453
host: '0.0.0.0',
55-
inline: true,
5654
port: 8001,
5755
},
5856
});
@@ -62,7 +60,7 @@ if (isDev) {
6260
config.plugins.push(new RsyncPlugin('dist/.', rsyncLocation, 'annotations'));
6361
}
6462

65-
config.devtool = isLinked ? 'cheap-module-eval-source-map' : 'source-map';
63+
config.devtool = isLinked ? 'eval-cheap-module-source-map' : 'source-map';
6664
config.plugins.push(new TranslationsPlugin());
6765
config.plugins.push(
6866
new CircularDependencyPlugin({
@@ -73,34 +71,13 @@ if (isDev) {
7371
}
7472

7573
if (isRelease && language === 'en-US') {
76-
// https://webpack.js.org/configuration/optimization/#optimization-minimize
7774
config.optimization = {
7875
minimizer: [
79-
new TerserPlugin({
80-
terserOptions: {
81-
warnings: false, // Don't output warnings
82-
compress: {
83-
drop_console: true, // Drop console statements
84-
},
85-
output: {
86-
comments: false, // Remove comments
87-
},
88-
},
89-
sourceMap: false,
90-
}),
76+
'...',
77+
new CssMinimizerPlugin(),
9178
],
9279
};
9380

94-
// Optimize CSS - minimize, remove comments and duplicate rules
95-
config.plugins.push(
96-
new OptimizeCssAssetsPlugin({
97-
cssProcessorOptions: {
98-
discardComments: { removeAll: true },
99-
safe: true,
100-
},
101-
}),
102-
);
103-
10481
// Add license message to top of code
10582
config.plugins.push(new BannerPlugin(license));
10683
}

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"compilerOptions": {
55
"lib": ["DOM", "ES2020"],
66
"jsx": "react-jsx",
7-
"esModuleInterop": true
7+
"esModuleInterop": true,
8+
"skipLibCheck": true,
9+
"types": ["classnames", "draft-js", "enzyme", "jest", "lodash", "react", "react-dom", "react-redux", "uuid"]
810
}
911
}

0 commit comments

Comments
 (0)