@@ -5,14 +5,37 @@ import { transformSync } from 'esbuild'
55const baseURL = pathToFileURL ( `${ process . cwd ( ) } /` ) . href
66const isWindows = process . platform === 'win32'
77
8- const extensionsRegex = / \. ( t s x ? | j s o n ) $ / ;
8+ const extensionsRegex = / \. ( t s x ? | j s o n ) $ /
99const excludeRegex = / ^ \w + : /
1010
11+ function esbuildTransformSync ( rawSource , filename , url , format ) {
12+ const {
13+ code : js ,
14+ warnings,
15+ map : jsSourceMap ,
16+ } = transformSync ( rawSource . toString ( ) , {
17+ sourcefile : filename ,
18+ sourcemap : 'both' ,
19+ loader : new URL ( url ) . pathname . match ( extensionsRegex ) [ 1 ] ,
20+ target : `node${ process . versions . node } ` ,
21+ format : format === 'module' ? 'esm' : 'cjs' ,
22+ } )
23+
24+ if ( warnings && warnings . length > 0 ) {
25+ for ( const warning of warnings ) {
26+ console . warn ( warning . location )
27+ console . warn ( warning . text )
28+ }
29+ }
30+
31+ return { js, jsSourceMap }
32+ }
33+
1134export function resolve ( specifier , context , defaultResolve ) {
1235 const { parentURL = baseURL } = context
1336 const url = new URL ( specifier , parentURL )
1437 if ( extensionsRegex . test ( url . pathname ) )
15- return { url : url . href }
38+ return { url : url . href , format : 'module' }
1639
1740 // ignore `data:` and `node:` prefix etc.
1841 if ( ! excludeRegex . test ( specifier ) ) {
@@ -22,14 +45,39 @@ export function resolve(specifier, context, defaultResolve) {
2245 url . pathname = `${ pathname } .${ ext } `
2346 const path = fileURLToPath ( url . href )
2447 if ( fs . existsSync ( path ) )
25- return { url : url . href }
48+ return {
49+ url : url . href ,
50+ format : extensionsRegex . test ( url . pathname ) && 'module' ,
51+ }
2652 }
2753 }
2854
2955 // Let Node.js handle all other specifiers.
3056 return defaultResolve ( specifier , context , defaultResolve )
3157}
3258
59+ // New hook starting from Node v16.12.0
60+ // See: https://github.com/nodejs/node/pull/37468
61+ export function load ( url , context , defaultLoad ) {
62+ if ( extensionsRegex . test ( new URL ( url ) . pathname ) ) {
63+ const { format } = context
64+
65+ let filename = url
66+ if ( ! isWindows ) filename = fileURLToPath ( url )
67+
68+ const rawSource = fs . readFileSync ( new URL ( url ) , { encoding : 'utf8' } )
69+ const { js } = esbuildTransformSync ( rawSource , filename , url , format )
70+
71+ return {
72+ format : 'module' ,
73+ source : js ,
74+ }
75+ }
76+
77+ // Let Node.js handle all other format / sources.
78+ return defaultLoad ( url , context , defaultLoad )
79+ }
80+
3381export function getFormat ( url , context , defaultGetFormat ) {
3482 if ( extensionsRegex . test ( new URL ( url ) . pathname ) ) {
3583 return {
@@ -46,23 +94,9 @@ export function transformSource(source, context, defaultTransformSource) {
4694
4795 if ( extensionsRegex . test ( new URL ( url ) . pathname ) ) {
4896 let filename = url
49- if ( ! isWindows )
50- filename = fileURLToPath ( url )
51-
52- const { code : js , warnings, map : jsSourceMap } = transformSync ( source . toString ( ) , {
53- sourcefile : filename ,
54- sourcemap : 'both' ,
55- loader : new URL ( url ) . pathname . match ( extensionsRegex ) [ 1 ] ,
56- target : `node${ process . versions . node } ` ,
57- format : format === 'module' ? 'esm' : 'cjs' ,
58- } )
59-
60- if ( warnings && warnings . length > 0 ) {
61- for ( const warning of warnings ) {
62- console . warn ( warning . location )
63- console . warn ( warning . text )
64- }
65- }
97+ if ( ! isWindows ) filename = fileURLToPath ( url )
98+
99+ const { js } = esbuildTransformSync ( source , filename , url , format )
66100
67101 return {
68102 source : js ,
0 commit comments