From 2ab1194844934e11dc0c6e935a261cf437705741 Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Wed, 28 Feb 2024 15:40:51 +0000 Subject: [PATCH] fix(dev-scripts): Ignore more jsdom subdependencies Add bufferutils and utf-8-validate to IgnorePlugin config when building tests. These are optional dependencies of wd, which is itself a dependency of jsdom. Also refactor how plugins config is generated to improve readability. --- plugins/dev-scripts/config/webpack.config.js | 42 ++++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/plugins/dev-scripts/config/webpack.config.js b/plugins/dev-scripts/config/webpack.config.js index 60eec94833..c340cbecac 100644 --- a/plugins/dev-scripts/config/webpack.config.js +++ b/plugins/dev-scripts/config/webpack.config.js @@ -29,6 +29,18 @@ module.exports = (env) => { let entry; let outputFile; let target = 'web'; + let plugins = [ + // Use DefinePlugin (https://webpack.js.org/plugins/define-plugin/) + // to pass the name of the package being built to the dev-tools + // playground (via plugins/dev-tools/src/playground/id.js). The + // "process.env." prefix is arbitrary: the stringified value + // gets substituted directly into the source code of that file + // at build time. + new webpack.DefinePlugin({ + 'process.env.PACKAGE_NAME': JSON.stringify(packageJson.name), + }), + ]; + if (isProduction) { // Production. if (exists('./src/index.js')) entry = './src/index.js'; @@ -51,6 +63,18 @@ module.exports = (env) => { }); outputFile = '[name].mocha.js'; target = 'node'; + // Certain optional plugins wanted by dependencies of blockly + // (jsdom want canvas, jsdom depends on ws which wants + // bufferutils and utf-8-validate) are loaded via: + // + // try {/*...*/ = require('package')} catch (e) {/*...*/} + // + // Webpack tries to satisfy the require even though it's in a + // try/catch, and issues a warning if it can't be found. + // IgnorePlugin suppresses this. + plugins.push(new webpack.IgnorePlugin({ + resourceRegExp: /^(canvas|bufferutil|utf-8-validate)$/}), + ); } return { @@ -93,23 +117,7 @@ module.exports = (env) => { // Ignore spurious warnings from source-map-loader // It can't find source maps for some Closure modules and that is expected ignoreWarnings: [/Failed to parse source map/], - plugins: [ - // Use DefinePlugin (https://webpack.js.org/plugins/define-plugin/) - // to pass the name of the package being built to the dev-tools - // playground (via plugins/dev-tools/src/playground/id.js). The - // "process.env." prefix is arbitrary: the stringified value - // gets substituted directly into the source code of that file - // at build time. - new webpack.DefinePlugin({ - 'process.env.PACKAGE_NAME': JSON.stringify(packageJson.name), - }), - // canvas should only be required by jsdom if the 'canvas' package is - // installed in package.json. Ignoring canvas require errors. - isTest && - new webpack.IgnorePlugin({ - resourceRegExp: /canvas$/, - }), - ].filter(Boolean), + plugins, externals: isProduction ? { 'blockly': {