Created
June 29, 2019 15:30
-
-
Save FireNeslo/ef903c390594da05446b517c826da01d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.setImmediate = cb => Promise.resolve().then(cb) | |
var fsModule = new Blob([` | |
module.exports = { | |
readdirSync() { | |
return [] | |
}, | |
readFileSync(file) { | |
const xhr = new XMLHttpRequest() | |
xhr.open('GET', file, false) | |
xhr.send() | |
return xhr.response | |
} | |
} | |
`]) | |
window.require = function commonjs(options = {}) { | |
const { root = '/', modules = 'https://unpkg.com/', cache = {} } = options | |
const { map = {} } = options | |
const base = new URL(root, location.origin) | |
const bare = new URL(modules, location.origin) | |
const process = { env: {}, versions: { node: 10 }, argv: [], async nextTick(cb) { cb() } } | |
load.resolve = function (local) { | |
local = map[local] || local | |
const dest = new URL(local, local[0] === '.' ? base.href : bare.href) | |
return dest.href | |
} | |
function load(local) { | |
const id = load.resolve(local) | |
if(cache[id]) return cache[id] | |
console.count(id) | |
const xhr = new XMLHttpRequest() | |
xhr.open('GET', id, false) | |
xhr.send() | |
const params = ['module', 'exports', 'require', '__dirname', '__filename', 'process', 'global'] | |
const __filename = xhr.responseURL | |
const __dirname = xhr.responseURL.split('/').slice(0, -1).join('/') | |
const require = commonjs({ root: __filename, modules, cache, map }) | |
const exports = cache[id] = {} | |
const module = { exports } | |
let code = xhr.response | |
const [ type ] = (xhr.getResponseHeader('Content-Type')||'').split(';') | |
if(type.endsWith('json')) { | |
code = `module.exports = ${code}` | |
} | |
const factory = Function(params, code) | |
factory(module, exports, require, __dirname, __filename, process, self) | |
return cache[id] = module.exports | |
} | |
return load | |
}({ | |
map: { | |
'fs': URL.createObjectURL(fsModule), | |
'os': 'os-browserify', | |
'util/': 'util', | |
'crypto': 'crypto-browserify', | |
'stream': 'stream-browserify', | |
'https': 'https-browserify', | |
'http': 'stream-http', | |
} | |
}) | |
window.Buffer = require('buffer').Buffer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment