Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ export async function main(argv, options) {

// Initialize the program
program = assemblyscript.newProgram(compilerOptions);
program.parser.baseDir = baseDir;

// Collect transforms *constructors* from the `--transform` CLI flag as well
// as the `transform` option into the `transforms` array.
Expand Down Expand Up @@ -576,13 +577,34 @@ export async function main(argv, options) {
}
paths.push(...opts.path);
for (const currentDir of paths.map(p => path.relative(baseDir, p))) {
const plainName = filePath;
// Check for package.json and resolve main
let entryPath = filePath;
const packageJsonPath = path.join(currentDir, packageName, 'package.json');
const packageJsonText = await readFile(packageJsonPath, baseDir);
if (packageJsonText != null) {
try {
const pkg = JSON.parse(packageJsonText);
let main = pkg.ascMain || pkg.assembly || pkg.main || pkg.module;
if (!main && pkg.exports && pkg.exports["."] && pkg.exports["."].default) {
main = pkg.exports["."].default;
}
if (main) {
// Remove .ts extension if present
entryPath = main.replace(/\.ts$/, '');
if (entryPath.startsWith("./")) entryPath = entryPath.substring(2);
}
} catch (e) {
// Ignore parse errors
}
}
if (!entryPath) entryPath = "index";
const plainName = entryPath;
if ((sourceText = await readFile(path.join(currentDir, packageName, plainName + extension), baseDir)) != null) {
sourcePath = `${libraryPrefix}${packageName}/${plainName}${extension}`;
packageBases.set(sourcePath.replace(extension_re, ""), path.join(currentDir, packageName));
break;
}
const indexName = `${filePath}/index`;
const indexName = `${entryPath}/index`;
if ((sourceText = await readFile(path.join(currentDir, packageName, indexName + extension), baseDir)) != null) {
sourcePath = `${libraryPrefix}${packageName}/${indexName}${extension}`;
packageBases.set(sourcePath.replace(extension_re, ""), path.join(currentDir, packageName));
Expand Down
2 changes: 2 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
CharCode
} from "./util";

import { fs, path } from "../util/node.js";

Check warning on line 37 in src/ast.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

'path' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

Check warning on line 37 in src/ast.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

'fs' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

Check warning on line 37 in src/ast.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

'path' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

Check warning on line 37 in src/ast.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

'fs' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

Check warning on line 37 in src/ast.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

'path' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

Check warning on line 37 in src/ast.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

'fs' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

Check warning on line 37 in src/ast.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

'path' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

Check warning on line 37 in src/ast.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

'fs' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

import {
ExpressionRef
} from "./module";
Expand Down
41 changes: 40 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
mangleInternalPath
} from "./ast";

import { fs, path, process } from "../util/node.js";

Check warning on line 94 in src/parser.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

'process' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

Check warning on line 94 in src/parser.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

'process' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

Check warning on line 94 in src/parser.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

'process' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

Check warning on line 94 in src/parser.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

'process' is defined but never used. Allowed unused vars must match /^[A-Z](?:From|To)?$/u

/** Represents a dependee. */
class Dependee {
constructor(
Expand All @@ -102,6 +104,8 @@
/** Parser interface. */
export class Parser extends DiagnosticEmitter {

/** Base directory for resolving files. */
baseDir: string;
/** Source file names to be requested next. */
backlog: string[] = new Array();
/** Source file names already seen, that is processed or backlogged. */
Expand All @@ -122,10 +126,12 @@
/** Constructs a new parser. */
constructor(
diagnostics: DiagnosticMessage[] | null = null,
sources: Source[] = []
sources: Source[] = [],
baseDir: string = ""
) {
super(diagnostics);
this.sources = sources;
this.baseDir = baseDir;
}

/** Parses a file and adds its definitions to the program. */
Expand Down Expand Up @@ -2791,6 +2797,13 @@
} else {
ret = Node.createImportStatement(members, path, tn.range(startPos, tn.pos));
}
// Resolve package.json for non-relative imports
if (!path.value.startsWith(".")) {
const resolvedPath = this.resolvePackagePath(path.value);
if (resolvedPath) {
ret.internalPath = resolvedPath;
}
}
let internalPath = ret.internalPath;
if (!this.seenlog.has(internalPath)) {
this.dependees.set(internalPath, new Dependee(assert(this.currentSource), path));
Expand Down Expand Up @@ -2854,6 +2867,32 @@
return null;
}

/** Resolves a package name to its internal path via package.json. */
private resolvePackagePath(packageName: string): string | null {
try {
const nodeModulesPath = path.join(this.baseDir, 'node_modules');
const packageJsonPath = path.join(nodeModulesPath, packageName, 'package.json');
if (fs.existsSync(packageJsonPath)) {
const content = fs.readFileSync(packageJsonPath, 'utf8');
const pkg = JSON.parse(content);
let entry = pkg.ascMain || pkg.assembly || pkg.main || pkg.module;
if (!entry && pkg.exports && pkg.exports["."] && pkg.exports["."].default) {
entry = pkg.exports["."].default;
}
if (entry) {
if (entry.startsWith("./")) entry = entry.substring(2);
// Remove .ts extension if present
const entryPath = entry.replace(/\.ts$/, '');
const result = mangleInternalPath(LIBRARY_PREFIX + packageName + '/' + entryPath);
return result;
}
}
} catch (e) {
// Ignore errors
}
return null;
}

parseExportImport(
tn: Tokenizer,
startPos: i32
Expand Down
Loading