Skip to content

Commit f9a38b5

Browse files
author
Chuck Dumont
authored
Merge pull request #169 from chuckdumont/work2
Fix memory leak issues
2 parents d3991ed + 5f41569 commit f9a38b5

8 files changed

+144
-70
lines changed

lib/DojoAMDModuleFactoryPlugin.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
/* (C) Copyright HCL Technologies Ltd. 2018
23
* (C) Copyright IBM Corp. 2012, 2016 All Rights Reserved.
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -43,14 +44,13 @@ module.exports = class DojoAMDModuleFactoryPlugin {
4344
"resolver" : this.resolver,
4445
"create-module" : this.createModule
4546
}, context);
46-
tap(compiler, {"compilation" : (compilation, params) => {
47-
tap(factory, "module", this.module, Object.create(context, {
48-
compilation: {value: compilation},
49-
params: {value: params}
50-
}));
51-
}});
52-
}, this);
53-
}
47+
});
48+
tap(compiler, {"compilation" : (compilation, params) => {
49+
tap(params.normalModuleFactory, {
50+
"module": this.module.bind(this, compilation, params.normalModuleFactory)
51+
});
52+
}});
53+
}
5454

5555
toAbsMid(request, issuerAbsMid, dojoRequire) {
5656
var result = request;
@@ -210,29 +210,29 @@ module.exports = class DojoAMDModuleFactoryPlugin {
210210
return module;
211211
}
212212

213-
module(module) {
213+
module(compilation, moduleFactory, module) {
214214
if (module.originalRequest instanceof Module) {
215215
// Copy absMids from original request to the new request
216-
callSync(this.factory, "filter absMids", module.originalRequest, absMid => {
217-
callSync(this.factory, "add absMid", module, absMid);
216+
callSync(moduleFactory, "filter absMids", module.originalRequest, absMid => {
217+
callSync(moduleFactory, "add absMid", module, absMid);
218218
return true;
219219
});
220220
}
221221
// If the module already exists in the compilation, then copy the absMid data from
222222
// this module to the existing module since this module will be discarded
223-
const existing = this.compilation.findModule(module.request);
223+
const existing = compilation.findModule(module.request);
224224
if (existing) {
225-
callSync(this.factory, "filter absMids", module, absMid => {
226-
callSync(this.factory, "add absMid", existing, absMid);
225+
callSync(moduleFactory, "filter absMids", module, absMid => {
226+
callSync(moduleFactory, "add absMid", existing, absMid);
227227
return true;
228228
});
229229
}
230230
// Add functions to the module object for adding/filtering absMids (for use by loaders)
231231
module.addAbsMid = absMid => {
232-
callSync(this.factory, "add absMid", module, absMid);
232+
callSync(moduleFactory, "add absMid", module, absMid);
233233
};
234234
module.filterAbsMids = callback => {
235-
callSync(this.factory, "filter absMids", module, callback);
235+
callSync(moduleFactory, "filter absMids", module, callback);
236236
};
237237
return module;
238238
}

lib/DojoAMDPlugin.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* (C) Copyright HCL Technologies Ltd. 2018
23
* (C) Copyright IBM Corp. 2012, 2016 All Rights Reserved.
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,6 +29,7 @@ const DojoAMDMainTemplatePlugin = require("./DojoAMDMainTemplatePlugin");
2829
const DojoAMDChunkTemplatePlugin = require("./DojoAMDChunkTemplatePlugin");
2930
const DojoAMDModuleFactoryPlugin = require("./DojoAMDModuleFactoryPlugin");
3031
const DojoLoaderPlugin = require("./DojoLoaderPlugin");
32+
const DojoLoaderValidatorPlugin = require("./DojoLoaderValidatorPlugin");
3133
const DojoAMDRequireArrayDependency = require("./DojoAMDRequireArrayDependency");
3234
const ScopedRequirePlugin = require("./ScopedRequirePlugin");
3335
const {applyResolverPlugin} = require("./compat");
@@ -45,6 +47,7 @@ module.exports = class DojoAMDPlugin {
4547
tap(compiler, "dojo-webpack-plugin-options", () => this.options);
4648
applyResolverPlugin(this.options, compiler, this.newResolverPlugin);
4749
this.newDojoLoaderPlugin(this.options).apply(compiler);
50+
this.newDojoLoaderValidatorPlugin(this.options).apply(compiler);
4851
this.newMainTemplatePlugin(this.options).apply(compiler);
4952
this.newChunkTemplatePlugin(this.options).apply(compiler);
5053
this.newModuleFactoryPlugin(this.options).apply(compiler);
@@ -114,6 +117,9 @@ module.exports = class DojoAMDPlugin {
114117
newDojoLoaderPlugin(options) {
115118
return new DojoLoaderPlugin(options);
116119
}
120+
newDojoLoaderValidatorPlugin(options) {
121+
return new DojoLoaderValidatorPlugin(options);
122+
}
117123
newMainTemplatePlugin(options) {
118124
return new DojoAMDMainTemplatePlugin(options);
119125
}

lib/DojoAMDResolverPluginV4.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* (C) Copyright HCL Technologies Ltd. 2018
23
* (C) Copyright IBM Corp. 2012, 2016 All Rights Reserved.
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -24,12 +25,10 @@ module.exports = class DojoAMDResolverPluginV4 extends DojoAMDResolverPluginBase
2425

2526
apply(compiler) {
2627
this.compiler = compiler;
27-
tap(compiler, "compilation", () => {
28-
tap(compiler.resolverFactory, "resolver normal", resolver => {
29-
tap(resolver, "module", this.module, Object.create(this, {
30-
resolver: {value: resolver}
31-
}), {stage:-1});
32-
});
28+
tap(compiler.resolverFactory, "resolver normal", resolver => {
29+
tap(resolver, "module", this.module, Object.create(this, {
30+
resolver: {value: resolver}
31+
}), {stage:-1});
3332
});
3433
}
3534

lib/DojoLoaderPlugin.js

+2-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* (C) Copyright HCL Technologies Ltd. 2018
23
* (C) Copyright IBM Corp. 2017 All Rights Reserved.
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,7 +26,6 @@ const BasicEvaluatedExpression = require("webpack/lib/BasicEvaluatedExpression")
2526

2627
const embeddedLoaderFilenameExpression = "__embedded_dojo_loader__";
2728

28-
2929
module.exports = class DojoLoaderPlugin {
3030
constructor(options) {
3131
this.options = options;
@@ -54,11 +54,6 @@ module.exports = class DojoLoaderPlugin {
5454
compilation:{value: compilation},
5555
params:{value: params}
5656
});
57-
if (compilation.compiler === compiler) {
58-
// Don't do this for child compilations
59-
// https://github.com/OpenNTF/dojo-webpack-plugin/issues/115
60-
tap(compiler, "make", this.validateEmbeddedLoader, context);
61-
}
6257
tap(compilation, [
6358
["succeed-module", this.succeedModule],
6459
["after-optimize-chunks", this.afterOptimizeChunks]
@@ -171,28 +166,6 @@ module.exports = class DojoLoaderPlugin {
171166
return loaderScope;
172167
}
173168

174-
validateEmbeddedLoader(compilation__, callback) {
175-
// Vefiry that embedded loader version and dojo version are the same
176-
this.params.normalModuleFactory.create({
177-
dependencies: [{request: "dojo/package.json"}]
178-
}, (err, pkgModule) => {
179-
if (!err) {
180-
const dojoVersion = require(pkgModule.request).version;
181-
const scope = callSyncBail(this.compiler, "create-embedded-loader-scope", {}, this.embeddedLoader, this.filename);
182-
if (dojoVersion !== scope.loaderVersion) {
183-
err = new Error(
184-
`Dojo loader version does not match the version of Dojo.
185-
Loader version = ${scope.loaderVersion}.
186-
Dojo version = ${dojoVersion}.
187-
You may need to rebuild the Dojo loader.
188-
Refer to https://github.com/OpenNTF/dojo-webpack-plugin/blob/master/README.md#building-the-dojo-loader`);
189-
}
190-
return callback(err, scope);
191-
}
192-
callback(err);
193-
});
194-
}
195-
196169
getBuildLoaderConfig() {
197170
var loaderConfig = this.options.loaderConfig;
198171
if (util.isString(loaderConfig)) {
@@ -339,4 +312,4 @@ Refer to https://github.com/OpenNTF/dojo-webpack-plugin/blob/master/README.md#bu
339312
getDefaultFeaturesForEmbeddedLoader() {
340313
return require("../buildDojo/loaderDefaultFeatures");
341314
}
342-
};
315+
};

lib/DojoLoaderValidatorPlugin.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* (C) Copyright HCL Technologies Ltd. 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
const {tap, callSyncBail} = require("./pluginCompat").for("dojo-webpack-plugin");
17+
18+
module.exports = class DojoLoaderValidatorPlugin {
19+
constructor(options) {
20+
this.options = options;
21+
}
22+
23+
apply(compiler) {
24+
this.compiler = compiler;
25+
tap(compiler, {
26+
"compile": params => this.params = params,
27+
"embedded-dojo-loader": this.embeddedDojoLoader,
28+
"make": this.validateEmbeddedLoader
29+
}, this);
30+
}
31+
32+
embeddedDojoLoader(content, filename) {
33+
this.embeddedLoader = content;
34+
this.embeddedLoaderFilename = filename;
35+
}
36+
37+
validateEmbeddedLoader(compilation, callback) {
38+
if (compilation.compiler !== this.compiler) {
39+
// Don't do this for child compilations
40+
// https://github.com/OpenNTF/dojo-webpack-plugin/issues/115
41+
return callback();
42+
}
43+
// Vefiry that embedded loader version and dojo version are the same
44+
this.params.normalModuleFactory.create({
45+
dependencies: [{request: "dojo/package.json"}]
46+
}, (err, pkgModule) => {
47+
if (!err) {
48+
const dojoVersion = require(pkgModule.request).version;
49+
const scope = callSyncBail(this.compiler, "create-embedded-loader-scope", {}, this.embeddedLoader, this.filename);
50+
if (dojoVersion !== scope.loaderVersion) {
51+
err = new Error(
52+
`Dojo loader version does not match the version of Dojo.
53+
Loader version = ${scope.loaderVersion}.
54+
Dojo version = ${dojoVersion}.
55+
You may need to rebuild the Dojo loader.
56+
Refer to https://github.com/OpenNTF/dojo-webpack-plugin/blob/master/README.md#building-the-dojo-loader`);
57+
}
58+
return callback(err, scope);
59+
}
60+
callback(err);
61+
});
62+
}
63+
};

test/DojoAMDModuleFactoryPlugin.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe("DojoAMDModuleFactoryPlugin tests", function() {
4141
plugin.apply(compiler);
4242
plugin.factory = factory;
4343
callSync(compiler, "normal-module-factory", factory);
44-
callSync(compiler, "compilation", compilation, {});
44+
callSync(compiler, "compilation", compilation, {normalModuleFactory: factory});
4545
});
4646
describe("addAbsMid tests", function() {
4747
it("should add the absMid", function() {
@@ -210,7 +210,6 @@ describe("DojoAMDModuleFactoryPlugin tests", function() {
210210
const delegated = {originalRequest: originalModule};
211211
callSync(factory, "add absMid", originalModule, "a");
212212
compilation.findModule = function() { return null; };
213-
debugger; // eslint-disable-line
214213
const result = callSyncWaterfall(factory, "module", delegated);
215214
result.should.be.eql(delegated);
216215
result.absMid.should.eql('a');

test/DojoLoaderPlugin.test.js

-17
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,6 @@ describe("DojoLoaderPlugin tests", function() {
6060
});
6161
});
6262
});
63-
describe("validateEmbeddedLoader edge cases", function() {
64-
it("Should invoke callback with error if nomralModuleFactory.create returns an error", function(done) {
65-
var error = new Error("Failed to create module");
66-
plugin.params = {
67-
normalModuleFactory: {
68-
create: function(params__, callback) {
69-
callback(error);
70-
}
71-
}
72-
};
73-
plugin.embeddedLoader = plugin.filename = "";
74-
plugin.validateEmbeddedLoader(null, err => {
75-
err.should.be.eql(error);
76-
done();
77-
});
78-
});
79-
});
8063
describe("compiler run1 edge cases", function() {
8164
afterEach(function() {
8265
delete plugin.getDojoPath;
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Tests to provide complete test coverage for DojoLoaderValidatorPlugin. These
3+
* tests exercise code paths that are difficult or impossible to invoke from
4+
* within webpack. As such, they provide only enough scafoliding to support
5+
* execution of the targeted paths. Code changes to the module under
6+
* test may require additional scafolding in this file, even if the code
7+
* changes are not related to the paths being tested.
8+
*/
9+
const {Tapable} = require("../lib/pluginCompat").for("DojoLoaderPlugin.test");
10+
11+
const DojoLoaderValidatorPlugin = require("../lib/DojoLoaderValidatorPlugin");
12+
var plugin;
13+
describe("DojoLoaderValidatorPlugin tests", function() {
14+
describe("validateEmbeddedLoader edge cases", function() {
15+
var compiler;
16+
beforeEach(function() {
17+
plugin = new DojoLoaderValidatorPlugin({loaderConfig:{}, noConsole:true});
18+
compiler = new Tapable();
19+
compiler.context = '.';
20+
plugin.compiler = compiler;
21+
});
22+
it("Should invoke callback with error if nomralModuleFactory.create returns an error", function(done) {
23+
var error = new Error("Failed to create module");
24+
plugin.compilation = {
25+
compiler:plugin.compiler
26+
};
27+
plugin.params = {
28+
normalModuleFactory: {
29+
create: function(params__, callback) {
30+
callback(error);
31+
}
32+
}
33+
};
34+
plugin.embeddedLoader = plugin.filename = "";
35+
plugin.validateEmbeddedLoader(plugin.compilation, err => {
36+
err.should.be.eql(error);
37+
done();
38+
});
39+
});
40+
41+
it("Should invoke callback with no error for child compiler", function(done) {
42+
plugin.compilation = {
43+
compiler:{}
44+
};
45+
plugin.validateEmbeddedLoader(plugin.compilation, err => {
46+
(typeof err === 'undefined').should.be.true;
47+
done();
48+
});
49+
});
50+
});
51+
});

0 commit comments

Comments
 (0)