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

Commit 5bbcc69

Browse files
committed
feat: allow loading webpack config by filename
1 parent b5fcf70 commit 5bbcc69

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ module.exports = on => {
181181

182182
**Bonus:** re-using the config means if you create your application using `create-react-app --typescript`, then TypeScript transpile just works out of the box. See [bahmutov/try-cra-app-typescript](https://github.com/bahmutov/try-cra-app-typescript).
183183

184+
## Your webpack config
185+
186+
If you have your own webpack config, you can use included plugins file to load it. Here is the configuration using the included plugins file and passing the name of the config file via `env` variable in the `cypress.json` file
187+
188+
```json
189+
{
190+
"pluginsFile": "node_modules/cypress-react-unit-test/plugins/load-webpack",
191+
"env": {
192+
"webpackFilename": "demo/config/webpack.dev.js"
193+
}
194+
}
195+
```
196+
184197
## Examples
185198

186199
All components are in [src](src) folder. All tests are in [cypress/integration](cypress/integration) folder.

plugins/load-webpack/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @ts-check
2+
const path = require('path')
3+
const debug = require('debug')('cypress-react-unit-test')
4+
const webpack = require('@cypress/webpack-preprocessor')
5+
const findWebpack = require('find-webpack')
6+
7+
module.exports = (on, config) => {
8+
require('@cypress/code-coverage/task')(on, config)
9+
10+
const webpackFilename = config.env && config.env.webpackFilename
11+
if (!webpackFilename) {
12+
throw new Error(
13+
'Could not find "webpackFilename" option in Cypress env variables %o',
14+
config.env,
15+
)
16+
}
17+
debug('got webpack config filename %s', webpackFilename)
18+
const resolved = path.resolve(webpackFilename)
19+
debug('resolved webpack at %s', webpackFilename)
20+
const webpackOptions = require(resolved)
21+
22+
debug('webpack options: %o', webpackOptions)
23+
24+
const coverageIsDisabled =
25+
config && config.env && config.env.coverage === false
26+
debug('coverage is disabled? %o', { coverageIsDisabled })
27+
28+
const opts = {
29+
coverage: !coverageIsDisabled,
30+
}
31+
32+
findWebpack.cleanForCypress(opts, webpackOptions)
33+
debug('claned webpack options: %o', webpackOptions)
34+
35+
const options = {
36+
webpackOptions,
37+
watchOptions: {},
38+
}
39+
40+
on('file:preprocessor', webpack(options))
41+
42+
// IMPORTANT to return the config object
43+
// with the any changed environment variables
44+
return config
45+
}

0 commit comments

Comments
 (0)