Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 625c42a

Browse files
authored
feat(webpack): add getWebpackConfig hook (#297)
* add getWebpackConfig hook * add /docs/working-with-webpack.html * tweaks
1 parent 6a38cf6 commit 625c42a

File tree

5 files changed

+61
-15
lines changed

5 files changed

+61
-15
lines changed

packages/saber/lib/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class Saber {
3333
beforeRun: new AsyncSeriesHook(),
3434
onUpdateConfigFile: new AsyncSeriesHook(),
3535
// Extend webpack config
36-
chainWebpack: new SyncHook(['config', 'opts']),
36+
chainWebpack: new SyncHook(['webpackChain', 'opts']),
37+
getWebpackConfig: new SyncWaterfallHook(['config', 'opts']),
3738
// Extend markdown-it config
3839
chainMarkdown: new SyncHook(['config']),
3940
emitRoutes: new AsyncSeriesHook(),
@@ -272,10 +273,11 @@ class Saber {
272273
return this.resolveCwd(this.config.build.outDir, ...args)
273274
}
274275

275-
createWebpackChain(opts) {
276+
getWebpackConfig(opts) {
276277
opts = Object.assign({ type: 'client' }, opts)
277-
const config = require('./webpack/webpack.config')(this, opts)
278-
this.hooks.chainWebpack.call(config, opts)
278+
const chain = require('./webpack/webpack.config')(this, opts)
279+
this.hooks.chainWebpack.call(chain, opts)
280+
const config = this.hooks.getWebpackConfig.call(chain.toConfig(), opts)
279281
return config
280282
}
281283

packages/saber/vue-renderer/lib/index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,8 @@ class VueRenderer {
228228
}
229229

230230
async build() {
231-
const clientConfig = this.api
232-
.createWebpackChain({ type: 'client' })
233-
.toConfig()
234-
const serverConfig = this.api
235-
.createWebpackChain({ type: 'server' })
236-
.toConfig()
231+
const clientConfig = this.api.getWebpackConfig({ type: 'client' })
232+
const serverConfig = this.api.getWebpackConfig({ type: 'server' })
237233

238234
// Remove dist-client
239235
await fs.remove(this.api.resolveCache('dist-client'))
@@ -375,9 +371,7 @@ class VueRenderer {
375371

376372
this.api.hooks.onCreateServer.call(server)
377373

378-
const clientConfig = this.api
379-
.createWebpackChain({ type: 'client' })
380-
.toConfig()
374+
const clientConfig = this.api.getWebpackConfig({ type: 'client' })
381375

382376
clientConfig.plugins.push(new webpack.HotModuleReplacementPlugin())
383377

website/pages/docs/saber-instance.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,19 @@ Called after emitting the routes file.
172172

173173
- Hook Type: `SyncHook`
174174
- Params:
175-
- `config`: `WebpackChain`
175+
- `webpackChain`: `WebpackChain`
176176
- `opts`: `{ type: 'client' | 'server' }`
177177

178-
Called to get the webpack config before creating webpack compiler.
178+
Called with the `webpack-chain` instance before creating webpack compiler.
179+
180+
### `getWebpackConfig`
181+
182+
- Hook Type: `SyncWaterfallHook`
183+
- Params:
184+
- `config`: `WebpackConfig`
185+
- `opts`: `{ type: 'client' | 'server' }`
186+
187+
Called to get the webpack config before creating webpack compiler. You should return the webpack config object in this hook.
179188

180189
### `onCreateServer`
181190

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: Working with Webpack
3+
layout: docs
4+
---
5+
6+
## Simple Configuration
7+
8+
The easiest way to tweak webpack configuration is using the [`getWebpackConfig`](saber-instance.md#getwebpackconfig) hook in your [`saber-node.js`](node-apis.md) like this:
9+
10+
```js
11+
exports.getWebpackConfig = function(config, { type }) {
12+
// e.g. Adding a webpack plugin
13+
config.plugins.push(new SomeWebpackPlugin())
14+
15+
// `type` is either `client` or `server`
16+
if (type === 'client') {
17+
config.plugins.push(new SomeWebpackPluginForClientBuild())
18+
}
19+
20+
// You must return the `config`!
21+
return config
22+
}
23+
```
24+
25+
## Advanced Configuration
26+
27+
You can use [webpack-chain](https://github.com/neutrinojs/webpack-chain) to tweak webpack configuration in a more predictable way.
28+
29+
> webpack's core configuration is based on creating and modifying a potentially unwieldy JavaScript object. While this is OK for configurations on individual projects, trying to share these objects across projects and make subsequent modifications gets messy, as you need to have a deep understanding of the underlying object structure to make those changes.
30+
>
31+
> webpack-chain attempts to improve this process by providing a chainable or fluent API for creating and modifying webpack configurations. Key portions of the API can be referenced by user-specified names, which helps to standardize how to modify a configuration across projects.
32+
33+
Using the [chainWebpack](saber-instance.md#chainwebpack) hook in [`saber-node.js`](node-apis.md) to access the webpack-chain instance:
34+
35+
```js
36+
exports.chainWebpack = function(chain) {
37+
// e.g. Resolve .css files
38+
chain.resolve.extensions.add('.css')
39+
}
40+
```

website/saber-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333
{ title: 'Manipulating <head>', link: '/docs/manipulating-head.html' },
3434
{ title: 'Page Transition', link: '/docs/page-transition.html' },
3535
{ title: 'Internationalization', link: '/docs/i18n.html' },
36+
{ title: 'Working with Webpack', link: '/docs/working-with-webpack.html' },
3637
{ title: 'Deployment', link: '/docs/deployment.html' }
3738
]
3839
},

0 commit comments

Comments
 (0)