Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
make jest behave using (webpack) loader-runner
const deasync = require('deasync')
const runLoaders = deasync(require('loader-runner').runLoaders)
const { module: { rules } } = config = require('./webpack.base')
const fs = require('fs')
const resolveLoader = req => {
let loader = req
req = ~req.indexOf`?`
if (req) {
const index = ~req
req = loader.substring(index)
loader = loader.substring(0, index)
}
return require.resolve(loader) + (req || '')
}
exports.process = (src, resource) => {
try {
if (/node_modules/.test(resource)) return src
let use, loaders = (rules || config.module.loaders)
.find(({ test }) => resource.match(test))
if (loaders) ({ use, loaders = use } = loaders); else return src
loaders = typeof loaders == 'string'
? [ resolveLoader(loaders) ]
: loaders.map(({ options, loader }, key) => loader
? { loader: require.resolve(loader), options }
: resolveLoader(loaders[key])
)
const { result } = runLoaders({ resource, loaders, readResource: fs.readFile.bind(fs) })
return result[0]
}
catch (e) { console.error(e) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment