Skip to content

Commit

Permalink
build(webpack): add testing env that fails on warnings #1197
Browse files Browse the repository at this point in the history
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
  • Loading branch information
MyPyDavid committed Nov 28, 2024
1 parent cde2c42 commit cbb50fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "rdmo",
"scripts": {
"build:prod": "webpack --config webpack.config.js --mode production",
"build:testing": "webpack --config webpack.config.js --mode production --env testing --fail-on-warnings",
"build": "webpack --config webpack.config.js --mode development",
"watch": "webpack --config webpack.config.js --mode development --watch"
},
Expand Down
36 changes: 33 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,54 @@ const developmentConfig = {

// special config for production
const productionConfig = {
bail: true, // Stop the build on errors
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
],
optimization: {
minimize: true,
minimizer: [new TerserPlugin()]
}
minimizer: [new TerserPlugin()],
},
}

// additional config for testing
const testingConfig = {
bail: true, // Stop on errors or critical warnings
performance: {
hints: false // Suppress performance warnings in testing
},
ignoreWarnings: [
{ message: /performance recommendations/ },
{ message: /asset size limit/ },
{ message: /entrypoint size limit/ }
]
}

// combine config depending on the provided --mode command line option
module.exports = (env, argv) => {
// Determine if testing mode is enabled
const isTesting = env && env.testing

return configList.map(config => {
if (argv.mode === 'development') {
// Return the development configuration directly
return merge(config, baseConfig, developmentConfig)
} else if (argv.mode === 'production') {
// Start with the base production configuration
let finalConfig = merge(config, baseConfig, productionConfig)

// Add testing-specific configuration if in testing mode
if (isTesting) {
finalConfig = merge(finalConfig, testingConfig)
}

return finalConfig
} else {
return merge(config, baseConfig, productionConfig)
throw new Error(
'Invalid configuration: use \'development\', \'production\', or \'--env testing\' for testing builds.'
)
}
})
}

0 comments on commit cbb50fa

Please sign in to comment.