add access to helpers used by the current file and allow a helper whitelist to be passed to buildHelpers - closes #898
This commit is contained in:
parent
372c06eb80
commit
0a0931dc2e
@ -3,7 +3,7 @@ var generator = require("./generation");
|
|||||||
var util = require("./util");
|
var util = require("./util");
|
||||||
var t = require("./types");
|
var t = require("./types");
|
||||||
|
|
||||||
module.exports = function () {
|
module.exports = function (whitelist) {
|
||||||
var namespace = t.identifier("babelHelpers");
|
var namespace = t.identifier("babelHelpers");
|
||||||
|
|
||||||
var body = [];
|
var body = [];
|
||||||
@ -17,7 +17,7 @@ module.exports = function () {
|
|||||||
)
|
)
|
||||||
]));
|
]));
|
||||||
|
|
||||||
buildHelpers(body, namespace);
|
buildHelpers(body, namespace, whitelist);
|
||||||
|
|
||||||
return generator(tree).code;
|
return generator(tree).code;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,9 +3,11 @@ var util = require("./util");
|
|||||||
var each = require("lodash/collection/each");
|
var each = require("lodash/collection/each");
|
||||||
var t = require("./types");
|
var t = require("./types");
|
||||||
|
|
||||||
module.exports = function (body, namespace) {
|
module.exports = function (body, namespace, whitelist = []) {
|
||||||
each(File.helpers, function (name) {
|
each(File.helpers, function (name) {
|
||||||
var key = t.identifier(t.toIdentifier(name));
|
var key = t.identifier(t.toIdentifier(name));
|
||||||
|
if (whitelist.length && whitelist.indexOf(key) >= 0) return;
|
||||||
|
|
||||||
body.push(t.expressionStatement(
|
body.push(t.expressionStatement(
|
||||||
t.assignmentExpression("=", t.memberExpression(namespace, key), util.template(name))
|
t.assignmentExpression("=", t.memberExpression(namespace, key), util.template(name))
|
||||||
));
|
));
|
||||||
|
|||||||
@ -22,12 +22,13 @@ function File(opts) {
|
|||||||
this.dynamicImported = [];
|
this.dynamicImported = [];
|
||||||
this.dynamicImports = [];
|
this.dynamicImports = [];
|
||||||
|
|
||||||
this.dynamicData = {};
|
this.usedHelpers = {};
|
||||||
this.data = {};
|
this.dynamicData = {};
|
||||||
|
this.data = {};
|
||||||
|
|
||||||
this.lastStatements = [];
|
this.lastStatements = [];
|
||||||
this.opts = this.normalizeOptions(opts);
|
this.opts = this.normalizeOptions(opts);
|
||||||
this.ast = {};
|
this.ast = {};
|
||||||
|
|
||||||
this.buildTransformers();
|
this.buildTransformers();
|
||||||
}
|
}
|
||||||
@ -85,6 +86,7 @@ File.validOptions = [
|
|||||||
"externalHelpers",
|
"externalHelpers",
|
||||||
"auxiliaryComment",
|
"auxiliaryComment",
|
||||||
"compact",
|
"compact",
|
||||||
|
"returnUsedHelpers",
|
||||||
|
|
||||||
"resolveModuleSource",
|
"resolveModuleSource",
|
||||||
"moduleId",
|
"moduleId",
|
||||||
@ -111,6 +113,7 @@ File.prototype.normalizeOptions = function (opts) {
|
|||||||
defaults(opts, {
|
defaults(opts, {
|
||||||
keepModuleIdExtensions: false,
|
keepModuleIdExtensions: false,
|
||||||
resolveModuleSource: null,
|
resolveModuleSource: null,
|
||||||
|
returnUsedHelpers: false,
|
||||||
externalHelpers: false,
|
externalHelpers: false,
|
||||||
auxilaryComment: "",
|
auxilaryComment: "",
|
||||||
experimental: false,
|
experimental: false,
|
||||||
@ -331,6 +334,8 @@ File.prototype.addHelper = function (name) {
|
|||||||
var declar = program._declarations && program._declarations[name];
|
var declar = program._declarations && program._declarations[name];
|
||||||
if (declar) return declar.id;
|
if (declar) return declar.id;
|
||||||
|
|
||||||
|
this.usedHelpers[name] = true;
|
||||||
|
|
||||||
var runtime = this.get("helpersNamespace");
|
var runtime = this.get("helpersNamespace");
|
||||||
if (runtime) {
|
if (runtime) {
|
||||||
name = t.identifier(t.toIdentifier(name));
|
name = t.identifier(t.toIdentifier(name));
|
||||||
@ -448,6 +453,10 @@ File.prototype.generate = function () {
|
|||||||
ast: null
|
ast: null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (this.opts.returnUsedHelpers) {
|
||||||
|
result.usedHelpers = Object.keys(this.usedHelpers);
|
||||||
|
}
|
||||||
|
|
||||||
if (opts.ast) result.ast = ast;
|
if (opts.ast) result.ast = ast;
|
||||||
if (!opts.code) return result;
|
if (!opts.code) return result;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user