11import { dim } from 'https://deno.land/[email protected] /fmt/colors.ts' 22import { basename , dirname , join } from 'https://deno.land/[email protected] /path/mod.ts' 33import { ensureDir , } from 'https://deno.land/[email protected] /fs/ensure_dir.ts' 4- import { parseExportNames , transform } from '../compiler/mod.ts'
4+ import { transform } from '../compiler/mod.ts'
55import { trimModuleExt } from '../framework/core/module.ts'
66import { ensureTextFile , existsDirSync , existsFileSync , lazyRemove } from '../shared/fs.ts'
77import log from '../shared/log.ts'
@@ -12,17 +12,17 @@ import { cache } from '../server/cache.ts'
1212import { computeHash , esbuild , stopEsbuild , getAlephPkgUri } from '../server/helper.ts'
1313
1414const hashShort = 8
15- const reHashJS = new RegExp ( `\\.[0-9a-fx ]{${ hashShort } }\\.js$` , 'i' )
15+ const reHashJS = new RegExp ( `\\.[0-9a-f ]{${ hashShort } }\\.js$` , 'i' )
1616
1717export const bundlerRuntimeCode = `
1818 window.__ALEPH = {
1919 basePath: '/',
2020 pack: {},
21- bundledFiles : {},
21+ bundled : {},
2222 import: function(u, F) {
2323 var b = this.basePath,
2424 a = this.pack,
25- l = this.bundledFiles ;
25+ l = this.bundled ;
2626 if (u in a) {
2727 return Promise.resolve(a[u]);
2828 }
@@ -52,11 +52,13 @@ export const bundlerRuntimeCode = `
5252/** The bundler class for aleph server. */
5353export class Bundler {
5454 #app: Application
55- #bundledFiles: Map < string , string >
55+ #bundled: Map < string , string >
56+ #compiled: Map < string , string >
5657
5758 constructor ( app : Application ) {
5859 this . #app = app
59- this . #bundledFiles = new Map ( )
60+ this . #bundled = new Map ( )
61+ this . #compiled = new Map ( )
6062 }
6163
6264 async bundle ( entryMods : Array < { url : string , shared : boolean } > ) {
@@ -111,12 +113,12 @@ export class Bundler {
111113 }
112114
113115 getBundledFile ( name : string ) : string | null {
114- return this . #bundledFiles . get ( name ) || null
116+ return this . #bundled . get ( name ) || null
115117 }
116118
117119 async copyDist ( ) {
118120 await Promise . all (
119- Array . from ( this . #bundledFiles . values ( ) ) . map ( jsFile => this . copyBundleFile ( jsFile ) )
121+ Array . from ( this . #bundled . values ( ) ) . map ( jsFile => this . copyBundleFile ( jsFile ) )
120122 )
121123 }
122124
@@ -129,6 +131,10 @@ export class Bundler {
129131 }
130132
131133 private async compile ( mod : Module , external : string [ ] ) : Promise < string > {
134+ if ( this . #compiled. has ( mod . url ) ) {
135+ return this . #compiled. get ( mod . url ) !
136+ }
137+
132138 const bundlingFile = util . trimSuffix ( mod . jsFile , '.js' ) + '.bundling.js'
133139
134140 if ( existsFileSync ( bundlingFile ) ) {
@@ -162,6 +168,8 @@ export class Bundler {
162168 }
163169 }
164170
171+ this . #compiled. set ( mod . url , bundlingFile ) !
172+
165173 // compile deps
166174 for ( const dep of mod . deps ) {
167175 if ( ! dep . url . startsWith ( '#' ) && ! external . includes ( dep . url ) ) {
@@ -178,18 +186,18 @@ export class Bundler {
178186 }
179187
180188 private async createMainJS ( ) {
181- const bundledFiles = Array . from ( this . #bundledFiles . entries ( ) )
189+ const bundled = Array . from ( this . #bundled . entries ( ) )
182190 . filter ( ( [ name ] ) => ! [ 'polyfill' , 'deps' , 'shared' ] . includes ( name ) )
183191 . reduce ( ( r , [ name , filename ] ) => {
184192 r [ name ] = filename
185193 return r
186194 } , { } as Record < string , string > )
187- const mainJS = `__ALEPH.bundledFiles =${ JSON . stringify ( bundledFiles ) } ;` + this . #app. getMainJS ( true )
195+ const mainJS = `__ALEPH.bundled =${ JSON . stringify ( bundled ) } ;` + this . #app. getMainJS ( true )
188196 const hash = computeHash ( mainJS )
189197 const bundleFilename = `main.bundle.${ hash . slice ( 0 , hashShort ) } .js`
190198 const bundleFilePath = join ( this . #app. buildDir , bundleFilename )
191199 await Deno . writeTextFile ( bundleFilePath , mainJS )
192- this . #bundledFiles . set ( 'main' , bundleFilename )
200+ this . #bundled . set ( 'main' , bundleFilename )
193201 log . info ( ` {} main.js ${ dim ( '• ' + util . formatBytes ( mainJS . length ) ) } ` )
194202 }
195203
@@ -205,7 +213,7 @@ export class Bundler {
205213 const rawPolyfillsFile = `${ alephPkgUri } /bundler/polyfills/${ polyfillTarget } /mod.ts`
206214 await this . build ( rawPolyfillsFile , bundleFilePath )
207215 }
208- this . #bundledFiles . set ( 'polyfills' , bundleFilename )
216+ this . #bundled . set ( 'polyfills' , bundleFilename )
209217 log . info ( ` {} polyfills.js (${ buildTarget . toUpperCase ( ) } ) ${ dim ( '• ' + util . formatBytes ( Deno . statSync ( bundleFilePath ) . size ) ) } ` )
210218 }
211219
@@ -238,7 +246,7 @@ export class Bundler {
238246 await this . build ( bundleEntryFile , bundleFilePath )
239247 lazyRemove ( bundleEntryFile )
240248 }
241- this . #bundledFiles . set ( name , bundleFilename )
249+ this . #bundled . set ( name , bundleFilename )
242250 log . info ( ` {} ${ name } .js ${ dim ( '• ' + util . formatBytes ( Deno . statSync ( bundleFilePath ) . size ) ) } ` )
243251 }
244252
0 commit comments