-
Notifications
You must be signed in to change notification settings - Fork 24
/
DojoLoaderPlugin.js
321 lines (304 loc) · 12.4 KB
/
DojoLoaderPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* (C) Copyright HCL Technologies Ltd. 2018, 2019
* (C) Copyright IBM Corp. 2017 All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const util = require('util');
const path = require('path');
const fs = require('fs');
const vm = require("vm");
const {SyncBailHook} = require('tapable');
const {Template} = require("webpack");
const {pluginName, getPluginProps} = require("./DojoAMDPlugin");
const loaderMainModulePatch = require("../runtime/DojoLoaderNonLocalMainPatch.runtime");
const CommonJsRequireDependency = require("webpack/lib/dependencies/CommonJsRequireDependency");
const ConstDependency = require("webpack/lib/dependencies/ConstDependency");
const BasicEvaluatedExpression = require("webpack/lib/javascript/BasicEvaluatedExpression");
const buildLoader = require("../buildDojo/buildapi");
const embeddedLoaderFilenameExpression = "__embedded_dojo_loader__";
module.exports = class DojoLoaderPlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
this.compiler = compiler;
this.pluginProps = getPluginProps(compiler);
Object.defineProperties(this.pluginProps, {
'dojoRequire': {
enumerable: true,
configurable: true,
get: () => this.loaderScope.require
},
'dojoLoader': {
enumerable: true,
configurable: true,
get: () => this.dojoLoader
},
'dojoLoaderFilename': {
enumerable: true,
configurable: true,
get: () => this.dojoLoaderFilename
},
'embeddedLoader': {
enumerable: true,
configurable: true,
get: () => this.embeddedLoader
},
'embeddedLoaderFilename': {
enumerable: true,
configurable: true,
get: () => this.embeddedLoaderFilename
},
'embeddedLoaderHasConfigApi': {
enumerable: true,
configurable: true,
get: () => this.embeddedLoaderHasConfigApi
}
});
this.pluginProps.hooks.getDojoConfig = new SyncBailHook();
this.pluginProps.hooks.createDojoLoaderScope = new SyncBailHook(["loaderConfig", "loader", "filename"]);
this.pluginProps.hooks.createEmbeddedLoaderScope = new SyncBailHook(["userConfig", "embeddedLoader", "filename"]);
this.pluginProps.hooks.getDojoConfig.tap(pluginName, this.getBuildLoaderConfig.bind(this));
this.pluginProps.hooks.createDojoLoaderScope.tap(pluginName, this.createLoaderScope.bind(this));
this.pluginProps.hooks.createEmbeddedLoaderScope.tap(pluginName, this.createEmbeddedLoaderScope.bind(this));
compiler.hooks.run.tapAsync(pluginName, this.run1.bind(this));
compiler.hooks.run.tapAsync(pluginName, this.run2.bind(this));
compiler.hooks.watchRun.tapAsync(pluginName, this.run1.bind(this));
compiler.hooks.watchRun.tapAsync(pluginName, this.run2.bind(this));
compiler.hooks.compilation.tap(pluginName, (compilation, params) => {
if (!this.options.isSkipCompilation(compilation)) {
let context = Object.create(this, {
compilation:{value: compilation},
params:{value: params}
});
compilation.hooks.succeedModule.tap(pluginName, this.addDojoDeps.bind(context));
compilation.hooks.afterOptimizeChunks.tap(pluginName, this.afterOptimizeChunks.bind(context));
// Support for the __embedded_dojo_loader__ webpack variable. This allows applications (and unit tests)
// to require the embedded loader module with require(__embedded_dojo_loader__);
params.normalModuleFactory.hooks.parser.for('javascript/auto').tap(pluginName, parser => {
context = Object.create(context, {parser: {value: parser}});
parser.hooks.expression.for(embeddedLoaderFilenameExpression).tap(pluginName, this.expressionLoader.bind(context));
parser.hooks.evaluateTypeof.for(embeddedLoaderFilenameExpression).tap(pluginName, this.evaluateTypeofLoader.bind(context));
parser.hooks.evaluateIdentifier.for(embeddedLoaderFilenameExpression).tap(pluginName, this.evaluateIdentifierLoader.bind(context));
});
}
});
}
getDojoPath(loaderConfig) {
var dojoPath;
if (!loaderConfig.packages || !loaderConfig.packages.some((pkg) => {
if (pkg.name === "dojo") {
return dojoPath = path.resolve(loaderConfig.baseUrl, pkg.location);
}
})) {
return path.join(require.resolve("dojo/dojo.js"), "..");
}
return dojoPath;
}
getOrCreateEmbeddedLoader(dojoPath, loaderConfig, options, callback) {
var dojoLoaderPath;
if (options.loader) {
try {
dojoLoaderPath = require.resolve(options.loader);
fs.readFile(dojoLoaderPath, "utf-8", (err, content) => {
return callback(err, content);
});
} catch (error) {
return callback(error);
}
} else {
if (!options.noConsole) {
console.log("Dojo loader not specified in options. Building the loader...");
}
const tmp = require("tmp");
// create temporary directory to hold output
tmp.dir({unsafeCleanup: true}, (err, tempDir) => {
if (err) {
return callback(err);
}
const featureOverrides = {};
if (!util.isString(options.loaderConfig)) {
// If config is not a module, then honor the 'dojo-config-api' has feature if specified
if (loaderConfig.has && ('dojo-config-api' in loaderConfig.has) && !loaderConfig.has['dojo-config-api']) {
featureOverrides['dojo-config-api'] = 0;
}
}
buildLoader({
dojoPath: path.resolve(loaderConfig.baseUrl, dojoPath, "./dojo"), // path to dojo.js
releaseDir: tempDir, // target location
has: featureOverrides,
noConsole: options.noConsole
}).then(() => {
options.loader = path.join(tempDir, "dojo/dojo.js");
dojoLoaderPath = require.resolve(path.join(options.loader));
fs.readFile(dojoLoaderPath, "utf-8", (err, content) => { // eslint-disable-line no-shadow
callback(err, content);
});
}).catch(err => { // eslint-disable-line no-shadow
callback(err);
});
});
}
}
createLoaderScope(loaderConfig, loader, filename) {
const loaderScope = {};
loaderScope.global = loaderScope.window = loaderScope;
loaderScope.dojoConfig = Object.assign({}, loaderConfig);
loaderScope.dojoConfig.has = Object.assign({}, this.getDefaultFeaturesForEmbeddedLoader(), loaderScope.dojoConfig.has, {"dojo-config-api":1, "dojo-publish-privates":1});
var context = vm.createContext(loaderScope);
const patch = "(function(loaderScope){" + Template.getFunctionContent(loaderMainModulePatch) + "})(global);";
vm.runInContext('(function(global, window) {' + loader + patch + '});', context, filename).call(context, context);
return loaderScope;
}
createEmbeddedLoaderScope(userConfig, embeddedLoader, filename) {
const loaderScope = {};
const defaultConfig = {hasCache:{}, modules:{}};
loaderScope.global = loaderScope.window = loaderScope;
var context = vm.createContext(loaderScope);
vm.runInContext("var module = {};" + embeddedLoader, context, filename).call(context, userConfig, defaultConfig, context, context);
return loaderScope;
}
getBuildLoaderConfig() {
var loaderConfig = this.options.loaderConfig;
if (util.isString(loaderConfig)) {
loaderConfig = require(loaderConfig);
}
if (typeof loaderConfig === 'function') {
loaderConfig = loaderConfig(this.options.buildEnvironment || this.options.environment || {});
}
loaderConfig.baseUrl = path.resolve(this.compiler.context, loaderConfig.baseUrl || ".").replace(/\\/g, "/");
return loaderConfig;
}
run1(__, callback) {
// Load the Dojo loader and get the require function into loaderScope
var loaderConfig = this.pluginProps.hooks.getDojoConfig.call();
var dojoPath;
try {
dojoPath = this.getDojoPath(loaderConfig);
} catch (e) {
return callback(e);
}
this.dojoLoaderFilename = path.join(dojoPath, "dojo.js");
fs.readFile(this.dojoLoaderFilename, 'utf-8', (err, content) => {
this.dojoLoader = content;
if (err) return callback(err);
//callSync(this.compiler, "dojo-loader", content, filename);
this.loaderScope = this.pluginProps.hooks.createDojoLoaderScope.call(loaderConfig, content, this.dojoLoaderFilename);
return callback();
});
}
run2(__, callback) {
// Load the Dojo loader and get the require function into loaderScope
var loaderConfig = this.pluginProps.hooks.getDojoConfig.call();
var dojoPath;
try {
dojoPath = this.getDojoPath(loaderConfig);
} catch (e) {
return callback(e);
}
this.getOrCreateEmbeddedLoader(dojoPath, loaderConfig, this.options, (err, content) => {
// options.loader specifies path to the embedded loader (set by createEmbeddedLoader if created)
if (!err) {
var scope = this.createEmbeddedLoaderScope({packages:[{name:"dojo", location:"./dojo"}]}, content, this.options.loader);
this.embeddedLoader = content;
this.embeddedLoaderFilename = this.options.loader;
this.embeddedLoaderHasConfigApi = !!scope.require.packs;
}
callback(err);
});
}
addDojoDeps(module) {
const {options} = this;
if (!this.compilation.moduleGraph.getIssuer(module)) {
// No issuer generally means an entry module, so add a Dojo loader dependency. It doesn't
// hurt to add extra dependencies because the Dojo loader module will be removed from chunks
// that don't need it in the 'after-optimize-chunks' handler below.
var loaderDep = new CommonJsRequireDependency(options.loader);
loaderDep.loc = {
start: {line: -1, column: 0},
end: {line: -1, column: 0},
index: -2
};
module.addDependency(loaderDep);
if (util.isString(options.loaderConfig)) {
var configDep = new CommonJsRequireDependency(options.loaderConfig);
configDep.loc = {
start: {line: -1, column: 0},
end: {line: -1, column: 0},
index: -1
};
module.addDependency(configDep);
}
}
}
afterOptimizeChunks(chunks) {
// Get the loader and loader config
const {options, compilation} = this;
const loaderModule = Array.from(compilation.modules).find((module) => { return module.rawRequest === options.loader;});
const configModule = util.isString(options.loaderConfig) &&
Array.from(compilation.modules).find((module) => { return module.rawRequest === options.loaderConfig;});
// Ensure that the Dojo loader, and optionally the loader config, are included
// only in the entry chunks that contain the webpack runtime.
chunks.forEach((chunk) => {
if (chunk.hasRuntime()) {
if (!loaderModule) {
throw Error("Can't locate " + options.loader + " in compilation");
}
if (util.isString(options.loaderConfig) && !configModule) {
throw Error("Can't locate " + options.loaderConfig + " in compilation");
}
if (!this.compilation.chunkGraph.isModuleInChunk(loaderModule, chunk)) {
this.compilation.chunkGraph.connectChunkAndModule(chunk, loaderModule);
}
if (configModule && !this.compilation.chunkGraph.isModuleInChunk(configModule, chunk)) {
this.compilation.chunkGraph.connectChunkAndModule(chunk, configModule);
}
} else if (loaderModule) {
if (this.compilation.chunkGraph.isModuleInChunk(loaderModule, chunk)) {
this.compilation.chunkGraph.disconnectChunkAndModule(chunk, loaderModule);
}
if (configModule && this.compilation.chunkGraph.isModuleInChunk(configModule, chunk)) {
this.compilation.chunkGraph.disconnectChunkAndModule(chunk, configModule);
}
}
});
}
expressionLoader(expr) {
// change __embedded_dojo_loader__ expressions in the source to the filename value as a string.
const {parser} = this;
const fn = parser.hooks.evaluateIdentifier.for(embeddedLoaderFilenameExpression).call(expr).string.replace(/\\/g, "\\\\");
const dep = new ConstDependency("\"" + fn + "\"", expr.range);
dep.loc = expr.loc;
parser.state.current.addDependency(dep);
return true;
}
evaluateTypeofLoader(expr) {
// implement typeof operator for the expression
var result = new BasicEvaluatedExpression().setString("string");
if (expr) {
result.setRange(expr.range);
}
return result;
}
evaluateIdentifierLoader(expr) {
var result = new BasicEvaluatedExpression().setString(this.embeddedLoaderFilename);
if (expr) {
result.setRange(expr.range);
}
return result;
}
getDefaultFeaturesForEmbeddedLoader() {
return require("../buildDojo/loaderDefaultFeatures");
}
};