1
1
import assert from "node:assert" ;
2
- import { getDependencyResolver , rewriteNpmImports } from "../../src/npm.js" ;
2
+ import { extractNpmSpecifier , getDependencyResolver , rewriteNpmImports } from "../../src/npm.js" ;
3
+ import { fromJsDelivrPath } from "../../src/npm.js" ;
3
4
import { relativePath } from "../../src/path.js" ;
4
5
import { mockJsDelivr } from "../mocks/jsdelivr.js" ;
5
6
@@ -8,67 +9,86 @@ describe("getDependencyResolver(root, path, input)", () => {
8
9
it ( "finds /npm/ imports and re-resolves their versions" , async ( ) => {
9
10
const root = "test/input/build/simple-public" ;
10
11
const specifier = "/npm/[email protected] /dist/d3-array.js" ;
11
- const resolver = await getDependencyResolver ( root , "/_npm/[email protected] /+esm .js" , `import '${ specifier } ';\n` ) ; // prettier-ignore
12
+ const resolver = await getDependencyResolver ( root , "/_npm/[email protected] /_esm .js" , `import '${ specifier } ';\n` ) ; // prettier-ignore
12
13
assert . strictEqual ( resolver ( specifier ) , "../[email protected] /dist/d3-array.js" ) ;
13
14
} ) ;
14
15
it ( "finds /npm/ import resolutions and re-resolves their versions" , async ( ) => {
15
16
const root = "test/input/build/simple-public" ;
16
17
const specifier = "/npm/[email protected] /dist/d3-array.js" ;
17
- const resolver = await getDependencyResolver ( root , "/_npm/[email protected] /+esm .js" , `import.meta.resolve('${ specifier } ');\n` ) ; // prettier-ignore
18
+ const resolver = await getDependencyResolver ( root , "/_npm/[email protected] /_esm .js" , `import.meta.resolve('${ specifier } ');\n` ) ; // prettier-ignore
18
19
assert . strictEqual ( resolver ( specifier ) , "../[email protected] /dist/d3-array.js" ) ;
19
20
} ) ;
20
21
} ) ;
21
22
23
+ describe ( "extractNpmSpecifier(path)" , ( ) => {
24
+ it ( "returns the npm specifier for the given local npm path" , ( ) => {
25
+ assert . strictEqual ( extractNpmSpecifier ( "/_npm/[email protected] /_esm.js" ) , "[email protected] /+esm" ) ;
26
+ assert . strictEqual ( extractNpmSpecifier ( "/_npm/[email protected] /dist/d3.js" ) , "[email protected] /dist/d3.js" ) ;
27
+ } ) ;
28
+ it ( "throws if not given a local npm path" , ( ) => {
29
+ assert . throws ( ( ) => extractNpmSpecifier ( "/npm/[email protected] /+esm" ) , / i n v a l i d n p m p a t h / ) ;
30
+ assert . throws ( ( ) => extractNpmSpecifier ( "[email protected] " ) , / i n v a l i d n p m p a t h / ) ;
31
+ } ) ;
32
+ } ) ;
33
+
34
+ describe ( "fromJsDelivrPath(path)" , ( ) => {
35
+ it ( "returns the local npm path for the given jsDelivr path" , ( ) => {
36
+ assert . strictEqual ( fromJsDelivrPath ( "/npm/[email protected] /+esm" ) , "/_npm/[email protected] /_esm.js" ) ;
37
+ assert . strictEqual ( fromJsDelivrPath ( "/npm/[email protected] /dist/d3.js" ) , "/_npm/[email protected] /dist/d3.js" ) ;
38
+ } ) ;
39
+ it ( "throws if not given a jsDelivr path" , ( ) => {
40
+ assert . throws ( ( ) => fromJsDelivrPath ( "/_npm/[email protected] /_esm.js" ) , / i n v a l i d j s D e l i v r p a t h / ) ;
41
+ assert . throws ( ( ) => fromJsDelivrPath ( "[email protected] " ) , / i n v a l i d j s D e l i v r p a t h / ) ;
42
+ } ) ;
43
+ } ) ;
44
+
22
45
// prettier-ignore
23
46
describe ( "rewriteNpmImports(input, resolve)" , ( ) => {
24
47
it ( "rewrites /npm/ imports to /_npm/" , ( ) => {
25
48
assert . strictEqual ( rewriteNpmImports ( 'export * from "/npm/[email protected] /dist/d3-array.js";\n' , ( v ) => resolve ( "/_npm/[email protected] /dist/d3.js" , v ) ) , 'export * from "../../[email protected] /dist/d3-array.js";\n' ) ;
26
49
} ) ;
27
- it ( "rewrites /npm/…+esm imports to +esm .js" , ( ) => {
28
- assert . strictEqual ( rewriteNpmImports ( 'export * from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'export * from "../[email protected] /+esm .js";\n' ) ;
50
+ it ( "rewrites /npm/…+esm imports to _esm .js" , ( ) => {
51
+ assert . strictEqual ( rewriteNpmImports ( 'export * from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'export * from "../[email protected] /_esm .js";\n' ) ;
29
52
} ) ;
30
53
it ( "rewrites /npm/ imports to a relative path" , ( ) => {
31
54
assert . strictEqual ( rewriteNpmImports ( 'import "/npm/[email protected] /dist/d3-array.js";\n' , ( v ) => resolve ( "/_npm/[email protected] /dist/d3.js" , v ) ) , 'import "../../[email protected] /dist/d3-array.js";\n' ) ;
32
55
assert . strictEqual ( rewriteNpmImports ( 'import "/npm/[email protected] /dist/d3-array.js";\n' , ( v ) => resolve ( "/_npm/[email protected] /d3.js" , v ) ) , 'import "../[email protected] /dist/d3-array.js";\n' ) ;
33
56
} ) ;
34
57
it ( "rewrites named imports" , ( ) => {
35
- assert . strictEqual ( rewriteNpmImports ( 'import {sort} from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import {sort} from "../[email protected] /+esm .js";\n' ) ;
58
+ assert . strictEqual ( rewriteNpmImports ( 'import {sort} from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import {sort} from "../[email protected] /_esm .js";\n' ) ;
36
59
} ) ;
37
60
it ( "rewrites empty imports" , ( ) => {
38
- assert . strictEqual ( rewriteNpmImports ( 'import "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import "../[email protected] /+esm .js";\n' ) ;
61
+ assert . strictEqual ( rewriteNpmImports ( 'import "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import "../[email protected] /_esm .js";\n' ) ;
39
62
} ) ;
40
63
it ( "rewrites default imports" , ( ) => {
41
- assert . strictEqual ( rewriteNpmImports ( 'import d3 from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import d3 from "../[email protected] /+esm .js";\n' ) ;
64
+ assert . strictEqual ( rewriteNpmImports ( 'import d3 from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import d3 from "../[email protected] /_esm .js";\n' ) ;
42
65
} ) ;
43
66
it ( "rewrites namespace imports" , ( ) => {
44
- assert . strictEqual ( rewriteNpmImports ( 'import * as d3 from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import * as d3 from "../[email protected] /+esm .js";\n' ) ;
67
+ assert . strictEqual ( rewriteNpmImports ( 'import * as d3 from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import * as d3 from "../[email protected] /_esm .js";\n' ) ;
45
68
} ) ;
46
69
it ( "rewrites named exports" , ( ) => {
47
- assert . strictEqual ( rewriteNpmImports ( 'export {sort} from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'export {sort} from "../[email protected] /+esm .js";\n' ) ;
70
+ assert . strictEqual ( rewriteNpmImports ( 'export {sort} from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'export {sort} from "../[email protected] /_esm .js";\n' ) ;
48
71
} ) ;
49
72
it ( "rewrites namespace exports" , ( ) => {
50
- assert . strictEqual ( rewriteNpmImports ( 'export * from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'export * from "../[email protected] /+esm .js";\n' ) ;
73
+ assert . strictEqual ( rewriteNpmImports ( 'export * from "/npm/[email protected] /+esm";\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'export * from "../[email protected] /_esm .js";\n' ) ;
51
74
} ) ;
52
75
it ( "rewrites dynamic imports with static module specifiers" , ( ) => {
53
- assert . strictEqual ( rewriteNpmImports ( 'import("/npm/[email protected] /+esm");\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import("../[email protected] /+esm .js");\n' ) ;
54
- assert . strictEqual ( rewriteNpmImports ( "import(`/npm/[email protected] /+esm`);\n" , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import("../[email protected] /+esm .js");\n' ) ;
55
- assert . strictEqual ( rewriteNpmImports ( "import('/npm/[email protected] /+esm');\n" , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import("../[email protected] /+esm .js");\n' ) ;
76
+ assert . strictEqual ( rewriteNpmImports ( 'import("/npm/[email protected] /+esm");\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import("../[email protected] /_esm .js");\n' ) ;
77
+ assert . strictEqual ( rewriteNpmImports ( "import(`/npm/[email protected] /+esm`);\n" , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import("../[email protected] /_esm .js");\n' ) ;
78
+ assert . strictEqual ( rewriteNpmImports ( "import('/npm/[email protected] /+esm');\n" , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import("../[email protected] /_esm .js");\n' ) ;
56
79
} ) ;
57
80
it ( "ignores dynamic imports with dynamic module specifiers" , ( ) => {
58
- assert . strictEqual ( rewriteNpmImports ( 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' ) ;
81
+ assert . strictEqual ( rewriteNpmImports ( 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' ) ;
59
82
} ) ;
60
83
it ( "ignores dynamic imports with dynamic module specifiers" , ( ) => {
61
- assert . strictEqual ( rewriteNpmImports ( 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' ) ;
84
+ assert . strictEqual ( rewriteNpmImports ( 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' ) ;
62
85
} ) ;
63
86
it ( "strips the sourceMappingURL declaration" , ( ) => {
64
- assert . strictEqual ( rewriteNpmImports ( 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n//# sourceMappingURL=index.js.map' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' ) ;
65
- assert . strictEqual ( rewriteNpmImports ( 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n//# sourceMappingURL=index.js.map\n' , ( v ) => resolve ( "/_npm/[email protected] /+esm .js" , v ) ) , 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' ) ;
87
+ assert . strictEqual ( rewriteNpmImports ( 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n//# sourceMappingURL=index.js.map' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' ) ;
88
+ assert . strictEqual ( rewriteNpmImports ( 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n//# sourceMappingURL=index.js.map\n' , ( v ) => resolve ( "/_npm/[email protected] /_esm .js" , v ) ) , 'import(`/npm/d3-array@${"3.2.4"}/+esm`);\n' ) ;
66
89
} ) ;
67
90
} ) ;
68
91
69
92
function resolve ( path : string , specifier : string ) : string {
70
- if ( ! specifier . startsWith ( "/npm/" ) ) return specifier ;
71
- specifier = `/_npm/${ specifier . slice ( "/npm/" . length ) } ` ;
72
- if ( specifier . endsWith ( "/+esm" ) ) specifier += ".js" ;
73
- return relativePath ( path , specifier ) ;
93
+ return specifier . startsWith ( "/npm/" ) ? relativePath ( path , fromJsDelivrPath ( specifier ) ) : specifier ;
74
94
}
0 commit comments