Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move code shared between server/client to "shared" folder #26734

Merged
merged 8 commits into from
Jun 30, 2021
2 changes: 1 addition & 1 deletion packages/next/amp.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './dist/next-server/lib/amp'
export * from './dist/shared/lib/amp'
2 changes: 1 addition & 1 deletion packages/next/amp.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./dist/next-server/lib/amp')
module.exports = require('./dist/shared/lib/amp')
2 changes: 1 addition & 1 deletion packages/next/bin/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ commands[command]()
})

if (command === 'dev') {
const { CONFIG_FILE } = require('../next-server/lib/constants')
const { CONFIG_FILE } = require('../shared/lib/constants')
const { watchFile } = require('fs')
watchFile(`${process.cwd()}/${CONFIG_FILE}`, (cur: any, prev: any) => {
if (cur.size > 0 || prev.size > 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/babel/loader/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NextBabelLoaderOptions, NextJsLoaderContext } from './types'
import { consumeIterator } from './util'
import * as Log from '../../output/log'

const nextDistPath = /(next[\\/]dist[\\/]next-server[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/
const nextDistPath = /(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/

/**
* The properties defined here are the conditions with which subsets of inputs
Expand Down
4 changes: 2 additions & 2 deletions packages/next/build/babel/plugins/commonjs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NodePath, PluginObj, types } from 'next/dist/compiled/babel/core'
import commonjsPlugin from 'next/dist/compiled/babel/plugin-transform-modules-commonjs'

// Rewrite imports using next/<something> to next-server/<something>
export default function NextToNextServer(...args: any): PluginObj {
// Handle module.exports in user code
export default function CommonJSModulePlugin(...args: any): PluginObj {
const commonjs = commonjsPlugin(...args)
return {
visitor: {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/babel/plugins/next-page-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Visitor,
} from 'next/dist/compiled/babel/core'
import { PageConfig } from 'next/types'
import { STRING_LITERAL_DROP_BUNDLE } from '../../../next-server/lib/constants'
import { STRING_LITERAL_DROP_BUNDLE } from '../../../shared/lib/constants'

const CONFIG_KEY = 'config'

Expand Down
5 changes: 1 addition & 4 deletions packages/next/build/babel/plugins/next-ssg-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import {
types as BabelTypes,
} from 'next/dist/compiled/babel/core'
import { SERVER_PROPS_SSG_CONFLICT } from '../../../lib/constants'
import {
SERVER_PROPS_ID,
STATIC_PROPS_ID,
} from '../../../next-server/lib/constants'
import { SERVER_PROPS_ID, STATIC_PROPS_ID } from '../../../shared/lib/constants'

export const EXPORT_NAME_GET_STATIC_PROPS = 'getStaticProps'
export const EXPORT_NAME_GET_STATIC_PATHS = 'getStaticPaths'
Expand Down
6 changes: 3 additions & 3 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ import {
SERVER_DIRECTORY,
SERVER_FILES_MANIFEST,
STATIC_STATUS_PAGES,
} from '../next-server/lib/constants'
} from '../shared/lib/constants'
import {
getRouteRegex,
getSortedRoutes,
isDynamicRoute,
} from '../next-server/lib/router/utils'
} from '../shared/lib/router/utils'
import { __ApiPreviewProps } from '../next-server/server/api-utils'
import loadConfig, {
isTargetLikeServerless,
Expand Down Expand Up @@ -89,7 +89,7 @@ import {
import getBaseWebpackConfig from './webpack-config'
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
import { writeBuildId } from './write-build-id'
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { isWebpack5 } from 'next/dist/compiled/webpack/webpack'

const staticCheckWorker = require.resolve('./utils')
Expand Down
14 changes: 7 additions & 7 deletions packages/next/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import {
} from '../lib/constants'
import prettyBytes from '../lib/pretty-bytes'
import { recursiveReadDir } from '../lib/recursive-readdir'
import { getRouteMatcher, getRouteRegex } from '../next-server/lib/router/utils'
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
import escapePathDelimiters from '../next-server/lib/router/utils/escape-path-delimiters'
import { getRouteMatcher, getRouteRegex } from '../shared/lib/router/utils'
import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
import escapePathDelimiters from '../shared/lib/router/utils/escape-path-delimiters'
import { findPageFile } from '../server/lib/find-page-file'
import { GetStaticPaths } from 'next/types'
import { denormalizePagePath } from '../next-server/server/normalize-page-path'
import { BuildManifest } from '../next-server/server/get-page-files'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
import { UnwrapPromise } from '../lib/coalesced-function'
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import * as Log from './output/log'
import { loadComponents } from '../next-server/server/load-components'
import { trace } from '../telemetry/trace'
Expand Down Expand Up @@ -765,7 +765,7 @@ export async function isPageStatic(
const isPageStaticSpan = trace('is-page-static-utils', parentId)
return isPageStaticSpan.traceAsyncFn(async () => {
try {
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
const components = await loadComponents(distDir, page, serverless)
const mod = components.ComponentMod
const Comp = mod.default || mod
Expand Down Expand Up @@ -880,7 +880,7 @@ export async function hasCustomGetInitialProps(
runtimeEnvConfig: any,
checkingApp: boolean
): Promise<boolean> {
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)

const components = await loadComponents(distDir, page, isLikeServerless)
let mod = components.ComponentMod
Expand All @@ -900,7 +900,7 @@ export async function getNamedExports(
isLikeServerless: boolean,
runtimeEnvConfig: any
): Promise<Array<string>> {
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
const components = await loadComponents(distDir, page, isLikeServerless)
let mod = components.ComponentMod

Expand Down
27 changes: 11 additions & 16 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {
REACT_LOADABLE_MANIFEST,
SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
} from '../next-server/lib/constants'
import { execOnce } from '../next-server/lib/utils'
} from '../shared/lib/constants'
import { execOnce } from '../shared/lib/utils'
import { NextConfig } from '../next-server/server/config'
import { findPageFile } from '../server/lib/find-page-file'
import { WebpackEntrypoints } from './entries'
Expand Down Expand Up @@ -305,7 +305,7 @@ export default async function getBaseWebpackConfig(
}

const babelIncludeRegexes: RegExp[] = [
/next[\\/]dist[\\/]next-server[\\/]lib/,
/next[\\/]dist[\\/]shared[\\/]lib/,
/next[\\/]dist[\\/]client/,
/next[\\/]dist[\\/]pages/,
/[\\/](strip-ansi|ansi-regex)[\\/]/,
Expand Down Expand Up @@ -396,10 +396,10 @@ export default async function getBaseWebpackConfig(
}

const clientResolveRewrites = require.resolve(
'../next-server/lib/router/utils/resolve-rewrites'
'../shared/lib/router/utils/resolve-rewrites'
)
const clientResolveRewritesNoop = require.resolve(
'../next-server/lib/router/utils/resolve-rewrites-noop'
'../shared/lib/router/utils/resolve-rewrites-noop'
)

const resolveConfig = {
Expand Down Expand Up @@ -676,11 +676,7 @@ export default async function getBaseWebpackConfig(
// are relative to requests we've already resolved here.
// Absolute requires (require('/foo')) are extremely uncommon, but
// also have no need for customization as they're already resolved.
if (isLocal) {
if (!/[/\\]next-server[/\\]/.test(request)) {
return
}
} else {
if (!isLocal) {
if (/^(?:next$|react(?:$|\/))/.test(request)) {
return `commonjs ${request}`
}
Expand Down Expand Up @@ -713,9 +709,10 @@ export default async function getBaseWebpackConfig(
}

if (isLocal) {
// we need to process next-server/lib/router/router so that
// Makes sure dist/shared and dist/next-server are not bundled
// we need to process shared/lib/router/router so that
// the DefinePlugin can inject process.env values
const isNextExternal = /next[/\\]dist[/\\]next-server[/\\](?!lib[/\\]router[/\\]router)/.test(
const isNextExternal = /next[/\\]dist[/\\](shared|next-server)[/\\](?!lib[/\\]router[/\\]router)/.test(
res
)

Expand Down Expand Up @@ -761,9 +758,7 @@ export default async function getBaseWebpackConfig(
}

if (
res.match(
/next[/\\]dist[/\\]next-server[/\\](?!lib[/\\]router[/\\]router)/
)
res.match(/next[/\\]dist[/\\]shared[/\\](?!lib[/\\]router[/\\]router)/)
) {
return `commonjs ${request}`
}
Expand Down Expand Up @@ -1193,7 +1188,7 @@ export default async function getBaseWebpackConfig(
!dev &&
new webpack.IgnorePlugin({
resourceRegExp: /react-is/,
contextRegExp: /(next-server|next)[\\/]dist[\\/]/,
contextRegExp: /next[\\/]dist[\\/]/,
}),
isServerless && isServer && new ServerlessPlugin(),
isServer &&
Expand Down
4 changes: 2 additions & 2 deletions packages/next/build/webpack/loaders/next-babel-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ const customBabelLoader = babelLoader((babel) => {
}
}

// As next-server/lib has stateful modules we have to transpile commonjs
// As shared/lib has stateful modules we have to transpile commonjs
options.overrides = [
...(options.overrides || []),
{
test: [
/next[\\/]dist[\\/]next-server[\\/]lib/,
/next[\\/]dist[\\/]shared[\\/]lib/,
/next[\\/]dist[\\/]client/,
/next[\\/]dist[\\/]pages/,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { join } from 'path'
import { parse } from 'querystring'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { API_ROUTE } from '../../../../lib/constants'
import { isDynamicRoute } from '../../../../next-server/lib/router/utils'
import { isDynamicRoute } from '../../../../shared/lib/router/utils'
import { __ApiPreviewProps } from '../../../../next-server/server/api-utils'
import {
BUILD_MANIFEST,
ROUTES_MANIFEST,
REACT_LOADABLE_MANIFEST,
} from '../../../../next-server/lib/constants'
} from '../../../../shared/lib/constants'
import { trace } from '../../../../telemetry/trace'

export type ServerlessLoaderQuery = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomingMessage, ServerResponse } from 'http'
import { parse as parseUrl, format as formatUrl, UrlWithParsedQuery } from 'url'
import { isResSent } from '../../../../next-server/lib/utils'
import { isResSent } from '../../../../shared/lib/utils'
import { sendPayload } from '../../../../next-server/server/send-payload'
import { getUtils, vercelHeader, ServerlessHandlerCtx } from './utils'

Expand All @@ -12,8 +12,8 @@ import {
getCookieParser,
} from '../../../../next-server/server/api-utils'
import { getRedirectStatus } from '../../../../lib/load-custom-routes'
import getRouteNoAssetPath from '../../../../next-server/lib/router/utils/get-route-from-asset-path'
import { PERMANENT_REDIRECT_STATUS } from '../../../../next-server/lib/constants'
import getRouteNoAssetPath from '../../../../shared/lib/router/utils/get-route-from-asset-path'
import { PERMANENT_REDIRECT_STATUS } from '../../../../shared/lib/constants'

export function getPageHandler(ctx: ServerlessHandlerCtx) {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { IncomingMessage, ServerResponse } from 'http'
import { format as formatUrl, UrlWithParsedQuery, parse as parseUrl } from 'url'
import { parse as parseQs, ParsedUrlQuery } from 'querystring'
import { Rewrite } from '../../../../lib/load-custom-routes'
import { normalizeLocalePath } from '../../../../next-server/lib/i18n/normalize-locale-path'
import pathMatch from '../../../../next-server/lib/router/utils/path-match'
import { getRouteRegex } from '../../../../next-server/lib/router/utils/route-regex'
import { getRouteMatcher } from '../../../../next-server/lib/router/utils/route-matcher'
import { normalizeLocalePath } from '../../../../shared/lib/i18n/normalize-locale-path'
import pathMatch from '../../../../shared/lib/router/utils/path-match'
import { getRouteRegex } from '../../../../shared/lib/router/utils/route-regex'
import { getRouteMatcher } from '../../../../shared/lib/router/utils/route-matcher'
import prepareDestination, {
matchHas,
} from '../../../../next-server/lib/router/utils/prepare-destination'
} from '../../../../shared/lib/router/utils/prepare-destination'
import { __ApiPreviewProps } from '../../../../next-server/server/api-utils'
import { BuildManifest } from '../../../../next-server/server/get-page-files'
import {
Expand All @@ -17,11 +17,11 @@ import {
GetStaticProps,
} from '../../../../types'
import accept from '@hapi/accept'
import { detectLocaleCookie } from '../../../../next-server/lib/i18n/detect-locale-cookie'
import { detectDomainLocale } from '../../../../next-server/lib/i18n/detect-domain-locale'
import { detectLocaleCookie } from '../../../../shared/lib/i18n/detect-locale-cookie'
import { detectDomainLocale } from '../../../../shared/lib/i18n/detect-domain-locale'
import { denormalizePagePath } from '../../../../next-server/server/denormalize-page-path'
import cookie from 'next/dist/compiled/cookie'
import { TEMPORARY_REDIRECT_STATUS } from '../../../../next-server/lib/constants'
import { TEMPORARY_REDIRECT_STATUS } from '../../../../shared/lib/constants'
import { NextConfig } from '../../../../next-server/server/config'

const getCustomRouteMatcher = pathMatch(true)
Expand Down
4 changes: 2 additions & 2 deletions packages/next/build/webpack/plugins/build-manifest-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
CLIENT_STATIC_FILES_RUNTIME_POLYFILLS,
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
CLIENT_STATIC_FILES_RUNTIME_AMP,
} from '../../../next-server/lib/constants'
} from '../../../shared/lib/constants'
import { BuildManifest } from '../../../next-server/server/get-page-files'
import getRouteFromEntrypoint from '../../../next-server/server/get-route-from-entrypoint'
import { ampFirstEntryNamesMap } from './next-drop-client-page-plugin'
import { Rewrite } from '../../../lib/load-custom-routes'
import { getSortedRoutes } from '../../../next-server/lib/router/utils'
import { getSortedRoutes } from '../../../shared/lib/router/utils'
import { spans } from './profiling-plugin'
import { CustomRoutes } from '../../../lib/load-custom-routes'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import minifier from 'cssnano-simple'
import {
FONT_MANIFEST,
OPTIMIZED_FONT_PROVIDERS,
} from '../../../next-server/lib/constants'
} from '../../../shared/lib/constants'

function minifyCss(css: string): Promise<string> {
return postcss([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { STRING_LITERAL_DROP_BUNDLE } from '../../../next-server/lib/constants'
import { STRING_LITERAL_DROP_BUNDLE } from '../../../shared/lib/constants'

export const ampFirstEntryNamesMap: WeakMap<
webpack.compilation.Compilation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
isWebpack5,
sources,
} from 'next/dist/compiled/webpack/webpack'
import { PAGES_MANIFEST } from '../../../next-server/lib/constants'
import { PAGES_MANIFEST } from '../../../shared/lib/constants'
import getRouteFromEntrypoint from '../../../next-server/server/get-route-from-entrypoint'

export type PagesManifest = { [page: string]: string }
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/write-build-id.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises } from 'fs'
import { join } from 'path'
import { BUILD_ID_FILE } from '../next-server/lib/constants'
import { BUILD_ID_FILE } from '../shared/lib/constants'

export async function writeBuildId(
distDir: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/cli/next-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { runLintCheck } from '../lib/eslint/runLintCheck'
import { printAndExit } from '../server/lib/utils'
import { Telemetry } from '../telemetry/storage'
import loadConfig from '../next-server/server/config'
import { PHASE_PRODUCTION_BUILD } from '../next-server/lib/constants'
import { PHASE_PRODUCTION_BUILD } from '../shared/lib/constants'
import { eventLintCheckCompleted } from '../telemetry/events'
import { CompileError } from '../lib/compile-error'

Expand Down
4 changes: 2 additions & 2 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import Head from '../next-server/lib/head'
import { toBase64 } from '../next-server/lib/to-base-64'
import Head from '../shared/lib/head'
import { toBase64 } from '../shared/lib/to-base-64'
import {
ImageConfig,
imageConfigDefault,
Expand Down
Loading