Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle webpack configs exported as default #3427

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/plugin/webpack/src/WebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,18 @@ export default class WebpackConfigGenerator {
}

async resolveConfig(config: Configuration | ConfigurationFactory | string): Promise<Configuration> {
const rawConfig =
type MaybeESM<T> = 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<Configuration | ConfigurationFactory>)
: config;

if (rawConfig && typeof rawConfig === 'object' && 'default' in rawConfig) {
rawConfig = rawConfig.default;
}

return processConfig(this.preprocessConfig, rawConfig);
}

Expand Down
13 changes: 13 additions & 0 deletions packages/plugin/webpack/test/WebpackConfig_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = {
entry: './foo/main.js',
};