bundler variable disambiguation
This commit is contained in:
parent
2ca8fa6e66
commit
e5391a1957
10 changed files with 75 additions and 542 deletions
|
|
@ -4,21 +4,23 @@ var fs = require("fs")
|
|||
var path = require("path")
|
||||
|
||||
var modules = {}
|
||||
var usedVariables = {}
|
||||
|
||||
function resolve(dir, data) {
|
||||
var replacements = []
|
||||
data = data.replace(/((?:var|let|const|)\s*)([\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
|
||||
|
||||
var filename = new Function("return " + dep).call()
|
||||
var pathname = path.dirname(path.resolve(dir, filename))
|
||||
var normalized = path.normalize(dir + "/" + filename)
|
||||
var normalized = path.resolve(dir, filename)
|
||||
var pathname = path.dirname(normalized)
|
||||
if (modules[normalized] === undefined) {
|
||||
modules[normalized] = variable
|
||||
return resolve(pathname,
|
||||
fs.readFileSync(dir + "/" + filename + ".js", "utf8")
|
||||
.replace(/"use strict"\s*/gm, "")
|
||||
.replace(/module\.exports\s*=\s*/gm, def + variable + eq)
|
||||
//.replace(/module\.exports(\.[\w_$]|\["[^\"]"\])/, def + variable + eq + "{}\n" + variable + "$1")
|
||||
)
|
||||
var exported = fixCollisions(fs.readFileSync(dir + "/" + filename + ".js", "utf8"))
|
||||
.replace(/"use strict"\s*/gm, "") // remove extraneous "use strict"
|
||||
.replace(/module\.exports\s*=\s*/gm, def + variable + eq)
|
||||
//.replace(/module\.exports(\.[\w_$]|\["[^\"]"\])/, def + variable + eq + "{}\n" + variable + "$1")
|
||||
return resolve(pathname, exported)
|
||||
}
|
||||
else {
|
||||
if (modules[normalized] !== variable) {
|
||||
|
|
@ -33,6 +35,25 @@ function resolve(dir, data) {
|
|||
}
|
||||
}
|
||||
return data
|
||||
.replace(/(?:var|let|const)[\t ]([\w_$\.]+)(\s*=\s*)\1([\r\n;]+)/g, "$3") // remove assignments to itself
|
||||
.replace(/(\r\n){2,}/g, "$1$1") // remove multiple consecutive line breaks
|
||||
}
|
||||
|
||||
fs.writeFileSync("mithril.js", resolve(".", fs.readFileSync("index.js", "utf8")), "utf8")
|
||||
function fixCollisions(code) {
|
||||
for (var variable in usedVariables) {
|
||||
var collision = new RegExp("\\b" + variable + "\\b(?![\"'`])", "g")
|
||||
var exported = new RegExp("module\\.exports\\s*=\\s*" + variable)
|
||||
if (collision.test(code) && !exported.test(code)) {
|
||||
var fixed = variable + usedVariables[variable]++
|
||||
code = code.replace(collision, fixed)
|
||||
}
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
function bundle(input, output) {
|
||||
var code = resolve(".", fs.readFileSync(input, "utf8"))
|
||||
if (new Function(code)) fs.writeFileSync(output, code, "utf8")
|
||||
}
|
||||
|
||||
bundle("index.js", "mithril.js")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue