diff --git a/packages/plugin/webpack/src/WebpackConfig.ts b/packages/plugin/webpack/src/WebpackConfig.ts index ee409ce74e..17b023a5eb 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 (rawConfig && typeof rawConfig === 'object' && 'default' in rawConfig) { + rawConfig = rawConfig.default; + } + return processConfig(this.preprocessConfig, rawConfig); } 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', +};