Ionic 2开发和prod环境变量

时间:2022-06-13 10:57:07

I am working on Ionic 2 project and I want to configure it for a different environment like Development and Production. But, I have no idea where to put config files in ionic 2. Are there any ways to put config file and run commands

我正在研究Ionic 2项目,我想为开发和生产等不同的环境配置它。但是,我不知道在ionic 2中把配置文件放在哪里。有什么方法可以放置配置文件和运行命令吗?

like

就像

ionic build android --prod

and

ionic build android --dev

3 个解决方案

#1


4  

There seems to be a ticket for that already:

似乎已经有了一张门票:

https://github.com/driftyco/ionic-cli/issues/1205

https://github.com/driftyco/ionic-cli/issues/1205

#2


0  

I used ionic-configuration-service and it worked fine for me.

我使用了离子配置服务,它对我很有效。

#3


0  

Webpack plugin can be used webpack-environment-suffix-plugin to setup several environments for ionic.

Webpack插件可以使用Webpack -environment-suffix-plugin来为ionic设置几个环境。

Install plugin

安装插件

npm install webpack-environment-suffix-plugin --save

npm安装webpack-environment-suffix-plugin——保存

Create your own webpack.config.js file.

创建自己的webpack.config。js文件。

const webpackConfig = require('@ionic/app-scripts/config/webpack.config');
const EnvironmentSuffixPlugin = require('webpack-environment-suffix-plugin');

const ionicEnv = ['prod', 'dev'];

const addPluginToWebpackConfig = (config, env) => {
  const plugins = config[env].plugins || [];

  config[env].plugins = [
    ...plugins,
    new EnvironmentSuffixPlugin({
      ext: 'ts',
      suffix: process.env.NODE_ENV || 'dev'
    })
  ];

  return config;
};

module.exports = () => ionicEnv.reduce(addPluginToWebpackConfig, webpackConfig);

Update package.json

更新package.json

"scripts": {
    //...
    "build": "<you build script>",
    "build:prod": "NODE_ENV=\"prod\" npm run build",
    "build:dev": "NODE_ENV=\"dev\" npm run build",
    "build:test": "NODE_ENV=\"qa\" npm run build
    //...
},
"config": {
   // path to a new webpack config file.
   "ionic_webpack": "./webpack.config.js" 
}

#1


4  

There seems to be a ticket for that already:

似乎已经有了一张门票:

https://github.com/driftyco/ionic-cli/issues/1205

https://github.com/driftyco/ionic-cli/issues/1205

#2


0  

I used ionic-configuration-service and it worked fine for me.

我使用了离子配置服务,它对我很有效。

#3


0  

Webpack plugin can be used webpack-environment-suffix-plugin to setup several environments for ionic.

Webpack插件可以使用Webpack -environment-suffix-plugin来为ionic设置几个环境。

Install plugin

安装插件

npm install webpack-environment-suffix-plugin --save

npm安装webpack-environment-suffix-plugin——保存

Create your own webpack.config.js file.

创建自己的webpack.config。js文件。

const webpackConfig = require('@ionic/app-scripts/config/webpack.config');
const EnvironmentSuffixPlugin = require('webpack-environment-suffix-plugin');

const ionicEnv = ['prod', 'dev'];

const addPluginToWebpackConfig = (config, env) => {
  const plugins = config[env].plugins || [];

  config[env].plugins = [
    ...plugins,
    new EnvironmentSuffixPlugin({
      ext: 'ts',
      suffix: process.env.NODE_ENV || 'dev'
    })
  ];

  return config;
};

module.exports = () => ionicEnv.reduce(addPluginToWebpackConfig, webpackConfig);

Update package.json

更新package.json

"scripts": {
    //...
    "build": "<you build script>",
    "build:prod": "NODE_ENV=\"prod\" npm run build",
    "build:dev": "NODE_ENV=\"dev\" npm run build",
    "build:test": "NODE_ENV=\"qa\" npm run build
    //...
},
"config": {
   // path to a new webpack config file.
   "ionic_webpack": "./webpack.config.js" 
}