Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.

Commit 3a7d2c9

Browse files
authored
fix: resolver cases (#386)
1 parent ac746e4 commit 3a7d2c9

File tree

9 files changed

+54
-33
lines changed

9 files changed

+54
-33
lines changed

src/install/installer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export class Installer {
119119
) {
120120
this.log = log;
121121
this.resolver = resolver;
122+
this.resolver.installer = this;
122123
this.resolutions = opts.resolutions || {};
123124
this.installBaseUrl = baseUrl;
124125
this.opts = opts;

src/trace/resolver.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export class Resolver {
105105
traceTs: boolean;
106106
traceSystem: boolean;
107107
context: Record<string, any>;
108+
installer: Installer;
108109
constructor({
109110
env,
110111
log,
@@ -471,10 +472,7 @@ export class Resolver {
471472
const subpath = "./" + url.slice(pkgUrl.length);
472473
if (subpath in pcfg.browser) {
473474
const target = pcfg.browser[subpath];
474-
if (target === false)
475-
throw new Error(
476-
`TODO: Empty browser map for ${subpath} in ${url}`
477-
);
475+
if (target === false) return this.resolveEmpty(parentIsCjs, url);
478476
if (!target.startsWith("./"))
479477
throw new Error(
480478
`TODO: External browser map for ${subpath} to ${target} in ${url}`
@@ -720,14 +718,36 @@ export class Resolver {
720718
return null;
721719
}
722720

721+
async resolveEmpty(cjsEnv: boolean, originalSpecifier: string, parentUrl?: URL) {
722+
const stdlibTarget = {
723+
registry: "npm",
724+
name: "@jspm/core",
725+
ranges: [new SemverRange("*")],
726+
unstable: true,
727+
};
728+
const provider = this.installer.getProvider(stdlibTarget);
729+
const pkg = await this.resolveLatestTarget(
730+
stdlibTarget,
731+
provider,
732+
parentUrl?.href
733+
);
734+
return this.resolveExport(
735+
await this.pkgToUrl(pkg, provider),
736+
"./nodelibs/@empty",
737+
cjsEnv,
738+
false,
739+
originalSpecifier,
740+
parentUrl
741+
);
742+
}
743+
723744
// Note: updates here must be tracked in function above
724745
async resolveExport(
725746
pkgUrl: `${string}/`,
726747
subpath: `.${string}`,
727748
cjsEnv: boolean,
728749
parentIsCjs: boolean,
729750
originalSpecifier: string,
730-
installer: Installer,
731751
parentUrl?: URL
732752
): Promise<string> {
733753
const env = cjsEnv ? this.cjsEnv : this.env;
@@ -739,27 +759,7 @@ export class Resolver {
739759
pcfg.exports !== null &&
740760
Object.keys(pcfg.exports).length === 0
741761
) {
742-
const stdlibTarget = {
743-
registry: "npm",
744-
name: "@jspm/core",
745-
ranges: [new SemverRange("*")],
746-
unstable: true,
747-
};
748-
const provider = installer.getProvider(stdlibTarget);
749-
const pkg = await this.resolveLatestTarget(
750-
stdlibTarget,
751-
provider,
752-
parentUrl.href
753-
);
754-
return this.resolveExport(
755-
await this.pkgToUrl(pkg, provider),
756-
"./nodelibs/@empty",
757-
cjsEnv,
758-
parentIsCjs,
759-
originalSpecifier,
760-
installer,
761-
parentUrl
762-
);
762+
return this.resolveEmpty(cjsEnv, originalSpecifier, parentUrl);
763763
}
764764

765765
function throwExportNotDefined() {

src/trace/tracemap.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ function combineSubpaths(
7474
installSubpath: "." | `./${string}` | null,
7575
traceSubpath: "." | `./${string}`
7676
): `./${string}` | "." {
77+
if (traceSubpath.endsWith('/'))
78+
throw new Error('Trailing slash subpaths unsupported');
7779
return installSubpath === null ||
7880
installSubpath === "." ||
7981
traceSubpath === "."
@@ -508,7 +510,6 @@ export default class TraceMap {
508510
cjsEnv,
509511
parentIsCjs,
510512
specifier,
511-
this.installer,
512513
new URL(parentUrl)
513514
)
514515
);
@@ -561,11 +562,10 @@ export default class TraceMap {
561562
const resolved = await this.resolver.realPath(
562563
await this.resolver.resolveExport(
563564
installUrl,
564-
combineSubpaths(installSubpath, subpath),
565+
combineSubpaths(installSubpath, parentIsCjs && subpath.endsWith('/') ? subpath.slice(0, -1) as `./${string}` : subpath),
565566
cjsEnv,
566567
parentIsCjs,
567568
specifier,
568-
this.installer,
569569
new URL(parentUrl)
570570
)
571571
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'excluded';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'jquery';

test/resolve/cjspkg/browser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import './browser-dep-exclude.js';
2+
import './browser-dep-include.js';

test/resolve/cjspkg/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"dependencies": {
33
"process": "^0.11.10"
4+
},
5+
"browser": {
6+
"./browser-dep-exclude.js": false
47
}
58
}

test/resolve/unused-cjs.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import assert from "assert";
33

44
const generator = new Generator({
55
mapUrl: import.meta.url,
6+
commonJS: true
67
});
78

89
// Should not throw, index file doesn't use CJS:
@@ -15,3 +16,15 @@ await (async () => {
1516
assert(false);
1617
} catch {}
1718
})();
19+
20+
await generator.install({ target: './cjspkg', subpath: './browser.js' });
21+
assert.deepStrictEqual(generator.getMap(), {
22+
imports: {
23+
'./cjspkg/browser-dep-exclude.js': 'https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/@empty.js',
24+
'cjspkg/browser.js': './cjspkg/browser.js',
25+
unusedcjspkg: './unusedcjspkg/index.js'
26+
},
27+
scopes: {
28+
'./cjspkg/': { jquery: 'https://ga.jspm.io/npm:[email protected]/dist/jquery.js' }
29+
}
30+
});

test/test.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"#lib/config/files/index.js": "https://ga.jspm.io/npm:@babel/[email protected]/lib/config/files/index-browser.js",
2727
"#lib/config/resolve-targets.js": "https://ga.jspm.io/npm:@babel/[email protected]/lib/config/resolve-targets-browser.js",
2828
"#lib/transform-file.js": "https://ga.jspm.io/npm:@babel/[email protected]/lib/transform-file-browser.js",
29-
"#node.js": "https://ga.jspm.io/npm:browserslist@4.23.3/browser.js",
29+
"#node.js": "https://ga.jspm.io/npm:browserslist@4.24.0/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",
@@ -63,15 +63,15 @@
6363
"@jridgewell/sourcemap-codec": "https://ga.jspm.io/npm:@jridgewell/[email protected]/dist/sourcemap-codec.umd.js",
6464
"@jridgewell/trace-mapping": "https://ga.jspm.io/npm:@jridgewell/[email protected]/dist/trace-mapping.umd.js",
6565
"ansi-styles": "https://ga.jspm.io/npm:[email protected]/index.js",
66-
"browserslist": "https://ga.jspm.io/npm:browserslist@4.23.3/index.js",
66+
"browserslist": "https://ga.jspm.io/npm:browserslist@4.24.0/index.js",
6767
"buffer": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/buffer.js",
68-
"caniuse-lite/dist/unpacker/agents": "https://ga.jspm.io/npm:[email protected].30001658/dist/unpacker/agents.js",
68+
"caniuse-lite/dist/unpacker/agents": "https://ga.jspm.io/npm:[email protected].30001664/dist/unpacker/agents.js",
6969
"chalk": "https://ga.jspm.io/npm:[email protected]/index.js",
7070
"color-convert": "https://ga.jspm.io/npm:[email protected]/index.js",
7171
"color-name": "https://ga.jspm.io/npm:[email protected]/index.js",
7272
"convert-source-map": "https://ga.jspm.io/npm:[email protected]/index.js",
7373
"debug": "https://ga.jspm.io/npm:[email protected]/src/browser.js",
74-
"electron-to-chromium/versions": "https://ga.jspm.io/npm:[email protected].18/versions.js",
74+
"electron-to-chromium/versions": "https://ga.jspm.io/npm:[email protected].29/versions.js",
7575
"escape-string-regexp": "https://ga.jspm.io/npm:[email protected]/index.js",
7676
"fs": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/fs.js",
7777
"gensync": "https://ga.jspm.io/npm:[email protected]/index.js",

0 commit comments

Comments
 (0)