You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BREAKING CHANGE: PluginAPI.setMode() has been removed. Instead, for a plugin to
sepcify the default mode for a registered command, the plugins should expose
`module.exports.defaultModes` in the form of `{ [commandName]: mode }`.
close#959
An important thing to note about env variables is knowing when they are resolved. Typically, a command like `vue-cli-service serve` or `vue-cli-service build` will always call `api.setMode()` as the first thing it does. However, this also means those env variables may not yet be available when a service plugin is invoked:
77
+
> Note: the way plugins set modes has been changed in beta.10.
78
+
79
+
If a plugin-registered command needs to run in a specific default mode,
80
+
the plugin needs to expose it via `module.exports.defaultModes` in the form
81
+
of `{ [commandName]: mode }`:
78
82
79
83
```js
80
84
module.exports=api=> {
81
-
process.env.NODE_ENV// may not be resolved yet
82
-
83
85
api.registerCommand('build', () => {
84
-
api.setMode('production')
86
+
// ...
85
87
})
86
88
}
87
-
```
88
-
89
-
Instead, it's safer to rely on env variables in `configureWebpack` or `chainWebpack` functions, which are called lazily only when `api.resolveWebpackConfig()` is finally called:
90
89
91
-
```js
92
-
module.exports=api=> {
93
-
api.configureWebpack(config=> {
94
-
if (process.env.NODE_ENV==='production') {
95
-
// ...
96
-
}
97
-
})
90
+
module.exports.defaultModes= {
91
+
build:'production'
98
92
}
99
93
```
100
94
95
+
This is because the command's expected mode needs to be known before loading environment variables, which in turn needs to happen before loading user options / applying the plugins.
96
+
101
97
#### Resolving Webpack Config in Plugins
102
98
103
99
A plugin can retrieve the resolved webpack config by calling `api.resolveWebpackConfig()`. Every call generates a fresh webpack config which can be further mutated as needed:
104
100
105
101
```js
106
-
api.registerCommand('my-build', args=> {
107
-
// make sure to set mode and load env variables
108
-
api.setMode('production')
102
+
module.exports=api=> {
103
+
api.registerCommand('my-build', args=> {
104
+
constconfigA=api.resolveWebpackConfig()
105
+
constconfigB=api.resolveWebpackConfig()
109
106
110
-
constconfigA=api.resolveWebpackConfig()
111
-
constconfigB=api.resolveWebpackConfig()
107
+
// mutate configA and configB for different purposes...
108
+
})
109
+
}
112
110
113
-
// mutate configA and configB for different purposes...
114
-
})
111
+
// make sure to specify the default mode for correct env variables
112
+
module.exports.defaultModes= {
113
+
'my-build':'production'
114
+
}
115
115
```
116
116
117
117
Alternatively, a plugin can also obtain a fresh [chainable config](https://github.com/mozilla-neutrino/webpack-chain) by calling `api.resolveChainableWebpackConfig()`:
0 commit comments