more bundler fixes
This commit is contained in:
parent
d0851f8a3b
commit
40b2053895
1 changed files with 15 additions and 7 deletions
|
|
@ -15,7 +15,7 @@ module.exports = function(input, output, options) {
|
||||||
var globalMatch = data.match(/window\.([\w_$]+)\s*=\s*/)
|
var globalMatch = data.match(/window\.([\w_$]+)\s*=\s*/)
|
||||||
if (globalMatch) globals[globalMatch[1]] = true
|
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(/^\s*\/\/[^\n]*/g, "").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
|
||||||
|
|
||||||
var filename = new Function("return " + dep).call()
|
var filename = new Function("return " + dep).call()
|
||||||
|
|
@ -36,7 +36,7 @@ module.exports = function(input, output, options) {
|
||||||
var normalized = path.resolve(dir, filename)
|
var normalized = path.resolve(dir, filename)
|
||||||
if (modules[normalized] === undefined) {
|
if (modules[normalized] === undefined) {
|
||||||
modules[normalized] = variable
|
modules[normalized] = variable
|
||||||
return resolve(path.dirname(dependency), exportCode(dependency, def + variable + eq))
|
return resolve(path.dirname(dependency), exportCode(dependency, def, variable, eq))
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (modules[normalized] !== variable) {
|
if (modules[normalized] !== variable) {
|
||||||
|
|
@ -57,19 +57,27 @@ module.exports = function(input, output, options) {
|
||||||
.replace(/\}[\r\n]+\(/g, "}(") // remove space from iife
|
.replace(/\}[\r\n]+\(/g, "}(") // remove space from iife
|
||||||
}
|
}
|
||||||
|
|
||||||
function exportCode(file, assignment) {
|
function exportCode(file, def, variable, eq) {
|
||||||
|
var declared = {}
|
||||||
return fixCollisions(fs.readFileSync(file, "utf8"))
|
return fixCollisions(fs.readFileSync(file, "utf8"))
|
||||||
.replace(/("|')use strict\1;?\s*/gm, "") // remove extraneous "use strict"
|
.replace(/("|')use strict\1;?\s*/gm, "") // remove extraneous "use strict"
|
||||||
.replace(/module\.exports\s*=\s*/gm, assignment)
|
.replace(/module\.exports\s*=\s*/gm, def + variable + eq)
|
||||||
|
.replace(/module\.exports(\.|\[)/gm, function(match, token, length, code) {
|
||||||
|
if (new RegExp("\\b" + variable + "\\b").test(variable) && !declared[variable]) {
|
||||||
|
declared[variable] = true
|
||||||
|
return def + variable + eq + "{}\n" + variable + token
|
||||||
|
}
|
||||||
|
return variable + token
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
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("([^\\.])" + 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) && !globals[variable.match(/[^\.]+/)]) {
|
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, "$1" + fixed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
|
|
@ -83,7 +91,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, "new function() {" + code + "}", "utf8")
|
if (new Function(code)) fs.writeFileSync(output, "new function() {\n" + code + "\n}", "utf8")
|
||||||
console.log("done")
|
console.log("done")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue