Skip to content

Commit

Permalink
Add node api to build the loader
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckdumont committed Nov 16, 2019
1 parent f958292 commit 605ee6b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 45 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2017,
},
"env": {
"browser": true,
Expand Down
69 changes: 69 additions & 0 deletions buildDojo/buildapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* (C) Copyright HCL Technologies Ltd. 2019
*
* 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 path = require("path");
const fork = require("child_process").fork;

/*
* parameters:
* dojoPath - The path to dojo.js source file
* releaseDir - The path to the directory where the built loader will be written
* The build loader will be in <releaseDir>/dojo/dojo.js
* featureOverrides - Overrides to ./loaderDefaultFeatures.js
* noConsole - True to suppress stdout from the builder
*/
async function buildLoader(params) {
const ls = fork(
path.resolve(__dirname, "buildRunner.js"),
[
"load=build",
"--dojoPath",
path.resolve(params.dojoPath),
"--profile",
path.join(__dirname, "loader.profile.js"),
"--release",
"--releaseDir",
path.resolve(params.releaseDir),
"--has",
JSON.stringify(params.featureOverrides || {})
],
{
silent: true
}
);
return new Promise(function(resolve, reject) {
const errors = [];
ls.stdout.on('data', data => {
if (!params.noConsole) {
console.log(`${data}`);
}
});
ls.stderr.on('data', data => {
const decoded = `${data}`;
errors.push(decoded);
console.error(decoded);
});
ls.on('close', code => {
if (code == 0) {
resolve();
} else {
const err = new Error(errors.join(""));
err.code = code;
reject(err);
}
});
});
}
module.exports = buildLoader;
3 changes: 2 additions & 1 deletion lib/DojoAMDPlugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright HCL Technologies Ltd. 2018
* (C) Copyright HCL Technologies Ltd. 2018, 2019
* (C) Copyright IBM Corp. 2012, 2016 All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -185,3 +185,4 @@ Please upgrade webpack-i18n-extractor-plugin to the latest version.`);
}
};
module.exports.ScopedRequirePlugin = ScopedRequirePlugin;
module.exports.buildLoader = require('../buildDojo/buildapi');
48 changes: 16 additions & 32 deletions lib/DojoLoaderPlugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright HCL Technologies Ltd. 2018
* (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");
Expand All @@ -25,6 +25,7 @@ const {containsModule} = require("./compat");
const CommonJsRequireDependency = require("webpack/lib/dependencies/CommonJsRequireDependency");
const ConstDependency = require("webpack/lib/dependencies/ConstDependency");
const BasicEvaluatedExpression = require("webpack/lib/BasicEvaluatedExpression");
const buildLoader = require("../buildDojo/buildapi");

const embeddedLoaderFilenameExpression = "__embedded_dojo_loader__";

Expand Down Expand Up @@ -101,7 +102,6 @@ module.exports = class DojoLoaderPlugin {
if (!options.noConsole) {
console.log("Dojo loader not specified in options. Building the loader...");
}
const child_process = require("child_process");
const tmp = require("tmp");
// create temporary directory to hold output
tmp.dir({unsafeCleanup: true}, (err, tempDir) => {
Expand All @@ -115,36 +115,20 @@ module.exports = class DojoLoaderPlugin {
featureOverrides['dojo-config-api'] = 0;
}
}
child_process.execFile(
process.execPath, // the executable to run
[ // The arguments
path.resolve(__dirname, "../buildDojo", "buildRunner.js"),
"load=build",
"--dojoPath",
path.resolve(loaderConfig.baseUrl, dojoPath, "./dojo"), // path to dojo.js
"--profile",
path.join(__dirname, "../buildDojo/loader.profile.js"), // the build profile
"--release",
"--releaseDir",
tempDir, // target location
"--has",
JSON.stringify(featureOverrides)
], (error, stdout, stderr) => {
if (error) {
console.error(stderr.toString());
callback(error);
} else {
if (!options.noConsole) {
console.log(stdout.toString());
}
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);
});
}
}
);
buildLoader({
dojoPath: path.resolve(loaderConfig.baseUrl, dojoPath, "./dojo"), // path to dojo.js
releaseDir: tempDir, // target location
featureOverrides: 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);
});
});
}
}
Expand Down
15 changes: 3 additions & 12 deletions test/DojoLoaderPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
const proxyquire = require("proxyquire");
const {reg, tap, Tapable} = require("webpack-plugin-compat").for("DojoLoaderPlugin.test");

const tmpStub = {}, child_processStub = {};
const tmpStub = {};

const DojoLoaderPlugin = proxyquire("../lib/DojoLoaderPlugin", {
tmp: tmpStub,
child_process: child_processStub
tmp: tmpStub
});
var plugin;
describe("DojoLoaderPlugin tests", function() {
Expand All @@ -32,10 +31,6 @@ describe("DojoLoaderPlugin tests", function() {
Object.keys(tmpStub).forEach(key => {
delete tmpStub[key];
});
Object.keys(child_processStub).forEach(key => {
delete child_processStub[key];
});

});
describe("getOrCreateEmbeddedLoader edge cases", function() {

Expand All @@ -50,12 +45,8 @@ describe("DojoLoaderPlugin tests", function() {
});
});
it("Should call callback with error returned by exec", function(done) {
var error = new Error("Error from execFile");
child_processStub.execFile = function(executable__, options__, callback) {
callback(error, "", "Error from execFile");
};
plugin.getOrCreateEmbeddedLoader("path", {baseUrl:'.'}, {}, err => {
err.should.be.eql(error);
err.message.should.match(/Cannot find module/);
done();
});
});
Expand Down

0 comments on commit 605ee6b

Please sign in to comment.