Skip to content

Commit 20e882c

Browse files
MaxGraeydcodeIO
authored andcommitted
Improve transform module setup (AssemblyScript#985)
1 parent fad3a76 commit 20e882c

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

cli/asc.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,13 @@ exports.main = function main(argv, options, callback) {
284284
// Set up transforms
285285
const transforms = [];
286286
if (args.transform) {
287+
let tsNodeRegistered = false;
287288
let transformArgs = args.transform;
288289
for (let i = 0, k = transformArgs.length; i < k; ++i) {
289290
let filename = transformArgs[i].trim();
290-
if (/\.ts$/.test(filename)) {
291+
if (!tsNodeRegistered && filename.endsWith('.ts')) {
291292
require("ts-node").register({ transpileOnly: true, skipProject: true, compilerOptions: { target: "ES2016" } });
293+
tsNodeRegistered = true;
292294
}
293295
try {
294296
const classOrModule = require(require.resolve(filename, { paths: [baseDir, process.cwd()] }));
@@ -370,8 +372,11 @@ exports.main = function main(argv, options, callback) {
370372
var sourceText = null; // text reported back to the compiler
371373
var sourcePath = null; // path reported back to the compiler
372374

375+
const libraryPrefix = exports.libraryPrefix;
376+
const libraryFiles = exports.libraryFiles;
377+
373378
// Try file.ts, file/index.ts, file.d.ts
374-
if (!internalPath.startsWith(exports.libraryPrefix)) {
379+
if (!internalPath.startsWith(libraryPrefix)) {
375380
if ((sourceText = readFile(sourcePath = internalPath + ".ts", baseDir)) == null) {
376381
if ((sourceText = readFile(sourcePath = internalPath + "/index.ts", baseDir)) == null) {
377382
// portable d.ts: uses the .js file next to it in JS or becomes an import in Wasm
@@ -381,22 +386,22 @@ exports.main = function main(argv, options, callback) {
381386

382387
// Search library in this order: stdlib, custom lib dirs, paths
383388
} else {
384-
const plainName = internalPath.substring(exports.libraryPrefix.length);
389+
const plainName = internalPath.substring(libraryPrefix.length);
385390
const indexName = plainName + "/index";
386-
if (exports.libraryFiles.hasOwnProperty(plainName)) {
387-
sourceText = exports.libraryFiles[plainName];
388-
sourcePath = exports.libraryPrefix + plainName + ".ts";
389-
} else if (exports.libraryFiles.hasOwnProperty(indexName)) {
390-
sourceText = exports.libraryFiles[indexName];
391-
sourcePath = exports.libraryPrefix + indexName + ".ts";
391+
if (libraryFiles.hasOwnProperty(plainName)) {
392+
sourceText = libraryFiles[plainName];
393+
sourcePath = libraryPrefix + plainName + ".ts";
394+
} else if (libraryFiles.hasOwnProperty(indexName)) {
395+
sourceText = libraryFiles[indexName];
396+
sourcePath = libraryPrefix + indexName + ".ts";
392397
} else { // custom lib dirs
393398
for (const libDir of customLibDirs) {
394399
if ((sourceText = readFile(plainName + ".ts", libDir)) != null) {
395-
sourcePath = exports.libraryPrefix + plainName + ".ts";
400+
sourcePath = libraryPrefix + plainName + ".ts";
396401
break;
397402
} else {
398403
if ((sourceText = readFile(indexName + ".ts", libDir)) != null) {
399-
sourcePath = exports.libraryPrefix + indexName + ".ts";
404+
sourcePath = libraryPrefix + indexName + ".ts";
400405
break;
401406
}
402407
}
@@ -412,7 +417,7 @@ exports.main = function main(argv, options, callback) {
412417
const absBasePath = path.isAbsolute(basePath) ? basePath : path.join(baseDir, basePath);
413418
const paths = [];
414419
for (let parts = absBasePath.split(SEP), i = parts.length, k = SEP == "/" ? 0 : 1; i >= k; --i) {
415-
if (parts[i - 1] != "node_modules") paths.push(parts.slice(0, i).join(SEP) + SEP + "node_modules");
420+
if (parts[i - 1] !== "node_modules") paths.push(parts.slice(0, i).join(SEP) + SEP + "node_modules");
416421
}
417422
for (const currentPath of paths.concat(...args.path).map(p => path.relative(baseDir, p))) {
418423
if (args.traceResolution) stderr.write(" in " + path.join(currentPath, packageName) + EOL);
@@ -435,14 +440,14 @@ exports.main = function main(argv, options, callback) {
435440
const mainDir = path.join(currentPath, packageName, mainPath);
436441
const plainName = filePath;
437442
if ((sourceText = readFile(path.join(mainDir, plainName + ".ts"), baseDir)) != null) {
438-
sourcePath = exports.libraryPrefix + packageName + "/" + plainName + ".ts";
443+
sourcePath = libraryPrefix + packageName + "/" + plainName + ".ts";
439444
packageBases.set(sourcePath.replace(/\.ts$/, ""), path.join(currentPath, packageName));
440445
if (args.traceResolution) stderr.write(" -> " + path.join(mainDir, plainName + ".ts") + EOL);
441446
break;
442447
} else if (!isPackageRoot) {
443448
const indexName = filePath + "/index";
444449
if ((sourceText = readFile(path.join(mainDir, indexName + ".ts"), baseDir)) !== null) {
445-
sourcePath = exports.libraryPrefix + packageName + "/" + indexName + ".ts";
450+
sourcePath = libraryPrefix + packageName + "/" + indexName + ".ts";
446451
packageBases.set(sourcePath.replace(/\.ts$/, ""), path.join(currentPath, packageName));
447452
if (args.traceResolution) stderr.write(" -> " + path.join(mainDir, indexName + ".ts") + EOL);
448453
break;
@@ -453,10 +458,8 @@ exports.main = function main(argv, options, callback) {
453458
}
454459
}
455460
}
456-
457461
// No such file
458462
if (sourceText == null) return null;
459-
460463
return { sourceText, sourcePath };
461464
}
462465

0 commit comments

Comments
 (0)