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

Commit

Permalink
feat: if serving outside resource, serve base64 image
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 16, 2020
1 parent b5686c3 commit a62fc22
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
16 changes: 7 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
"@cypress/webpack-preprocessor": "4.1.3",
"babel-plugin-istanbul": "6.0.0",
"debug": "4.1.1",
"find-webpack": "1.4.0"
"find-webpack": "1.4.0",
"mime-types": "2.1.26"
},
"release": {
"branches": [
Expand Down
12 changes: 11 additions & 1 deletion plugins/utils/redirect-resource.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const debug = require('debug')('cypress-react-unit-test')
const path = require('path')
const mime = require('mime-types')

/**
* User code:
Expand All @@ -9,11 +10,20 @@ const path = require('path')
* image is "/__root/path/to/image.png"
* <img src={image} />
*/
function staticResourceLoader() {
function staticResourceLoader(content) {
debug('loading static resource %s', this.resourcePath)
debug('cwd', process.cwd())
const relativeResourcePath = path.relative(process.cwd(), this.resourcePath)
debug('relative resource', relativeResourcePath)

if (relativeResourcePath.startsWith('..')) {
debug('resource is outside of the current working directory')
debug('inlining it instead (performance hit!)')
const mimetype = mime.lookup(this.resourcePath)
const encoded = content.toString('base64')
return `module.exports = "data:${mimetype};base64,${encoded}"`
}

const staticResourceUrl = `/__root/${relativeResourcePath}`
debug('static resource url: %s', staticResourceUrl)

Expand Down

0 comments on commit a62fc22

Please sign in to comment.