From d0851f8a3b6ea0b95d16d97f775fa85c9c8aa508 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Sat, 30 Jul 2016 00:05:00 -0400 Subject: [PATCH] handle globals in bundler --- bundler/bundle.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/bundler/bundle.js b/bundler/bundle.js index b897ff38..57909ea9 100644 --- a/bundler/bundle.js +++ b/bundler/bundle.js @@ -7,9 +7,14 @@ module.exports = function(input, output, options) { function run(e, file) { var modules = {} var usedVariables = {} + var globals = {} function resolve(dir, data) { var replacements = [] + + var globalMatch = data.match(/window\.([\w_$]+)\s*=\s*/) + if (globalMatch) globals[globalMatch[1]] = true + data = data.replace(/((?:var|let|const|)[\t ]*)([\w_$\.]+)(\s*=\s*)require\(([^\)]+)\)/g, function(match, def, variable, eq, dep) { usedVariables[variable] = usedVariables[variable] ? usedVariables[variable]++ : 1 @@ -60,9 +65,9 @@ module.exports = function(input, output, options) { function fixCollisions(code) { for (var variable in usedVariables) { - var collision = new RegExp("(?!.)\\b" + variable + "\\b(?![\"'`])", "g") + var collision = new RegExp("\\b" + variable + "\\b(?![\"'`])", "g") var exported = new RegExp("module\\.exports\\s*=\\s*" + variable) - if (collision.test(code) && !exported.test(code)) { + if (collision.test(code) && !exported.test(code) && !globals[variable.match(/[^\.]+/)]) { var fixed = variable + usedVariables[variable]++ code = code.replace(collision, fixed) } @@ -78,7 +83,7 @@ module.exports = function(input, output, options) { function bundle(input, output) { console.log("bundling...") var code = setVersion(resolve(path.dirname(input), fs.readFileSync(input, "utf8"))) - if (new Function(code)) fs.writeFileSync(output, code, "utf8") + if (new Function(code)) fs.writeFileSync(output, "new function() {" + code + "}", "utf8") console.log("done") }