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
|
//resolve npm dependencies
|
||||||
if (filename[0] !== ".") {
|
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")
|
var dependencyEntry = "./node_modules/" + filename + "/" + (meta.main || filename + ".js")
|
||||||
try {fs.statSync(dependencyEntry).isFile()} catch (e) {dependencyEntry = "./node_modules/" + filename + "/index.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
|
//resolve local dependencies
|
||||||
var normalized = path.resolve(dir, filename)
|
return process(dir + "/" + filename + ".js")
|
||||||
var pathname = path.dirname(normalized)
|
|
||||||
if (modules[normalized] === undefined) {
|
function process(dependency) {
|
||||||
modules[normalized] = variable
|
var normalized = path.resolve(dir, filename)
|
||||||
var exported = exportCode(dir + "/" + filename + ".js", def + variable + eq)
|
if (modules[normalized] === undefined) {
|
||||||
return resolve(pathname, exported)
|
modules[normalized] = variable
|
||||||
}
|
return resolve(path.dirname(dependency), exportCode(dependency, def + variable + eq))
|
||||||
else {
|
}
|
||||||
if (modules[normalized] !== variable) {
|
else {
|
||||||
replacements.push({variable: variable, replacement: modules[normalized]})
|
if (modules[normalized] !== variable) {
|
||||||
|
replacements.push({variable: variable, replacement: modules[normalized]})
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if (replacements.length > 0) {
|
if (replacements.length > 0) {
|
||||||
|
|
@ -75,7 +78,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, code, "utf8")
|
||||||
console.log("done")
|
console.log("done")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue