From 6713a5335f65c566a9c3acff6ec6ad292c54a511 Mon Sep 17 00:00:00 2001 From: hrmny Date: Fri, 13 Oct 2023 19:56:17 +0200 Subject: [PATCH] fix empty string imports and change order because the transpiler is weird about removing comments --- packages/next/src/build/load-entrypoint.ts | 4 ++-- packages/next/src/build/templates/edge-ssr-app.ts | 14 ++++++-------- packages/next/src/build/templates/edge-ssr.ts | 11 ++++------- .../webpack/loaders/next-edge-ssr-loader/index.ts | 8 ++++---- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/next/src/build/load-entrypoint.ts b/packages/next/src/build/load-entrypoint.ts index b13158a8c46bb..4e34c351297fb 100644 --- a/packages/next/src/build/load-entrypoint.ts +++ b/packages/next/src/build/load-entrypoint.ts @@ -186,14 +186,14 @@ export async function loadEntrypoint( `// OPTIONAL_IMPORT:(\\* as )?(${Object.keys(imports).join('|')})`, 'g' ), - (_, asNamespace, key) => { + (_, asNamespace = '', key) => { if (!(key in imports)) { throw new Error(`Invariant: Unexpected optional import ${key}`) } importsAdded.add(key) - if (imports[key] != null) { + if (imports[key]) { return `import ${asNamespace}${key} from ${JSON.stringify( imports[key] )}` diff --git a/packages/next/src/build/templates/edge-ssr-app.ts b/packages/next/src/build/templates/edge-ssr-app.ts index 655fe694f6900..3842bf2c361bb 100644 --- a/packages/next/src/build/templates/edge-ssr-app.ts +++ b/packages/next/src/build/templates/edge-ssr-app.ts @@ -11,34 +11,32 @@ import type { BuildManifest } from '../../server/get-page-files' import type { RequestData } from '../../server/web/types' import type { NextConfigComplete } from '../../server/config-shared' +declare const incrementalCacheHandler: any +// OPTIONAL_IMPORT:incrementalCacheHandler + const Document: DocumentType = null! const appMod = null const errorMod = null const error500Mod = null // injected by the loader afterwards. -declare const page: string declare const sriEnabled: boolean declare const isServerComponent: boolean declare const dev: boolean declare const serverActionsBodySizeLimit: any declare const nextConfig: NextConfigComplete -// INJECT:page // INJECT:sriEnabled // INJECT:isServerComponent // INJECT:dev // INJECT:serverActionsBodySizeLimit -// INJECT:config - -declare const incrementalCacheHandler: any -// OPTIONAL_IMPORT:incrementalCacheHandler +// INJECT:nextConfig const maybeJSONParse = (str?: string) => (str ? JSON.parse(str) : undefined) const buildManifest: BuildManifest = self.__BUILD_MANIFEST as any const prerenderManifest = maybeJSONParse(self.__PRERENDER_MANIFEST) const reactLoadableManifest = maybeJSONParse(self.__REACT_LOADABLE_MANIFEST) -const rscManifest = self.__RSC_MANIFEST?.[page] +const rscManifest = self.__RSC_MANIFEST?.['VAR_PAGE'] const rscServerManifest = maybeJSONParse(self.__RSC_SERVER_MANIFEST) const subresourceIntegrityManifest = sriEnabled ? maybeJSONParse(self.__SUBRESOURCE_INTEGRITY_MANIFEST) @@ -48,7 +46,7 @@ const nextFontManifest = maybeJSONParse(self.__NEXT_FONT_MANIFEST) const render = getRender({ pagesType: 'app', dev, - page, + page: 'VAR_PAGE', appMod, pageMod, errorMod, diff --git a/packages/next/src/build/templates/edge-ssr.ts b/packages/next/src/build/templates/edge-ssr.ts index 5b85e3b61dab6..ecb4ddb812fb1 100644 --- a/packages/next/src/build/templates/edge-ssr.ts +++ b/packages/next/src/build/templates/edge-ssr.ts @@ -9,7 +9,9 @@ import * as userlandPage from 'VAR_USERLAND' import * as userlandErrorPage from 'VAR_MODULE_GLOBAL_ERROR' declare const userland500Page: any +declare const incrementalCacheHandler: any // OPTIONAL_IMPORT:* as userland500Page +// OPTIONAL_IMPORT:incrementalCacheHandler // TODO: re-enable this once we've refactored to use implicit matches // const renderToHTML = undefined @@ -23,7 +25,6 @@ import type { NextConfigComplete } from '../../server/config-shared' // injected by the loader afterwards. declare const pagesType: 'app' | 'pages' | 'root' -declare const page: string declare const sriEnabled: boolean declare const dev: boolean declare const nextConfig: NextConfigComplete @@ -31,10 +32,9 @@ declare const pageRouteModuleOptions: any declare const errorRouteModuleOptions: any declare const user500RouteModuleOptions: any // INJECT:pagesType -// INJECT:page // INJECT:sriEnabled // INJECT:dev -// INJECT:config +// INJECT:nextConfig // INJECT:pageRouteModuleOptions // INJECT:errorRouteModuleOptions // INJECT:user500RouteModuleOptions @@ -78,9 +78,6 @@ const error500Mod = userland500Page } : null -declare const incrementalCacheHandler: any -// OPTIONAL_IMPORT:incrementalCacheHandler - const maybeJSONParse = (str?: string) => (str ? JSON.parse(str) : undefined) const buildManifest: BuildManifest = self.__BUILD_MANIFEST as any @@ -94,7 +91,7 @@ const nextFontManifest = maybeJSONParse(self.__NEXT_FONT_MANIFEST) const render = getRender({ pagesType, dev, - page, + page: 'VAR_PAGE', appMod, pageMod, errorMod, diff --git a/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts b/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts index 9604b3dea65f7..1129b16052e8a 100644 --- a/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts +++ b/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts @@ -140,11 +140,11 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction = return await loadEntrypoint( 'edge-ssr-app', { - VAR_BUILD_ID: buildId, VAR_USERLAND: pageModPath, + VAR_PAGE: page, + VAR_BUILD_ID: buildId, }, { - page, sriEnabled: JSON.stringify(sriEnabled), nextConfig: stringifiedConfig, isServerComponent: JSON.stringify(isServerComponent), @@ -162,15 +162,15 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction = return await loadEntrypoint( 'edge-ssr', { + VAR_USERLAND: pageModPath, + VAR_PAGE: page, VAR_BUILD_ID: buildId, VAR_MODULE_DOCUMENT: documentPath, VAR_MODULE_APP: appPath, VAR_MODULE_GLOBAL_ERROR: errorPath, - VAR_USERLAND: pageModPath, }, { pagesType: JSON.stringify(pagesType), - page, sriEnabled: JSON.stringify(sriEnabled), nextConfig: stringifiedConfig, dev: JSON.stringify(dev),