Nuxt项目分环境打包

2021/5/25 18:46:01

本文主要是介绍Nuxt项目分环境打包,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.安装cross-env
npm install --save-dev cross-env
2.根目录新建env.js

module.exports={
  dev:{
    MODE:'devlopment',
    ENV_API:'http://xxx/',
    OTHER_URL:"xxx"
  },
  test:{
    MODE:'test',
    ENV_API:'http://xxx',
    OTHER_URL:"xxx"
  },
  pro:{
    MODE:'production',
    ENV_API:'http://xxx',
    OTHER_URL:"xxx"
  }
}

3.nuxt.config.js

env:{
   OTHER_URL:env[process.env.MODE].OTHER_URL,
  },
 proxy: {
    '/api': {
      target: env[process.env.MODE].ENV_API,
      pathRewrite: {
        changeOrigin: true,
        '^/api' : ''
      }
    }
  },

4.配置package.json

"scripts": {
    "dev": "cross-env MODE=dev nuxt",
    "pro": "cross-env MODE=pro nuxt",
    "build:test": "cross-env MODE=test nuxt build",
    "build": "cross-env MODE=pro nuxt build",
    "start:test": "cross-env MODE=test nuxt start",
    "start": "cross-env MODE=pro nuxt start",
    "generate": "nuxt generate"
  },

5.测试环境部署

npm run build:test
npm run start:test

生产环境部署

npm run build
npm run start


这篇关于Nuxt项目分环境打包的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程