Unverified Commit 4558fac2 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

move webpack specific logic into a separate file (#53114)

### What?

move webpack specific code out of the `client/index.ts` file

### Why?

sharing with turbopack. Avoiding extra shims to fake webpack.
parent d8c4700a
...@@ -2,5 +2,5 @@ ...@@ -2,5 +2,5 @@
* This is the runtime entry point for Next.js App Router client-side bundles. * This is the runtime entry point for Next.js App Router client-side bundles.
*/ */
import './shims' import '../shims'
import 'next/dist/client/app-next' import 'next/dist/client/app-next'
...@@ -2,5 +2,25 @@ ...@@ -2,5 +2,25 @@
* This is the runtime entry point for Next.js Page Router client-side bundles. * This is the runtime entry point for Next.js Page Router client-side bundles.
*/ */
import './shims' import '../shims'
import 'next/dist/client/next' import { initialize, hydrate, version, router, emitter } from 'next/dist/client'
declare global {
interface Window {
next: any
}
}
window.next = {
version: `${version}-turbo`,
// router is initialized later so it has to be live-binded
get router() {
return router
},
emitter,
}
;(self as any).__next_set_public_path__ = () => {}
initialize({})
.then(() => hydrate())
.catch(console.error)
import '../shims'
// Next uses __webpack_require__ extensively.
globalThis.__webpack_require__ = (name) => {
console.error(
`__webpack_require__ is not implemented (when requiring ${name})`
)
}
// initialize() needs `__webpack_public_path__` to be defined.
globalThis.__webpack_public_path__ = undefined
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
import { formatWithValidation } from 'next/dist/shared/lib/router/utils/format-url' import { formatWithValidation } from 'next/dist/shared/lib/router/utils/format-url'
import { initializeHMR } from '../dev/client' import { initializeHMR } from '../dev/client'
import { subscribeToUpdate } from '@vercel/turbopack-ecmascript-runtime/dev/client/hmr-client' import { subscribeToUpdate } from '@vercel/turbopack-ecmascript-runtime/dev/client/hmr-client'
;(self as any).__next_set_public_path__ = () => {}
async function loadPageChunk(assetPrefix: string, chunkData: ChunkData) { async function loadPageChunk(assetPrefix: string, chunkData: ChunkData) {
if (typeof chunkData === 'string') { if (typeof chunkData === 'string') {
......
...@@ -7,13 +7,3 @@ process.env.NODE_ENV = 'development' ...@@ -7,13 +7,3 @@ process.env.NODE_ENV = 'development'
// This is a fix for web-vitals.js not being linked properly. // This is a fix for web-vitals.js not being linked properly.
globalThis.__dirname = '' globalThis.__dirname = ''
// Next uses __webpack_require__ extensively.
globalThis.__webpack_require__ = (name) => {
console.error(
`__webpack_require__ is not implemented (when requiring ${name})`
)
}
// initialize() needs `__webpack_public_path__` to be defined.
globalThis.__webpack_public_path__ = undefined
...@@ -49,8 +49,6 @@ import onRecoverableError from './on-recoverable-error' ...@@ -49,8 +49,6 @@ import onRecoverableError from './on-recoverable-error'
/// <reference types="react-dom/experimental" /> /// <reference types="react-dom/experimental" />
declare let __webpack_public_path__: string
declare global { declare global {
interface Window { interface Window {
/* test fns */ /* test fns */
...@@ -62,34 +60,6 @@ declare global { ...@@ -62,34 +60,6 @@ declare global {
__NEXT_P: any[] __NEXT_P: any[]
} }
} }
const addChunkSuffix =
(getOriginalChunk: (chunkId: any) => string) => (chunkId: any) => {
return (
getOriginalChunk(chunkId) +
`${
process.env.NEXT_DEPLOYMENT_ID
? `?dpl=${process.env.NEXT_DEPLOYMENT_ID}`
: ''
}`
)
}
// ensure dynamic imports have deployment id added if enabled
const getChunkScriptFilename = __webpack_require__.u
// eslint-disable-next-line no-undef
__webpack_require__.u = addChunkSuffix(getChunkScriptFilename)
// eslint-disable-next-line no-undef
const getChunkCssFilename = __webpack_require__.k
// eslint-disable-next-line no-undef
__webpack_require__.k = addChunkSuffix(getChunkCssFilename)
// eslint-disable-next-line no-undef
const getMiniCssFilename = __webpack_require__.miniCssF
// eslint-disable-next-line no-undef
__webpack_require__.miniCssF = addChunkSuffix(getMiniCssFilename)
type RenderRouteInfo = PrivateRouteInfo & { type RenderRouteInfo = PrivateRouteInfo & {
App: AppComponent App: AppComponent
scroll?: { x: number; y: number } | null scroll?: { x: number; y: number } | null
...@@ -122,10 +92,6 @@ let webpackHMR: any ...@@ -122,10 +92,6 @@ let webpackHMR: any
let CachedApp: AppComponent, onPerfEntry: (metric: any) => void let CachedApp: AppComponent, onPerfEntry: (metric: any) => void
let CachedComponent: React.ComponentType let CachedComponent: React.ComponentType
// Ignore the module ID transform in client.
// @ts-ignore
;(self as any).__next_require__ = __webpack_require__
class Container extends React.Component<{ class Container extends React.Component<{
children?: React.ReactNode children?: React.ReactNode
fn: (err: Error, info?: any) => void fn: (err: Error, info?: any) => void
...@@ -234,7 +200,7 @@ export async function initialize(opts: { webpackHMR?: any } = {}): Promise<{ ...@@ -234,7 +200,7 @@ export async function initialize(opts: { webpackHMR?: any } = {}): Promise<{
const prefix: string = initialData.assetPrefix || '' const prefix: string = initialData.assetPrefix || ''
// With dynamic assetPrefix it's no longer possible to set assetPrefix at the build time // With dynamic assetPrefix it's no longer possible to set assetPrefix at the build time
// So, this is how we do it in the client side at runtime // So, this is how we do it in the client side at runtime
__webpack_public_path__ = `${prefix}/_next/` //eslint-disable-line ;(self as any).__next_set_public_path__(`${prefix}/_next/`) //eslint-disable-line
// Initialize next/config with the environment configuration // Initialize next/config with the environment configuration
setConfig({ setConfig({
......
// TODO: Remove use of `any` type. // TODO: Remove use of `any` type.
import './webpack'
import { initialize, hydrate, version, router, emitter } from './' import { initialize, hydrate, version, router, emitter } from './'
import initOnDemandEntries from './dev/on-demand-entries-client' import initOnDemandEntries from './dev/on-demand-entries-client'
import initWebpackHMR from './dev/webpack-hot-middleware-client' import initWebpackHMR from './dev/webpack-hot-middleware-client'
......
import './webpack'
import { initialize, hydrate, version, router, emitter } from './' import { initialize, hydrate, version, router, emitter } from './'
declare global { declare global {
......
declare let __webpack_public_path__: string
const addChunkSuffix =
(getOriginalChunk: (chunkId: any) => string) => (chunkId: any) => {
return (
getOriginalChunk(chunkId) +
`${
process.env.NEXT_DEPLOYMENT_ID
? `?dpl=${process.env.NEXT_DEPLOYMENT_ID}`
: ''
}`
)
}
// ensure dynamic imports have deployment id added if enabled
const getChunkScriptFilename = __webpack_require__.u
// eslint-disable-next-line no-undef
__webpack_require__.u = addChunkSuffix(getChunkScriptFilename)
// eslint-disable-next-line no-undef
const getChunkCssFilename = __webpack_require__.k
// eslint-disable-next-line no-undef
__webpack_require__.k = addChunkSuffix(getChunkCssFilename)
// eslint-disable-next-line no-undef
const getMiniCssFilename = __webpack_require__.miniCssF
// eslint-disable-next-line no-undef
__webpack_require__.miniCssF = addChunkSuffix(getMiniCssFilename)
// Ignore the module ID transform in client.
// @ts-ignore
;(self as any).__next_require__ = __webpack_require__
;(self as any).__next_set_public_path__ = (path: string) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
__webpack_public_path__ = path
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment