Skip to content

Commit 75c753b

Browse files
penxlmiller1990
andauthored
feat: custom webpack config for react/plugins/babel (#16597)
* Allow babel loader options to be configurable Allow babel loader options to be configurable to support running within a monorepo. Fixes #16596 * new configuration option in cypress.schema.json * Revert "new configuration option in cypress.schema.json" This reverts commit 0a39322. * support custom webpack configuration via config function * fix linting issue * Update getBabelWebpackConfig.js * linting Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
1 parent fd012e2 commit 75c753b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

npm/react/plugins/babel/getBabelWebpackConfig.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,28 @@ const webpackConfigLoadsBabel = {
2222
},
2323
}
2424

25-
module.exports = (on, config) => {
25+
/**
26+
* `on` and `config` are mandatory and must be forwarded from
27+
* your plugins file (`cypress/plugins/index.js` by default).
28+
* the third argument is an optional object with a `setWebpackConfig`
29+
* property. It's a function that will receive the webpack configuration
30+
* (after babel-loader is added) that allows you to further modify
31+
* the webpack configuration
32+
*
33+
* @example
34+
* module.exports = (on, config) => {
35+
* require('@cypress/react/plugins/babel')(on, config, {
36+
* setWebpackConfig: (webpackConfig) => {
37+
* webpackConfig.resolve.alias = {
38+
* '@my-monorepo/my-package': '../../my-package/src',
39+
* }
40+
* return webpackConfig
41+
* }
42+
* })
43+
* return config
44+
* }
45+
*/
46+
module.exports = (on, config, { setWebpackConfig } = { setWebpackConfig: null }) => {
2647
debug('env object %o', config.env)
2748

2849
debug('initial environments %o', {
@@ -47,5 +68,9 @@ module.exports = (on, config) => {
4768
NODE_ENV: process.env.NODE_ENV,
4869
})
4970

71+
if (setWebpackConfig) {
72+
return setWebpackConfig(webpackConfigLoadsBabel)
73+
}
74+
5075
return webpackConfigLoadsBabel
5176
}

npm/react/plugins/babel/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const getBabelWebpackConfig = require('./getBabelWebpackConfig')
22
const { startDevServer } = require('@cypress/webpack-dev-server')
33

4-
module.exports = (on, config) => {
4+
module.exports = (on, config, moduleOptions) => {
55
on('dev-server:start', async (options) => {
6-
return startDevServer({ options, webpackConfig: getBabelWebpackConfig(on, config) })
6+
return startDevServer({ options, webpackConfig: getBabelWebpackConfig(on, config, moduleOptions) })
77
})
88

99
config.env.reactDevtools = true

0 commit comments

Comments
 (0)