UNPKG

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