From 1fe11b05e285e4e9663b51d4aecfa2a74dd1c32a Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Tue, 28 Nov 2023 14:40:20 -0800 Subject: [PATCH 1/2] fix: handle webpack configs exported as default --- packages/plugin/webpack/src/WebpackConfig.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/plugin/webpack/src/WebpackConfig.ts b/packages/plugin/webpack/src/WebpackConfig.ts index ee409ce74e..c1030dfd0d 100644 --- a/packages/plugin/webpack/src/WebpackConfig.ts +++ b/packages/plugin/webpack/src/WebpackConfig.ts @@ -71,12 +71,18 @@ export default class WebpackConfigGenerator { } async resolveConfig(config: Configuration | ConfigurationFactory | string): Promise { - const rawConfig = + type MaybeESM = T | { default: T }; + + let rawConfig = typeof config === 'string' ? // eslint-disable-next-line @typescript-eslint/no-var-requires - (require(path.resolve(this.projectDir, config)) as Configuration | ConfigurationFactory) + (require(path.resolve(this.projectDir, config)) as MaybeESM) : config; + if ('default' in rawConfig) { + rawConfig = rawConfig.default; + } + return processConfig(this.preprocessConfig, rawConfig); } From b8bc568e9114d75dc8da821041c6b5f6301cc792 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Tue, 28 Nov 2023 16:09:27 -0800 Subject: [PATCH 2/2] spec: add test --- packages/plugin/webpack/src/WebpackConfig.ts | 2 +- packages/plugin/webpack/test/WebpackConfig_spec.ts | 13 +++++++++++++ .../main_config_external/mainConfig.module.js | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 packages/plugin/webpack/test/fixtures/main_config_external/mainConfig.module.js diff --git a/packages/plugin/webpack/src/WebpackConfig.ts b/packages/plugin/webpack/src/WebpackConfig.ts index c1030dfd0d..17b023a5eb 100644 --- a/packages/plugin/webpack/src/WebpackConfig.ts +++ b/packages/plugin/webpack/src/WebpackConfig.ts @@ -79,7 +79,7 @@ export default class WebpackConfigGenerator { (require(path.resolve(this.projectDir, config)) as MaybeESM) : config; - if ('default' in rawConfig) { + if (rawConfig && typeof rawConfig === 'object' && 'default' in rawConfig) { rawConfig = rawConfig.default; } diff --git a/packages/plugin/webpack/test/WebpackConfig_spec.ts b/packages/plugin/webpack/test/WebpackConfig_spec.ts index 6a390c2824..4a7a853059 100644 --- a/packages/plugin/webpack/test/WebpackConfig_spec.ts +++ b/packages/plugin/webpack/test/WebpackConfig_spec.ts @@ -277,6 +277,19 @@ describe('WebpackConfigGenerator', () => { expect(webpackConfig.entry).to.equal(path.resolve(baseDir, 'foo/main.js')); }); + it('generates a config from a requirable transpiled module file', async () => { + const config = { + mainConfig: 'mainConfig.module.js', + renderer: { + entryPoints: [] as WebpackPluginEntryPoint[], + }, + } as WebpackPluginConfig; + const baseDir = path.resolve(__dirname, 'fixtures/main_config_external'); + const generator = new WebpackConfigGenerator(config, baseDir, true, 3000); + const webpackConfig = await generator.getMainConfig(); + expect(webpackConfig.entry).to.equal(path.resolve(baseDir, 'foo/main.js')); + }); + it('generates a config from function', async () => { const generateWebpackConfig = (webpackConfig: WebpackConfiguration) => { const config = { diff --git a/packages/plugin/webpack/test/fixtures/main_config_external/mainConfig.module.js b/packages/plugin/webpack/test/fixtures/main_config_external/mainConfig.module.js new file mode 100644 index 0000000000..90f19ceca9 --- /dev/null +++ b/packages/plugin/webpack/test/fixtures/main_config_external/mainConfig.module.js @@ -0,0 +1,3 @@ +module.exports.default = { + entry: './foo/main.js', +};