| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Naoyuki Kanezawa @nkzawa
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const EntryOptionPlugin = require("./EntryOptionPlugin");
|
|---|
| 9 | const EntryPlugin = require("./EntryPlugin");
|
|---|
| 10 | const EntryDependency = require("./dependencies/EntryDependency");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescriptionNormalized */
|
|---|
| 13 | /** @typedef {import("../declarations/WebpackOptions").EntryStatic} EntryStatic */
|
|---|
| 14 | /** @typedef {import("../declarations/WebpackOptions").EntryStaticNormalized} EntryStaticNormalized */
|
|---|
| 15 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 16 |
|
|---|
| 17 | const PLUGIN_NAME = "DynamicEntryPlugin";
|
|---|
| 18 |
|
|---|
| 19 | /** @typedef {() => EntryStatic | Promise<EntryStatic>} RawEntryDynamic */
|
|---|
| 20 | /** @typedef {() => Promise<EntryStaticNormalized>} EntryDynamic */
|
|---|
| 21 |
|
|---|
| 22 | class DynamicEntryPlugin {
|
|---|
| 23 | /**
|
|---|
| 24 | * Creates an instance of DynamicEntryPlugin.
|
|---|
| 25 | * @param {string} context the context path
|
|---|
| 26 | * @param {EntryDynamic} entry the entry value
|
|---|
| 27 | */
|
|---|
| 28 | constructor(context, entry) {
|
|---|
| 29 | /** @type {string} */
|
|---|
| 30 | this.context = context;
|
|---|
| 31 | /** @type {EntryDynamic} */
|
|---|
| 32 | this.entry = entry;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 37 | * @param {Compiler} compiler the compiler instance
|
|---|
| 38 | * @returns {void}
|
|---|
| 39 | */
|
|---|
| 40 | apply(compiler) {
|
|---|
| 41 | compiler.hooks.compilation.tap(
|
|---|
| 42 | PLUGIN_NAME,
|
|---|
| 43 | (compilation, { normalModuleFactory }) => {
|
|---|
| 44 | compilation.dependencyFactories.set(
|
|---|
| 45 | EntryDependency,
|
|---|
| 46 | normalModuleFactory
|
|---|
| 47 | );
|
|---|
| 48 | }
|
|---|
| 49 | );
|
|---|
| 50 |
|
|---|
| 51 | compiler.hooks.make.tapPromise(PLUGIN_NAME, (compilation) =>
|
|---|
| 52 | Promise.resolve(this.entry())
|
|---|
| 53 | .then((entry) => {
|
|---|
| 54 | /** @type {Promise<void>[]} */
|
|---|
| 55 | const promises = [];
|
|---|
| 56 | for (const name of Object.keys(entry)) {
|
|---|
| 57 | const desc = entry[name];
|
|---|
| 58 | const options = EntryOptionPlugin.entryDescriptionToOptions(
|
|---|
| 59 | compiler,
|
|---|
| 60 | name,
|
|---|
| 61 | desc
|
|---|
| 62 | );
|
|---|
| 63 | for (const entry of /** @type {NonNullable<EntryDescriptionNormalized["import"]>} */ (
|
|---|
| 64 | desc.import
|
|---|
| 65 | )) {
|
|---|
| 66 | promises.push(
|
|---|
| 67 | new Promise(
|
|---|
| 68 | /**
|
|---|
| 69 | * Handles the callback logic for this hook.
|
|---|
| 70 | * @param {(value?: undefined) => void} resolve resolve
|
|---|
| 71 | * @param {(reason?: Error) => void} reject reject
|
|---|
| 72 | */
|
|---|
| 73 | (resolve, reject) => {
|
|---|
| 74 | compilation.addEntry(
|
|---|
| 75 | this.context,
|
|---|
| 76 | EntryPlugin.createDependency(entry, options),
|
|---|
| 77 | options,
|
|---|
| 78 | (err) => {
|
|---|
| 79 | if (err) return reject(err);
|
|---|
| 80 | resolve();
|
|---|
| 81 | }
|
|---|
| 82 | );
|
|---|
| 83 | }
|
|---|
| 84 | )
|
|---|
| 85 | );
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 | return Promise.all(promises);
|
|---|
| 89 | })
|
|---|
| 90 | .then(() => {})
|
|---|
| 91 | );
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | module.exports = DynamicEntryPlugin;
|
|---|