@next/env
根据运行模式,加载全局环境配置文件,使用dotenv
解析
.env.${mode}.local
.env.local
.env.${mode}
.env
next.config.js
配置
function
配置,(phase, {defaultConfig}) => config
target
必须是['server', 'serverless', 'experimental-serverless-trace']
中的一项next.config.js
,但找到不同后缀名的文件,提示格式不支持buildId
headers, rewrites, redirects
(只影响服务端)Spinner
提示当前任务(后面不再列出)Telemetry
数据收集(后面不再列出) · 配置NEXT_TELEMETRY_DISABLED = true
不运行 package.json
中的dep以及versionTypeScript
安装
tsconfig.json
文件的存在”、“pages目录下有ts/tsx文件”确认项目使用了TypeScript
typescript
/@types/react
/@types/node
安装tsconfig.json
内的配置next-env.d.ts
文件typescript.ignoreBuildErrors=true
不运行pagePaths
· 删除不用的页面,非页面脚本移到其他目录下pageKey
到pagePath
的映射对象mappedPages
mappedPages
构建用于webpack配置entry
,分为client
和server
client
如:pages/user: next-client-pages-loader?page=xxx&absolutePagePath=xxx
server
如:pages/user: [absolutePagePath_xxx]
public
下有冲突_app|_document|_error
只在pages
根目录下有routes-manifest.json
headers, rewrites, redirects
构成webpack
编译配置
webpack
编译(此时编译结果已在.next
目录下)webpack
编译errors
和warnings
webpack4 开始使用
SplitChunksPlugin
拆分 chunks
{
chunks: 'all',
cacheGroups: {
default: false,
vendors: false,
// In webpack 5 vendors was renamed to defaultVendors
defaultVendors: false,
framework: {
chunks: 'all',
name: 'framework',
// This regex ignores nested copies of framework libraries so they're
// bundled with their issuer.
// https://github.com/vercel/next.js/pull/9012
test: /(?<!node_modules.*)[\\/]node_modules[\\/](react|react-dom|scheduler|prop-types|use-subscription)[\\/]/,
priority: 40,
// Don't let webpack eliminate this chunk (prevents this chunk from
// becoming a part of the commons chunk)
enforce: true,
},
lib: {
test(module: { size: Function; identifier: Function }): boolean {
return (
module.size() > 160000 &&
/node_modules[/\\]/.test(module.identifier())
)
},
name(module: {
type: string
libIdent?: Function
updateHash: (hash: crypto.Hash) => void
}): string {
const hash = crypto.createHash('sha1')
if (isModuleCSS(module)) {
module.updateHash(hash)
} else {
if (!module.libIdent) {
throw new Error(
`Encountered unknown module type: ${module.type}. Please open an issue.`
)
}
hash.update(module.libIdent({ context: dir }))
}
return hash.digest('hex').substring(0, 8)
},
priority: 30,
minChunks: 1,
reuseExistingChunk: true,
},
commons: {
name: 'commons',
minChunks: totalPages,
priority: 20,
},
shared: {
name(module, chunks) {
return (
crypto
.createHash('sha1')
.update(
chunks.reduce(
(acc: string, chunk: webpack.compilation.Chunk) => {
return acc + chunk.name
},
''
)
)
.digest('hex') + (isModuleCSS(module) ? '_CSS' : '')
)
},
priority: 10,
minChunks: 2,
reuseExistingChunk: true,
},
},
maxInitialRequests: 25,
minSize: 20000,
}
tsconfig.json
tsconfig.json
文件typescript
和tsconfig.json
webpackConfig.resolve?.modules?.push(tsconfig.compilerOptions.baseUrl)
webpackConfig.resolve?.plugins?.unshift(new JsConfigPathsPlugin(tsconfig.compilerOptions.paths, tsconfig.compilerOptions.baseUrl))