| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const EntryDependency = require("./dependencies/EntryDependency");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 11 | /** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
|
|---|
| 12 |
|
|---|
| 13 | const PLUGIN_NAME = "EntryPlugin";
|
|---|
| 14 |
|
|---|
| 15 | class EntryPlugin {
|
|---|
| 16 | /**
|
|---|
| 17 | * An entry plugin which will handle creation of the EntryDependency
|
|---|
| 18 | * @param {string} context context path
|
|---|
| 19 | * @param {string} entry entry path
|
|---|
| 20 | * @param {EntryOptions | string=} options entry options (passing a string is deprecated)
|
|---|
| 21 | */
|
|---|
| 22 | constructor(context, entry, options) {
|
|---|
| 23 | this.context = context;
|
|---|
| 24 | this.entry = entry;
|
|---|
| 25 | this.options = options || "";
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 30 | * @param {Compiler} compiler the compiler instance
|
|---|
| 31 | * @returns {void}
|
|---|
| 32 | */
|
|---|
| 33 | apply(compiler) {
|
|---|
| 34 | compiler.hooks.compilation.tap(
|
|---|
| 35 | PLUGIN_NAME,
|
|---|
| 36 | (compilation, { normalModuleFactory }) => {
|
|---|
| 37 | compilation.dependencyFactories.set(
|
|---|
| 38 | EntryDependency,
|
|---|
| 39 | normalModuleFactory
|
|---|
| 40 | );
|
|---|
| 41 | }
|
|---|
| 42 | );
|
|---|
| 43 |
|
|---|
| 44 | const { entry, options, context } = this;
|
|---|
| 45 | const dep = EntryPlugin.createDependency(entry, options);
|
|---|
| 46 |
|
|---|
| 47 | compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
|
|---|
| 48 | compilation.addEntry(context, dep, options, (err) => {
|
|---|
| 49 | callback(err);
|
|---|
| 50 | });
|
|---|
| 51 | });
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Creates a dependency.
|
|---|
| 56 | * @param {string} entry entry request
|
|---|
| 57 | * @param {EntryOptions | string} options entry options (passing string is deprecated)
|
|---|
| 58 | * @returns {EntryDependency} the dependency
|
|---|
| 59 | */
|
|---|
| 60 | static createDependency(entry, options) {
|
|---|
| 61 | const dep = new EntryDependency(entry);
|
|---|
| 62 | // TODO webpack 6 remove string option
|
|---|
| 63 | dep.loc = {
|
|---|
| 64 | name:
|
|---|
| 65 | typeof options === "object"
|
|---|
| 66 | ? /** @type {string} */ (options.name)
|
|---|
| 67 | : options
|
|---|
| 68 | };
|
|---|
| 69 | return dep;
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | module.exports = EntryPlugin;
|
|---|