Mic*_*ski 9 javascript environment-variables typescript ionic3 angular
以何种方式使用与离子命令行界面一起使用的环境特定参数ionic build android --prod --device
,以便根据环境(例如生产和开发)对javascript/typescript代码进行区分?
我应该用process.env.IONIC_ENV
吗?或者我可以通过哪种方式查询这种区别?
Mic*_*ski 16
根据Rob Ferguson的教程,有三件事要做.取决于您的文件结构是完全可互换的(./
标记应用程序的根目录).
./tsconfig.json
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@env": [ "env/env" ]
},
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
./package.json
{
"config": {
"ionic_source_map_type": "source-map",
"ionic_webpack": "./config/webpack.config.js"
},
...
}
Run Code Online (Sandbox Code Playgroud)
./config/webpack.config.js (取决于ionic_webpack
你的package.json
)
/*
* The webpack config exports an object that has a valid webpack configuration
* For each environment name. By default, there are two Ionic environments:
* "dev" and "prod". As such, the webpack.config.js exports a dictionary object
* with "keys" for "dev" and "prod", where the value is a valid webpack configuration
* For details on configuring webpack, see their documentation here
* https://webpack.js.org/configuration/
*/
const path = require('path');
// If you start your building process with the flag --prod this will equal "prod" otherwise "dev"
const ENV = process.env.IONIC_ENV;
const devConfig = {
entry: ...,
output: {...},
devtool: ...,
resolve: {
extensions: [...],
modules: [...],
alias: {
// this distincts your specific environment "dev" and "prod"
"@env": path.resolve(`./src/env/env.ts`),
}
},
module: {...},
plugins: [...],
node: {...}
};
const prodConfig = {
entry: ...,
output: {...},
devtool: ...,
resolve: {
extensions: [...],
modules: [...],
alias: {
// this distincts your specific environment "dev" and "prod"
"@env": path.resolve(`./src/env/env.prod.ts`),
}
},
module: {...},
plugins: [...],
node: {...}
};
module.exports = {
dev: devConfig,
prod: prodConfig
}
Run Code Online (Sandbox Code Playgroud)
魔术带来了devConfig.resolve.alias
和prodConfig.resolve.alias
.这行代码创建了一个可导入的别名,如您自己的模块或节点模块.现在可以注入import { ENV } from '@env'
任何模块,组件,服务,管道或任何你喜欢的东西.
不要忘记创建特定于环境的文件.在这个例子中,你需要一个像这样的文件结构:
./
| package.json
? tsconfig.json
?
??? config
? webpack.config.js
?
??? src
?
??? env
env.ts (dev environment variables)
env.prod.ts (prod environment variables)
Run Code Online (Sandbox Code Playgroud)
./src/env/env.ts (默认情况下是开发)
export const ENV = {
production: false,
isDebugMode: true
};
Run Code Online (Sandbox Code Playgroud)
./src/env/env.prod.ts
export const ENV = {
production: true,
isDebugMode: false
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4929 次 |
最近记录: |