1
- import { buildChecksum , TransformOptions , transformSync } from '../compiler/mod.ts'
1
+ import { TransformOptions , transform } from '../compiler/mod.ts'
2
2
import type { ECMA } from '../deps.ts'
3
3
import { colors , minify , path } from '../deps.ts'
4
- import { defaultReactVersion } from " ../shared/constants.ts"
4
+ import { defaultReactVersion , hashShortLength } from ' ../shared/constants.ts'
5
5
import { existsFileSync , lazyRemove } from '../shared/fs.ts'
6
6
import log from '../shared/log.ts'
7
7
import util from '../shared/util.ts'
8
8
import { VERSION } from '../version.ts'
9
- import type { Application } from './app.ts'
10
- import { AlephRuntimeCode , clearCompilation , computeHash , getAlephPkgUri } from './helper.ts'
9
+ import type { Application , Module } from './app.ts'
10
+ import { AlephRuntimeCode , clearCompilation , computeHash , getAlephPkgUri , isLoaderPlugin , trimModuleExt } from './helper.ts'
11
11
12
- /**
13
- * The Aleph Server Application class.
14
- */
12
+ /** The bundler class for aleph server. */
15
13
export class Bundler {
16
- readonly #app: Application
14
+ #app: Application
15
+ #deps: Array < string >
17
16
18
17
constructor ( app : Application ) {
19
18
this . #app = app
19
+ this . #deps = [ ]
20
20
}
21
21
22
- async bundle ( entryMods : Set < string > , depMods : Map < string , boolean > ) {
23
- console . log ( entryMods )
24
- console . log ( depMods )
22
+ async bundle ( entryMods : Array < string > , depMods : Map < string , boolean > ) {
23
+ this . #deps = Array . from ( depMods . keys ( ) )
24
+ await Promise . all ( [
25
+ await this . createPolyfillBundle ( ) ,
26
+ await this . createBundleChunk (
27
+ '/deps.js' ,
28
+ Array . from ( depMods . entries ( ) )
29
+ . filter ( ( [ url , exported ] ) => exported && util . isLikelyHttpURL ( url ) )
30
+ . map ( ( [ url ] ) => url )
31
+ ) ,
32
+ await this . createBundleChunk (
33
+ '/shared.js' ,
34
+ Array . from ( depMods . entries ( ) )
35
+ . filter ( ( [ url , exported ] ) => exported && ! util . isLikelyHttpURL ( url ) )
36
+ . map ( ( [ url ] ) => url )
37
+ ) ,
38
+ ...entryMods . map ( async url => {
39
+ await this . createBundleChunk ( trimModuleExt ( url ) + '.js' , [ url ] )
40
+ } )
41
+ ] )
42
+ }
43
+
44
+ private async compile ( module : Module ) {
45
+ const { content : sourceCode } = await this . #app. fetchModule ( module . url )
46
+ const { code, map, deps } = await transform ( module . url , ( new TextDecoder ) . decode ( sourceCode ) , {
47
+ importMap : this . #app. importMap ,
48
+ alephPkgUri : getAlephPkgUri ( ) ,
49
+ reactVersion : defaultReactVersion ,
50
+ swcOptions : {
51
+ target : 'es2020' ,
52
+ sourceType : 'js'
53
+ } ,
54
+ bundleMode : true ,
55
+ bundleExternal : this . #deps,
56
+ loaders : this . #app. config . plugins . filter ( isLoaderPlugin )
57
+ } )
25
58
}
26
59
27
60
async copyDist ( ) {
@@ -59,27 +92,17 @@ export class Bundler {
59
92
// ])
60
93
}
61
94
62
- /** transpile code without type check. */
63
- private async transpile ( url : string , sourceCode : string , options : TransformOptions ) {
64
- return transformSync ( url , sourceCode , {
65
- ...options ,
66
- importMap : this . #app. importMap ,
67
- alephPkgUri : getAlephPkgUri ( ) ,
68
- reactVersion : defaultReactVersion ,
69
- } )
70
- }
71
-
72
95
/** create polyfill bundle. */
73
96
private async createPolyfillBundle ( ) {
74
97
const alephPkgUri = getAlephPkgUri ( )
75
98
const { buildTarget } = this . #app. config
76
- const hash = computeHash ( AlephRuntimeCode + buildTarget + buildChecksum + Deno . version . deno )
77
- const polyfillFile = path . join ( this . #app. buildDir , `polyfill.bundle.${ util . shortHash ( hash ) } .js` )
78
- if ( ! existsFileSync ( polyfillFile ) ) {
79
- const rawPolyfillFile = `${ alephPkgUri } /compiler/polyfills/${ buildTarget } /polyfill.js `
80
- await this . runDenoBundle ( rawPolyfillFile , polyfillFile , AlephRuntimeCode , true )
99
+ const hash = computeHash ( AlephRuntimeCode + buildTarget + Deno . version . deno + VERSION )
100
+ const bundleFile = path . join ( this . #app. buildDir , `polyfill.bundle.${ hash . slice ( 0 , hashShortLength ) } .js` )
101
+ if ( ! existsFileSync ( bundleFile ) ) {
102
+ const rawPolyfillFile = `${ alephPkgUri } /compiler/polyfills/${ buildTarget } /mod.ts `
103
+ await this . runDenoBundle ( rawPolyfillFile , bundleFile , AlephRuntimeCode , true )
81
104
}
82
- log . info ( ` {} polyfill (${ buildTarget . toUpperCase ( ) } ) ${ colors . dim ( '• ' + util . formatBytes ( Deno . statSync ( polyfillFile ) . size ) ) } ` )
105
+ log . info ( ` {} polyfill (${ buildTarget . toUpperCase ( ) } ) ${ colors . dim ( '• ' + util . formatBytes ( Deno . statSync ( bundleFile ) . size ) ) } ` )
83
106
}
84
107
85
108
/** create bundle chunk. */
@@ -96,7 +119,7 @@ export class Bundler {
96
119
} ) . flat ( ) . join ( '\n' )
97
120
const hash = computeHash ( entryCode + VERSION + Deno . version . deno )
98
121
const bundleEntryFile = path . join ( this . #app. buildDir , `${ name } .bundle.entry.js` )
99
- const bundleFile = path . join ( this . #app. buildDir , `${ name } .bundle.${ util . shortHash ( hash ) } .js` )
122
+ const bundleFile = path . join ( this . #app. buildDir , `${ name } .bundle.${ hash . slice ( 0 , hashShortLength ) } .js` )
100
123
if ( ! existsFileSync ( bundleFile ) ) {
101
124
await Deno . writeTextFile ( bundleEntryFile , entryCode )
102
125
await this . runDenoBundle ( bundleEntryFile , bundleFile )
@@ -124,7 +147,7 @@ export class Bundler {
124
147
// transpile bundle code to `buildTarget`
125
148
const { buildTarget } = this . #app. config
126
149
127
- let { code } = transformSync (
150
+ let { code } = await transform (
128
151
'/bundle.js' ,
129
152
await Deno . readTextFile ( bundleFile ) ,
130
153
{
@@ -135,11 +158,6 @@ export class Bundler {
135
158
}
136
159
)
137
160
138
- // workaround for https://github.com/denoland/deno/issues/9212
139
- if ( Deno . version . deno === '1.7.0' && bundleEntryFile . endsWith ( 'deps.bundle.entry.js' ) ) {
140
- code = code . replace ( ' _ = l.baseState, ' , ' var _ = l.baseState, ' )
141
- }
142
-
143
161
// IIFEify
144
162
code = `(() => { ${ header } ;${ code } })()`
145
163
0 commit comments