3
3
// https://raw.githubusercontent.com/node-loader/
4
4
//
5
5
6
- import path from " path" ;
7
- import url from " url" ;
8
- import { promises as fs } from "fs" ;
6
+ import path from ' path' ;
7
+ import url from ' url' ;
8
+ import { promises as fs } from 'fs' ;
9
9
10
- export const IMPORT_MAP_FILE_NAME = " node.importmap" ;
10
+ export const IMPORT_MAP_FILE_NAME = ' node.importmap' ;
11
11
12
12
const baseURL = url . pathToFileURL ( process . cwd ( ) ) + path . sep ;
13
13
@@ -25,7 +25,7 @@ export function resolveSpecifier(importMap, specifier, parentURL) {
25
25
for ( let scopePrefix in importMap . scopes ) {
26
26
if (
27
27
scopePrefix === currentBaseURL ||
28
- ( scopePrefix . endsWith ( "/" ) && currentBaseURL . startsWith ( scopePrefix ) )
28
+ ( scopePrefix . endsWith ( '/' ) && currentBaseURL . startsWith ( scopePrefix ) )
29
29
) {
30
30
const scopeImportsMatch = resolveImportsMatch (
31
31
normalizedSpecifier ,
@@ -61,7 +61,7 @@ function resolveImportsMatch(normalizedSpecifier, specifierMap) {
61
61
}
62
62
return resolutionResult ;
63
63
} else if (
64
- specifierKey . endsWith ( "/" ) &&
64
+ specifierKey . endsWith ( '/' ) &&
65
65
normalizedSpecifier . startsWith ( specifierKey )
66
66
) {
67
67
if ( resolutionResult === null ) {
@@ -94,7 +94,7 @@ export function resolveAndComposeImportMap(parsed) {
94
94
let sortedAndNormalizedImports = { } ;
95
95
96
96
// Step 4
97
- if ( parsed . hasOwnProperty ( " imports" ) ) {
97
+ if ( parsed . hasOwnProperty ( ' imports' ) ) {
98
98
// Step 4.1
99
99
if ( ! isPlainObject ( parsed . imports ) ) {
100
100
throw Error ( `Invalid import map - "imports" property must be an object` ) ;
@@ -111,7 +111,7 @@ export function resolveAndComposeImportMap(parsed) {
111
111
let sortedAndNormalizedScopes = { } ;
112
112
113
113
// Step 6
114
- if ( parsed . hasOwnProperty ( " scopes" ) ) {
114
+ if ( parsed . hasOwnProperty ( ' scopes' ) ) {
115
115
// Step 6.1
116
116
if ( ! isPlainObject ( parsed . scopes ) ) {
117
117
throw Error ( `Invalid import map - "scopes" property must be an object` ) ;
@@ -123,13 +123,13 @@ export function resolveAndComposeImportMap(parsed) {
123
123
124
124
// Step 7
125
125
const invalidKeys = Object . keys ( parsed ) . filter (
126
- ( key ) => key !== " imports" && key !== " scopes"
126
+ ( key ) => key !== ' imports' && key !== ' scopes'
127
127
) ;
128
128
if ( invalidKeys . length > 0 ) {
129
129
console . warn (
130
130
`Invalid top-level key${
131
- invalidKeys . length > 0 ? "s" : ""
132
- } in import map - ${ invalidKeys . join ( ", " ) } `
131
+ invalidKeys . length > 0 ? 's' : ''
132
+ } in import map - ${ invalidKeys . join ( ', ' ) } `
133
133
) ;
134
134
}
135
135
@@ -161,7 +161,7 @@ function sortAndNormalizeSpecifierMap(map, baseURL) {
161
161
continue ;
162
162
}
163
163
164
- if ( specifierKey . endsWith ( "/" ) && ! addressURL . endsWith ( "/" ) ) {
164
+ if ( specifierKey . endsWith ( '/' ) && ! addressURL . endsWith ( '/' ) ) {
165
165
console . warn (
166
166
`Invalid URL address for import map specifier '${ specifierKey } ' - since the specifier ends in slash, so must the address`
167
167
) ;
@@ -177,7 +177,7 @@ function sortAndNormalizeSpecifierMap(map, baseURL) {
177
177
178
178
// https://wicg.github.io/import-maps/#normalize-a-specifier-key
179
179
function normalizeSpecifierKey ( key ) {
180
- if ( key === "" ) {
180
+ if ( key === '' ) {
181
181
console . warn ( `Specifier keys in import maps may not be the empty string` ) ;
182
182
return null ;
183
183
}
@@ -188,9 +188,9 @@ function normalizeSpecifierKey(key) {
188
188
// https://wicg.github.io/import-maps/#parse-a-url-like-import-specifier
189
189
function parseURLLikeSpecifier ( specifier , baseURL ) {
190
190
const useBaseUrlAsParent =
191
- specifier . startsWith ( "/" ) ||
192
- specifier . startsWith ( "./" ) ||
193
- specifier . startsWith ( " ../" ) ;
191
+ specifier . startsWith ( '/' ) ||
192
+ specifier . startsWith ( './' ) ||
193
+ specifier . startsWith ( ' ../' ) ;
194
194
195
195
try {
196
196
return new URL ( specifier , useBaseUrlAsParent ? baseURL : undefined ) . href ;
@@ -236,21 +236,23 @@ function isPlainObject(obj) {
236
236
237
237
// ---
238
238
239
-
240
239
let importMapPromise = getImportMapPromise ( ) ;
241
240
242
241
export async function resolve ( specifier , context , defaultResolve ) {
243
242
const { parentURL = null } = context ;
244
243
const importMap = await importMapPromise ;
245
244
let importMapUrl = resolveSpecifier ( importMap , specifier , parentURL ) ;
246
245
247
- if ( importMapUrl ?. startsWith ( 'http://' ) || importMapUrl ?. startsWith ( 'https://' ) ) {
246
+ if (
247
+ importMapUrl ?. startsWith ( 'http://' ) ||
248
+ importMapUrl ?. startsWith ( 'https://' )
249
+ ) {
248
250
importMapUrl = await cacheBundle ( importMapUrl ) ;
249
251
}
250
252
251
253
const r = defaultResolve ( importMapUrl ?? specifier , context , defaultResolve ) ;
252
254
253
- return r . then ( r => {
255
+ return r . then ( ( r ) => {
254
256
return { ...r , format : 'module' } ;
255
257
} ) ;
256
258
}
@@ -259,7 +261,7 @@ async function cacheBundle(importMapUrl) {
259
261
const fileName = importMapUrl . replace ( / [ ^ a - z A - Z 0 - 9 . ] / g, '_' ) ;
260
262
const filePath = path . join ( './cache' , fileName ) ;
261
263
262
- if ( ! await exists ( filePath ) ) {
264
+ if ( ! ( await exists ( filePath ) ) ) {
263
265
const res = await fetch ( importMapUrl ) ;
264
266
const source = await res . text ( ) ;
265
267
await ensureCacheFolder ( ) ;
@@ -271,7 +273,7 @@ async function cacheBundle(importMapUrl) {
271
273
}
272
274
273
275
async function ensureCacheFolder ( ) {
274
- if ( ! await exists ( './cache' ) ) {
276
+ if ( ! ( await exists ( './cache' ) ) ) {
275
277
await fs . mkdir ( './cache' ) ;
276
278
}
277
279
}
@@ -321,7 +323,6 @@ function emptyMap() {
321
323
return { imports : { } , scopes : { } } ;
322
324
}
323
325
324
-
325
326
async function exists ( path ) {
326
327
try {
327
328
await fs . access ( path ) ;
0 commit comments