diff --git a/package-lock.json b/package-lock.json index f4dcf195..0d024e6c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,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", @@ -589,6 +590,12 @@ "@jridgewell/sourcemap-codec": "1.4.14" } }, + "node_modules/@jspm/core": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/@jspm/core/-/core-2.0.1.tgz", + "integrity": "sha512-Lg3PnLp0QXpxwLIAuuJboLeRaIhrgJjeuh797QADg3xz8wGLugQOS5DpsE8A6i6Adgzf+bacllkKZG3J0tGfDw==", + "dev": true + }, "node_modules/@jspm/import-map": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/@jspm/import-map/-/import-map-1.0.7.tgz", diff --git a/package.json b/package.json index 07c6805c..c8754822 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/generator.ts b/src/generator.ts index 5d112214..9b83e455 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -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 { @@ -382,6 +389,7 @@ export class Generator { fetchOptions = {}, ignore = [], commonJS = false, + typeScript = false, }: GeneratorOptions = {}) { // Initialise the debug logger: const { log, logStream } = createLogger(); @@ -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]); diff --git a/src/trace/resolver.ts b/src/trace/resolver.ts index ed172061..a8269ad3 100644 --- a/src/trace/resolver.ts +++ b/src/trace/resolver.ts @@ -55,16 +55,27 @@ export class Resolver { pcfgPromises: Record> = Object.create(null); pcfgs: Record = 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"); @@ -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) { @@ -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); @@ -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") && @@ -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); } diff --git a/test/resolve/ts.test.js b/test/resolve/ts.test.js index 8bedafbd..c7d3af9b 100644 --- a/test/resolve/ts.test.js +++ b/test/resolve/ts.test.js @@ -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" ); diff --git a/test/resolve/tspkg/dep.ts b/test/resolve/tspkg/dep.ts index 39d8fdb9..2369f8a3 100644 --- a/test/resolve/tspkg/dep.ts +++ b/test/resolve/tspkg/dep.ts @@ -1 +1,2 @@ +import 'node:fs'; export var dep: string = "dep"; diff --git a/test/test.html b/test/test.html index f393db0c..c72c5f2a 100644 --- a/test/test.html +++ b/test/test.html @@ -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", @@ -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", @@ -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",