UNPKG

1.44 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 DllEntryPlugin = require("./DllEntryPlugin");
8const LibManifestPlugin = require("./LibManifestPlugin");
9const FlagInitialModulesAsUsedPlugin = require("./FlagInitialModulesAsUsedPlugin");
10
11const validateOptions = require("schema-utils");
12const schema = require("../schemas/plugins/DllPlugin.json");
13
14/** @typedef {import("../declarations/plugins/DllPlugin").DllPluginOptions} DllPluginOptions */
15
16class DllPlugin {
17 /**
18 * @param {DllPluginOptions} options options object
19 */
20 constructor(options) {
21 validateOptions(schema, options, "Dll Plugin");
22 this.options = options;
23 }
24
25 apply(compiler) {
26 compiler.hooks.entryOption.tap("DllPlugin", (context, entry) => {
27 const itemToPlugin = (item, name) => {
28 if (Array.isArray(item)) {
29 return new DllEntryPlugin(context, item, name);
30 }
31 throw new Error("DllPlugin: supply an Array as entry");
32 };
33 if (typeof entry === "object" && !Array.isArray(entry)) {
34 Object.keys(entry).forEach(name => {
35 itemToPlugin(entry[name], name).apply(compiler);
36 });
37 } else {
38 itemToPlugin(entry, "main").apply(compiler);
39 }
40 return true;
41 });
42 new LibManifestPlugin(this.options).apply(compiler);
43 if (!this.options.entryOnly) {
44 new FlagInitialModulesAsUsedPlugin("DllPlugin").apply(compiler);
45 }
46 }
47}
48
49module.exports = DllPlugin;