diff --git a/README.md b/README.md index d182ed913d..616e3af3ff 100644 --- a/README.md +++ b/README.md @@ -192,21 +192,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 69f68322c0..40dabc000a 100644 --- a/lib/6to5/register.js +++ b/lib/6to5/register.js @@ -24,7 +24,11 @@ var blacklist = []; var blacklistTest = function (transformer, code) { try { - new Function(code); + if (_.isFunction(code)) { + code(); + } else { + new Function(code); + } blacklist.push(transformer); } catch (err) { if (err.name !== "SyntaxError") throw err; @@ -34,34 +38,37 @@ var blacklistTest = function (transformer, code) { blacklistTest("arrayComprehension", "var foo = [for (foo of bar) foo * foo];"); blacklistTest("arrowFunctions", "var foo = x => x * x;"); blacklistTest("classes", "class Foo {}"); -//blacklistTest("computedPropertyNames", ""); -blacklistTest("constants", "const foo = 0;"); +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", ""); -//blacklistTest("propertyMethodAssignment", ""); +blacklistTest("modules", 'import foo from "from";'); +blacklistTest("propertyMethodAssignment", "{ get foo() {} }"); blacklistTest("propertyNameShorthand", "var foo = { x, y };"); -//blacklistTest("restParameters", ""); -//blacklistTest("spread", ""); +blacklistTest("restParameters", "function foo(...bar) {}"); +blacklistTest("spread", "foo(...bar);"); blacklistTest("templateLiterals", "`foo`"); -blacklistTest("unicodeRegex", "/foo/u"); +blacklistTest("unicodeRegex", function () { new RegExp("foo", "u"); }); // var ignoreRegex = /node_modules/; +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, { + whitelist: whitelist, blacklist: blacklist, sourceMap: true }); @@ -87,12 +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; };