handle globals in bundler
This commit is contained in:
parent
a548290dda
commit
d0851f8a3b
1 changed files with 8 additions and 3 deletions
|
|
@ -7,9 +7,14 @@ module.exports = function(input, output, options) {
|
||||||
function run(e, file) {
|
function run(e, file) {
|
||||||
var modules = {}
|
var modules = {}
|
||||||
var usedVariables = {}
|
var usedVariables = {}
|
||||||
|
var globals = {}
|
||||||
|
|
||||||
function resolve(dir, data) {
|
function resolve(dir, data) {
|
||||||
var replacements = []
|
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) {
|
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
|
usedVariables[variable] = usedVariables[variable] ? usedVariables[variable]++ : 1
|
||||||
|
|
||||||
|
|
@ -60,9 +65,9 @@ module.exports = function(input, output, options) {
|
||||||
|
|
||||||
function fixCollisions(code) {
|
function fixCollisions(code) {
|
||||||
for (var variable in usedVariables) {
|
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)
|
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]++
|
var fixed = variable + usedVariables[variable]++
|
||||||
code = code.replace(collision, fixed)
|
code = code.replace(collision, fixed)
|
||||||
}
|
}
|
||||||
|
|
@ -78,7 +83,7 @@ module.exports = function(input, output, options) {
|
||||||
function bundle(input, output) {
|
function bundle(input, output) {
|
||||||
console.log("bundling...")
|
console.log("bundling...")
|
||||||
var code = setVersion(resolve(path.dirname(input), fs.readFileSync(input, "utf8")))
|
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")
|
console.log("done")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue