diff --git a/.eslintrc b/.eslintrc index 9af7b79..b6a1402 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,6 +1,7 @@ { "parserOptions": { "sourceType": "module", + "ecmaVersion": 2017, }, "env": { "browser": true, diff --git a/buildDojo/buildapi.js b/buildDojo/buildapi.js new file mode 100644 index 0000000..248accd --- /dev/null +++ b/buildDojo/buildapi.js @@ -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 /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; \ No newline at end of file diff --git a/lib/DojoAMDPlugin.js b/lib/DojoAMDPlugin.js index abd5b72..5d11581 100644 --- a/lib/DojoAMDPlugin.js +++ b/lib/DojoAMDPlugin.js @@ -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"); @@ -185,3 +185,4 @@ Please upgrade webpack-i18n-extractor-plugin to the latest version.`); } }; module.exports.ScopedRequirePlugin = ScopedRequirePlugin; +module.exports.buildLoader = require('../buildDojo/buildapi'); diff --git a/lib/DojoLoaderPlugin.js b/lib/DojoLoaderPlugin.js index af60d21..bd9df77 100644 --- a/lib/DojoLoaderPlugin.js +++ b/lib/DojoLoaderPlugin.js @@ -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"); @@ -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__"; @@ -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) => { @@ -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); + }); }); } } diff --git a/test/DojoLoaderPlugin.test.js b/test/DojoLoaderPlugin.test.js index 4092b25..9c166ab 100644 --- a/test/DojoLoaderPlugin.test.js +++ b/test/DojoLoaderPlugin.test.js @@ -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() { @@ -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() { @@ -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(); }); });