Skip to content

use modify entry plugin in after env hook #278

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
56 changes: 36 additions & 20 deletions libs/mf/src/utils/modify-entry-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
const PLUGIN_NAME = 'modify-entry-plugin';
export class ModifyEntryPlugin {
config: unknown;
constructor(config) {
this.config = config;
}

apply(compiler) {
const mergeEntry = (keyFn, key) => [
...(keyFn(this.config[key]) || []),
...(keyFn(compiler.options.entry[key]) || []),
];
const cfgOrRemove = (objFn, valueFn, key) => {
const values = mergeEntry(valueFn, key);
return values.length > 0 ? objFn(values) : {};
};
Object.keys(this.config).forEach((key) => {
compiler.options.entry[key] = {
...cfgOrRemove(
(v) => ({ import: v }),
(c) => c.import,
key
),
...cfgOrRemove(
(v) => ({ dependOn: v }),
(c) => c.dependOn,
key
),
compiler.hooks.afterEnvironment.tap(PLUGIN_NAME, () => {
const webpackOptions = compiler.options;
const entry =
typeof webpackOptions.entry === 'function'
? webpackOptions.entry()
: webpackOptions.entry;

webpackOptions.entry = async () => {
const entries = await entry;

const mergeEntry = (keyFn, key) => [
...(keyFn(this.config[key]) || []),
...(keyFn(entries[key]) || []),
];
const cfgOrRemove = (objFn, valueFn, key) => {
const values = mergeEntry(valueFn, key);
return values.length > 0 ? objFn(values) : {};
};

Object.keys(this.config).forEach((key) => {
entries[key] = {
...cfgOrRemove(
(v) => ({ import: v }),
(c) => c.import,
key
),
...cfgOrRemove(
(v) => ({ dependOn: v }),
(c) => c.dependOn,
key
),
};
});

return entries;
};
});
}
Expand Down