UNPKG

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