Skip to content
Permalink
Browse files

refactor assemble.js

  • Loading branch information...
raimohanska committed Jun 19, 2018
1 parent e31b093 commit a1aeaf6e70bb3d04d3aa4bdd31f3d203b2131ec5
Showing with 75 additions and 59 deletions.
  1. +1 −1 package-lock.json
  2. +3 −58 scripts/assemble.js
  3. +32 −0 scripts/assemble/partialBuildPlugin.js
  4. +39 −0 scripts/assemble/stripAsserts.js

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -18,72 +18,17 @@ var esprima = require("esprima");
var estraverse = require("estraverse");
var escodegen = require("escodegen");
var jsstana = require("jsstana");
var stripAsserts = require("./assemble/stripAsserts")
var partialBuildPlugin = require("./assemble/partialBuildPlugin")

var argPieceNames = process.argv.slice(2);
var defaultOutput = path.join(__dirname, "..", "dist", "Bacon.js");
var defaultNoAssert = path.join(__dirname, "..", "dist", "Bacon.noAssert.js");
var defaultMinified = path.join(__dirname, "..", "dist", "Bacon.min.js");

function partialBuildPlugin(options) {
var pieces = (options || {}).pieces || [];
var filter = function(id) {
return path.basename(id) === 'bacon.js';
}

return {
transform (code, id) {
if (!filter(id)) return;

var ast = recast.parse(code, { sourceFileName: id });
recast.visit(ast, {
visitImportDeclaration: function(path) {
this.traverse(path);
var name = path.node.source.value.replace(/^.\//, '');
if (name !== 'core' && pieces.indexOf(name) === -1) {
path.replace(null);
}

}
});

return recast.print(ast, { sourceMapName: "map.json" });
}
};
}

var main = function(options) {
function main(options) {
options = options || {};

function notAssertStatement(node) {
if (node.type == "FunctionDeclaration" && node.id.name.match(/^assert/)) {
return false;
}

var m1 = jsstana.match("(expr (call (ident ?fn) ??))", node);
if (m1 && m1.fn.match(/^assert/)) {
return false;
}
var m2 = jsstana.match("(expr (assign = (ident ?fn) ?))", node);
if (m2 && m2.fn.match(/^assert/)) {
return false;
}

return true;
}

function stripAsserts(code) {
var ast = esprima.parse(code, { sourceType: 'module' });
estraverse.replace(ast, {
enter: function (node) {
if (node !== null && node.type === "BlockStatement") {
node.body = node.body.filter(notAssertStatement);
return node;
}
}
})
return escodegen.generate(ast);
}

try {fs.mkdirSync("dist")} catch (e) {
// directory exists, do nothing
}
@@ -0,0 +1,32 @@
"use strict";

var recast = require("recast");
var path = require("path");

function partialBuildPlugin(options) {
var pieces = (options || {}).pieces || [];
var filter = function(id) {
return path.basename(id) === 'bacon.js';
}

return {
transform (code, id) {
if (!filter(id)) return;

var ast = recast.parse(code, { sourceFileName: id });
recast.visit(ast, {
visitImportDeclaration: function(path) {
this.traverse(path);
var name = path.node.source.value.replace(/^.\//, '');
if (name !== 'core' && pieces.indexOf(name) === -1) {
path.replace(null);
}

}
});

return recast.print(ast, { sourceMapName: "map.json" });
}
};
}
module.exports = partialBuildPlugin
@@ -0,0 +1,39 @@
"use strict";

var esprima = require("esprima");
var estraverse = require("estraverse");
var escodegen = require("escodegen");
var jsstana = require("jsstana");


function stripAsserts(code) {
function notAssertStatement(node) {
if (node.type == "FunctionDeclaration" && node.id.name.match(/^assert/)) {
return false;
}

var m1 = jsstana.match("(expr (call (ident ?fn) ??))", node);
if (m1 && m1.fn.match(/^assert/)) {
return false;
}
var m2 = jsstana.match("(expr (assign = (ident ?fn) ?))", node);
if (m2 && m2.fn.match(/^assert/)) {
return false;
}

return true;
}

var ast = esprima.parse(code, { sourceType: 'module' });
estraverse.replace(ast, {
enter: function (node) {
if (node !== null && node.type === "BlockStatement") {
node.body = node.body.filter(notAssertStatement);
return node;
}
}
})
return escodegen.generate(ast);
}

module.exports = stripAsserts

0 comments on commit a1aeaf6

Please sign in to comment.
You can’t perform that action at this time.