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 createSchemaValidation = require("./util/create-schema-validation");
|
14 | const makePathsRelative = require("./util/identifier").makePathsRelative;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const validate = createSchemaValidation(
|
21 | require("../schemas/plugins/DllReferencePlugin.check.js"),
|
22 | () => require("../schemas/plugins/DllReferencePlugin.json"),
|
23 | {
|
24 | name: "Dll Reference Plugin",
|
25 | baseDataPath: "options"
|
26 | }
|
27 | );
|
28 |
|
29 | class DllReferencePlugin {
|
30 | |
31 |
|
32 |
|
33 | constructor(options) {
|
34 | validate(options);
|
35 | this.options = options;
|
36 |
|
37 | this._compilationData = new WeakMap();
|
38 | }
|
39 |
|
40 | apply(compiler) {
|
41 | compiler.hooks.compilation.tap(
|
42 | "DllReferencePlugin",
|
43 | (compilation, { normalModuleFactory }) => {
|
44 | compilation.dependencyFactories.set(
|
45 | DelegatedSourceDependency,
|
46 | normalModuleFactory
|
47 | );
|
48 | }
|
49 | );
|
50 |
|
51 | compiler.hooks.beforeCompile.tapAsync(
|
52 | "DllReferencePlugin",
|
53 | (params, callback) => {
|
54 | if ("manifest" in this.options) {
|
55 | const manifest = this.options.manifest;
|
56 | if (typeof manifest === "string") {
|
57 | compiler.inputFileSystem.readFile(manifest, (err, result) => {
|
58 | if (err) return callback(err);
|
59 | const data = {
|
60 | path: manifest,
|
61 | data: undefined,
|
62 | error: undefined
|
63 | };
|
64 |
|
65 |
|
66 | try {
|
67 | data.data = parseJson(result.toString("utf-8"));
|
68 | } catch (e) {
|
69 |
|
70 |
|
71 | const manifestPath = makePathsRelative(
|
72 | compiler.options.context,
|
73 | manifest,
|
74 | compiler.root
|
75 | );
|
76 | data.error = new DllManifestError(manifestPath, e.message);
|
77 | }
|
78 | this._compilationData.set(params, data);
|
79 | return callback();
|
80 | });
|
81 | return;
|
82 | }
|
83 | }
|
84 | return callback();
|
85 | }
|
86 | );
|
87 |
|
88 | compiler.hooks.compile.tap("DllReferencePlugin", params => {
|
89 | let name = this.options.name;
|
90 | let sourceType = this.options.sourceType;
|
91 | let content =
|
92 | "content" in this.options ? this.options.content : undefined;
|
93 | if ("manifest" in this.options) {
|
94 | let manifestParameter = this.options.manifest;
|
95 | let manifest;
|
96 | if (typeof manifestParameter === "string") {
|
97 | const data = this._compilationData.get(params);
|
98 |
|
99 |
|
100 |
|
101 | if (data.error) {
|
102 | return;
|
103 | }
|
104 | manifest = data.data;
|
105 | } else {
|
106 | manifest = manifestParameter;
|
107 | }
|
108 | if (manifest) {
|
109 | if (!name) name = manifest.name;
|
110 | if (!sourceType) sourceType = manifest.type;
|
111 | if (!content) content = manifest.content;
|
112 | }
|
113 | }
|
114 |
|
115 | const externals = {};
|
116 | const source = "dll-reference " + name;
|
117 | externals[source] = name;
|
118 | const normalModuleFactory = params.normalModuleFactory;
|
119 | new ExternalModuleFactoryPlugin(sourceType || "var", externals).apply(
|
120 | normalModuleFactory
|
121 | );
|
122 | new DelegatedModuleFactoryPlugin({
|
123 | source: source,
|
124 | type: this.options.type,
|
125 | scope: this.options.scope,
|
126 | context: this.options.context || compiler.options.context,
|
127 | content,
|
128 | extensions: this.options.extensions,
|
129 | associatedObjectForCache: compiler.root
|
130 | }).apply(normalModuleFactory);
|
131 | });
|
132 |
|
133 | compiler.hooks.compilation.tap(
|
134 | "DllReferencePlugin",
|
135 | (compilation, params) => {
|
136 | if ("manifest" in this.options) {
|
137 | let manifest = this.options.manifest;
|
138 | if (typeof manifest === "string") {
|
139 | const data = this._compilationData.get(params);
|
140 |
|
141 |
|
142 | if (data.error) {
|
143 | compilation.errors.push(data.error);
|
144 | }
|
145 | compilation.fileDependencies.add(manifest);
|
146 | }
|
147 | }
|
148 | }
|
149 | );
|
150 | }
|
151 | }
|
152 |
|
153 | class DllManifestError extends WebpackError {
|
154 | constructor(filename, message) {
|
155 | super();
|
156 |
|
157 | this.name = "DllManifestError";
|
158 | this.message = `Dll manifest ${filename}\n${message}`;
|
159 | }
|
160 | }
|
161 |
|
162 | module.exports = DllReferencePlugin;
|