webpack-environment-suffix-plugin
Overview
Plugin allows to use different versions of config files for the different environments. You can create several versions of same file:
config.js
config.dev.js
config.prod.js
config.qa.js
and plugin webpack-environment-suffix-plugin
helps resolve it properly. Idea is similar to what angular cli ng build --prod
does.
Install
npm install webpack-environment-suffix-plugin --save
Configure webpack
- Find your
webpack.config.js
file. - Import
webpack-environment-suffix-plugin
const EnvironmentSuffixPlugin = ;
- Add instance of plugin to a list of plugins.
├── webpackconfigjs const EnvironmentSuffixPlugin = ; //...const config = //... plugins: //... suffix: processenvNODE_ENV || 'dev' //...
- Update
package.json
.
├── package.json //..."scripts":
Options
suffix
-- defines file suffix that should to be used by resolver. For instance"dev"
,"prod"
,"qa"
or ony other you want to use. (Default:"dev"
).ext
-- file extension. (Default:"js"
; For typescript need to set to"ts"
).include
-- Tests files against array of regexps. By default value is[/.*\.<your ext>$/]
. Note: To improve plugin performance setinclude
values as precise as possible. For example:
"include":/src\/config\/*.js/ /src\/environment\/*.js/
exclude
-- Array of patterns to exclude. (Default:[/node_modules/]
).pattern
-- Pattern that is resolver is looking for. (Default[name].[suffix]
). Example:
//Add resolve js files with suffix defined in process.env.NODE_ENV suffix: processenvNODE_ENV //add resolve for typescript for with custom pattern ext: 'ts' suffix: processenvNODE_ENV include:/src\/config/ /src\/environment/ // search fo custom pattern like: environment-dev.ts pattern: '[name]-[suffix]'
Ionic >= 3.8.0
This package might be configured to be used with ionic
.
- Create new
webpack.config.js
file
├── webpackconfigjs const webpackConfig = ;const EnvironmentSuffixPlugin = ; const ionicEnv = 'prod' 'dev'; const addPluginToWebpackConfig = { const plugins = configenvplugins || ; configenvplugins = ...plugins ext: 'ts' suffix: processenvNODE_ENV || 'dev' ; return config;}; module { return ionicEnv;};
- Add
"ionic_webpack": <new webpack.config.js path>"
to aconfig
section of yourpackage.json
.
├── package.json "config":
Ionic < 3.8.0
Earlier versions of ionic
have different structure of @ionic/app-scripts/config/webpack.config
.
├── webpackconfigjs const originalWebpackConfig = ;const EnvironmentSuffixPlugin = ; module { const plugins = originalWebpackConfigplugins || ; originalWebpackConfigplugins = ...plugins ext: 'ts' suffix: processenvNODE_ENV || 'dev' ; return originalWebpackConfig;}