Skip to content

Commit

Permalink
feat: typescript flag for ts tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jan 22, 2024
1 parent 9744f23 commit 559ef8b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 37 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"sver": "^1.8.4"
},
"devDependencies": {
"@jspm/core": "^2.0.1",
"@swc/cli": "^0.1.61",
"@swc/core": "^1.3.35",
"@types/vscode": "^1.75.1",
Expand Down
10 changes: 9 additions & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,15 @@ export interface GeneratorOptions {
/**
* Support tracing CommonJS dependencies locally. This is necessary if you
* are using the "nodemodules" provider and have CommonJS dependencies.
* Disabled by default.
*/
commonJS?: boolean;

/**
* Support tracing TypeScript dependencies when generating the import map.
* Disabled by default.
*/
typeScript?: boolean;
}

export interface ModuleAnalysis {
Expand Down Expand Up @@ -382,6 +389,7 @@ export class Generator {
fetchOptions = {},
ignore = [],
commonJS = false,
typeScript = false,
}: GeneratorOptions = {}) {
// Initialise the debug logger:
const { log, logStream } = createLogger();
Expand Down Expand Up @@ -443,7 +451,7 @@ export class Generator {
}

// Initialise the resolver:
const resolver = new Resolver(env, log, fetchOpts, true);
const resolver = new Resolver({ env, log, fetchOpts, preserveSymlinks: true, traceCjs: commonJS, traceTs: typeScript });
if (customProviders) {
for (const provider of Object.keys(customProviders)) {
resolver.addCustomProvider(provider, customProviders[provider]);
Expand Down
53 changes: 35 additions & 18 deletions src/trace/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,27 @@ export class Resolver {
pcfgPromises: Record<string, Promise<void>> = Object.create(null);
pcfgs: Record<string, PackageConfig | null> = Object.create(null);
fetchOpts: any;
preserveSymlinks = false;
preserveSymlinks;
providers = defaultProviders;
env: string[];
cjsEnv: string[];
constructor(
env: string[],
log: Log,
fetchOpts?: any,
preserveSymlinks = false
) {
traceCjs;
traceTs;
constructor({
env,
log,
fetchOpts,
preserveSymlinks = false,
traceCjs = true,
traceTs = true,
}: {
env: string[];
log: Log;
fetchOpts?: any;
preserveSymlinks?: boolean;
traceCjs?: boolean;
traceTs?: boolean;
}) {
if (env.includes("require"))
throw new Error("Cannot manually pass require condition");
if (!env.includes("import")) env.push("import");
Expand All @@ -73,6 +84,8 @@ export class Resolver {
this.log = log;
this.fetchOpts = fetchOpts;
this.preserveSymlinks = preserveSymlinks;
this.traceCjs = traceCjs;
this.traceTs = traceTs;
}

addCustomProvider(name: string, provider: Provider) {
Expand Down Expand Up @@ -838,9 +851,10 @@ export class Resolver {
// TODO: headers over extensions for non-file URLs
try {
if (
resolvedUrl.endsWith(".ts") ||
resolvedUrl.endsWith(".tsx") ||
resolvedUrl.endsWith(".jsx")
this.traceTs &&
(resolvedUrl.endsWith(".ts") ||
resolvedUrl.endsWith(".tsx") ||
resolvedUrl.endsWith(".jsx"))
)
return await createTsAnalysis(source, resolvedUrl);

Expand Down Expand Up @@ -876,6 +890,7 @@ export class Resolver {
// Support CommonJS package boundary checks for non-ESM on file: protocol only
if (isRequire) {
if (
this.traceCjs &&
!(
resolvedUrl.endsWith(".mjs") ||
(resolvedUrl.endsWith(".js") &&
Expand All @@ -885,16 +900,18 @@ export class Resolver {
)
)?.type === "module")
)
)
) {
return createCjsAnalysis(imports, source, resolvedUrl);
}
} else if (
resolvedUrl.endsWith(".cjs") ||
(resolvedUrl.endsWith(".js") &&
(
await this.getPackageConfig(
await this.getPackageBase(resolvedUrl)
)
)?.type !== "module")
this.traceCjs &&
(resolvedUrl.endsWith(".cjs") ||
(resolvedUrl.endsWith(".js") &&
(
await this.getPackageConfig(
await this.getPackageBase(resolvedUrl)
)
)?.type !== "module"))
) {
return createCjsAnalysis(imports, source, resolvedUrl);
}
Expand Down
8 changes: 6 additions & 2 deletions test/resolve/ts.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Generator } from "@jspm/generator";
import assert from "assert";
import { strictEqual } from "assert";

if (typeof document === "undefined") {
const generator = new Generator({
mapUrl: import.meta.url,
defaultProvider: "nodemodules",
typeScript: true,
});

await generator.link("./tspkg/main.ts");

assert.strictEqual(
const map = generator.getMap();
strictEqual(typeof map.imports['node:fs'], 'string');

strictEqual(
generator.getAnalysis(new URL("./tspkg/dep.ts", import.meta.url)).format,
"typescript"
);
Expand Down
1 change: 1 addition & 0 deletions test/resolve/tspkg/dep.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import 'node:fs';
export var dep: string = "dep";
32 changes: 16 additions & 16 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scopes": {
"../": {
"#fetch": "../dist/fetch-native.js",
"@babel/core": "https://ga.jspm.io/npm:@babel/core@7.23.5/lib/index.js",
"@babel/core": "https://ga.jspm.io/npm:@babel/core@7.23.7/lib/index.js",
"@babel/plugin-syntax-import-assertions": "https://ga.jspm.io/npm:@babel/plugin-syntax-import-assertions@7.23.3/lib/index.js",
"@babel/preset-typescript": "https://ga.jspm.io/npm:@babel/preset-typescript@7.23.3/lib/index.js",
"@jspm/import-map": "https://ga.jspm.io/npm:@jspm/import-map@1.0.7/dist/map.js",
Expand All @@ -23,19 +23,19 @@
"url": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/url.js"
},
"https://ga.jspm.io/": {
"#lib/config/files/index.js": "https://ga.jspm.io/npm:@babel/core@7.23.5/lib/config/files/index-browser.js",
"#lib/config/resolve-targets.js": "https://ga.jspm.io/npm:@babel/core@7.23.5/lib/config/resolve-targets-browser.js",
"#lib/transform-file.js": "https://ga.jspm.io/npm:@babel/core@7.23.5/lib/transform-file-browser.js",
"#lib/config/files/index.js": "https://ga.jspm.io/npm:@babel/core@7.23.7/lib/config/files/index-browser.js",
"#lib/config/resolve-targets.js": "https://ga.jspm.io/npm:@babel/core@7.23.7/lib/config/resolve-targets-browser.js",
"#lib/transform-file.js": "https://ga.jspm.io/npm:@babel/core@7.23.7/lib/transform-file-browser.js",
"#node.js": "https://ga.jspm.io/npm:browserslist@4.22.2/browser.js",
"@ampproject/remapping": "https://ga.jspm.io/npm:@ampproject/remapping@2.2.1/dist/remapping.umd.js",
"@babel/code-frame": "https://ga.jspm.io/npm:@babel/code-frame@7.23.5/lib/index.js",
"@babel/compat-data/native-modules": "https://ga.jspm.io/npm:@babel/compat-data@7.23.5/native-modules.js",
"@babel/compat-data/plugins": "https://ga.jspm.io/npm:@babel/compat-data@7.23.5/plugins.js",
"@babel/core": "https://ga.jspm.io/npm:@babel/core@7.23.5/lib/index.js",
"@babel/generator": "https://ga.jspm.io/npm:@babel/generator@7.23.5/lib/index.js",
"@babel/core": "https://ga.jspm.io/npm:@babel/core@7.23.7/lib/index.js",
"@babel/generator": "https://ga.jspm.io/npm:@babel/generator@7.23.6/lib/index.js",
"@babel/helper-annotate-as-pure": "https://ga.jspm.io/npm:@babel/helper-annotate-as-pure@7.22.5/lib/index.js",
"@babel/helper-compilation-targets": "https://ga.jspm.io/npm:@babel/helper-compilation-targets@7.22.15/lib/index.js",
"@babel/helper-create-class-features-plugin": "https://ga.jspm.io/npm:@babel/helper-create-class-features-plugin@7.23.5/lib/index.js",
"@babel/helper-compilation-targets": "https://ga.jspm.io/npm:@babel/helper-compilation-targets@7.23.6/lib/index.js",
"@babel/helper-create-class-features-plugin": "https://ga.jspm.io/npm:@babel/helper-create-class-features-plugin@7.23.7/lib/index.js",
"@babel/helper-environment-visitor": "https://ga.jspm.io/npm:@babel/helper-environment-visitor@7.22.20/lib/index.js",
"@babel/helper-function-name": "https://ga.jspm.io/npm:@babel/helper-function-name@7.23.0/lib/index.js",
"@babel/helper-hoist-variables": "https://ga.jspm.io/npm:@babel/helper-hoist-variables@7.22.5/lib/index.js",
Expand All @@ -51,31 +51,31 @@
"@babel/helper-string-parser": "https://ga.jspm.io/npm:@babel/helper-string-parser@7.23.4/lib/index.js",
"@babel/helper-validator-identifier": "https://ga.jspm.io/npm:@babel/helper-validator-identifier@7.22.20/lib/index.js",
"@babel/helper-validator-option": "https://ga.jspm.io/npm:@babel/helper-validator-option@7.23.5/lib/index.js",
"@babel/helpers": "https://ga.jspm.io/npm:@babel/helpers@7.23.5/lib/index.js",
"@babel/helpers": "https://ga.jspm.io/npm:@babel/helpers@7.23.8/lib/index.js",
"@babel/highlight": "https://ga.jspm.io/npm:@babel/highlight@7.23.4/lib/index.js",
"@babel/parser": "https://ga.jspm.io/npm:@babel/parser@7.23.5/lib/index.js",
"@babel/parser": "https://ga.jspm.io/npm:@babel/parser@7.23.6/lib/index.js",
"@babel/plugin-syntax-jsx": "https://ga.jspm.io/npm:@babel/plugin-syntax-jsx@7.23.3/lib/index.js",
"@babel/plugin-syntax-typescript": "https://ga.jspm.io/npm:@babel/plugin-syntax-typescript@7.23.3/lib/index.js",
"@babel/plugin-transform-modules-commonjs": "https://ga.jspm.io/npm:@babel/plugin-transform-modules-commonjs@7.23.3/lib/index.js",
"@babel/plugin-transform-typescript": "https://ga.jspm.io/npm:@babel/plugin-transform-typescript@7.23.5/lib/index.js",
"@babel/plugin-transform-typescript": "https://ga.jspm.io/npm:@babel/plugin-transform-typescript@7.23.6/lib/index.js",
"@babel/template": "https://ga.jspm.io/npm:@babel/template@7.22.15/lib/index.js",
"@babel/traverse": "https://ga.jspm.io/npm:@babel/traverse@7.23.5/lib/index.js",
"@babel/types": "https://ga.jspm.io/npm:@babel/types@7.23.5/lib/index.js",
"@babel/traverse": "https://ga.jspm.io/npm:@babel/traverse@7.23.7/lib/index.js",
"@babel/types": "https://ga.jspm.io/npm:@babel/types@7.23.6/lib/index.js",
"@jridgewell/gen-mapping": "https://ga.jspm.io/npm:@jridgewell/gen-mapping@0.3.3/dist/gen-mapping.umd.js",
"@jridgewell/resolve-uri": "https://ga.jspm.io/npm:@jridgewell/resolve-uri@3.1.1/dist/resolve-uri.umd.js",
"@jridgewell/set-array": "https://ga.jspm.io/npm:@jridgewell/set-array@1.1.2/dist/set-array.umd.js",
"@jridgewell/sourcemap-codec": "https://ga.jspm.io/npm:@jridgewell/sourcemap-codec@1.4.15/dist/sourcemap-codec.umd.js",
"@jridgewell/trace-mapping": "https://ga.jspm.io/npm:@jridgewell/trace-mapping@0.3.20/dist/trace-mapping.umd.js",
"@jridgewell/trace-mapping": "https://ga.jspm.io/npm:@jridgewell/trace-mapping@0.3.22/dist/trace-mapping.umd.js",
"ansi-styles": "https://ga.jspm.io/npm:ansi-styles@3.2.1/index.js",
"browserslist": "https://ga.jspm.io/npm:browserslist@4.22.2/index.js",
"buffer": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/buffer.js",
"caniuse-lite/dist/unpacker/agents": "https://ga.jspm.io/npm:caniuse-lite@1.0.30001566/dist/unpacker/agents.js",
"caniuse-lite/dist/unpacker/agents": "https://ga.jspm.io/npm:caniuse-lite@1.0.30001579/dist/unpacker/agents.js",
"chalk": "https://ga.jspm.io/npm:chalk@2.4.2/index.js",
"color-convert": "https://ga.jspm.io/npm:color-convert@1.9.3/index.js",
"color-name": "https://ga.jspm.io/npm:color-name@1.1.3/index.js",
"convert-source-map": "https://ga.jspm.io/npm:convert-source-map@2.0.0/index.js",
"debug": "https://ga.jspm.io/npm:debug@4.3.4/src/browser.js",
"electron-to-chromium/versions": "https://ga.jspm.io/npm:electron-to-chromium@1.4.609/versions.js",
"electron-to-chromium/versions": "https://ga.jspm.io/npm:electron-to-chromium@1.4.640/versions.js",
"escape-string-regexp": "https://ga.jspm.io/npm:escape-string-regexp@1.0.5/index.js",
"fs": "https://ga.jspm.io/npm:@jspm/core@2.0.1/nodelibs/browser/fs.js",
"gensync": "https://ga.jspm.io/npm:gensync@1.0.0-beta.2/index.js",
Expand Down

0 comments on commit 559ef8b

Please sign in to comment.