1- import { join } from "node:path" ;
2- import { parseUrlPkg } from "@jspm/generator" ;
3- import { cache , cacheMap , nodeImportMapPath } from "./config" ;
4- import { constructImportMap } from "./utils" ;
5- import { parseNodeModuleCachePath } from "./parser" ;
6- import { IS_DEBUGGING } from "./constants" ;
7- import { logger } from "./logger" ;
8- import { Context , NextResolve } from "./types" ;
1+ import { cacheMap , nodeImportMapPath } from "src/config" ;
2+ import { isNodeOrFileProtocol , resolveNodeModuleCachePath , resolveModulePath , resolveParsedModulePath } from 'src/utils' ;
3+ import { Context , NextResolve } from "src/types" ;
94
105/**
116 * ******************************************************
127 * LOADER
138 * ------------------------------------------------------
14- * @description generates a node.importmap
159 * @summary loads node modules via an *assumed root working directory with a cache and node.importmap*
1610 * @notes
1711 * The node loader api is being redesigned.
@@ -24,58 +18,22 @@ import { Context, NextResolve } from "./types";
2418 * ******************************************************
2519 */
2620
27- const log = logger ( { file : "loader" } ) ;
28- log . setLogger ( IS_DEBUGGING ) ;
29-
3021/**
3122 * resolve
32- * @description a convenience function to resolve the modules, this function is called by the loader automatically
23+ * @description resolves modules, this function is called by the loader automatically
3324 * @param {string } specifier
3425 * @param {object } context
3526 * @param {callback } nextResolve
3627 * @returns {function } nextResolve
3728 */
38-
3929export const resolve = async ( specifier : string , { parentURL } : Context , nextResolve : NextResolve ) => {
4030 if ( ! parentURL || ! nodeImportMapPath ) return nextResolve ( specifier ) ;
41-
42- const importmap = constructImportMap ( nodeImportMapPath ) ;
43- log . debug ( "resolve:importmap:" , { importmap } ) ;
44- const cacheMapPath = cacheMap . get ( parentURL ) || parentURL ;
45- log . debug ( "resolve:cacheMapPath:" , { cacheMapPath } ) ;
46-
47- // construct module path
48- const modulePath = importmap . resolve ( specifier , cacheMapPath ) ;
49- log . debug ( "resolve:modulePath:" , { modulePath } ) ;
50-
51- // resolve URL
52- const { protocol = "" } = new URL ( modulePath ) ;
53- const isNode = protocol === "node:" ;
54- const isFile = protocol === "file:" ;
55- log . debug ( "resolve:protocol:" , { isNode, isFile } ) ;
56- if ( isNode || isFile ) return nextResolve ( specifier ) ;
57-
58- // get node module information
59- try {
60- const moduleMetadata = await parseUrlPkg ( modulePath ) ;
61- // debugged to here
62- log . debug ( "resolve:moduleMetaData:" , { moduleMetadata } ) ;
63- if ( ! moduleMetadata ) return nextResolve ( specifier ) ;
64-
65- // construct node module cache path
66- const {
67- pkg : { name, version } ,
68- } = moduleMetadata ;
69- const moduleFile = modulePath . split ( "/" ) . reverse ( ) [ 0 ] || "" ;
70- const nodeModuleCachePath = join ( cache , `${ name } @${ version } ` , moduleFile ) ;
71- cacheMap . set ( `file://${ nodeModuleCachePath } ` , modulePath ) ;
72- const parsedNodeModuleCachePath = await parseNodeModuleCachePath ( modulePath , nodeModuleCachePath ) ;
73- log . debug ( "resolve:nodeModuleCachePath:" , { nodeModuleCachePath, parsedNodeModuleCachePath } ) ;
74-
75- // resolve node module cache path
76- return nextResolve ( parsedNodeModuleCachePath ) ;
77- } catch ( err ) {
78- log . error ( `resolve: ${ err } ` ) ;
79- return nextResolve ( specifier ) ;
80- }
31+ const modulePath = resolveModulePath ( specifier , cacheMap . get ( parentURL ) || parentURL ) ;
32+ if ( isNodeOrFileProtocol ( modulePath ) ) return nextResolve ( specifier ) ;
33+ const nodeModuleCachePath = await resolveNodeModuleCachePath ( modulePath ) ;
34+ if ( ! nodeModuleCachePath ) return nextResolve ( specifier ) ;
35+ cacheMap . set ( `file://${ nodeModuleCachePath } ` , modulePath ) ;
36+ const parsedNodeModuleCachePath = await resolveParsedModulePath ( modulePath , nodeModuleCachePath ) ;
37+ if ( ! parsedNodeModuleCachePath ) return nextResolve ( specifier ) ;
38+ return nextResolve ( parsedNodeModuleCachePath ) ;
8139} ;
0 commit comments