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

Commit b5686c3

Browse files
committed
feat: add image rule for both plugins
1 parent 12d859e commit b5686c3

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

plugins/cra-v3/file-preprocessor.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const debug = require('debug')('cypress-react-unit-test')
33
const findWebpack = require('find-webpack')
44
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
5+
const { addImageRedirect } = require('../utils/add-image-redirect')
56

67
const getWebpackOptions = opts => {
78
debug('top level opts %o', opts)
@@ -18,22 +19,7 @@ const getWebpackOptions = opts => {
1819
findWebpack.cleanForCypress(opts, webpackOptions)
1920
debug('claned webpack options: %o', webpackOptions)
2021

21-
// we need to handle static images and redirect them to
22-
// the existing files. Cypress has fallthrough static server
23-
// for anything like "/_root/<path>" which is perfect - because
24-
// importing a static image gives us that <path>!
25-
// insert our loader first before any built-in loaders kick in
26-
const loaderRules = webpackOptions.module.rules.find(rule =>
27-
Array.isArray(rule.oneOf),
28-
)
29-
if (loaderRules) {
30-
debug('found oneOf rule %o', loaderRules.oneOf)
31-
debug('adding our static image loader')
32-
loaderRules.oneOf.unshift({
33-
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.svg$/],
34-
loader: require.resolve('./redirect-resource'),
35-
})
36-
}
22+
addImageRedirect(webpackOptions)
3723

3824
const options = {
3925
webpackOptions,

plugins/load-webpack/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path')
33
const debug = require('debug')('cypress-react-unit-test')
44
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
55
const findWebpack = require('find-webpack')
6+
const { addImageRedirect } = require('../utils/add-image-redirect')
67

78
module.exports = (on, config) => {
89
require('@cypress/code-coverage/task')(on, config)
@@ -39,6 +40,8 @@ module.exports = (on, config) => {
3940
findWebpack.cleanForCypress(opts, webpackOptions)
4041
debug('claned webpack options: %o', webpackOptions)
4142

43+
addImageRedirect(webpackOptions)
44+
4245
const options = {
4346
webpackOptions,
4447
watchOptions: {},

plugins/utils/add-image-redirect.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @ts-check
2+
const debug = require('debug')('cypress-react-unit-test')
3+
4+
/**
5+
* Note: modifies the input options object
6+
*/
7+
function addImageRedirect(webpackOptions) {
8+
if (!webpackOptions.module) {
9+
debug('webpack options has no "module"')
10+
return
11+
}
12+
13+
if (!Array.isArray(webpackOptions.module.rules)) {
14+
debug('webpack module rules is not an array')
15+
return
16+
}
17+
18+
// we need to handle static images and redirect them to
19+
// the existing files. Cypress has fallthrough static server
20+
// for anything like "/_root/<path>" which is perfect - because
21+
// importing a static image gives us that <path>!
22+
// insert our loader first before any built-in loaders kick in
23+
const loaderRules = webpackOptions.module.rules.find(rule =>
24+
Array.isArray(rule.oneOf),
25+
)
26+
27+
const imageRedirectLoaderRule = {
28+
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/, /\.svg$/],
29+
loader: require.resolve('./redirect-resource'),
30+
}
31+
32+
if (loaderRules) {
33+
debug('found oneOf rule %o', loaderRules.oneOf)
34+
debug('adding our static image loader')
35+
loaderRules.oneOf.unshift(imageRedirectLoaderRule)
36+
} else {
37+
debug('could not find oneOf rules, inserting directly into rules')
38+
webpackOptions.module.rules.unshift(imageRedirectLoaderRule)
39+
}
40+
}
41+
42+
module.exports = { addImageRedirect }

0 commit comments

Comments
 (0)