@next/env根据运行模式,加载全局环境配置文件,使用dotenv解析
.env.${mode}.local.env.local.env.${mode}.envnext.config.js配置
function配置,(phase, {defaultConfig}) => configtarget必须是['server', 'serverless', 'experimental-serverless-trace']中的一项next.config.js,但找到不同后缀名的文件,提示格式不支持buildIdheaders, rewrites, redirects(只影响服务端)Spinner提示当前任务(后面不再列出)Telemetry数据收集(后面不再列出) · 配置NEXT_TELEMETRY_DISABLED = true不运行 package.json中的dep以及versionTypeScript安装
tsconfig.json文件的存在”、“pages目录下有ts/tsx文件”确认项目使用了TypeScripttypescript/@types/react/@types/node安装tsconfig.json内的配置next-env.d.ts文件typescript.ignoreBuildErrors=true不运行pagePaths · 删除不用的页面,非页面脚本移到其他目录下pageKey到pagePath的映射对象mappedPagesmappedPages构建用于webpack配置entry,分为client和server client如:pages/user: next-client-pages-loader?page=xxx&absolutePagePath=xxxserver如:pages/user: [absolutePagePath_xxx]public下有冲突_app|_document|_error只在pages根目录下有routes-manifest.json headers, rewrites, redirects构成webpack编译配置
webpack编译(此时编译结果已在.next目录下)webpack编译errors和warningswebpack4 开始使用
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.jsontsconfig.json文件typescript和tsconfig.jsonwebpackConfig.resolve?.modules?.push(tsconfig.compilerOptions.baseUrl)webpackConfig.resolve?.plugins?.unshift(new JsConfigPathsPlugin(tsconfig.compilerOptions.paths, tsconfig.compilerOptions.baseUrl))