11import {
2+ getPathAndExtension ,
23 isAbsolutePath ,
34 isUrl ,
45 normalizePath ,
@@ -15,7 +16,14 @@ import {
1516 OutputFile ,
1617 stop ,
1718} from "../deps/esbuild.ts" ;
18- import { dirname , extname , fromFileUrl , toFileUrl } from "../deps/path.ts" ;
19+ import {
20+ dirname ,
21+ extname ,
22+ fromFileUrl ,
23+ join ,
24+ relative ,
25+ toFileUrl ,
26+ } from "../deps/path.ts" ;
1927import { prepareAsset , saveAsset } from "./source_maps.ts" ;
2028import { Page } from "../core/file.ts" ;
2129import textLoader from "../core/loaders/text.ts" ;
@@ -127,14 +135,20 @@ export function esbuild(userOptions?: Options) {
127135 pages : Page [ ] ,
128136 ) : Promise < [ OutputFile [ ] , Metafile , boolean ] > {
129137 let enableAllSourceMaps = false ;
130- const entryPoints : string [ ] = [ ] ;
138+ const entryPoints : { in : string ; out : string } [ ] = [ ] ;
131139
132140 pages . forEach ( ( page ) => {
133141 const { content, filename, enableSourceMap } = prepareAsset ( site , page ) ;
134142 if ( enableSourceMap ) {
135143 enableAllSourceMaps = true ;
136144 }
137- entryPoints . push ( filename ) ;
145+
146+ let outUri = getPathAndExtension ( page . data . url ) [ 0 ] ;
147+ if ( outUri . startsWith ( "/" ) ) {
148+ outUri = outUri . slice ( 1 ) ;
149+ }
150+
151+ entryPoints . push ( { in : filename , out : outUri } ) ;
138152 entryContent [ toFileUrl ( filename ) . href ] = content ;
139153 } ) ;
140154
@@ -261,8 +275,8 @@ export function esbuild(userOptions?: Options) {
261275 }
262276
263277 // Replace .tsx and .jsx extensions with .js
264- const content = ( ! options . options . splitting && ! options . options . bundle )
265- ? resolveImports ( outputFile . text , options . esm )
278+ const content = ! options . options . bundle
279+ ? resolveImports ( outputFile , basePath , options . esm , allPages )
266280 : outputFile . text ;
267281
268282 // Get the associated source map
@@ -309,10 +323,7 @@ export function esbuild(userOptions?: Options) {
309323 }
310324
311325 // The page is an entry point
312- entryPoint . data . url = normalizedOutPath . replaceAll (
313- dirname ( entryPoint . sourcePath ) + "/" ,
314- dirname ( entryPoint . data . url ) + "/" ,
315- ) ;
326+ entryPoint . data . url = normalizedOutPath ;
316327 saveAsset ( site , entryPoint , content , map ?. text ) ;
317328 }
318329 } ) ;
@@ -369,22 +380,63 @@ function buildJsxConfig(config?: DenoConfig): BuildOptions | undefined {
369380}
370381
371382function resolveImports (
372- content : string ,
383+ outfile : OutputFile ,
384+ basePath : string ,
373385 esm : EsmOptions ,
386+ pages : Page [ ] ,
374387) : string {
375- return content . replaceAll (
376- / ( f r o m \s * ) [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / g,
377- ( _ , from , path ) => {
378- if ( path . startsWith ( "." ) || path . startsWith ( "/" ) ) {
379- const resolved = path . endsWith ( ".json" )
380- ? path
381- : replaceExtension ( path , ".js" ) ;
382- return `${ from } "${ resolved } "` ;
383- }
384- const resolved = import . meta. resolve ( path ) ;
385- return `${ from } "${ handleEsm ( resolved , esm ) || resolved } "` ;
386- } ,
388+ let source = outfile . text ;
389+
390+ source = source . replaceAll (
391+ / \b f r o m \s * [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / g,
392+ ( _ , path ) =>
393+ `from "${ resolveImport ( path , outfile . path , basePath , esm , pages ) } "` ,
394+ ) ;
395+
396+ source = source . replaceAll (
397+ / \b i m p o r t \s * [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / g,
398+ ( _ , path ) =>
399+ `import "${ resolveImport ( path , outfile . path , basePath , esm , pages ) } "` ,
400+ ) ;
401+
402+ source = source . replaceAll (
403+ / \b i m p o r t \( [ \s \n ] * [ " ' ] ( [ ^ " ' ] + ) [ " ' ] (? = [ \s \n ] * [ , ) ] ) / g,
404+ ( _ , path ) =>
405+ `import("${ resolveImport ( path , outfile . path , basePath , esm , pages ) } "` ,
387406 ) ;
407+
408+ return source ;
409+ }
410+
411+ function resolveImport (
412+ importPath : string ,
413+ sourceFile : string ,
414+ basePath : string ,
415+ esm : EsmOptions ,
416+ pages : Page [ ] ,
417+ ) : string {
418+ if ( importPath . startsWith ( "." ) || importPath . startsWith ( "/" ) ) {
419+ const absoluteImportPath = join ( dirname ( sourceFile ) , importPath ) ;
420+ const sourcePathOfImport = normalizePath ( absoluteImportPath , basePath ) ;
421+ const sourcePageOfImport = pages . find ( ( page ) =>
422+ page . sourcePath === sourcePathOfImport
423+ ) ;
424+
425+ const sourceUriOfImport = sourcePageOfImport
426+ ? "./" +
427+ relative (
428+ dirname ( sourceFile ) ,
429+ basePath + "/" + sourcePageOfImport . data . url ,
430+ )
431+ : importPath ;
432+
433+ return sourceUriOfImport . endsWith ( ".json" )
434+ ? sourceUriOfImport
435+ : replaceExtension ( sourceUriOfImport , ".js" ) ;
436+ }
437+
438+ const resolved = import . meta. resolve ( importPath ) ;
439+ return handleEsm ( resolved , esm ) || resolved ;
388440}
389441
390442function handleEsm ( path : string , options : EsmOptions ) : string | undefined {
0 commit comments