UNPKG

2.06 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5"use strict";
6
7const MultiEntryDependency = require("./dependencies/MultiEntryDependency");
8const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
9const MultiModuleFactory = require("./MultiModuleFactory");
10
11/** @typedef {import("./Compiler")} Compiler */
12
13class MultiEntryPlugin {
14 /**
15 * The MultiEntryPlugin is invoked whenever this.options.entry value is an array of paths
16 * @param {string} context context path
17 * @param {string[]} entries array of entry paths
18 * @param {string} name entry key name
19 */
20 constructor(context, entries, name) {
21 this.context = context;
22 this.entries = entries;
23 this.name = name;
24 }
25
26 /**
27 * @param {Compiler} compiler the compiler instance
28 * @returns {void}
29 */
30 apply(compiler) {
31 compiler.hooks.compilation.tap(
32 "MultiEntryPlugin",
33 (compilation, { normalModuleFactory }) => {
34 const multiModuleFactory = new MultiModuleFactory();
35
36 compilation.dependencyFactories.set(
37 MultiEntryDependency,
38 multiModuleFactory
39 );
40 compilation.dependencyFactories.set(
41 SingleEntryDependency,
42 normalModuleFactory
43 );
44 }
45 );
46
47 compiler.hooks.make.tapAsync(
48 "MultiEntryPlugin",
49 (compilation, callback) => {
50 const { context, entries, name } = this;
51
52 const dep = MultiEntryPlugin.createDependency(entries, name);
53 compilation.addEntry(context, dep, name, callback);
54 }
55 );
56 }
57
58 /**
59 * @param {string[]} entries each entry path string
60 * @param {string} name name of the entry
61 * @returns {MultiEntryDependency} returns a constructed Dependency
62 */
63 static createDependency(entries, name) {
64 return new MultiEntryDependency(
65 entries.map((e, idx) => {
66 const dep = new SingleEntryDependency(e);
67 // Because entrypoints are not dependencies found in an
68 // existing module, we give it a synthetic id
69 dep.loc = {
70 name,
71 index: idx
72 };
73 return dep;
74 }),
75 name
76 );
77 }
78}
79
80module.exports = MultiEntryPlugin;