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

Commit a62fc22

Browse files
committed
feat: if serving outside resource, serve base64 image
1 parent b5686c3 commit a62fc22

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

package-lock.json

Lines changed: 7 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@
102102
"@cypress/webpack-preprocessor": "4.1.3",
103103
"babel-plugin-istanbul": "6.0.0",
104104
"debug": "4.1.1",
105-
"find-webpack": "1.4.0"
105+
"find-webpack": "1.4.0",
106+
"mime-types": "2.1.26"
106107
},
107108
"release": {
108109
"branches": [

plugins/utils/redirect-resource.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const debug = require('debug')('cypress-react-unit-test')
22
const path = require('path')
3+
const mime = require('mime-types')
34

45
/**
56
* User code:
@@ -9,11 +10,20 @@ const path = require('path')
910
* image is "/__root/path/to/image.png"
1011
* <img src={image} />
1112
*/
12-
function staticResourceLoader() {
13+
function staticResourceLoader(content) {
1314
debug('loading static resource %s', this.resourcePath)
1415
debug('cwd', process.cwd())
1516
const relativeResourcePath = path.relative(process.cwd(), this.resourcePath)
1617
debug('relative resource', relativeResourcePath)
18+
19+
if (relativeResourcePath.startsWith('..')) {
20+
debug('resource is outside of the current working directory')
21+
debug('inlining it instead (performance hit!)')
22+
const mimetype = mime.lookup(this.resourcePath)
23+
const encoded = content.toString('base64')
24+
return `module.exports = "data:${mimetype};base64,${encoded}"`
25+
}
26+
1727
const staticResourceUrl = `/__root/${relativeResourcePath}`
1828
debug('static resource url: %s', staticResourceUrl)
1929

0 commit comments

Comments
 (0)