webpack/examples/build-common.js

41 lines
1.7 KiB
JavaScript
Raw Normal View History

2012-04-05 20:59:01 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
2012-04-05 20:59:01 +08:00
const cp = require("child_process");
const path = require("path");
const tc = require("./template-common");
const fs = require("fs");
2012-04-05 20:59:01 +08:00
const extraArgs = "";
2017-11-21 18:04:18 +08:00
const hasConfiguration = fs.existsSync("webpack.config.js");
const targetArgs = global.NO_TARGET_ARGS ? "" : " ./example.js js/output.js ";
const displayReasons = global.NO_REASONS ? "" : " --display-reasons --display-used-exports --display-provided-exports";
2017-11-21 18:04:18 +08:00
const modeArgs = hasConfiguration ? "" : "--mode production";
cp.exec(`node ${path.resolve(__dirname, "../bin/webpack.js")} ${modeArgs} ${displayReasons} --display-chunks --display-max-modules 99999 --display-origins --display-entrypoints --output-public-path "js/" -p ${extraArgs} ${targetArgs}`, function(error, stdout, stderr) {
2017-06-05 22:12:12 +08:00
if(stderr)
console.log(stderr);
if(error !== null)
console.log(error);
let readme;
2017-06-05 22:12:12 +08:00
try {
readme = tc.replaceResults(fs.readFileSync(require("path").join(process.cwd(), "template.md"), "utf-8"), process.cwd(), stdout.replace(/[\r\n]*$/, ""), "min");
2017-06-05 22:12:12 +08:00
} catch(e) {
console.log(stderr);
throw e;
}
2017-11-21 18:04:18 +08:00
cp.exec(`node ${path.resolve(__dirname, "../bin/webpack.js")} ${modeArgs} ${displayReasons} --display-chunks --display-max-modules 99999 --display-origins --display-entrypoints --output-public-path "js/" --output-pathinfo ${extraArgs} ${targetArgs}`, function(error, stdout, stderr) {
2017-06-05 22:12:12 +08:00
console.log(stdout);
if(stderr)
2012-04-05 20:59:01 +08:00
console.log(stderr);
2017-06-05 22:12:12 +08:00
if(error !== null)
2012-04-05 20:59:01 +08:00
console.log(error);
2017-06-05 22:12:12 +08:00
readme = tc.replaceResults(readme, process.cwd(), stdout.replace(/[\r\n]*$/, ""));
readme = tc.replaceBase(readme);
fs.writeFile("README.md", readme, "utf-8", function() {});
2012-04-05 20:59:01 +08:00
});
2017-06-05 22:12:12 +08:00
});