Tie everything together in the new build system

This commit is contained in:
Marijn Haverbeke
2015-03-20 16:36:24 +01:00
parent cf613ce287
commit 6dd254d999
13 changed files with 385 additions and 4502 deletions

48
bin/build-acorn.js Normal file
View File

@@ -0,0 +1,48 @@
var fs = require("fs"), path = require("path")
var stream = require("stream")
var browserify = require("browserify")
var babelify = require("babelify")
process.chdir(path.resolve(__dirname, ".."))
browserify({standalone: "acorn"})
.transform(babelify)
.require("./src/index.js", {entry: true})
.bundle()
.on("error", function (err) { console.log("Error: " + err.message) })
.pipe(fs.createWriteStream("dist/acorn.js"))
function acornShim(file) {
var tr = new stream.Transform
if (file == path.resolve(__dirname, "../src/index.js")) {
var sent = false
tr._transform = function(chunk, _, callback) {
if (!sent) {
sent = true
callback(null, "module.exports = typeof window != 'undefined' ? window.acorn : require(('suppress', './acorn'))")
} else {
callback()
}
}
} else {
tr._transform = function(chunk, _, callback) { callback(null, chunk) }
}
return tr
}
browserify({standalone: "acorn.loose"})
.transform(acornShim)
.transform(babelify)
.require("./src/loose/index.js", {entry: true})
.bundle()
.on("error", function (err) { console.log("Error: " + err.message) })
.pipe(fs.createWriteStream("dist/acorn_loose.js"))
browserify({standalone: "acorn.walk"})
.transform(acornShim)
.transform(babelify)
.require("./src/walk/index.js", {entry: true})
.bundle()
.on("error", function (err) { console.log("Error: " + err.message) })
.pipe(fs.createWriteStream("dist/walk.js"))

View File

@@ -0,0 +1,47 @@
// Note: run `npm install unicode-7.0.0` first.
// Which Unicode version should be used?
var version = '7.0.0';
var start = require('unicode-' + version + '/properties/ID_Start/code-points')
.filter(function(ch) { return ch > 127; });
var cont = [0x200c, 0x200d].concat(require('unicode-' + version + '/properties/ID_Continue/code-points')
.filter(function(ch) { return ch > 127 && start.indexOf(ch) == -1; }));
function pad(str, width) {
while (str.length < width) str = "0" + str;
return str;
}
function esc(code) {
var hex = code.toString(16);
if (hex.length <= 2) return "\\x" + pad(hex, 2);
else return "\\u" + pad(hex, 4);
}
function generate(chars) {
var astral = [], re = "";
for (var i = 0, at = 0x10000; i < chars.length; i++) {
var from = chars[i], to = from;
while (i < chars.length - 1 && chars[i + 1] == to + 1) {
i++;
to++;
}
if (to <= 0xffff) {
if (from == to) re += esc(from);
else if (from + 1 == to) re += esc(from) + esc(to);
else re += esc(from) + "-" + esc(to);
} else {
astral.push(from - at, to - from);
at = to;
}
}
return {nonASCII: re, astral: astral};
}
var startData = generate(start), contData = generate(cont);
console.log(" var nonASCIIidentifierStartChars = \"" + startData.nonASCII + "\";");
console.log(" var nonASCIIidentifierChars = \"" + contData.nonASCII + "\";");
console.log(" var astralIdentifierStartCodes = " + JSON.stringify(startData.astral) + ";");
console.log(" var astralIdentifierCodes = " + JSON.stringify(contData.astral) + ";");

2
bin/prepublish.sh Executable file
View File

@@ -0,0 +1,2 @@
node bin/build-acorn.js
node bin/without_eval > dist/acorn_csp.js

View File

@@ -1,45 +1,48 @@
#!/usr/bin/env node
var fs = require("fs");
var fs = require("fs")
var acornSrc = fs.readFileSync(require.resolve("../acorn"), "utf8");
var acorn = require("../acorn"), walk = require("../util/walk");
var acornSrc = fs.readFileSync(require.resolve("../dist/acorn"), "utf8")
var acorn = require("../dist/acorn"), walk = require("../dist/walk")
var ast = acorn.parse(acornSrc);
var touchups = [], uses = [];
var ast = acorn.parse(acornSrc)
var touchups = [], uses = []
var makePred
walk.simple(ast, {
FunctionDeclaration: function(node) {
if (node.id.name == "makePredicate")
touchups.push({text: "// Removed to create an eval-free library", from: node.start, to: node.end});
if (node.id.name == "makePredicate") {
makePred = node
touchups.push({text: "// Removed to create an eval-free library", from: node.start, to: node.end})
}
},
VariableDeclaration: function(node) {
node.declarations.forEach(function(decl) {
if (decl.init && decl.init.type == "CallExpression" &&
decl.init.callee.name == "makePredicate")
uses.push(decl);
});
ObjectExpression: function(node) {
node.properties.forEach(function(prop) {
if (prop.value.type == "CallExpression" &&
prop.value.callee.name == "makePredicate")
uses.push(prop.value)
})
}
});
})
var results = Object.create(null);
var functions = new Function("predicates", acornSrc.replace(
/\}\);\s*$/, uses.map(function(decl) {
return "predicates[" + JSON.stringify(decl.id.name) + "] = " + decl.id.name + ";";
}).join("") + "});"))(results);
var results = []
var dryRun = acornSrc.slice(0, makePred.end) + "; makePredicate = (function(mp) {" +
"return function(words) { var r = mp(words); predicates.push(r); return r }})(makePredicate);" +
acornSrc.slice(makePred.end)
;(new Function("predicates", dryRun))(results)
uses.forEach(function(decl) {
touchups.push({text: results[decl.id.name].toString(),
from: decl.init.start, to: decl.init.end});
});
uses.forEach(function (node, i) {
touchups.push({text: results[i].toString(), from: node.start, to: node.end})
})
var result = "", pos = 0;
touchups.sort(function(a, b) { return a.from - b.from; });
var result = "", pos = 0
touchups.sort(function(a, b) { return a.from - b.from })
touchups.forEach(function(touchup) {
result += acornSrc.slice(pos, touchup.from);
result += touchup.text;
pos = touchup.to;
});
result += acornSrc.slice(pos);
result += acornSrc.slice(pos, touchup.from)
result += touchup.text
pos = touchup.to
})
result += acornSrc.slice(pos)
process.stdout.write(result);
process.stdout.write(result)