Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit f7413f9

Browse files
authored
Move code shared between server/client to "shared" folder (vercel#26734)
1 parent 95ec1ee commit f7413f9

File tree

110 files changed

+192
-182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+192
-182
lines changed

packages/next/amp.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './dist/next-server/lib/amp'
1+
export * from './dist/shared/lib/amp'

packages/next/amp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('./dist/next-server/lib/amp')
1+
module.exports = require('./dist/shared/lib/amp')

packages/next/bin/next.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ commands[command]()
118118
})
119119

120120
if (command === 'dev') {
121-
const { CONFIG_FILE } = require('../next-server/lib/constants')
121+
const { CONFIG_FILE } = require('../shared/lib/constants')
122122
const { watchFile } = require('fs')
123123
watchFile(`${process.cwd()}/${CONFIG_FILE}`, (cur: any, prev: any) => {
124124
if (cur.size > 0 || prev.size > 0) {

packages/next/build/babel/loader/get-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { NextBabelLoaderOptions, NextJsLoaderContext } from './types'
88
import { consumeIterator } from './util'
99
import * as Log from '../../output/log'
1010

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

1313
/**
1414
* The properties defined here are the conditions with which subsets of inputs

packages/next/build/babel/plugins/commonjs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { NodePath, PluginObj, types } from 'next/dist/compiled/babel/core'
22
import commonjsPlugin from 'next/dist/compiled/babel/plugin-transform-modules-commonjs'
33

4-
// Rewrite imports using next/<something> to next-server/<something>
5-
export default function NextToNextServer(...args: any): PluginObj {
4+
// Handle module.exports in user code
5+
export default function CommonJSModulePlugin(...args: any): PluginObj {
66
const commonjs = commonjsPlugin(...args)
77
return {
88
visitor: {

packages/next/build/babel/plugins/next-page-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Visitor,
77
} from 'next/dist/compiled/babel/core'
88
import { PageConfig } from 'next/types'
9-
import { STRING_LITERAL_DROP_BUNDLE } from '../../../next-server/lib/constants'
9+
import { STRING_LITERAL_DROP_BUNDLE } from '../../../shared/lib/constants'
1010

1111
const CONFIG_KEY = 'config'
1212

packages/next/build/babel/plugins/next-ssg-transform.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import {
44
types as BabelTypes,
55
} from 'next/dist/compiled/babel/core'
66
import { SERVER_PROPS_SSG_CONFLICT } from '../../../lib/constants'
7-
import {
8-
SERVER_PROPS_ID,
9-
STATIC_PROPS_ID,
10-
} from '../../../next-server/lib/constants'
7+
import { SERVER_PROPS_ID, STATIC_PROPS_ID } from '../../../shared/lib/constants'
118

129
export const EXPORT_NAME_GET_STATIC_PROPS = 'getStaticProps'
1310
export const EXPORT_NAME_GET_STATIC_PATHS = 'getStaticPaths'

packages/next/build/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ import {
4545
SERVER_DIRECTORY,
4646
SERVER_FILES_MANIFEST,
4747
STATIC_STATUS_PAGES,
48-
} from '../next-server/lib/constants'
48+
} from '../shared/lib/constants'
4949
import {
5050
getRouteRegex,
5151
getSortedRoutes,
5252
isDynamicRoute,
53-
} from '../next-server/lib/router/utils'
53+
} from '../shared/lib/router/utils'
5454
import { __ApiPreviewProps } from '../next-server/server/api-utils'
5555
import loadConfig, {
5656
isTargetLikeServerless,
@@ -89,7 +89,7 @@ import {
8989
import getBaseWebpackConfig from './webpack-config'
9090
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
9191
import { writeBuildId } from './write-build-id'
92-
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path'
92+
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
9393
import { isWebpack5 } from 'next/dist/compiled/webpack/webpack'
9494

9595
const staticCheckWorker = require.resolve('./utils')

packages/next/build/utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ import {
1919
} from '../lib/constants'
2020
import prettyBytes from '../lib/pretty-bytes'
2121
import { recursiveReadDir } from '../lib/recursive-readdir'
22-
import { getRouteMatcher, getRouteRegex } from '../next-server/lib/router/utils'
23-
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
24-
import escapePathDelimiters from '../next-server/lib/router/utils/escape-path-delimiters'
22+
import { getRouteMatcher, getRouteRegex } from '../shared/lib/router/utils'
23+
import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
24+
import escapePathDelimiters from '../shared/lib/router/utils/escape-path-delimiters'
2525
import { findPageFile } from '../server/lib/find-page-file'
2626
import { GetStaticPaths } from 'next/types'
2727
import { denormalizePagePath } from '../next-server/server/normalize-page-path'
2828
import { BuildManifest } from '../next-server/server/get-page-files'
2929
import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
3030
import { UnwrapPromise } from '../lib/coalesced-function'
31-
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path'
31+
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
3232
import * as Log from './output/log'
3333
import { loadComponents } from '../next-server/server/load-components'
3434
import { trace } from '../telemetry/trace'
@@ -765,7 +765,7 @@ export async function isPageStatic(
765765
const isPageStaticSpan = trace('is-page-static-utils', parentId)
766766
return isPageStaticSpan.traceAsyncFn(async () => {
767767
try {
768-
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
768+
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
769769
const components = await loadComponents(distDir, page, serverless)
770770
const mod = components.ComponentMod
771771
const Comp = mod.default || mod
@@ -880,7 +880,7 @@ export async function hasCustomGetInitialProps(
880880
runtimeEnvConfig: any,
881881
checkingApp: boolean
882882
): Promise<boolean> {
883-
require('../next-server/lib/runtime-config').setConfig(runtimeEnvConfig)
883+
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
884884

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

packages/next/build/webpack-config.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import {
2525
REACT_LOADABLE_MANIFEST,
2626
SERVERLESS_DIRECTORY,
2727
SERVER_DIRECTORY,
28-
} from '../next-server/lib/constants'
29-
import { execOnce } from '../next-server/lib/utils'
28+
} from '../shared/lib/constants'
29+
import { execOnce } from '../shared/lib/utils'
3030
import { NextConfig } from '../next-server/server/config'
3131
import { findPageFile } from '../server/lib/find-page-file'
3232
import { WebpackEntrypoints } from './entries'
@@ -305,7 +305,7 @@ export default async function getBaseWebpackConfig(
305305
}
306306

307307
const babelIncludeRegexes: RegExp[] = [
308-
/next[\\/]dist[\\/]next-server[\\/]lib/,
308+
/next[\\/]dist[\\/]shared[\\/]lib/,
309309
/next[\\/]dist[\\/]client/,
310310
/next[\\/]dist[\\/]pages/,
311311
/[\\/](strip-ansi|ansi-regex)[\\/]/,
@@ -396,10 +396,10 @@ export default async function getBaseWebpackConfig(
396396
}
397397

398398
const clientResolveRewrites = require.resolve(
399-
'../next-server/lib/router/utils/resolve-rewrites'
399+
'../shared/lib/router/utils/resolve-rewrites'
400400
)
401401
const clientResolveRewritesNoop = require.resolve(
402-
'../next-server/lib/router/utils/resolve-rewrites-noop'
402+
'../shared/lib/router/utils/resolve-rewrites-noop'
403403
)
404404

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

715711
if (isLocal) {
716-
// we need to process next-server/lib/router/router so that
712+
// Makes sure dist/shared and dist/next-server are not bundled
713+
// we need to process shared/lib/router/router so that
717714
// the DefinePlugin can inject process.env values
718-
const isNextExternal = /next[/\\]dist[/\\]next-server[/\\](?!lib[/\\]router[/\\]router)/.test(
715+
const isNextExternal = /next[/\\]dist[/\\](shared|next-server)[/\\](?!lib[/\\]router[/\\]router)/.test(
719716
res
720717
)
721718

@@ -761,9 +758,7 @@ export default async function getBaseWebpackConfig(
761758
}
762759

763760
if (
764-
res.match(
765-
/next[/\\]dist[/\\]next-server[/\\](?!lib[/\\]router[/\\]router)/
766-
)
761+
res.match(/next[/\\]dist[/\\]shared[/\\](?!lib[/\\]router[/\\]router)/)
767762
) {
768763
return `commonjs ${request}`
769764
}
@@ -1193,7 +1188,7 @@ export default async function getBaseWebpackConfig(
11931188
!dev &&
11941189
new webpack.IgnorePlugin({
11951190
resourceRegExp: /react-is/,
1196-
contextRegExp: /(next-server|next)[\\/]dist[\\/]/,
1191+
contextRegExp: /next[\\/]dist[\\/]/,
11971192
}),
11981193
isServerless && isServer && new ServerlessPlugin(),
11991194
isServer &&

0 commit comments

Comments
 (0)