| 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 DllEntryDependency = require("../dependencies/DllEntryDependency");
|
|---|
| 9 | const EntryDependency = require("../dependencies/EntryDependency");
|
|---|
| 10 | const DllModuleFactory = require("./DllModuleFactory");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 13 | /** @typedef {import("../Entrypoint").EntryOptions} EntryOptions */
|
|---|
| 14 |
|
|---|
| 15 | /** @typedef {string[]} Entries */
|
|---|
| 16 | /** @typedef {EntryOptions & { name: string }} Options */
|
|---|
| 17 |
|
|---|
| 18 | const PLUGIN_NAME = "DllEntryPlugin";
|
|---|
| 19 |
|
|---|
| 20 | class DllEntryPlugin {
|
|---|
| 21 | /**
|
|---|
| 22 | * Creates an instance of DllEntryPlugin.
|
|---|
| 23 | * @param {string} context context
|
|---|
| 24 | * @param {Entries} entries entry names
|
|---|
| 25 | * @param {Options} options options
|
|---|
| 26 | */
|
|---|
| 27 | constructor(context, entries, options) {
|
|---|
| 28 | this.context = context;
|
|---|
| 29 | this.entries = entries;
|
|---|
| 30 | this.options = options;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 35 | * @param {Compiler} compiler the compiler instance
|
|---|
| 36 | * @returns {void}
|
|---|
| 37 | */
|
|---|
| 38 | apply(compiler) {
|
|---|
| 39 | compiler.hooks.compilation.tap(
|
|---|
| 40 | PLUGIN_NAME,
|
|---|
| 41 | (compilation, { normalModuleFactory }) => {
|
|---|
| 42 | const dllModuleFactory = new DllModuleFactory();
|
|---|
| 43 | compilation.dependencyFactories.set(
|
|---|
| 44 | DllEntryDependency,
|
|---|
| 45 | dllModuleFactory
|
|---|
| 46 | );
|
|---|
| 47 | compilation.dependencyFactories.set(
|
|---|
| 48 | EntryDependency,
|
|---|
| 49 | normalModuleFactory
|
|---|
| 50 | );
|
|---|
| 51 | }
|
|---|
| 52 | );
|
|---|
| 53 | compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
|
|---|
| 54 | compilation.addEntry(
|
|---|
| 55 | this.context,
|
|---|
| 56 | new DllEntryDependency(
|
|---|
| 57 | this.entries.map((e, idx) => {
|
|---|
| 58 | const dep = new EntryDependency(e);
|
|---|
| 59 | dep.loc = {
|
|---|
| 60 | name: this.options.name,
|
|---|
| 61 | index: idx
|
|---|
| 62 | };
|
|---|
| 63 | return dep;
|
|---|
| 64 | }),
|
|---|
| 65 | this.options.name
|
|---|
| 66 | ),
|
|---|
| 67 | this.options,
|
|---|
| 68 | (error) => {
|
|---|
| 69 | if (error) return callback(error);
|
|---|
| 70 | callback();
|
|---|
| 71 | }
|
|---|
| 72 | );
|
|---|
| 73 | });
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | module.exports = DllEntryPlugin;
|
|---|