1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const parseJson = require("json-parse-better-errors");
|
8 | const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency");
|
9 | const DelegatedModuleFactoryPlugin = require("./DelegatedModuleFactoryPlugin");
|
10 | const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
|
11 | const DelegatedExportsDependency = require("./dependencies/DelegatedExportsDependency");
|
12 | const NullFactory = require("./NullFactory");
|
13 | const makePathsRelative = require("./util/identifier").makePathsRelative;
|
14 | const WebpackError = require("./WebpackError");
|
15 |
|
16 | const validateOptions = require("schema-utils");
|
17 | const schema = require("../schemas/plugins/DllReferencePlugin.json");
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | class DllReferencePlugin {
|
23 | |
24 |
|
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 |
|
56 |
|
57 | try {
|
58 | params["dll reference " + manifest] = parseJson(
|
59 | result.toString("utf-8")
|
60 | );
|
61 | } catch (e) {
|
62 |
|
63 |
|
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 |
|
91 |
|
92 |
|
93 | if (params["dll reference parse error " + manifestParameter]) {
|
94 | return;
|
95 | }
|
96 | manifest =
|
97 | (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 |
|
133 |
|
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 |
|
145 | class 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 |
|
156 | module.exports = DllReferencePlugin;
|