1
1
import { existsSync , writeFileSync } from 'node:fs' ;
2
2
import * as path from 'node:path' ;
3
3
import { fileURLToPath } from 'node:url' ;
4
- import * as esbuild from 'esbuild' ;
5
4
import { getPlatformProxy } from 'wrangler' ;
6
5
7
- // list from https://developers.cloudflare.com/workers/runtime-apis/nodejs/
8
- const compatible_node_modules = [
9
- 'assert' ,
10
- 'async_hooks' ,
11
- 'buffer' ,
12
- 'crypto' ,
13
- 'diagnostics_channel' ,
14
- 'events' ,
15
- 'path' ,
16
- 'process' ,
17
- 'stream' ,
18
- 'string_decoder' ,
19
- 'util'
20
- ] ;
21
-
22
6
/** @type {import('./index.js').default } */
23
7
export default function ( options = { } ) {
24
8
return {
@@ -52,7 +36,7 @@ export default function (options = {}) {
52
36
const written_files = builder . writeClient ( dest_dir ) ;
53
37
builder . writePrerendered ( dest_dir ) ;
54
38
55
- const relativePath = path . posix . relative ( tmp , builder . getServerDirectory ( ) ) ;
39
+ const relativePath = path . posix . relative ( dest , builder . getServerDirectory ( ) ) ;
56
40
57
41
writeFileSync (
58
42
`${ tmp } /manifest.js` ,
@@ -74,77 +58,14 @@ export default function (options = {}) {
74
58
} ) ;
75
59
}
76
60
77
- builder . copy ( `${ files } /worker.js` , `${ tmp } /_worker.js` , {
61
+ writeFileSync ( `${ dest } /.assetsignore` , generate_assetsignore ( ) , { flag : 'a' } ) ;
62
+
63
+ builder . copy ( `${ files } /worker.js` , `${ dest } /_worker.js` , {
78
64
replace : {
79
65
SERVER : `${ relativePath } /index.js` ,
80
- MANIFEST : '. /manifest.js'
66
+ MANIFEST : ` ${ path . posix . relative ( dest , tmp ) } /manifest.js`
81
67
}
82
68
} ) ;
83
-
84
- const external = [ 'cloudflare:*' , ...compatible_node_modules . map ( ( id ) => `node:${ id } ` ) ] ;
85
-
86
- try {
87
- const result = await esbuild . build ( {
88
- platform : 'browser' ,
89
- // https://github.com/cloudflare/workers-sdk/blob/a12b2786ce745f24475174bcec994ad691e65b0f/packages/wrangler/src/deployment-bundle/bundle.ts#L35-L36
90
- conditions : [ 'workerd' , 'worker' , 'browser' ] ,
91
- sourcemap : 'linked' ,
92
- target : 'es2022' ,
93
- entryPoints : [ `${ tmp } /_worker.js` ] ,
94
- outfile : `${ dest } /_worker.js` ,
95
- allowOverwrite : true ,
96
- format : 'esm' ,
97
- bundle : true ,
98
- loader : {
99
- '.wasm' : 'copy' ,
100
- '.woff' : 'copy' ,
101
- '.woff2' : 'copy' ,
102
- '.ttf' : 'copy' ,
103
- '.eot' : 'copy' ,
104
- '.otf' : 'copy'
105
- } ,
106
- external,
107
- alias : Object . fromEntries ( compatible_node_modules . map ( ( id ) => [ id , `node:${ id } ` ] ) ) ,
108
- logLevel : 'silent'
109
- } ) ;
110
-
111
- if ( result . warnings . length > 0 ) {
112
- const formatted = await esbuild . formatMessages ( result . warnings , {
113
- kind : 'warning' ,
114
- color : true
115
- } ) ;
116
-
117
- console . error ( formatted . join ( '\n' ) ) ;
118
- }
119
- } catch ( error ) {
120
- for ( const e of error . errors ) {
121
- for ( const node of e . notes ) {
122
- const match =
123
- / T h e p a c k a g e " ( .+ ) " w a s n ' t f o u n d o n t h e f i l e s y s t e m b u t i s b u i l t i n t o n o d e / . exec (
124
- node . text
125
- ) ;
126
-
127
- if ( match ) {
128
- node . text = `Cannot use "${ match [ 1 ] } " when deploying to Cloudflare.` ;
129
- }
130
- }
131
- }
132
-
133
- const formatted = await esbuild . formatMessages ( error . errors , {
134
- kind : 'error' ,
135
- color : true
136
- } ) ;
137
-
138
- console . error ( formatted . join ( '\n' ) ) ;
139
-
140
- throw new Error (
141
- `Bundling with esbuild failed with ${ error . errors . length } ${
142
- error . errors . length === 1 ? 'error' : 'errors'
143
- } `
144
- ) ;
145
- }
146
-
147
- writeFileSync ( `${ dest } /.assetsignore` , generate_assetsignore ( ) , { flag : 'a' } ) ;
148
69
} ,
149
70
async emulate ( ) {
150
71
const proxy = await getPlatformProxy ( options . platformProxy ) ;
0 commit comments