@@ -117,10 +117,12 @@ async function runify(packagePath: string, reporter: Consola) {
117117 }
118118
119119 if ( isNext ( pkg ) ) {
120- await buildNext ( cwd ) ;
120+ const additionalRequire = pkg ?. buildOptions ?. runify ?. next ?. header ?? null ;
121+ await buildNext ( cwd , additionalRequire ) ;
121122 await rewritePackageJson ( pkg , cwd , ( newPkg ) => ( {
122123 ...newPkg ,
123124 dependencies : pkg . dependencies ,
125+ type : "commonjs" ,
124126 } ) ) ;
125127 } else {
126128 await compile (
@@ -195,7 +197,7 @@ function isNext(pkg: any): boolean {
195197 return pkg ?. dependencies ?. next || pkg ?. devDependencies ?. next ;
196198}
197199
198- async function buildNext ( cwd : string ) {
200+ async function buildNext ( cwd : string , additionalRequire : string | null ) {
199201 await new Promise ( ( resolve , reject ) => {
200202 const child = spawn ( "next" , [ "build" ] , {
201203 stdio : "inherit" ,
@@ -206,6 +208,17 @@ async function buildNext(cwd: string) {
206208 } ) ;
207209
208210 await fs . mkdirp ( join ( cwd , "dist" ) ) ;
211+ if ( additionalRequire ) {
212+ await tsup ( {
213+ entryPoints : [ join ( cwd , additionalRequire ) ] ,
214+ outDir : join ( cwd , "dist" ) ,
215+ target : "node16" ,
216+ format : [ "cjs" ] ,
217+ splitting : false ,
218+ skipNodeModulesBundle : true ,
219+ } ) ;
220+ }
221+
209222 await Promise . all ( [
210223 fs . copy ( join ( cwd , ".next" ) , join ( cwd , "dist/.next" ) , {
211224 filter ( src ) {
@@ -220,11 +233,15 @@ async function buildNext(cwd: string) {
220233 `#!/usr/bin/env node` ,
221234 `process.on('SIGTERM', () => process.exit(0))` ,
222235 `process.on('SIGINT', () => process.exit(0))` ,
236+ additionalRequire
237+ ? `require('${ additionalRequire . replace ( ".ts" , "" ) } ')`
238+ : `` ,
223239 `
224240 require('next/dist/server/lib/start-server').startServer({
225241 dir: __dirname,
226242 hostname: '0.0.0.0',
227- port: parseInt(process.env.PORT)
243+ port: parseInt(process.env.PORT),
244+ conf: {},
228245 }).then(async (app)=>{
229246 const appUrl = 'http://' + app.hostname + ':' + app.port;
230247 console.log('started server on '+ app.hostname + ':' + app.port + ', url: ' + appUrl);
@@ -257,6 +274,7 @@ async function compile(
257274 splitting : false ,
258275 sourcemap : true ,
259276 clean : true ,
277+ shims : true ,
260278 skipNodeModulesBundle : false ,
261279 noExternal : dependencies ,
262280 external : buildOptions . external ,
0 commit comments