|
1 | 1 | // @ts-check
|
2 | 2 | const debug = require('debug')('@cypress/react')
|
3 | 3 | const tryLoadWebpackConfig = require('../utils/tryLoadWebpackConfig')
|
| 4 | +const { allowModuleSourceInPlace } = require('../utils/webpack-helpers') |
| 5 | +const { addCypressToWebpackEslintRulesInPlace } = require('../utils/eslint-helpers') |
4 | 6 | const { getTranspileFolders } = require('../utils/get-transpile-folders')
|
5 | 7 | const { addFolderToBabelLoaderTranspileInPlace } = require('../utils/babel-helpers')
|
6 | 8 |
|
7 |
| -/** |
8 |
| - * Finds the ModuleScopePlugin plugin and adds given folder |
9 |
| - * to that list. This allows react-scripts to import folders |
10 |
| - * outside of the default "/src" folder. |
11 |
| - * WARNING modifies the input webpack options argument. |
12 |
| - * @see https://github.com/bahmutov/cypress-react-unit-test/issues/289 |
13 |
| - * @param {string} folderName Folder to add, should be absolute |
14 |
| - */ |
15 |
| -function allowModuleSourceInPlace (folderName, webpackOptions) { |
16 |
| - if (!folderName) { |
17 |
| - return |
18 |
| - } |
19 |
| - |
20 |
| - if (!webpackOptions.resolve) { |
21 |
| - return |
22 |
| - } |
23 |
| - |
24 |
| - if (!Array.isArray(webpackOptions.resolve.plugins)) { |
25 |
| - return |
26 |
| - } |
27 |
| - |
28 |
| - const moduleSourcePlugin = webpackOptions.resolve.plugins.find((plugin) => { |
29 |
| - return Array.isArray(plugin.appSrcs) |
30 |
| - }) |
31 |
| - |
32 |
| - if (!moduleSourcePlugin) { |
33 |
| - debug('cannot find module source plugin') |
34 |
| - |
35 |
| - return |
36 |
| - } |
37 |
| - |
38 |
| - debug('found module source plugin %o', moduleSourcePlugin) |
39 |
| - if (!moduleSourcePlugin.appSrcs.includes(folderName)) { |
40 |
| - moduleSourcePlugin.appSrcs.push(folderName) |
41 |
| - debug('added folder %s to allowed sources', folderName) |
42 |
| - } |
43 |
| -} |
44 |
| - |
45 |
| -const addCypressToWebpackEslintRulesInPlace = (webpackOptions) => { |
46 |
| - const globalsToAdd = ['cy', 'Cypress', 'before', 'after', 'context'] |
47 |
| - |
48 |
| - if (webpackOptions.module && Array.isArray(webpackOptions.module.rules)) { |
49 |
| - const modulePre = webpackOptions.module.rules.find( |
50 |
| - (rule) => rule.enforce === 'pre', |
51 |
| - ) |
52 |
| - |
53 |
| - if (modulePre && Array.isArray(modulePre.use)) { |
54 |
| - debug('found Pre block %o', modulePre) |
55 |
| - |
56 |
| - const useEslintLoader = modulePre.use.find( |
57 |
| - (use) => use.loader && use.loader.includes('eslint-loader'), |
58 |
| - ) |
59 |
| - |
60 |
| - if (useEslintLoader) { |
61 |
| - debug('found useEslintLoader %o', useEslintLoader) |
62 |
| - |
63 |
| - if (useEslintLoader.options) { |
64 |
| - if (Array.isArray(useEslintLoader.options.globals)) { |
65 |
| - debug( |
66 |
| - 'adding cy to existing globals %o', |
67 |
| - useEslintLoader.options.globals, |
68 |
| - ) |
69 |
| - |
70 |
| - useEslintLoader.options.globals.push(...globalsToAdd) |
71 |
| - } else { |
72 |
| - debug('setting new list of globals with cy and Cypress') |
73 |
| - useEslintLoader.options.globals = globalsToAdd |
74 |
| - } |
75 |
| - |
76 |
| - debug('updated globals %o', useEslintLoader.options.globals) |
77 |
| - } else { |
78 |
| - debug('eslint loader does not have options ⚠️') |
79 |
| - } |
80 |
| - } |
81 |
| - } |
82 |
| - } |
83 |
| -} |
84 |
| - |
85 |
| -module.exports = function findReactScriptsWebpackConfig (config) { |
| 9 | +module.exports = function findReactScriptsWebpackConfig (config, { webpackConfigPath }) { |
86 | 10 | // this is required because
|
87 | 11 | // 1) we use our own HMR and we don't need react-refresh transpiling overhead
|
88 | 12 | // 2) it doesn't work with process.env=test @see https://github.com/cypress-io/cypress-realworld-app/pull/832
|
89 | 13 | process.env.FAST_REFRESH = 'false'
|
90 |
| - const webpackConfig = tryLoadWebpackConfig('react-scripts/config/webpack.config') |
| 14 | + const webpackConfig = tryLoadWebpackConfig(webpackConfigPath) |
91 | 15 |
|
92 | 16 | if (!webpackConfig) {
|
93 | 17 | throw new Error('⚠️ Could not find Webpack options for react-scripts. Make sure that you have react-scripts module available.')
|
|
0 commit comments