/
babelrc-util.js
46 lines (41 loc) · 1.23 KB
/
babelrc-util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"use strict";
var fs = require('fs');
var path = require('path');
var extend = require('xtend');
var util = require('babel-core').util;
/**
* look up ".babelrc" file and merge options.
* @param {string} loc the location of file
* @param {Object} opts the options is merged .babelrc
* @returns {Object}
*/
function resolveBabelrc(loc, opts) {
var babelrc;
opts = opts !== undefined ? opts : {};
try {
babelrc = JSON.parse(fs.readFileSync(path.join(loc, '.babelrc'), 'utf-8'));
opts = extend(babelrc, opts);
} catch (e) {
// not found .babelrc then ignore error
}
return opts;
}
/**
* decide ignore or not by Babel's only/ignore options.
* @param filename
* @param babelrc
* @returns {boolean}
*/
function shouldIgnoreByBabel(filename, babelrc) {
if (!babelrc.ignore && !babelrc.only) {
return /node_modules/.test(filename);
} else {
var ignore = babelrc.ignore ? util.arrayify(babelrc.ignore, util.regexify) : null;
var only = babelrc.only ? util.arrayify(babelrc.only, util.regexify) : null;
return util.shouldIgnore(filename, ignore, only);
}
}
module.exports = {
resolveBabelrc: resolveBabelrc,
shouldIgnoreByBabel: shouldIgnoreByBabel
};