react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / insert-module-globals / index.js
80713 viewsvar parseScope = require('lexical-scope');1var through = require('through2');2var merge = require('xtend');34var path = require('path');5var processPath = require.resolve('process/browser.js');6var combineSourceMap = require('combine-source-map');78var defaultVars = {9process: function () {10return 'require(' + JSON.stringify(processPath) + ')';11},12global: function () {13return 'typeof global !== "undefined" ? global : '14+ 'typeof self !== "undefined" ? self : '15+ 'typeof window !== "undefined" ? window : {}'16;17},18Buffer: function () {19return 'require("buffer").Buffer';20},21__filename: function (file, basedir) {22var filename = '/' + path.relative(basedir, file);23return JSON.stringify(filename);24},25__dirname: function (file, basedir) {26var dir = path.dirname('/' + path.relative(basedir, file));27return JSON.stringify(dir);28}29};3031module.exports = function (file, opts) {32if (/\.json$/i.test(file)) return through();33if (!opts) opts = {};3435var basedir = opts.basedir || '/';36var vars = merge(defaultVars, opts.vars);37var varNames = Object.keys(vars);3839var quick = RegExp(varNames.map(function (name) {40return '\\b' + name + '\\b';41}).join('|'));4243var chunks = [];4445return through(write, end);4647function write (chunk, enc, next) { chunks.push(chunk); next() }4849function end () {50var self = this;51var source = Buffer.isBuffer(chunks[0])52? Buffer.concat(chunks).toString('utf8')53: chunks.join('')54;55source = source56.replace(/^\ufeff/, '')57.replace(/^#![^\n]*\n/, '\n');5859if (opts.always !== true && !quick.test(source)) {60this.push(source);61this.push(null);62return;63}6465try {66var scope = opts.always67? { globals: { implicit: varNames } }68: parseScope('(function(){\n' + source + '\n})()')69;70}71catch (err) {72var e = new SyntaxError(73(err.message || err) + ' while parsing ' + file74);75e.type = 'syntax';76e.filename = file;77return this.emit('error', e);78}7980var globals = {};8182varNames.forEach(function (name) {83if (scope.globals.implicit.indexOf(name) >= 0) {84var value = vars[name](file, basedir);85if (value) {86globals[name] = value;87self.emit('global', name);88}89}90});9192this.push(closeOver(globals, source, file, opts));93this.push(null);94}95};9697module.exports.vars = defaultVars;9899function closeOver (globals, src, file, opts) {100var keys = Object.keys(globals);101if (keys.length === 0) return src;102var values = keys.map(function (key) { return globals[key] });103104var wrappedSource;105if (keys.length <= 3) {106wrappedSource = '(function (' + keys.join(',') + '){\n'107+ src + '\n}).call(this,' + values.join(',') + ')'108;109}110else {111// necessary to make arguments[3..6] still work for workerify etc112// a,b,c,arguments[3..6],d,e,f...113var extra = [ '__argument0', '__argument1', '__argument2', '__argument3' ];114var names = keys.slice(0,3).concat(extra).concat(keys.slice(3));115values.splice(3, 0,116'arguments[3]','arguments[4]',117'arguments[5]','arguments[6]'118);119wrappedSource = '(function (' + names.join(',') + '){\n'120+ src + '\n}).call(this,' + values.join(',') + ')';121}122123// Generate source maps if wanted. Including the right offset for124// the wrapped source.125if (!opts.debug) {126return wrappedSource;127}128var sourceFile = path.relative(opts.basedir, file)129.replace(/\\/g, '/');130var sourceMap = combineSourceMap.create().addFile(131{ sourceFile: sourceFile, source: src},132{ line: 1 });133return combineSourceMap.removeComments(wrappedSource) + "\n"134+ sourceMap.comment();135}136137138