1
1
import { dim } from 'https://deno.land/[email protected] /fmt/colors.ts'
2
2
import { basename , dirname , join } from 'https://deno.land/[email protected] /path/mod.ts'
3
3
import { 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'
5
5
import { trimModuleExt } from '../framework/core/module.ts'
6
6
import { ensureTextFile , existsDirSync , existsFileSync , lazyRemove } from '../shared/fs.ts'
7
7
import log from '../shared/log.ts'
@@ -12,17 +12,17 @@ import { cache } from '../server/cache.ts'
12
12
import { computeHash , esbuild , stopEsbuild , getAlephPkgUri } from '../server/helper.ts'
13
13
14
14
const hashShort = 8
15
- const reHashJS = new RegExp ( `\\.[0-9a-fx ]{${ hashShort } }\\.js$` , 'i' )
15
+ const reHashJS = new RegExp ( `\\.[0-9a-f ]{${ hashShort } }\\.js$` , 'i' )
16
16
17
17
export const bundlerRuntimeCode = `
18
18
window.__ALEPH = {
19
19
basePath: '/',
20
20
pack: {},
21
- bundledFiles : {},
21
+ bundled : {},
22
22
import: function(u, F) {
23
23
var b = this.basePath,
24
24
a = this.pack,
25
- l = this.bundledFiles ;
25
+ l = this.bundled ;
26
26
if (u in a) {
27
27
return Promise.resolve(a[u]);
28
28
}
@@ -52,11 +52,13 @@ export const bundlerRuntimeCode = `
52
52
/** The bundler class for aleph server. */
53
53
export class Bundler {
54
54
#app: Application
55
- #bundledFiles: Map < string , string >
55
+ #bundled: Map < string , string >
56
+ #compiled: Map < string , string >
56
57
57
58
constructor ( app : Application ) {
58
59
this . #app = app
59
- this . #bundledFiles = new Map ( )
60
+ this . #bundled = new Map ( )
61
+ this . #compiled = new Map ( )
60
62
}
61
63
62
64
async bundle ( entryMods : Array < { url : string , shared : boolean } > ) {
@@ -111,12 +113,12 @@ export class Bundler {
111
113
}
112
114
113
115
getBundledFile ( name : string ) : string | null {
114
- return this . #bundledFiles . get ( name ) || null
116
+ return this . #bundled . get ( name ) || null
115
117
}
116
118
117
119
async copyDist ( ) {
118
120
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 ) )
120
122
)
121
123
}
122
124
@@ -129,6 +131,10 @@ export class Bundler {
129
131
}
130
132
131
133
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
+
132
138
const bundlingFile = util . trimSuffix ( mod . jsFile , '.js' ) + '.bundling.js'
133
139
134
140
if ( existsFileSync ( bundlingFile ) ) {
@@ -162,6 +168,8 @@ export class Bundler {
162
168
}
163
169
}
164
170
171
+ this . #compiled. set ( mod . url , bundlingFile ) !
172
+
165
173
// compile deps
166
174
for ( const dep of mod . deps ) {
167
175
if ( ! dep . url . startsWith ( '#' ) && ! external . includes ( dep . url ) ) {
@@ -178,18 +186,18 @@ export class Bundler {
178
186
}
179
187
180
188
private async createMainJS ( ) {
181
- const bundledFiles = Array . from ( this . #bundledFiles . entries ( ) )
189
+ const bundled = Array . from ( this . #bundled . entries ( ) )
182
190
. filter ( ( [ name ] ) => ! [ 'polyfill' , 'deps' , 'shared' ] . includes ( name ) )
183
191
. reduce ( ( r , [ name , filename ] ) => {
184
192
r [ name ] = filename
185
193
return r
186
194
} , { } 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 )
188
196
const hash = computeHash ( mainJS )
189
197
const bundleFilename = `main.bundle.${ hash . slice ( 0 , hashShort ) } .js`
190
198
const bundleFilePath = join ( this . #app. buildDir , bundleFilename )
191
199
await Deno . writeTextFile ( bundleFilePath , mainJS )
192
- this . #bundledFiles . set ( 'main' , bundleFilename )
200
+ this . #bundled . set ( 'main' , bundleFilename )
193
201
log . info ( ` {} main.js ${ dim ( '• ' + util . formatBytes ( mainJS . length ) ) } ` )
194
202
}
195
203
@@ -205,7 +213,7 @@ export class Bundler {
205
213
const rawPolyfillsFile = `${ alephPkgUri } /bundler/polyfills/${ polyfillTarget } /mod.ts`
206
214
await this . build ( rawPolyfillsFile , bundleFilePath )
207
215
}
208
- this . #bundledFiles . set ( 'polyfills' , bundleFilename )
216
+ this . #bundled . set ( 'polyfills' , bundleFilename )
209
217
log . info ( ` {} polyfills.js (${ buildTarget . toUpperCase ( ) } ) ${ dim ( '• ' + util . formatBytes ( Deno . statSync ( bundleFilePath ) . size ) ) } ` )
210
218
}
211
219
@@ -238,7 +246,7 @@ export class Bundler {
238
246
await this . build ( bundleEntryFile , bundleFilePath )
239
247
lazyRemove ( bundleEntryFile )
240
248
}
241
- this . #bundledFiles . set ( name , bundleFilename )
249
+ this . #bundled . set ( name , bundleFilename )
242
250
log . info ( ` {} ${ name } .js ${ dim ( '• ' + util . formatBytes ( Deno . statSync ( bundleFilePath ) . size ) ) } ` )
243
251
}
244
252
0 commit comments