all files / src/ nutra-mock.js

35.29% Statements 6/17
100% Branches 0/0
100% Functions 0/0
35.29% Lines 6/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29                                              
import fs from 'fs-extra'
import path from 'path'
import inlineSourceMapComment from'inline-source-map-comment'
import sourceModifier from './source-modifier.js'
import genericMock from './generic-mock.js'
 
const preprocessor = (events, system, opts) => {
    global.NutraMock = genericMock
    events.onLoad = () => {}
    events.onFileLoad = (source, filename, key) => {
        source = sourceModifier(source, filename)
        const sourceMapComment = inlineSourceMapComment(source.map.toString())
        const sourceWithMap = source.code + '\n' + sourceMapComment
        const tmpFilename = path.join(system.tmpDirectory, 'mock', key)
 
        fs.ensureFileSync(tmpFilename)
        fs.writeFileSync(tmpFilename, sourceWithMap)
 
        return {
            filename: tmpFilename,
            source: source.code,
            key: key
        }
    }
    events.onExit = () => { }
}
 
export { preprocessor }