improve bundler
This commit is contained in:
parent
fbb324e433
commit
640b19228a
8 changed files with 97 additions and 97 deletions
|
|
@ -3,7 +3,7 @@
|
|||
var fs = require("fs")
|
||||
var path = require("path")
|
||||
|
||||
module.exports = function(input, output) {
|
||||
module.exports = function(input, output, options) {
|
||||
function run(e, file) {
|
||||
var modules = {}
|
||||
var usedVariables = {}
|
||||
|
|
@ -17,7 +17,7 @@ module.exports = function(input, output) {
|
|||
|
||||
//resolve npm dependencies
|
||||
if (filename[0] !== ".") {
|
||||
var meta = JSON.parse(fs.readFileSync("./node_modules/" + filename + "/package.json"))
|
||||
var meta = fs.statSync("./node_modules/" + filename + "/package.json").isFile() ? JSON.parse(fs.readFileSync("./node_modules/" + filename + "/package.json")) : {}
|
||||
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))
|
||||
|
|
@ -57,7 +57,7 @@ module.exports = function(input, output) {
|
|||
|
||||
function fixCollisions(code) {
|
||||
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)
|
||||
if (collision.test(code) && !exported.test(code)) {
|
||||
var fixed = variable + usedVariables[variable]++
|
||||
|
|
@ -68,14 +68,14 @@ module.exports = function(input, output) {
|
|||
}
|
||||
|
||||
function setVersion(code) {
|
||||
var metadata = JSON.parse(fs.readFileSync("./package.json"))
|
||||
var metadata = JSON.parse(fs.readFileSync(__dirname + "/../package.json"))
|
||||
return code.replace("bleeding-edge", metadata.version)
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
|
|
@ -83,5 +83,5 @@ module.exports = function(input, output) {
|
|||
}
|
||||
run()
|
||||
|
||||
//fs.watch(process.cwd(), {recursive: true}, run)
|
||||
if (options && options.watch) fs.watch(process.cwd(), {recursive: true}, run)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,18 +3,24 @@
|
|||
var bundle = require("./bundle")
|
||||
var minify = require("./minify")
|
||||
|
||||
var aliases = {o: "output"}
|
||||
var aliases = {o: "output", m: "minify", w: "watch"}
|
||||
var params = {}
|
||||
var args = process.argv.slice(2), command = null
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
if (args[i][0] === '"') args[i] = args[i].slice(1, -1)
|
||||
if (args[i][0] === "-") command = args[i].replace(/\-+/g, "")
|
||||
else if (command != null) {
|
||||
params[aliases[command] || command] = args[i]
|
||||
command = null
|
||||
if (args[i][0] === '"') args[i] = JSON.parse(args[i])
|
||||
if (args[i][0] === "-") {
|
||||
if (command != null) add(true)
|
||||
command = args[i].replace(/\-+/g, "")
|
||||
}
|
||||
else if (command != null) add(args[i])
|
||||
else params.input = args[i]
|
||||
}
|
||||
if (command != null) add(true)
|
||||
|
||||
bundle(params.input, params.output)
|
||||
minify(params.output, params.output.replace(/\.js$/, ".min.js"))
|
||||
function add(value) {
|
||||
params[aliases[command] || command] = value
|
||||
command = null
|
||||
}
|
||||
|
||||
bundle(params.input, params.output, {watch: params.watch})
|
||||
if (params.minify) minify(params.output, params.output, {watch: params.watch})
|
||||
|
|
@ -2,7 +2,7 @@ var http = require("http")
|
|||
var querystring = require("querystring")
|
||||
var fs = require("fs")
|
||||
|
||||
module.exports = function(input, output) {
|
||||
module.exports = function(input, output, options) {
|
||||
function format(n) {
|
||||
return n.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")
|
||||
}
|
||||
|
|
@ -61,5 +61,5 @@ module.exports = function(input, output) {
|
|||
}
|
||||
run()
|
||||
|
||||
//fs.watchFile(input, run)
|
||||
if (options && options.watch) fs.watchFile(input, run)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue