fix node module resolution in bundler
This commit is contained in:
parent
ca7d1e856b
commit
91a492b2e8
1 changed files with 17 additions and 14 deletions
|
|
@ -17,25 +17,28 @@ module.exports = function(input, output, options) {
|
|||
|
||||
//resolve npm dependencies
|
||||
if (filename[0] !== ".") {
|
||||
var meta = fs.statSync("./node_modules/" + filename + "/package.json").isFile() ? JSON.parse(fs.readFileSync("./node_modules/" + filename + "/package.json")) : {}
|
||||
var meta
|
||||
try {meta = JSON.parse(fs.readFileSync("./node_modules/" + filename + "/package.json"))} catch (e) {meta = {}}
|
||||
var dependencyEntry = "./node_modules/" + filename + "/" + (meta.main || filename + ".js")
|
||||
try {fs.statSync(dependencyEntry).isFile()} catch (e) {dependencyEntry = "./node_modules/" + filename + "/index.js"}
|
||||
return resolve(path.dirname(dependencyEntry), exportCode(dependencyEntry, def + variable + eq))
|
||||
return process(dependencyEntry)
|
||||
}
|
||||
|
||||
//resolve local dependencies
|
||||
var normalized = path.resolve(dir, filename)
|
||||
var pathname = path.dirname(normalized)
|
||||
if (modules[normalized] === undefined) {
|
||||
modules[normalized] = variable
|
||||
var exported = exportCode(dir + "/" + filename + ".js", def + variable + eq)
|
||||
return resolve(pathname, exported)
|
||||
}
|
||||
else {
|
||||
if (modules[normalized] !== variable) {
|
||||
replacements.push({variable: variable, replacement: modules[normalized]})
|
||||
return process(dir + "/" + filename + ".js")
|
||||
|
||||
function process(dependency) {
|
||||
var normalized = path.resolve(dir, filename)
|
||||
if (modules[normalized] === undefined) {
|
||||
modules[normalized] = variable
|
||||
return resolve(path.dirname(dependency), exportCode(dependency, def + variable + eq))
|
||||
}
|
||||
else {
|
||||
if (modules[normalized] !== variable) {
|
||||
replacements.push({variable: variable, replacement: modules[normalized]})
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return ""
|
||||
}
|
||||
})
|
||||
if (replacements.length > 0) {
|
||||
|
|
@ -75,7 +78,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, code, "utf8")
|
||||
console.log("done")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue