UNPKG

1.6 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 DllEntryPlugin = require("./DllEntryPlugin");
9const FlagAllModulesAsUsedPlugin = require("./FlagAllModulesAsUsedPlugin");
10const LibManifestPlugin = require("./LibManifestPlugin");
11
12const { validate } = require("schema-utils");
13const schema = require("../schemas/plugins/DllPlugin.json");
14
15/** @typedef {import("../declarations/plugins/DllPlugin").DllPluginOptions} DllPluginOptions */
16/** @typedef {import("./Compiler")} Compiler */
17
18class DllPlugin {
19 /**
20 * @param {DllPluginOptions} options options object
21 */
22 constructor(options) {
23 validate(schema, options, {
24 name: "Dll Plugin",
25 baseDataPath: "options"
26 });
27 this.options = {
28 ...options,
29 entryOnly: options.entryOnly !== false
30 };
31 }
32
33 /**
34 * Apply the plugin
35 * @param {Compiler} compiler the compiler instance
36 * @returns {void}
37 */
38 apply(compiler) {
39 compiler.hooks.entryOption.tap("DllPlugin", (context, entry) => {
40 if (typeof entry !== "function") {
41 for (const name of Object.keys(entry)) {
42 const options = {
43 name,
44 filename: entry.filename
45 };
46 new DllEntryPlugin(context, entry[name].import, options).apply(
47 compiler
48 );
49 }
50 } else {
51 throw new Error(
52 "DllPlugin doesn't support dynamic entry (function) yet"
53 );
54 }
55 return true;
56 });
57 new LibManifestPlugin(this.options).apply(compiler);
58 if (!this.options.entryOnly) {
59 new FlagAllModulesAsUsedPlugin("DllPlugin").apply(compiler);
60 }
61 }
62}
63
64module.exports = DllPlugin;