Skip to content

Commit

Permalink
Merge branch 'canary' into sokra/non-enumerable-exports
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 3, 2023
2 parents d2a99b4 + 3caf6de commit 42d5f53
Show file tree
Hide file tree
Showing 40 changed files with 244 additions and 234 deletions.
4 changes: 2 additions & 2 deletions docs/api-reference/create-next-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Options:

Initialize with ESLint config.

--app-dir
--app

Initialize as an `app/` directory project.
Initialize as an App Router project.

--src-dir

Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"registry": "https://registry.npmjs.org/"
}
},
"version": "13.3.5-canary.3"
"version": "13.3.5-canary.4"
}
6 changes: 3 additions & 3 deletions packages/create-next-app/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function createApp({
typescript,
tailwind,
eslint,
appDir,
appRouter,
srcDir,
importAlias,
}: {
Expand All @@ -47,13 +47,13 @@ export async function createApp({
typescript: boolean
tailwind: boolean
eslint: boolean
appDir: boolean
appRouter: boolean
srcDir: boolean
importAlias: string
}): Promise<void> {
let repoInfo: RepoInfo | undefined
const mode: TemplateMode = typescript ? 'ts' : 'js'
const template: TemplateType = appDir
const template: TemplateType = appRouter
? tailwind
? 'app-tw'
: 'app'
Expand Down
31 changes: 14 additions & 17 deletions packages/create-next-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ const program = new Commander.Command(packageJson.name)
`
)
.option(
'--app-dir',
'--app',
`
Initialize as an \`app/\` directory project.
Initialize as an App Router project.
`
)
.option(
Expand All @@ -91,14 +91,14 @@ const program = new Commander.Command(packageJson.name)
'--use-npm',
`
Explicitly tell the CLI to bootstrap the app using npm
Explicitly tell the CLI to bootstrap the application using npm
`
)
.option(
'--use-pnpm',
`
Explicitly tell the CLI to bootstrap the app using pnpm
Explicitly tell the CLI to bootstrap the application using pnpm
`
)
.option(
Expand Down Expand Up @@ -340,24 +340,21 @@ async function run(): Promise<void> {
}
}

if (
!process.argv.includes('--app-dir') &&
!process.argv.includes('--no-app-dir')
) {
if (!process.argv.includes('--app') && !process.argv.includes('--no-app')) {
if (ciInfo.isCI) {
program.appDir = false
program.app = true
} else {
const styledAppDir = chalk.hex('#007acc')('`app/` directory')
const { appDir } = await prompts({
const styledAppDir = chalk.hex('#007acc')('App Router')
const { appRouter } = await prompts({
onState: onPromptState,
type: 'toggle',
name: 'appDir',
message: `Would you like to use ${styledAppDir} with this project?`,
initial: false,
name: 'appRouter',
message: `Use ${styledAppDir} (recommended)?`,
initial: true,
active: 'Yes',
inactive: 'No',
})
program.appDir = Boolean(appDir)
program.app = Boolean(appRouter)
}
}

Expand Down Expand Up @@ -410,7 +407,7 @@ async function run(): Promise<void> {
typescript: program.typescript,
tailwind: program.tailwind,
eslint: program.eslint,
appDir: program.appDir,
appRouter: program.app,
srcDir: program.srcDir,
importAlias: program.importAlias,
})
Expand Down Expand Up @@ -438,7 +435,7 @@ async function run(): Promise<void> {
typescript: program.typescript,
eslint: program.eslint,
tailwind: program.tailwind,
appDir: program.appDir,
appRouter: program.app,
srcDir: program.srcDir,
importAlias: program.importAlias,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/create-next-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-next-app",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"keywords": [
"react",
"next",
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-config-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-config-next",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"description": "ESLint configuration used by NextJS.",
"main": "index.js",
"license": "MIT",
Expand All @@ -9,7 +9,7 @@
"directory": "packages/eslint-config-next"
},
"dependencies": {
"@next/eslint-plugin-next": "13.3.5-canary.3",
"@next/eslint-plugin-next": "13.3.5-canary.4",
"@rushstack/eslint-patch": "^1.1.3",
"@typescript-eslint/parser": "^5.42.0",
"eslint-import-resolver-node": "^0.3.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/eslint-plugin-next",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"description": "ESLint plugin for NextJS.",
"main": "dist/index.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/font/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/font",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"repository": {
"url": "vercel/next.js",
"directory": "packages/font"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-bundle-analyzer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/bundle-analyzer",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"main": "index.js",
"types": "index.d.ts",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-codemod/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/codemod",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-env/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/env",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"keywords": [
"react",
"next",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-mdx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/mdx",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-plugin-storybook/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/plugin-storybook",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-plugin-storybook"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-module/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-module",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)",
"main": "dist/polyfill-module.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-nomodule/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-nomodule",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"description": "A polyfill for non-dead, nomodule browsers.",
"main": "dist/polyfill-nomodule.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/swc",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"private": true,
"scripts": {
"clean": "rm -rf ./native/*",
Expand Down
14 changes: 7 additions & 7 deletions packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next",
"version": "13.3.5-canary.3",
"version": "13.3.5-canary.4",
"description": "The React Framework",
"main": "./dist/server/next.js",
"license": "MIT",
Expand Down Expand Up @@ -83,7 +83,7 @@
]
},
"dependencies": {
"@next/env": "13.3.5-canary.3",
"@next/env": "13.3.5-canary.4",
"@swc/helpers": "0.5.1",
"busboy": "1.6.0",
"caniuse-lite": "^1.0.30001406",
Expand Down Expand Up @@ -142,11 +142,11 @@
"@jest/types": "29.5.0",
"@napi-rs/cli": "2.14.7",
"@napi-rs/triples": "1.1.0",
"@next/polyfill-module": "13.3.5-canary.3",
"@next/polyfill-nomodule": "13.3.5-canary.3",
"@next/react-dev-overlay": "13.3.5-canary.3",
"@next/react-refresh-utils": "13.3.5-canary.3",
"@next/swc": "13.3.5-canary.3",
"@next/polyfill-module": "13.3.5-canary.4",
"@next/polyfill-nomodule": "13.3.5-canary.4",
"@next/react-dev-overlay": "13.3.5-canary.4",
"@next/react-refresh-utils": "13.3.5-canary.4",
"@next/swc": "13.3.5-canary.4",
"@opentelemetry/api": "1.4.1",
"@segment/ajv-human-errors": "2.1.2",
"@taskr/clear": "1.1.0",
Expand Down
69 changes: 57 additions & 12 deletions packages/next/src/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,23 @@ function checkExports(swcAST: any): {
ssg: boolean
runtime?: string
preferredRegion?: string | string[]
generateImageMetadata?: boolean
generateSitemaps?: boolean
} {
const exportsSet = new Set<string>([
'getStaticProps',
'getServerSideProps',
'generateImageMetadata',
'generateSitemaps',
])
if (Array.isArray(swcAST?.body)) {
try {
let runtime: string | undefined
let preferredRegion: string | string[] | undefined
let ssr: boolean = false
let ssg: boolean = false
let generateImageMetadata: boolean = false
let generateSitemaps: boolean = false

for (const node of swcAST.body) {
if (
Expand Down Expand Up @@ -122,22 +132,25 @@ function checkExports(swcAST: any): {
if (
node.type === 'ExportDeclaration' &&
node.declaration?.type === 'FunctionDeclaration' &&
['getStaticProps', 'getServerSideProps'].includes(
node.declaration.identifier?.value
)
exportsSet.has(node.declaration.identifier?.value)
) {
ssg = node.declaration.identifier.value === 'getStaticProps'
ssr = node.declaration.identifier.value === 'getServerSideProps'
const id = node.declaration.identifier.value
ssg = id === 'getStaticProps'
ssr = id === 'getServerSideProps'
generateImageMetadata = id === 'generateImageMetadata'
generateSitemaps = id === 'generateSitemaps'
}

if (
node.type === 'ExportDeclaration' &&
node.declaration?.type === 'VariableDeclaration'
) {
const id = node.declaration?.declarations[0]?.id.value
if (['getStaticProps', 'getServerSideProps'].includes(id)) {
if (exportsSet.has(id)) {
ssg = id === 'getStaticProps'
ssr = id === 'getServerSideProps'
generateImageMetadata = id === 'generateImageMetadata'
generateSitemaps = id === 'generateSitemaps'
}
}

Expand All @@ -149,18 +162,36 @@ function checkExports(swcAST: any): {
specifier.orig?.value
)

ssg = values.some((value: any) => ['getStaticProps'].includes(value))
ssr = values.some((value: any) =>
['getServerSideProps'].includes(value)
)
for (const value of values) {
if (!ssg && value === 'getStaticProps') ssg = true
if (!ssr && value === 'getServerSideProps') ssr = true
if (!generateImageMetadata && value === 'generateImageMetadata')
generateImageMetadata = true
if (!generateSitemaps && value === 'generateSitemaps')
generateSitemaps = true
}
}
}

return { ssr, ssg, runtime, preferredRegion }
return {
ssr,
ssg,
runtime,
preferredRegion,
generateImageMetadata,
generateSitemaps,
}
} catch (err) {}
}

return { ssg: false, ssr: false }
return {
ssg: false,
ssr: false,
runtime: undefined,
preferredRegion: undefined,
generateImageMetadata: false,
generateSitemaps: false,
}
}

async function tryToReadFile(filePath: string, shouldThrow: boolean) {
Expand Down Expand Up @@ -329,6 +360,20 @@ function warnAboutUnsupportedValue(
warnedUnsupportedValueMap.set(pageFilePath, true)
}

// Detect if metadata routes is a dynamic route, which containing
// generateImageMetadata or generateSitemaps as export
export async function isDynamicMetadataRoute(
pageFilePath: string
): Promise<boolean> {
const fileContent = (await tryToReadFile(pageFilePath, true)) || ''
if (!/generateImageMetadata|generateSitemaps/.test(fileContent)) return false

const swcAST = await parseModule(pageFilePath, fileContent)
const exportsInfo = checkExports(swcAST)

return !exportsInfo.generateImageMetadata || !exportsInfo.generateSitemaps
}

/**
* For a given pageFilePath and nextConfig, if the config supports it, this
* function will read the file and return the runtime that should be used.
Expand Down

0 comments on commit 42d5f53

Please sign in to comment.