diff --git a/packages/babel-cli/bin/babel/dir.js b/packages/babel-cli/bin/babel/dir.js index 7a19d53063..6e0e0cd65d 100644 --- a/packages/babel-cli/bin/babel/dir.js +++ b/packages/babel-cli/bin/babel/dir.js @@ -28,6 +28,8 @@ module.exports = function (commander, filenames, opts) { }; var handleFile = function (src, filename) { + if (util.shouldIgnore(src)) return; + if (util.canCompile(filename)) { write(src, filename); } else if (commander.copyFiles) { diff --git a/packages/babel-cli/bin/babel/file.js b/packages/babel-cli/bin/babel/file.js index f61efde461..9e4990e289 100644 --- a/packages/babel-cli/bin/babel/file.js +++ b/packages/babel-cli/bin/babel/file.js @@ -107,6 +107,8 @@ module.exports = function (commander, filenames, opts) { }); _.each(_filenames, function (filename) { + if (!util.shouldIgnore(filename)) return; + results.push(util.compile(filename)); }); diff --git a/packages/babel-cli/bin/babel/index.js b/packages/babel-cli/bin/babel/index.js index 04a61612fd..48b214d0a8 100755 --- a/packages/babel-cli/bin/babel/index.js +++ b/packages/babel-cli/bin/babel/index.js @@ -102,12 +102,15 @@ if (errors.length) { // -exports.opts = {}; +var opts = exports.opts = {}; each(options, function (opt, key) { - exports.opts[key] = commander[key]; + opts[key] = commander[key]; }); +opts.ignore = util.arrayify(opts.ignore, util.regexify); +opts.only = util.arrayify(opts.only, util.regexify); + var fn; if (commander.outDir) { diff --git a/packages/babel-cli/bin/babel/util.js b/packages/babel-cli/bin/babel/util.js index a7a640285c..1de4d894db 100644 --- a/packages/babel-cli/bin/babel/util.js +++ b/packages/babel-cli/bin/babel/util.js @@ -16,6 +16,10 @@ exports.readdir = readdir; exports.canCompile = util.canCompile; +exports.shouldIgnore = function (loc) { + return util.shouldIgnore(loc, index.opts.ignore, index.opts.only); +}; + exports.addSourceMappingUrl = function (code, loc) { return code + "\n//# sourceMappingURL=" + path.basename(loc); }; @@ -23,6 +27,8 @@ exports.addSourceMappingUrl = function (code, loc) { exports.transform = function (filename, code, opts) { opts = _.defaults(opts || {}, index.opts); opts.filename = filename; + opts.ignore = null; + opts.only = null; var result = babel.transform(code, opts); result.filename = filename; diff --git a/src/babel/transformation/file/index.js b/src/babel/transformation/file/index.js index eaa53be048..56026f03d0 100644 --- a/src/babel/transformation/file/index.js +++ b/src/babel/transformation/file/index.js @@ -422,23 +422,7 @@ export default class File { shouldIgnore() { var opts = this.opts; - - var filename = opts.filename; - var ignore = opts.ignore; - var only = opts.only; - - if (only.length) { - for (var i = 0; i < only.length; i++) { - if (only[i].test(filename)) return false; - } - return true; - } else if (ignore.length) { - for (var i = 0; i < ignore.length; i++) { - if (ignore[i].test(filename)) return true; - } - } - - return false; + return util.shouldIgnore(opts.filename, opts.ignore, opts.only); } parse(code: string) { diff --git a/src/babel/util.js b/src/babel/util.js index f0bbc4286c..298267bf59 100644 --- a/src/babel/util.js +++ b/src/babel/util.js @@ -93,6 +93,21 @@ export function booleanify(val: any): boolean { return val; } +export function shouldIgnore(filename, ignore, only) { + if (only.length) { + for (var i = 0; i < only.length; i++) { + if (only[i].test(filename)) return false; + } + return true; + } else if (ignore.length) { + for (var i = 0; i < ignore.length; i++) { + if (ignore[i].test(filename)) return true; + } + } + + return false; +} + var templateVisitor = { enter(node, parent, scope, nodes) { if (t.isExpressionStatement(node)) { diff --git a/test/core/fixtures/bin/babel/--ignore/in-files/src/bar/index.js b/test/core/fixtures/bin/babel/--ignore/in-files/src/bar/index.js new file mode 100644 index 0000000000..e46160df1c --- /dev/null +++ b/test/core/fixtures/bin/babel/--ignore/in-files/src/bar/index.js @@ -0,0 +1 @@ +bar; diff --git a/test/core/fixtures/bin/babel/--ignore/in-files/src/foo/foo.js b/test/core/fixtures/bin/babel/--ignore/in-files/src/foo/foo.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/core/fixtures/bin/babel/--ignore/options.json b/test/core/fixtures/bin/babel/--ignore/options.json new file mode 100644 index 0000000000..1911426f62 --- /dev/null +++ b/test/core/fixtures/bin/babel/--ignore/options.json @@ -0,0 +1,3 @@ +{ + "args": ["src", "--out-dir", "lib", "--ignore", "src/foo/*"] +} diff --git a/test/core/fixtures/bin/babel/--ignore/stdout.txt b/test/core/fixtures/bin/babel/--ignore/stdout.txt new file mode 100644 index 0000000000..1eb8f922d1 --- /dev/null +++ b/test/core/fixtures/bin/babel/--ignore/stdout.txt @@ -0,0 +1 @@ +src/bar/index.js -> lib/bar/index.js diff --git a/test/core/fixtures/bin/babel/--only/in-files/src/bar/index.js b/test/core/fixtures/bin/babel/--only/in-files/src/bar/index.js new file mode 100644 index 0000000000..e46160df1c --- /dev/null +++ b/test/core/fixtures/bin/babel/--only/in-files/src/bar/index.js @@ -0,0 +1 @@ +bar; diff --git a/test/core/fixtures/bin/babel/--only/in-files/src/foo/index.js b/test/core/fixtures/bin/babel/--only/in-files/src/foo/index.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/core/fixtures/bin/babel/--only/options.json b/test/core/fixtures/bin/babel/--only/options.json new file mode 100644 index 0000000000..7b05f3fb82 --- /dev/null +++ b/test/core/fixtures/bin/babel/--only/options.json @@ -0,0 +1,3 @@ +{ + "args": ["src", "--out-dir", "lib", "--only", "src/bar/*"] +} diff --git a/test/core/fixtures/bin/babel/--only/stdout.txt b/test/core/fixtures/bin/babel/--only/stdout.txt new file mode 100644 index 0000000000..1eb8f922d1 --- /dev/null +++ b/test/core/fixtures/bin/babel/--only/stdout.txt @@ -0,0 +1 @@ +src/bar/index.js -> lib/bar/index.js