1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const parseJson = require("json-parse-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 makePathsRelative = require("./util/identifier").makePathsRelative;
|
14 |
|
15 | const { validate } = require("schema-utils");
|
16 | const schema = require("../schemas/plugins/DllReferencePlugin.json");
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | class DllReferencePlugin {
|
23 | |
24 |
|
25 |
|
26 | constructor(options) {
|
27 | validate(schema, options, {
|
28 | name: "Dll Reference Plugin",
|
29 | baseDataPath: "options"
|
30 | });
|
31 | this.options = options;
|
32 |
|
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 |
|
61 |
|
62 | try {
|
63 | data.data = parseJson(result.toString("utf-8"));
|
64 | } catch (e) {
|
65 |
|
66 |
|
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 |
|
95 |
|
96 |
|
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 |
|
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 |
|
137 |
|
138 | if (data.error) {
|
139 | compilation.errors.push(data.error);
|
140 | }
|
141 | compilation.fileDependencies.add(manifest);
|
142 | }
|
143 | }
|
144 | }
|
145 | );
|
146 | }
|
147 | }
|
148 |
|
149 | class 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 |
|
160 | module.exports = DllReferencePlugin;
|