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

Commit 75b3d27

Browse files
committed
feat: set cy and Cypress global automatically
1 parent 2bec626 commit 75b3d27

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

plugins/cra-v3/file-preprocessor.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,36 @@ const webpackOptions = webpackFactory('development')
2222
debug('webpack options: %o', webpackOptions)
2323

2424
// remove bunch of options, we just need to bundle spec files
25-
// delete webpackOptions.entry
26-
// delete webpackOptions.output
27-
// delete webpackOptions.node
2825
delete webpackOptions.optimization
26+
delete webpackOptions.plugins
27+
28+
// ESLint loader does not know about our "cy" global so it will error
29+
// find it in the module processing rules and add global "cy" option
30+
debug('module property %o', webpackOptions.module)
31+
32+
if (webpackOptions.module && Array.isArray(webpackOptions.module.rules)) {
33+
const modulePre = webpackOptions.module.rules.find(rule => rule.enforce === 'pre')
34+
if (modulePre && Array.isArray(modulePre.use)) {
35+
debug('found Pre block %o', modulePre)
36+
37+
const useEslintLoader = modulePre.use.find(use => use.loader && use.loader.includes('eslint-loader'))
38+
if (useEslintLoader) {
39+
debug('found useEslintLoader %o', useEslintLoader)
40+
41+
if (useEslintLoader.options) {
42+
if (Array.isArray(useEslintLoader.options.globals)) {
43+
debug('adding cy to existing globals %o', useEslintLoader.options.globals)
44+
useEslintLoader.options.globals.push('cy')
45+
useEslintLoader.options.globals.push('Cypress')
46+
} else {
47+
debug('setting new list of globals with cy and Cypress')
48+
useEslintLoader.options.globals = ['cy', 'Cypress']
49+
}
50+
}
51+
}
52+
}
53+
}
54+
2955

3056
const options = {
3157
webpackOptions,

0 commit comments

Comments
 (0)