diff --git a/examples/build-common.js b/examples/build-common.js index 4c664e00c..24b61463f 100644 --- a/examples/build-common.js +++ b/examples/build-common.js @@ -2,26 +2,30 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -var cp = require("child_process"); -var tc = require("./template-common"); -var fs = require("fs"); +"use strict"; -var extraArgs = ""; +const cp = require("child_process"); +const path = require("path"); +const tc = require("./template-common"); +const fs = require("fs"); -var targetArgs = global.NO_TARGET_ARGS ? "" : " ./example.js js/output.js"; -var displayReasons = global.NO_REASONS ? "" : " --display-reasons --display-used-exports --display-provided-exports"; -cp.exec("node ../../bin/webpack.js" + displayReasons + " --display-chunks --display-max-modules 99999 --display-origins --display-entrypoints --output-public-path \"js/\" -p " + extraArgs + targetArgs, function(error, stdout, stderr) { +const extraArgs = ""; + +const targetArgs = global.NO_TARGET_ARGS ? "" : " ./example.js js/output.js"; +const displayReasons = global.NO_REASONS ? "" : " --display-reasons --display-used-exports --display-provided-exports"; +cp.exec(`node ${path.resolve(__dirname, "../bin/webpack.js")} ${displayReasons} --display-chunks --display-max-modules 99999 --display-origins --display-entrypoints --output-public-path "js/" -p ${extraArgs} ${targetArgs}`, function(error, stdout, stderr) { if(stderr) console.log(stderr); if(error !== null) console.log(error); + let readme; try { - var readme = tc.replaceResults(fs.readFileSync(require("path").join(process.cwd(), "template.md"), "utf-8"), process.cwd(), stdout.replace(/[\r\n]*$/, ""), "min"); + readme = tc.replaceResults(fs.readFileSync(require("path").join(process.cwd(), "template.md"), "utf-8"), process.cwd(), stdout.replace(/[\r\n]*$/, ""), "min"); } catch(e) { console.log(stderr); throw e; } - cp.exec("node ../../bin/webpack.js" + displayReasons + " --display-chunks --display-max-modules 99999 --display-origins --display-entrypoints --output-public-path \"js/\" --output-pathinfo " + extraArgs + targetArgs, function(error, stdout, stderr) { + cp.exec(`node ${path.resolve(__dirname, "../bin/webpack.js")} ${displayReasons} --display-chunks --display-max-modules 99999 --display-origins --display-entrypoints --output-public-path "js/" --output-pathinfo ${extraArgs} ${targetArgs}`, function(error, stdout, stderr) { console.log(stdout); if(stderr) console.log(stderr); diff --git a/examples/buildAll.js b/examples/buildAll.js index 45de48bcd..1d697df6d 100644 --- a/examples/buildAll.js +++ b/examples/buildAll.js @@ -1,27 +1,15 @@ -var cp = require('child_process'); -var path = require("path"); -var fs = require("fs"); +"use strict"; -var cmds = fs.readdirSync(__dirname).filter(function(dirname) { - return fs.statSync(path.join(__dirname, dirname)).isDirectory() && dirname !== "node_modules"; -}).sort().map(function(dirname) { +const cp = require("child_process"); +const examples = require("./examples"); + +const cmds = examples.map(function(dirname) { return "cd " + dirname + " && node build.js"; }); -var stack = function() { - console.log("done"); -}; -for(var i = cmds.length-1; i >= 0; i--) { - var cmd = cmds[i]; - stack = (function(next, cmd) { - return function() { - console.log(cmd); - cp.exec(cmd, function(error, stdout, stderr) { - if(error) console.error(error); - else if(stderr) console.error(stderr), next(); - else next(); - }); - } - }(stack, cmd)); +let i = 0; +for(const cmd of cmds.reverse()) { + console.log(`[${++i}/${cmds.length}] ${cmd}`); + cp.execSync(cmd, { encoding: "utf-8" }); } -stack(); \ No newline at end of file +console.log("done"); diff --git a/examples/examples.js b/examples/examples.js new file mode 100644 index 000000000..071a1ca40 --- /dev/null +++ b/examples/examples.js @@ -0,0 +1,26 @@ +"use strict"; + +const fs = require("fs"); +const path = require("path"); + +function findInFolder(folder, depth) { + if(fs.existsSync(path.join(folder, "template.md"))) { + return [folder]; + } else if(depth > 0) { + const files = fs.readdirSync(folder); + const results = []; + for(const file of files) { + const innerPath = path.join(folder, file); + if(fs.statSync(innerPath).isDirectory()) { + const innerResult = findInFolder(innerPath, depth - 1); + for(const item of innerResult) + results.push(item); + } + } + return results; + } else { + return []; + } +} + +module.exports = findInFolder(__dirname, 2).sort(); diff --git a/test/Examples.test.js b/test/Examples.test.js index 368fae1d0..d266ea2d8 100644 --- a/test/Examples.test.js +++ b/test/Examples.test.js @@ -7,12 +7,11 @@ const fs = require("fs"); const webpack = require("../"); describe("Examples", () => { - const examples = fs.readdirSync(path.join(__dirname, "..", "examples")).map((name) => - path.join(__dirname, "..", "examples", name)).filter((p) => - fs.statSync(p).isDirectory() && fs.existsSync(path.join(p, "build.js"))); + const basePath = path.join(__dirname, "..", "examples"); + const examples = require("../examples/examples.js"); examples.forEach((examplePath) => { - it("should compile " + path.basename(examplePath), function(done) { + it("should compile " + path.relative(basePath, examplePath), function(done) { this.timeout(20000); let options = {}; let webpackConfigPath = path.join(examplePath, "webpack.config.js");