From 08d9ba12aaada93f773b2072533ac200b7aa1b10 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Fri, 9 Feb 2018 16:40:53 +0100 Subject: [PATCH] Improve bundled asc example a bit, see #25 --- bin/asc.js | 6 +++--- tests/bundled-asc.js | 22 ++++++++++++++++------ webpack.config.js | 1 + 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/bin/asc.js b/bin/asc.js index 677c4707..4d798946 100644 --- a/bin/asc.js +++ b/bin/asc.js @@ -44,6 +44,7 @@ function main(argv, options, callback) { const readFile = options.readFile || readFileNode; const writeFile = options.writeFile || writeFileNode; const listFiles = options.listFiles || listFilesNode; + const stats = options.stats || createStats(); // All of the above must be specified in browser environments if (!stdout) throw Error("'options.stdout' must be specified"); @@ -54,9 +55,6 @@ function main(argv, options, callback) { if (listFiles === listFilesNode) throw Error("'options.listFiles' must be specified"); } - // Record compilation times - const stats = createStats(); - const args = parseArguments(argv); const indent = 24; @@ -507,6 +505,8 @@ function createStats() { }; } +exports.createStats = createStats; + if (!process.hrtime) process.hrtime = require("browser-process-hrtime"); diff --git a/tests/bundled-asc.js b/tests/bundled-asc.js index 28b4497a..d1e423f2 100644 --- a/tests/bundled-asc.js +++ b/tests/bundled-asc.js @@ -2,30 +2,40 @@ var asc = require("../dist/asc.js"); var stdout = asc.createMemoryStream(); var stderr = asc.createMemoryStream(); +var stats = asc.createStats(); process.exitCode = asc.main([ - "test.ts" + "test.ts", + "--validate", + "--optimize", + "--measure", + "--textFile", // -> stdout + "--binaryFile", "test.wasm", + "--sourceMap" ], { stdout: stdout, stderr: stderr, + stats: stats, readFile: function(filename) { - console.log("readFile: " + filename); + console.log("<< readFile: " + filename); if (filename === "/test.ts") { // sic: browser path return "export function foo(): void {}"; } throw Error("File not found: " + filename); }, writeFile: function(filename, contents) { - console.log("writeFile: " + filename); + console.log(">> writeFile: " + filename + " (" + contents.length + " bytes)"); }, listFiles: function(dirname) { - console.log("listFiles: " + dirname); + console.log("<< listFiles: " + dirname); return []; } }); -console.log("stdout >>>"); +console.log(">> stdout >>"); console.log(stdout.toString()); -console.log("stderr >>>"); +console.log(">> stderr >>"); console.error(stderr.toString()); + +console.log(">> stats >>", stats); diff --git a/webpack.config.js b/webpack.config.js index bd5937e9..b5b22503 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -68,6 +68,7 @@ const bin = { const libFiles = require("glob").sync("**/*.ts", { cwd: libDir }); const lib = {}; libFiles.forEach(file => { + // console.log("bundling '(lib)/" + file + "'"); var source = fs.readFileSync(path.join(libDir, file), { encoding: "utf8" }); lib["(lib)/" + file.replace(/\.ts$/, "")] = JSON.stringify(source); });