UNPKG

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