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 parseJson = require("json-parse-even-better-errors");
|
---|
9 | const DelegatedModuleFactoryPlugin = require("./DelegatedModuleFactoryPlugin");
|
---|
10 | const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
|
---|
11 | const WebpackError = require("./WebpackError");
|
---|
12 | const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency");
|
---|
13 | const createSchemaValidation = require("./util/create-schema-validation");
|
---|
14 | const makePathsRelative = require("./util/identifier").makePathsRelative;
|
---|
15 |
|
---|
16 | /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
|
---|
17 | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */
|
---|
18 | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsContent} DllReferencePluginOptionsContent */
|
---|
19 | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsManifest} DllReferencePluginOptionsManifest */
|
---|
20 | /** @typedef {import("./Compiler")} Compiler */
|
---|
21 | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
---|
22 |
|
---|
23 | const validate = createSchemaValidation(
|
---|
24 | require("../schemas/plugins/DllReferencePlugin.check.js"),
|
---|
25 | () => require("../schemas/plugins/DllReferencePlugin.json"),
|
---|
26 | {
|
---|
27 | name: "Dll Reference Plugin",
|
---|
28 | baseDataPath: "options"
|
---|
29 | }
|
---|
30 | );
|
---|
31 |
|
---|
32 | /** @typedef {{ path: string, data: DllReferencePluginOptionsManifest | undefined, error: Error | undefined }} CompilationDataItem */
|
---|
33 |
|
---|
34 | class DllReferencePlugin {
|
---|
35 | /**
|
---|
36 | * @param {DllReferencePluginOptions} options options object
|
---|
37 | */
|
---|
38 | constructor(options) {
|
---|
39 | validate(options);
|
---|
40 | this.options = options;
|
---|
41 | /** @type {WeakMap<object, CompilationDataItem>} */
|
---|
42 | this._compilationData = new WeakMap();
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Apply the plugin
|
---|
47 | * @param {Compiler} compiler the compiler instance
|
---|
48 | * @returns {void}
|
---|
49 | */
|
---|
50 | apply(compiler) {
|
---|
51 | compiler.hooks.compilation.tap(
|
---|
52 | "DllReferencePlugin",
|
---|
53 | (compilation, { normalModuleFactory }) => {
|
---|
54 | compilation.dependencyFactories.set(
|
---|
55 | DelegatedSourceDependency,
|
---|
56 | normalModuleFactory
|
---|
57 | );
|
---|
58 | }
|
---|
59 | );
|
---|
60 |
|
---|
61 | compiler.hooks.beforeCompile.tapAsync(
|
---|
62 | "DllReferencePlugin",
|
---|
63 | (params, callback) => {
|
---|
64 | if ("manifest" in this.options) {
|
---|
65 | const manifest = this.options.manifest;
|
---|
66 | if (typeof manifest === "string") {
|
---|
67 | /** @type {InputFileSystem} */
|
---|
68 | (compiler.inputFileSystem).readFile(manifest, (err, result) => {
|
---|
69 | if (err) return callback(err);
|
---|
70 | /** @type {CompilationDataItem} */
|
---|
71 | const data = {
|
---|
72 | path: manifest,
|
---|
73 | data: undefined,
|
---|
74 | error: undefined
|
---|
75 | };
|
---|
76 | // Catch errors parsing the manifest so that blank
|
---|
77 | // or malformed manifest files don't kill the process.
|
---|
78 | try {
|
---|
79 | data.data = parseJson(
|
---|
80 | /** @type {Buffer} */ (result).toString("utf-8")
|
---|
81 | );
|
---|
82 | } catch (parseErr) {
|
---|
83 | // Store the error in the params so that it can
|
---|
84 | // be added as a compilation error later on.
|
---|
85 | const manifestPath = makePathsRelative(
|
---|
86 | /** @type {string} */ (compiler.options.context),
|
---|
87 | manifest,
|
---|
88 | compiler.root
|
---|
89 | );
|
---|
90 | data.error = new DllManifestError(
|
---|
91 | manifestPath,
|
---|
92 | /** @type {Error} */ (parseErr).message
|
---|
93 | );
|
---|
94 | }
|
---|
95 | this._compilationData.set(params, data);
|
---|
96 | return callback();
|
---|
97 | });
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | return callback();
|
---|
102 | }
|
---|
103 | );
|
---|
104 |
|
---|
105 | compiler.hooks.compile.tap("DllReferencePlugin", params => {
|
---|
106 | let name = this.options.name;
|
---|
107 | let sourceType = this.options.sourceType;
|
---|
108 | let resolvedContent =
|
---|
109 | "content" in this.options ? this.options.content : undefined;
|
---|
110 | if ("manifest" in this.options) {
|
---|
111 | const manifestParameter = this.options.manifest;
|
---|
112 | let manifest;
|
---|
113 | if (typeof manifestParameter === "string") {
|
---|
114 | const data =
|
---|
115 | /** @type {CompilationDataItem} */
|
---|
116 | (this._compilationData.get(params));
|
---|
117 | // If there was an error parsing the manifest
|
---|
118 | // file, exit now because the error will be added
|
---|
119 | // as a compilation error in the "compilation" hook.
|
---|
120 | if (data.error) {
|
---|
121 | return;
|
---|
122 | }
|
---|
123 | manifest = data.data;
|
---|
124 | } else {
|
---|
125 | manifest = manifestParameter;
|
---|
126 | }
|
---|
127 | if (manifest) {
|
---|
128 | if (!name) name = manifest.name;
|
---|
129 | if (!sourceType) sourceType = manifest.type;
|
---|
130 | if (!resolvedContent) resolvedContent = manifest.content;
|
---|
131 | }
|
---|
132 | }
|
---|
133 | /** @type {Externals} */
|
---|
134 | const externals = {};
|
---|
135 | const source = `dll-reference ${name}`;
|
---|
136 | externals[source] = /** @type {string} */ (name);
|
---|
137 | const normalModuleFactory = params.normalModuleFactory;
|
---|
138 | new ExternalModuleFactoryPlugin(sourceType || "var", externals).apply(
|
---|
139 | normalModuleFactory
|
---|
140 | );
|
---|
141 | new DelegatedModuleFactoryPlugin({
|
---|
142 | source,
|
---|
143 | type: this.options.type,
|
---|
144 | scope: this.options.scope,
|
---|
145 | context:
|
---|
146 | /** @type {string} */
|
---|
147 | (this.options.context || compiler.options.context),
|
---|
148 | content:
|
---|
149 | /** @type {DllReferencePluginOptionsContent} */
|
---|
150 | (resolvedContent),
|
---|
151 | extensions: this.options.extensions,
|
---|
152 | associatedObjectForCache: compiler.root
|
---|
153 | }).apply(normalModuleFactory);
|
---|
154 | });
|
---|
155 |
|
---|
156 | compiler.hooks.compilation.tap(
|
---|
157 | "DllReferencePlugin",
|
---|
158 | (compilation, params) => {
|
---|
159 | if ("manifest" in this.options) {
|
---|
160 | const manifest = this.options.manifest;
|
---|
161 | if (typeof manifest === "string") {
|
---|
162 | const data = /** @type {CompilationDataItem} */ (
|
---|
163 | this._compilationData.get(params)
|
---|
164 | );
|
---|
165 | // If there was an error parsing the manifest file, add the
|
---|
166 | // error as a compilation error to make the compilation fail.
|
---|
167 | if (data.error) {
|
---|
168 | compilation.errors.push(
|
---|
169 | /** @type {DllManifestError} */ (data.error)
|
---|
170 | );
|
---|
171 | }
|
---|
172 | compilation.fileDependencies.add(manifest);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 | );
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | class DllManifestError extends WebpackError {
|
---|
181 | /**
|
---|
182 | * @param {string} filename filename of the manifest
|
---|
183 | * @param {string} message error message
|
---|
184 | */
|
---|
185 | constructor(filename, message) {
|
---|
186 | super();
|
---|
187 |
|
---|
188 | this.name = "DllManifestError";
|
---|
189 | this.message = `Dll manifest ${filename}\n${message}`;
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | module.exports = DllReferencePlugin;
|
---|