@@ -10,6 +10,7 @@ import {
10
10
} from "types/open-next" ;
11
11
import url from "url" ;
12
12
13
+ import { compileCache } from "../build.js" ;
13
14
import logger from "../logger.js" ;
14
15
import { minifyAll } from "../minimize-js.js" ;
15
16
import { openNextReplacementPlugin } from "../plugins/replacement.js" ;
@@ -37,6 +38,13 @@ export async function createServerBundle(
37
38
const defaultFn = config . default ;
38
39
const functions = Object . entries ( config . functions ?? { } ) ;
39
40
41
+ if (
42
+ defaultFn . runtime === "deno" ||
43
+ functions . some ( ( [ , fn ] ) => fn . runtime === "deno" )
44
+ ) {
45
+ compileCache ( "esm" ) ;
46
+ }
47
+
40
48
const promises = functions . map ( async ( [ name , fnOptions ] ) => {
41
49
const routes = fnOptions . routes ;
42
50
routes . forEach ( ( route ) => foundRoutes . add ( route ) ) ;
@@ -134,11 +142,16 @@ async function generateBundle(
134
142
const packagePath = path . relative ( monorepoRoot , appBuildOutputPath ) ;
135
143
fs . mkdirSync ( path . join ( outputPath , packagePath ) , { recursive : true } ) ;
136
144
145
+ const ext = fnOptions . runtime === "deno" ? "mjs" : "cjs" ;
137
146
fs . copyFileSync (
138
- path . join ( outputDir , ".build" , " cache.cjs" ) ,
147
+ path . join ( outputDir , ".build" , ` cache.${ ext } ` ) ,
139
148
path . join ( outputPath , packagePath , "cache.cjs" ) ,
140
149
) ;
141
150
151
+ if ( fnOptions . runtime === "deno" ) {
152
+ addDenoJson ( outputPath , packagePath ) ;
153
+ }
154
+
142
155
// Bundle next server if necessary
143
156
const isBundled = fnOptions . experimentalBundledNextServer ?? false ;
144
157
if ( isBundled ) {
@@ -227,14 +240,18 @@ async function generateBundle(
227
240
. join ( "," ) } ] for Next version: ${ options . nextVersion } `,
228
241
) ;
229
242
}
243
+
244
+ const outfileExt = fnOptions . runtime === "deno" ? "ts" : "mjs" ;
230
245
await esbuildAsync (
231
246
{
232
247
entryPoints : [ path . join ( __dirname , "../adapters" , "server-adapter.js" ) ] ,
233
248
external : [ "next" , "./middleware.mjs" , "./next-server.runtime.prod.js" ] ,
234
- outfile : path . join ( outputPath , packagePath , " index.mjs" ) ,
249
+ outfile : path . join ( outputPath , packagePath , ` index.${ outfileExt } ` ) ,
235
250
banner : {
236
251
js : [
237
252
`globalThis.monorepoPackagePath = "${ packagePath } ";` ,
253
+ "import process from 'node:process';" ,
254
+ "import { Buffer } from 'node:buffer';" ,
238
255
"import { createRequire as topLevelCreateRequire } from 'module';" ,
239
256
"const require = topLevelCreateRequire(import.meta.url);" ,
240
257
"import bannerUrl from 'url';" ,
@@ -280,6 +297,17 @@ function shouldGenerateDockerfile(options: FunctionOptions) {
280
297
return options . override ?. generateDockerfile ?? false ;
281
298
}
282
299
300
+ function addDenoJson ( outputPath : string , packagePath : string ) {
301
+ const config = {
302
+ // Enable "bring your own node_modules" mode
303
+ unstable : [ "byonm" , "fs" ] ,
304
+ } ;
305
+ fs . writeFileSync (
306
+ path . join ( outputPath , packagePath , "deno.json" ) ,
307
+ JSON . stringify ( config , null , 2 ) ,
308
+ ) ;
309
+ }
310
+
283
311
//TODO: check if this PR is still necessary https://github.com/sst/open-next/pull/341
284
312
function addMonorepoEntrypoint ( outputPath : string , packagePath : string ) {
285
313
// Note: in the monorepo case, the handler file is output to
0 commit comments