Last active
December 14, 2015 04:29
-
-
Save Raynos/5028211 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
| var parseScope = require('lexical-scope'); | |
| var browserResolve = require('browser-resolve'); | |
| var commondir = require('commondir'); | |
| var through = require('through'); | |
| var mdeps = require('module-deps'); | |
| var path = require('path'); | |
| var processModulePath = require.resolve('process/browser.js'); | |
| module.exports = function (files, opts) { | |
| if (!Array.isArray(files)) { | |
| opts = files; | |
| files = []; | |
| } | |
| if (!opts) opts = {}; | |
| if (!files) files = []; | |
| var resolver = opts.resolve || browserResolve; | |
| var basedir = opts.basedir || (files.length | |
| ? commondir(files.map(function (x) { | |
| return path.resolve(path.dirname(x)); | |
| })) | |
| : '/' | |
| ); | |
| var resolvedProcess = false; | |
| var chunksRemaining = 0; | |
| return through(write, end); | |
| function write (row) { | |
| var stream = this | |
| if (!opts.always | |
| && !/\bprocess\b/.test(row.source) | |
| && !/\bglobal\b/.test(row.source) | |
| && !/\b__filename\b/.test(row.source) | |
| && !/\b__dirname\b/.test(row.source) | |
| ) return stream.queue(row); | |
| var scope = opts.always | |
| ? { globals: { | |
| implicit: [ 'process', 'global', '__filename', '__dirname' ] | |
| } } | |
| : parseScope(row.source) | |
| ; | |
| var globals = {}; | |
| if (scope.globals.implicit.indexOf('global') >= 0) { | |
| globals.global = 'window'; | |
| } | |
| if (scope.globals.implicit.indexOf('__filename') >= 0) { | |
| var file = '/' + path.relative(basedir, row.id); | |
| globals.__filename = JSON.stringify(file); | |
| } | |
| if (scope.globals.implicit.indexOf('__dirname') >= 0) { | |
| var dir = path.dirname('/' + path.relative(basedir, row.id)); | |
| globals.__dirname = JSON.stringify(dir); | |
| } | |
| if (scope.globals.implicit.indexOf('process') >= 0) { | |
| if (!resolvedProcess) { | |
| chunksRemaining++ | |
| var d = mdeps(processModulePath, { resolve: resolver }); | |
| d.on('data', function (r) { | |
| r.entry = false; | |
| output.queue(r); | |
| }); | |
| d.on('end', function () { | |
| chunksRemaining--; | |
| if (chunksRemaining === 0 && stream.ended) { | |
| stream.queue(null); | |
| } | |
| }); | |
| } | |
| resolvedProcess = true; | |
| row.deps.__browserify_process = processModulePath; | |
| globals.process = 'require("__browserify_process")'; | |
| } | |
| row.source = closeOver(globals, row.source); | |
| input.queue(row); | |
| } | |
| function end () { | |
| this.ended = true; | |
| if (chunksRemaining === 0) { | |
| this.queue(null); | |
| } | |
| } | |
| return input | |
| }; | |
| function closeOver (globals, src) { | |
| var keys = Object.keys(globals); | |
| return '(function(' + keys + '){' + src + '\n})(' | |
| + keys.map(function (key) { return globals[key] }).join(',') + ')' | |
| ; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment