Skip to content

Instantly share code, notes, and snippets.

@ezidio
Created March 2, 2018 12:01
Show Gist options
  • Select an option

  • Save ezidio/f64c59d46b19a3fe671a9ded6441de18 to your computer and use it in GitHub Desktop.

Select an option

Save ezidio/f64c59d46b19a3fe671a9ded6441de18 to your computer and use it in GitHub Desktop.

Revisions

  1. ezidio created this gist Mar 2, 2018.
    8 changes: 8 additions & 0 deletions jest.config.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@

    module.exports = {
    verbose: true,
    moduleDirectories: ['node_modules'],
    transform: {
    '\\.js$': '<rootDir>/../build/utils/webpack_polyfill'
    }
    }
    42 changes: 42 additions & 0 deletions require_context_transform.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    module.exports = {
    process (src, filename) {

    // Return original document if don't have reference to require.context.
    if (!/require\.context\(.*\)/gm.test(src)) {
    return src
    }

    return `if (typeof require.context === 'undefined') {
    const fs = require('fs')
    const path = require('path')
    require.context = (base = '.', scanSubDirectories = false, regularExpression = /\.js$/) => {
    const files = {}
    function readDirectory (directory) {
    fs.readdirSync(directory).forEach((file) => {
    const fullPath = path.resolve(directory, file)
    if (fs.statSync(fullPath).isDirectory()) {
    if (scanSubDirectories) readDirectory(fullPath)
    return
    }
    if (!regularExpression.test(fullPath)) return
    files[\`./\${file}\`] = true
    })
    }
    readDirectory(path.resolve(__dirname, base))
    function Module (file) {
    return require(path.resolve(__dirname, base, file))
    }
    Module.keys = () => Object.keys(files)
    return Module
    }
    }
    ${src}`
    }
    }