Skip to content

Commit 60728c3

Browse files
committed
Initial asc browser bundle, see AssemblyScript#25
1 parent cd9a3b5 commit 60728c3

File tree

6 files changed

+722
-54
lines changed

6 files changed

+722
-54
lines changed

bin/asc.js

Lines changed: 90 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,32 @@ const fs = require("fs");
33
const os = require("os");
44

55
// Use distribution files if present, otherwise run the sources directly
6-
const { assemblyscript, isDev } = (function bootstrap() {
7-
var assemblyscript, isDev;
6+
var assemblyscript, isDev;
7+
try {
8+
assemblyscript = require("../dist/assemblyscript.js");
9+
isDev = false;
10+
try { require("source-map-support").install(); } catch (e) {} // optional
11+
} catch (e) {
812
try {
9-
assemblyscript = require("../dist/assemblyscript.js");
10-
isDev = false;
11-
try { require("source-map-support").install(); } catch (e) {} // optional
12-
} catch (e) {
1313
require("ts-node").register({ project: require("path").join(__dirname, "..", "src") });
1414
require("../src/glue/js");
1515
assemblyscript = require("../src");
1616
isDev = true;
17+
} catch (e) {
18+
assemblyscript = require("./assemblyscript"); // last resort: browser bundle under node
19+
isDev = false;
1720
}
18-
return { assemblyscript, isDev };
19-
})();
21+
}
2022

2123
// Common constants
22-
const VERSION = require("../package.json").version + (isDev ? "-dev" : "");
24+
25+
const VERSION = typeof BUNDLE_VERSION === "string" ? BUNDLE_VERSION : require("../package.json").version + (isDev ? "-dev" : "");
2326
const OPTIONS = require("./asc.json");
2427
const SOURCEMAP_ROOT = "assemblyscript:///";
2528
const LIBRARY_PREFIX = assemblyscript.LIBRARY_PREFIX;
2629
const DEFAULT_OPTIMIZE_LEVEL = 2;
2730
const DEFAULT_SHRINK_LEVEL = 1;
31+
const LIBRARY = typeof BUNDLE_LIBRARY !== "undefined" ? BUNDLE_LIBRARY : {};
2832

2933
exports.VERSION = VERSION;
3034

@@ -37,6 +41,18 @@ function main(argv, options, callback) {
3741

3842
const stdout = options.stdout || process.stdout;
3943
const stderr = options.stderr || process.stderr;
44+
const readFile = options.readFile || readFileNode;
45+
const writeFile = options.writeFile || writeFileNode;
46+
const listFiles = options.listFiles || listFilesNode;
47+
48+
// All of the above must be specified in browser environments
49+
if (!stdout) throw Error("'options.stdout' must be specified");
50+
if (!stderr) throw Error("'options.stderr' must be specified");
51+
if (!fs.readFileSync) {
52+
if (readFile === readFileNode) throw Error("'options.readFile' must be specified");
53+
if (writeFile === writeFileNode) throw Error("'options.writeFile' must be specified");
54+
if (listFiles === listFilesNode) throw Error("'options.listFiles' must be specified");
55+
}
4056

4157
// Record compilation times
4258
const stats = createStats();
@@ -98,7 +114,8 @@ function main(argv, options, callback) {
98114
const baseDir = args.baseDir != null ? path.resolve(args.baseDir) : process.cwd();
99115

100116
// Include standard library if --noLib isn't set
101-
const libDirs = args.noLib ? [] : [ path.join(__dirname, "..", "std", "assembly") ];
117+
const stdLibDir = path.join(__dirname, "..", "std", "assembly");
118+
const libDirs = args.noLib ? [] : [ stdLibDir ];
102119

103120
// Include custom library components (with or without stdlib)
104121
if (args.lib) {
@@ -136,10 +153,14 @@ function main(argv, options, callback) {
136153
// Load library file if explicitly requested
137154
if (sourcePath.startsWith(LIBRARY_PREFIX)) {
138155
for (let i = 0, k = libDirs.length; i < k; ++i) {
139-
sourceText = readFile(path.join(libDirs[i], sourcePath.substring(LIBRARY_PREFIX.length) + ".ts"));
140-
if (sourceText !== null) {
141-
sourcePath += ".ts";
142-
break;
156+
if (LIBRARY.hasOwnProperty(sourcePath))
157+
sourceText = LIBRARY[sourcePath];
158+
else {
159+
sourceText = readFile(path.join(libDirs[i], sourcePath.substring(LIBRARY_PREFIX.length) + ".ts"));
160+
if (sourceText !== null) {
161+
sourcePath += ".ts";
162+
break;
163+
}
143164
}
144165
}
145166

@@ -149,11 +170,15 @@ function main(argv, options, callback) {
149170
if (sourceText === null) {
150171
sourceText = readFile(path.join(baseDir, sourcePath, "index.ts"));
151172
if (sourceText === null) {
152-
for (let i = 0, k =libDirs.length; i < k; ++i) {
153-
sourceText = readFile(path.join(libDirs[i], sourcePath + ".ts"));
154-
if (sourceText !== null) {
155-
sourcePath = LIBRARY_PREFIX + sourcePath + ".ts";
156-
break;
173+
for (let i = 0, k = libDirs.length; i < k; ++i) {
174+
if (LIBRARY.hasOwnProperty(LIBRARY_PREFIX + sourcePath))
175+
sourceText = LIBRARY[LIBRARY_PREFIX + sourcePath];
176+
else {
177+
sourceText = readFile(path.join(libDirs[i], sourcePath + ".ts"));
178+
if (sourceText !== null) {
179+
sourcePath = LIBRARY_PREFIX + sourcePath + ".ts";
180+
break;
181+
}
157182
}
158183
}
159184
if (sourceText === null)
@@ -171,10 +196,18 @@ function main(argv, options, callback) {
171196
}
172197

173198
// Include (other) library components
199+
var hasBundledLibrary = false;
200+
if (!args.noLib)
201+
Object.keys(LIBRARY).forEach(libPath => {
202+
if (libPath.lastIndexOf("/") >= LIBRARY_PREFIX.length) return;
203+
stats.parseCount++;
204+
stats.parseTime += measure(() => { parser = assemblyscript.parseFile(LIBRARY[libPath], libPath + ".ts", parser, false); });
205+
hasBundledLibrary = true;
206+
});
174207
for (let i = 0, k = libDirs.length; i < k; ++i) {
208+
if (i === 0 && hasBundledLibrary) continue;
175209
let libDir = libDirs[i];
176-
let libFiles;
177-
stats.readTime += measure(() => { libFiles = require("glob").sync("*.ts", { cwd: libDir }) });
210+
let libFiles = listFiles(libDir);
178211
for (let j = 0, l = libFiles.length; j < l; ++j) {
179212
let libPath = libFiles[j];
180213
let libText = readFile(path.join(libDir, libPath));
@@ -383,18 +416,18 @@ function main(argv, options, callback) {
383416
printStats(stats, stderr);
384417
return callback(null);
385418

386-
function readFile(filename) {
419+
function readFileNode(filename) {
387420
try {
388421
var text;
389422
stats.readCount++;
390-
stats.readTime += measure(() => text = fs.readFileSync(filename, { encoding: "utf8" }));
423+
stats.readTime += measure(() => { text = fs.readFileSync(filename, { encoding: "utf8" }); });
391424
return text;
392425
} catch (e) {
393426
return null;
394427
}
395428
}
396429

397-
function writeFile(filename, contents) {
430+
function writeFileNode(filename, contents) {
398431
try {
399432
stats.writeCount++;
400433
stats.writeTime += measure(() => fs.writeFileSync(filename, contents, typeof contents === "string" ? { encoding: "utf8" } : undefined));
@@ -404,6 +437,16 @@ function main(argv, options, callback) {
404437
}
405438
}
406439

440+
function listFilesNode(dirname) {
441+
var files;
442+
try {
443+
stats.readTime += measure(() => { files = require("glob").sync("*.ts", { cwd: dirname }) });
444+
return files;
445+
} catch (e) {
446+
return [];
447+
}
448+
}
449+
407450
function writeStdout(contents) {
408451
if (!writeStdout.used) {
409452
stats.writeCount++;
@@ -464,7 +507,8 @@ function createStats() {
464507
};
465508
}
466509

467-
exports.createStats = createStats;
510+
if (!process.hrtime)
511+
process.hrtime = require("browser-process-hrtime");
468512

469513
function measure(fn) {
470514
const start = process.hrtime();
@@ -488,3 +532,24 @@ function printStats(stats, output) {
488532
}
489533

490534
exports.printStats = printStats;
535+
536+
function createMemoryStream(fn) {
537+
var stream = [];
538+
stream.write = function(chunk) {
539+
if (typeof chunk === "string") {
540+
this.push(Buffer.from(chunk, "utf8"));
541+
} else {
542+
this.push(chunk);
543+
}
544+
if (fn) fn(chunk);
545+
};
546+
stream.toBuffer = function() {
547+
return Buffer.concat(this);
548+
};
549+
stream.toString = function() {
550+
return this.toBuffer().toString("utf8");
551+
};
552+
return stream;
553+
}
554+
555+
exports.createMemoryStream = createMemoryStream;

0 commit comments

Comments
 (0)