From 54f901f131282ea3ca0668d0ec54cede7a45561e Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 7 Nov 2014 13:16:26 +1100 Subject: [PATCH] require: add missing blacklistTests, implement opts.whitelist and opts.only - closes #125 Conflicts: lib/6to5/register.js --- README.md | 25 ++++++++++++-------- lib/6to5/register.js | 55 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f74281f289..06e9d29c4b 100644 --- a/README.md +++ b/README.md @@ -190,21 +190,26 @@ require("6to5/register"); ``` **NOTE:** By default all requires to `node_modules` will be ignored. You can -override this by passing an ignore regex via: +override this by passing an ignore regex with`. -```javascript -require("6to5/register")(/regex/); -``` - -You can also customise the file extensions that the require hook will use via: +##### Options ```javascript require("6to5/register")({ - // optional ignore regex - ignoreRegex: /regex/, + // Optional ignore regex - if any filenames **do** match this regex then they + // aren't compiled + ignore: /regex/, - // this will remove the currently hooked extensions of .es6 and .js so you'll - // have to add them back if you want them to be used again + // Optional only regex - if any filenames **don't** match this regex then they + // aren't compiled + only: /my_es6_folder/, + + // See options above for usage + whitelist: [], + blacklist: [], + + // This will remove the currently hooked extensions of .es6 and .js so you'll + // have to add them back if you want them to be used again. extensions: [".js", ".es6"] }); ``` diff --git a/lib/6to5/register.js b/lib/6to5/register.js index 840039b385..1ccadaec56 100644 --- a/lib/6to5/register.js +++ b/lib/6to5/register.js @@ -20,20 +20,57 @@ sourceMapSupport.install({ // +var blacklist = []; + +var blacklistTest = function (transformer, code) { + try { + if (_.isFunction(code)) { + code(); + } else { + new Function(code); + } + blacklist.push(transformer); + } catch (err) { + if (err.name !== "SyntaxError") throw err; + } +}; + +blacklistTest("arrayComprehension", "var foo = [for (foo of bar) foo * foo];"); +blacklistTest("arrowFunctions", "var foo = x => x * x;"); +blacklistTest("classes", "class Foo {}"); +blacklistTest("computedPropertyNames", "var foo = { [foo]: bar };"); +//blacklistTest("constants", "const foo = 0;"); +blacklistTest("defaultParamaters", "var foo = function (bar = 0) {};"); +blacklistTest("destructuring", "var { x, y } = { x: 0, y: 0 };"); +blacklistTest("forOf", "for (var foo of bar) {}"); +blacklistTest("generators", "function* foo() {}"); +blacklistTest("letScoping", "let foo = 0;"); +blacklistTest("modules", 'import foo from "from";'); +blacklistTest("propertyMethodAssignment", "{ get foo() {} }"); +blacklistTest("propertyNameShorthand", "var foo = { x, y };"); +blacklistTest("restParameters", "function foo(...bar) {}"); +blacklistTest("spread", "foo(...bar);"); +blacklistTest("templateLiterals", "`foo`"); +blacklistTest("unicodeRegex", function () { new RegExp("foo", "u"); }); + +// + var ignoreRegex = /node_modules/; -var blacklist = []; +var onlyRegex; +var whitelist = []; var exts = {}; var maps = {}; var old = require.extensions[".js"]; var loader = function (m, filename) { - if (ignoreRegex && ignoreRegex.test(filename)) { + if ((ignoreRegex && ignoreRegex.test(filename)) || (onlyRegex && !onlyRegex.test(filename))) { return old.apply(this, arguments); } var result = to5.transformFileSync(filename, { - sourceMap: true, - blacklist: blacklist + whitelist: whitelist, + blacklist: blacklist, + sourceMap: true }); maps[filename] = result.map; @@ -57,14 +94,16 @@ var hookExtensions = function (_exts) { hookExtensions([".es6", ".js"]); module.exports = function (opts) { + // normalise options opts = opts || {}; - if (_.isRegExp(opts)) opts = { ignoreRegex: opts }; + if (_.isRegExp(opts)) opts = { ignore: opts }; + opts.ignore = opts.ignore || opts.ignoreRegex; - if (opts.ignoreRegex != null) { - ignoreRegex = opts.ignoreRegex; - } + if (opts.only != null) onlyRegex = opts.only; + if (opts.ignore != null) ignoreRegex = opts.ignore; if (opts.extensions) hookExtensions(opts.extensions); if (opts.blacklist) blacklist = opts.blacklist; + if (opts.whitelist) whitelist = opts.whitelist; };