我已经将我的Next.js项目部署到Azure服务中,该服务需要包含"node_modules“来运行生产("npm”)。
但是..。我发现"node_modules“的大小太大了(磁盘上有354,3MB)。
然后,我看到我们可以使用“输出文件跟踪”特性来大幅减少部署的规模。
那么我该如何使用这个功能呢?我认为这会影响我的Azure管道和释放
发布于 2022-09-15 02:30:36
下面是如何使用“输出文件跟踪”特性将我的Next.js项目部署到的解决方案步骤。
项目设置
output: "standalone
// example
const nextConfig = {
output: "standalone",
};
module.exports = nextConfig;
npm run build
standalone
内部将有一个名为.next
的文件夹standalone
内部,有一个server.js
。cd .next/standalone
将目录更改为cd .next/standalone
。您可以使用server.js
运行node
。例子:node server.js
.然后它将在3000端口运行。您可以在浏览器中打开它,然后访问localhost:3000
。static
在.next
中复制到/.next/standalone/.next/
并重新运行node server.js
。7.1。您可以在运行build
脚本时更新脚本以自动复制它。首先,将[医]氯安装为开发依赖项npm i cpy-cli -D
。
7.2。在package.json
中。更新生成脚本
"build": "next build && ./node_modules/.bin/cpy '.next/static/**' '.next/standalone/.next/static/'"
7.3。尝试重新构建,然后static
将被自动复制。
管道
只存档standalone
。
azure-pipelines.yml
# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
- task: NodeTool@0
inputs:
versionSpec: '18.x'
displayName: 'Install Node.js'
- script: |
npm install
npm run build
displayName: 'npm install and build'
- task: ArchiveFiles@2
inputs:
rootFolderOrFile: "./.next/standalone/"
includeRootFolder: false
archiveType: "zip"
archiveFile: "$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip"
replaceExistingArchive: true
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: "$(Build.ArtifactStagingDirectory)"
ArtifactName: "drop"
publishLocation: "Container"
发布
完成
如果您面临public
内容缺失这样的问题,则需要将public
复制到standalone
。您可以手动完成,也可以更新build
脚本,如下所示:
package.json
"build": "next build && ./node_modules/.bin/cpy '.next/static/**' '.next/standalone/.next/static/' && ./node_modules/.bin/cpy './public/**' '.next/standalone/public/'",
https://stackoverflow.com/questions/73725010
复制相似问题