Skip to content

Commit eb034be

Browse files
authored
feat: typescript flag for ts tracing (#340)
1 parent 9744f23 commit eb034be

File tree

7 files changed

+75
-37
lines changed

7 files changed

+75
-37
lines changed

package-lock.json

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"sver": "^1.8.4"
5353
},
5454
"devDependencies": {
55+
"@jspm/core": "^2.0.1",
5556
"@swc/cli": "^0.1.61",
5657
"@swc/core": "^1.3.35",
5758
"@types/vscode": "^1.75.1",

src/generator.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,15 @@ export interface GeneratorOptions {
293293
/**
294294
* Support tracing CommonJS dependencies locally. This is necessary if you
295295
* are using the "nodemodules" provider and have CommonJS dependencies.
296+
* Disabled by default.
296297
*/
297298
commonJS?: boolean;
299+
300+
/**
301+
* Support tracing TypeScript dependencies when generating the import map.
302+
* Disabled by default.
303+
*/
304+
typeScript?: boolean;
298305
}
299306

300307
export interface ModuleAnalysis {
@@ -382,6 +389,7 @@ export class Generator {
382389
fetchOptions = {},
383390
ignore = [],
384391
commonJS = false,
392+
typeScript = false,
385393
}: GeneratorOptions = {}) {
386394
// Initialise the debug logger:
387395
const { log, logStream } = createLogger();
@@ -443,7 +451,7 @@ export class Generator {
443451
}
444452

445453
// Initialise the resolver:
446-
const resolver = new Resolver(env, log, fetchOpts, true);
454+
const resolver = new Resolver({ env, log, fetchOpts, preserveSymlinks: true, traceCjs: commonJS, traceTs: typeScript });
447455
if (customProviders) {
448456
for (const provider of Object.keys(customProviders)) {
449457
resolver.addCustomProvider(provider, customProviders[provider]);

src/trace/resolver.ts

+35-18
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,27 @@ export class Resolver {
5555
pcfgPromises: Record<string, Promise<void>> = Object.create(null);
5656
pcfgs: Record<string, PackageConfig | null> = Object.create(null);
5757
fetchOpts: any;
58-
preserveSymlinks = false;
58+
preserveSymlinks;
5959
providers = defaultProviders;
6060
env: string[];
6161
cjsEnv: string[];
62-
constructor(
63-
env: string[],
64-
log: Log,
65-
fetchOpts?: any,
66-
preserveSymlinks = false
67-
) {
62+
traceCjs;
63+
traceTs;
64+
constructor({
65+
env,
66+
log,
67+
fetchOpts,
68+
preserveSymlinks = false,
69+
traceCjs = true,
70+
traceTs = true,
71+
}: {
72+
env: string[];
73+
log: Log;
74+
fetchOpts?: any;
75+
preserveSymlinks?: boolean;
76+
traceCjs?: boolean;
77+
traceTs?: boolean;
78+
}) {
6879
if (env.includes("require"))
6980
throw new Error("Cannot manually pass require condition");
7081
if (!env.includes("import")) env.push("import");
@@ -73,6 +84,8 @@ export class Resolver {
7384
this.log = log;
7485
this.fetchOpts = fetchOpts;
7586
this.preserveSymlinks = preserveSymlinks;
87+
this.traceCjs = traceCjs;
88+
this.traceTs = traceTs;
7689
}
7790

7891
addCustomProvider(name: string, provider: Provider) {
@@ -838,9 +851,10 @@ export class Resolver {
838851
// TODO: headers over extensions for non-file URLs
839852
try {
840853
if (
841-
resolvedUrl.endsWith(".ts") ||
842-
resolvedUrl.endsWith(".tsx") ||
843-
resolvedUrl.endsWith(".jsx")
854+
this.traceTs &&
855+
(resolvedUrl.endsWith(".ts") ||
856+
resolvedUrl.endsWith(".tsx") ||
857+
resolvedUrl.endsWith(".jsx"))
844858
)
845859
return await createTsAnalysis(source, resolvedUrl);
846860

@@ -876,6 +890,7 @@ export class Resolver {
876890
// Support CommonJS package boundary checks for non-ESM on file: protocol only
877891
if (isRequire) {
878892
if (
893+
this.traceCjs &&
879894
!(
880895
resolvedUrl.endsWith(".mjs") ||
881896
(resolvedUrl.endsWith(".js") &&
@@ -885,16 +900,18 @@ export class Resolver {
885900
)
886901
)?.type === "module")
887902
)
888-
)
903+
) {
889904
return createCjsAnalysis(imports, source, resolvedUrl);
905+
}
890906
} else if (
891-
resolvedUrl.endsWith(".cjs") ||
892-
(resolvedUrl.endsWith(".js") &&
893-
(
894-
await this.getPackageConfig(
895-
await this.getPackageBase(resolvedUrl)
896-
)
897-
)?.type !== "module")
907+
this.traceCjs &&
908+
(resolvedUrl.endsWith(".cjs") ||
909+
(resolvedUrl.endsWith(".js") &&
910+
(
911+
await this.getPackageConfig(
912+
await this.getPackageBase(resolvedUrl)
913+
)
914+
)?.type !== "module"))
898915
) {
899916
return createCjsAnalysis(imports, source, resolvedUrl);
900917
}

test/resolve/ts.test.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { Generator } from "@jspm/generator";
2-
import assert from "assert";
2+
import { strictEqual } from "assert";
33

44
if (typeof document === "undefined") {
55
const generator = new Generator({
66
mapUrl: import.meta.url,
77
defaultProvider: "nodemodules",
8+
typeScript: true,
89
});
910

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

12-
assert.strictEqual(
13+
const map = generator.getMap();
14+
strictEqual(typeof map.imports['node:fs'], 'string');
15+
16+
strictEqual(
1317
generator.getAnalysis(new URL("./tspkg/dep.ts", import.meta.url)).format,
1418
"typescript"
1519
);

test/resolve/tspkg/dep.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
import 'node:fs';
12
export var dep: string = "dep";

test/test.html

+16-16
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"scopes": {
1111
"../": {
1212
"#fetch": "../dist/fetch-native.js",
13-
"@babel/core": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/index.js",
13+
"@babel/core": "https://ga.jspm.io/npm:@babel/[email protected].7/lib/index.js",
1414
"@babel/plugin-syntax-import-assertions": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
1515
"@babel/preset-typescript": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
1616
"@jspm/import-map": "https://ga.jspm.io/npm:@jspm/[email protected]/dist/map.js",
@@ -23,19 +23,19 @@
2323
"url": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/url.js"
2424
},
2525
"https://ga.jspm.io/": {
26-
"#lib/config/files/index.js": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/config/files/index-browser.js",
27-
"#lib/config/resolve-targets.js": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/config/resolve-targets-browser.js",
28-
"#lib/transform-file.js": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/transform-file-browser.js",
26+
"#lib/config/files/index.js": "https://ga.jspm.io/npm:@babel/[email protected].7/lib/config/files/index-browser.js",
27+
"#lib/config/resolve-targets.js": "https://ga.jspm.io/npm:@babel/[email protected].7/lib/config/resolve-targets-browser.js",
28+
"#lib/transform-file.js": "https://ga.jspm.io/npm:@babel/[email protected].7/lib/transform-file-browser.js",
2929
"#node.js": "https://ga.jspm.io/npm:[email protected]/browser.js",
3030
"@ampproject/remapping": "https://ga.jspm.io/npm:@ampproject/[email protected]/dist/remapping.umd.js",
3131
"@babel/code-frame": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
3232
"@babel/compat-data/native-modules": "https://ga.jspm.io/npm:@babel/[email protected]/native-modules.js",
3333
"@babel/compat-data/plugins": "https://ga.jspm.io/npm:@babel/[email protected]/plugins.js",
34-
"@babel/core": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/index.js",
35-
"@babel/generator": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/index.js",
34+
"@babel/core": "https://ga.jspm.io/npm:@babel/[email protected].7/lib/index.js",
35+
"@babel/generator": "https://ga.jspm.io/npm:@babel/[email protected].6/lib/index.js",
3636
"@babel/helper-annotate-as-pure": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
37-
"@babel/helper-compilation-targets": "https://ga.jspm.io/npm:@babel/helper-compilation-targets@7.22.15/lib/index.js",
38-
"@babel/helper-create-class-features-plugin": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/index.js",
37+
"@babel/helper-compilation-targets": "https://ga.jspm.io/npm:@babel/helper-compilation-targets@7.23.6/lib/index.js",
38+
"@babel/helper-create-class-features-plugin": "https://ga.jspm.io/npm:@babel/[email protected].7/lib/index.js",
3939
"@babel/helper-environment-visitor": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
4040
"@babel/helper-function-name": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
4141
"@babel/helper-hoist-variables": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
@@ -51,31 +51,31 @@
5151
"@babel/helper-string-parser": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
5252
"@babel/helper-validator-identifier": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
5353
"@babel/helper-validator-option": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
54-
"@babel/helpers": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/index.js",
54+
"@babel/helpers": "https://ga.jspm.io/npm:@babel/[email protected].8/lib/index.js",
5555
"@babel/highlight": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
56-
"@babel/parser": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/index.js",
56+
"@babel/parser": "https://ga.jspm.io/npm:@babel/[email protected].6/lib/index.js",
5757
"@babel/plugin-syntax-jsx": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
5858
"@babel/plugin-syntax-typescript": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
5959
"@babel/plugin-transform-modules-commonjs": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
60-
"@babel/plugin-transform-typescript": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/index.js",
60+
"@babel/plugin-transform-typescript": "https://ga.jspm.io/npm:@babel/[email protected].6/lib/index.js",
6161
"@babel/template": "https://ga.jspm.io/npm:@babel/[email protected]/lib/index.js",
62-
"@babel/traverse": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/index.js",
63-
"@babel/types": "https://ga.jspm.io/npm:@babel/[email protected].5/lib/index.js",
62+
"@babel/traverse": "https://ga.jspm.io/npm:@babel/[email protected].7/lib/index.js",
63+
"@babel/types": "https://ga.jspm.io/npm:@babel/[email protected].6/lib/index.js",
6464
"@jridgewell/gen-mapping": "https://ga.jspm.io/npm:@jridgewell/[email protected]/dist/gen-mapping.umd.js",
6565
"@jridgewell/resolve-uri": "https://ga.jspm.io/npm:@jridgewell/[email protected]/dist/resolve-uri.umd.js",
6666
"@jridgewell/set-array": "https://ga.jspm.io/npm:@jridgewell/[email protected]/dist/set-array.umd.js",
6767
"@jridgewell/sourcemap-codec": "https://ga.jspm.io/npm:@jridgewell/[email protected]/dist/sourcemap-codec.umd.js",
68-
"@jridgewell/trace-mapping": "https://ga.jspm.io/npm:@jridgewell/[email protected].20/dist/trace-mapping.umd.js",
68+
"@jridgewell/trace-mapping": "https://ga.jspm.io/npm:@jridgewell/[email protected].22/dist/trace-mapping.umd.js",
6969
"ansi-styles": "https://ga.jspm.io/npm:[email protected]/index.js",
7070
"browserslist": "https://ga.jspm.io/npm:[email protected]/index.js",
7171
"buffer": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/buffer.js",
72-
"caniuse-lite/dist/unpacker/agents": "https://ga.jspm.io/npm:[email protected].30001566/dist/unpacker/agents.js",
72+
"caniuse-lite/dist/unpacker/agents": "https://ga.jspm.io/npm:[email protected].30001579/dist/unpacker/agents.js",
7373
"chalk": "https://ga.jspm.io/npm:[email protected]/index.js",
7474
"color-convert": "https://ga.jspm.io/npm:[email protected]/index.js",
7575
"color-name": "https://ga.jspm.io/npm:[email protected]/index.js",
7676
"convert-source-map": "https://ga.jspm.io/npm:[email protected]/index.js",
7777
"debug": "https://ga.jspm.io/npm:[email protected]/src/browser.js",
78-
"electron-to-chromium/versions": "https://ga.jspm.io/npm:[email protected].609/versions.js",
78+
"electron-to-chromium/versions": "https://ga.jspm.io/npm:[email protected].640/versions.js",
7979
"escape-string-regexp": "https://ga.jspm.io/npm:[email protected]/index.js",
8080
"fs": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/fs.js",
8181
"gensync": "https://ga.jspm.io/npm:[email protected]/index.js",

0 commit comments

Comments
 (0)