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 | class EntryPlugin {
|
---|
14 | /**
|
---|
15 | * An entry plugin which will handle creation of the EntryDependency
|
---|
16 | * @param {string} context context path
|
---|
17 | * @param {string} entry entry path
|
---|
18 | * @param {EntryOptions | string=} options entry options (passing a string is deprecated)
|
---|
19 | */
|
---|
20 | constructor(context, entry, options) {
|
---|
21 | this.context = context;
|
---|
22 | this.entry = entry;
|
---|
23 | this.options = options || "";
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Apply the plugin
|
---|
28 | * @param {Compiler} compiler the compiler instance
|
---|
29 | * @returns {void}
|
---|
30 | */
|
---|
31 | apply(compiler) {
|
---|
32 | compiler.hooks.compilation.tap(
|
---|
33 | "EntryPlugin",
|
---|
34 | (compilation, { normalModuleFactory }) => {
|
---|
35 | compilation.dependencyFactories.set(
|
---|
36 | EntryDependency,
|
---|
37 | normalModuleFactory
|
---|
38 | );
|
---|
39 | }
|
---|
40 | );
|
---|
41 |
|
---|
42 | const { entry, options, context } = this;
|
---|
43 | const dep = EntryPlugin.createDependency(entry, options);
|
---|
44 |
|
---|
45 | compiler.hooks.make.tapAsync("EntryPlugin", (compilation, callback) => {
|
---|
46 | compilation.addEntry(context, dep, options, err => {
|
---|
47 | callback(err);
|
---|
48 | });
|
---|
49 | });
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * @param {string} entry entry request
|
---|
54 | * @param {EntryOptions | string} options entry options (passing string is deprecated)
|
---|
55 | * @returns {EntryDependency} the dependency
|
---|
56 | */
|
---|
57 | static createDependency(entry, options) {
|
---|
58 | const dep = new EntryDependency(entry);
|
---|
59 | // TODO webpack 6 remove string option
|
---|
60 | dep.loc = {
|
---|
61 | name:
|
---|
62 | typeof options === "object"
|
---|
63 | ? /** @type {string} */ (options.name)
|
---|
64 | : options
|
---|
65 | };
|
---|
66 | return dep;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | module.exports = EntryPlugin;
|
---|