@@ -3,21 +3,27 @@ import { readFile } from "node:fs/promises";
33import { join } from "node:path" ;
44
55import { type BuildOptions , getPackagePath } from "@opennextjs/aws/build/helper.js" ;
6+ import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js" ;
67import type { PluginBuild } from "esbuild" ;
78
89import { patchCode , type RuleConfig } from "../ast/util.js" ;
910
10- export default function InlineRequirePagePlugin ( buildOpts : BuildOptions ) {
11+ export default function inlineRequirePagePlugin ( buildOpts : BuildOptions ) {
1112 return {
1213 name : "inline-require-page" ,
1314
1415 setup : async ( build : PluginBuild ) => {
15- build . onLoad ( { filter : / \/ n e x t \/ d i s t \/ s e r v e r \/ r e q u i r e \. j s $ / } , async ( { path } ) => {
16- const jsCode = await readFile ( path , "utf8" ) ;
17- if ( / f u n c t i o n r e q u i r e P a g e \( / . test ( jsCode ) ) {
18- return { contents : patchCode ( jsCode , getRule ( buildOpts ) ) } ;
16+ build . onLoad (
17+ {
18+ filter : getCrossPlatformPathRegex ( String . raw `/next/dist/server/require\.js$` , { escape : false } ) ,
19+ } ,
20+ async ( { path } ) => {
21+ const jsCode = await readFile ( path , "utf8" ) ;
22+ if ( / f u n c t i o n r e q u i r e P a g e \( / . test ( jsCode ) ) {
23+ return { contents : patchCode ( jsCode , getRule ( buildOpts ) ) } ;
24+ }
1925 }
20- } ) ;
26+ ) ;
2127 } ,
2228 } ;
2329}
@@ -42,24 +48,24 @@ function getRule(buildOpts: BuildOptions) {
4248 const jsFiles = manifests . filter ( ( file ) => file . endsWith ( ".js" ) ) ;
4349
4450 const fnBody = `
45- // html
46- ${ htmlFiles
51+ // html
52+ ${ htmlFiles
53+ . map (
54+ ( file ) => `if (pagePath.endsWith("${ file } ")) {
55+ return ${ JSON . stringify ( readFileSync ( join ( serverDir , file ) , "utf-8" ) ) } ;
56+ }`
57+ )
58+ . join ( "\n" ) }
59+ // js
60+ process.env.__NEXT_PRIVATE_RUNTIME_TYPE = isAppPath ? 'app' : 'pages';
61+ try {
62+ ${ jsFiles
4763 . map (
4864 ( file ) => `if (pagePath.endsWith("${ file } ")) {
49- return ${ JSON . stringify ( readFileSync ( join ( serverDir , file ) , "utf-8" ) ) } ;
50- }`
65+ return require( ${ JSON . stringify ( join ( serverDir , file ) ) } ) ;
66+ }`
5167 )
5268 . join ( "\n" ) }
53- // js
54- process.env.__NEXT_PRIVATE_RUNTIME_TYPE = isAppPath ? 'app' : 'pages';
55- try {
56- ${ jsFiles
57- . map (
58- ( file ) => `if (pagePath.endsWith("${ file } ")) {
59- return require(${ JSON . stringify ( join ( serverDir , file ) ) } );
60- }`
61- )
62- . join ( "\n" ) }
6369} finally {
6470 process.env.__NEXT_PRIVATE_RUNTIME_TYPE = '';
6571}
@@ -70,7 +76,7 @@ function getRule(buildOpts: BuildOptions) {
7076 pattern : `
7177function requirePage($PAGE, $DIST_DIR, $IS_APPP_ATH) {
7278 const $_ = getPagePath($$$ARGS);
73- $$$
79+ $$$_BODY
7480}` ,
7581 } ,
7682 fix : `
0 commit comments