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

feat: shareable webpack configs using extends #3738

Merged
merged 20 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/webpack-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ type WebpackDevServerOptions = DevServerConfig &
config: string[];
configName?: string[];
disableInterpret?: boolean;
extends?: string[];
argv: Argv;
};

Expand Down
89 changes: 83 additions & 6 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1950,17 +1950,94 @@ class WebpackCLI implements IWebpackCLI {

if (options.extends && options.extends.length > 0) {
// load the config from the extends option
options.extends = Array.isArray(options.extends) ? options.extends : [options.extends];
options.extends = await Promise.all(
const extendedConfigs = await Promise.all(
options.extends.map((configPath: string) =>
loadConfigByPath(path.resolve(configPath), options.argv),
),
);
config.options = options.extends.concat(
Array.isArray(config.options) ? config.options : [config.options],
);

options.merge = true;
const merge = await this.tryRequireThenImport<typeof webpackMerge>("webpack-merge");

const mergedConfig = extendedConfigs.reduce((accumulator: object, config) => {
return merge(accumulator, config.options);
}, {});

if (Array.isArray(config.options)) {
// merge extended config with all configs
config.options = config.options.map((options) => {
return merge(mergedConfig, options);
});
} else {
config.options = merge(mergedConfig, config.options);
}
} else if (
!Array.isArray(config.options) &&
// @ts-expect-error TODO remove when extends property type is added to webpack
config.options.extends
) {
const merge = await this.tryRequireThenImport<typeof webpackMerge>("webpack-merge");

let extendedConfig = {};

// @ts-expect-error TODO remove when extends property type is added to webpack
if (Array.isArray(config.options.extends)) {
const extendedConfigs: {
options: ConfigOptions | ConfigOptions[];
path: string;
}[] = await Promise.all(
// @ts-expect-error TODO remove when extends property type is added to webpack
config.options.extends.map((configPath: string) =>
loadConfigByPath(path.resolve(configPath), options.argv),
),
);

extendedConfig = extendedConfigs.reduce((accumulator: object, config) => {
return merge(accumulator, config.options);
}, {});
} else {
// load the config from the extends option
// @ts-expect-error TODO remove when extends property type is added to webpack
extendedConfig = loadConfigByPath(path.resolve(config.options.extends), options.argv);
}

config.options = merge(extendedConfig, config.options);
} else if (
Array.isArray(config.options) &&
// @ts-expect-error TODO remove when extends property type is added to webpack
config.options.some((options) => options.extends)
) {
const merge = await this.tryRequireThenImport<typeof webpackMerge>("webpack-merge");

for (let index = 0; index < config.options.length; index++) {
const configOptions = config.options[index];
// @ts-expect-error TODO remove when extends property type is added to webpack
if (configOptions.extends) {
let extendedConfig = {};

// @ts-expect-error TODO remove when extends property type is added to webpack
if (Array.isArray(configOptions.extends)) {
const extendedConfigs: {
options: ConfigOptions | ConfigOptions[];
path: string;
}[] = await Promise.all(
// @ts-expect-error TODO remove when extends property type is added to webpack
configOptions.extends.map((configPath: string) =>
loadConfigByPath(path.resolve(configPath), options.argv),
),
);

extendedConfig = extendedConfigs.reduce((accumulator: object, config) => {
return merge(accumulator, config.options);
}, {});
} else {
// load the config from the extends option
// @ts-expect-error TODO remove when extends property type is added to webpack
extendedConfig = loadConfigByPath(path.resolve(configOptions.extends), options.argv);
}

config.options[index] = merge(extendedConfig, configOptions);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move it to own function and just use && { extend: string[] } to avoid ts-except-error

}

if (options.merge) {
Expand Down