From 44e43df615baf50f826e60f9037f6f878585d0e4 Mon Sep 17 00:00:00 2001 From: John Dillick Date: Wed, 23 Mar 2022 22:43:34 -0400 Subject: [PATCH] Add webpack sdk resolveAliases registry as well as transpiledDependencies registry. --- .core/webpack.sdk.js | 57 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/.core/webpack.sdk.js b/.core/webpack.sdk.js index c82493af..2534b9a9 100644 --- a/.core/webpack.sdk.js +++ b/.core/webpack.sdk.js @@ -51,6 +51,20 @@ class WebpackReactiumWebpack { minimize: false, }; + this.resolveAliases = ReactiumWebpack.Utils.registryFactory( + 'resolveAliases', + 'id', + ReactiumWebpack.Utils.Registry.MODES.CLEAN, + ); + this.resolveAliases.sdk = this; + + this.transpiledDependencies = ReactiumWebpack.Utils.registryFactory( + 'transpiledDependencies', + 'module', + ReactiumWebpack.Utils.Registry.MODES.CLEAN, + ); + this.transpiledDependencies.sdk = this; + this.ignores = ReactiumWebpack.Utils.registryFactory( 'ignores', 'id', @@ -159,6 +173,10 @@ class WebpackReactiumWebpack { return this.overridesValue || {}; } + addResolveAlias(id, alias) { + this.resolveAliases.register(id, { alias }); + } + addRule(id, rule) { this.rules.register(id, { rule }); } @@ -171,6 +189,10 @@ class WebpackReactiumWebpack { this.plugins.register(id, { plugin }); } + addTranspiledDependency(module) { + this.transpiledDependencies.register(module); + } + addContext(id, context) { const { from, to } = context; this.plugins.register(id, { @@ -365,7 +387,29 @@ class WebpackReactiumWebpack { config() { ReactiumWebpack.Hook.runSync('before-config', this); - return { + if (this.transpiledDependencies.list.length > 0) { + this.addRule('babel-loader', { + test: [/\.jsx|js($|\?)/], + exclude: [ + new RegExp( + `node_modules\/(?!(${this.transpiledDependencies.list + .map(({ module }) => module) + .join('|')})\/).*`, + ), + /umd.js$/, + ], + resolve: { + extensions: ['.js', '.jsx', '.json'], + }, + use: [ + { + loader: 'babel-loader', + }, + ], + }); + } + + const theConfig = { mode: this.mode, target: this.target, output: this.output, @@ -381,6 +425,17 @@ class WebpackReactiumWebpack { plugins: this.getPlugins(), ...this.overrides, }; + + if (this.resolveAliases.list.length > 0) { + const alias = {}; + this.resolveAliases.list.forEach(({ id: from, alias: to }) => { + alias[from] = to; + }); + theConfig.resolve = { alias }; + } + + ReactiumWebpack.Hook.runSync('after-config', theConfig, this); + return theConfig; } }