Improve bundled asc example a bit, see #25

This commit is contained in:
dcodeIO 2018-02-09 16:40:53 +01:00
parent 60728c38fd
commit 08d9ba12aa
3 changed files with 20 additions and 9 deletions

View File

@ -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");

View File

@ -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);

View File

@ -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);
});