|
| 1 | +const debug = require('debug')('cypress-react-unit-test') |
| 2 | +const path = require('path') |
| 3 | +const findYarnWorkspaceRoot = require('find-yarn-workspace-root') |
| 4 | +const webpack = require('@cypress/webpack-preprocessor') |
| 5 | + |
| 6 | +const webpackConfigPath = path.resolve( |
| 7 | + findYarnWorkspaceRoot() || process.cwd(), |
| 8 | + 'node_modules', |
| 9 | + 'react-scripts', |
| 10 | + 'config', |
| 11 | + 'webpack.config.js' |
| 12 | +) |
| 13 | + |
| 14 | +debug('path to react-scripts own webpack.config.js: %s', webpackConfigPath) |
| 15 | + |
| 16 | +// Do this as the first thing so that any code reading it knows the right env. |
| 17 | +process.env.BABEL_ENV = 'development' |
| 18 | +process.env.NODE_ENV = 'development' |
| 19 | + |
| 20 | +const webpackFactory = require(webpackConfigPath) |
| 21 | +const webpackOptions = webpackFactory('development') |
| 22 | +debug('webpack options: %o', webpackOptions) |
| 23 | + |
| 24 | +// remove bunch of options, we just need to bundle spec files |
| 25 | +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 | + |
| 55 | + |
| 56 | +const options = { |
| 57 | + webpackOptions, |
| 58 | + watchOptions: {}, |
| 59 | +} |
| 60 | + |
| 61 | +module.exports = () => { |
| 62 | + return webpack(options) |
| 63 | +} |
0 commit comments