always resolve rc for absolute filenames
This commit is contained in:
parent
9cff51915d
commit
40a111abbf
@ -1,11 +1,10 @@
|
||||
var resolveRc = require("../../lib/babel/api/register/resolve-rc");
|
||||
var readdir = require("fs-readdir-recursive");
|
||||
var index = require("./index");
|
||||
var babel = require("../../lib/babel/api/node");
|
||||
var util = require("../../lib/babel/util");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
var readdir = require("fs-readdir-recursive");
|
||||
var index = require("./index");
|
||||
var babel = require("../../lib/babel/api/node");
|
||||
var util = require("../../lib/babel/util");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.readdirFilter = function (filename) {
|
||||
return readdir(filename).filter(function (filename) {
|
||||
@ -24,7 +23,6 @@ exports.addSourceMappingUrl = function (code, loc) {
|
||||
exports.transform = function (filename, code, opts) {
|
||||
opts = _.defaults(opts || {}, index.opts);
|
||||
opts.filename = filename;
|
||||
resolveRc(filename, opts);
|
||||
|
||||
var result = babel.transform(code, opts);
|
||||
result.filename = filename;
|
||||
|
||||
@ -54,6 +54,7 @@
|
||||
"leven": "^1.0.1",
|
||||
"line-numbers": "0.2.0",
|
||||
"lodash": "^3.2.0",
|
||||
"minimatch": "^2.0.3",
|
||||
"output-file-sync": "^1.1.0",
|
||||
"path-is-absolute": "^1.0.0",
|
||||
"private": "^0.1.6",
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
import isFunction from "lodash/lang/isFunction";
|
||||
import transform from "../transformation";
|
||||
import * as util from "../util";
|
||||
import fs from "fs";
|
||||
|
||||
import * as util from "../util";
|
||||
export { util as _util };
|
||||
export { util };
|
||||
export { canCompile } from "../util";
|
||||
|
||||
export { default as acorn } from "acorn-babel";
|
||||
export { default as Transformer } from "../transformation/transformer";
|
||||
export { default as transform } from "../transformation";
|
||||
export { default as traverse } from "../traversal";
|
||||
export { default as buildExternalHelpers } from "../tools/build-external-helpers";
|
||||
@ -15,7 +16,7 @@ export { version } from "../../../package";
|
||||
import * as t from "../types";
|
||||
export { t as types };
|
||||
|
||||
export function register(opts) {
|
||||
export function register(opts?: Object) {
|
||||
var callback = require("./register/node");
|
||||
if (opts != null) callback(opts);
|
||||
return callback;
|
||||
@ -25,7 +26,7 @@ export function polyfill() {
|
||||
require("../polyfill");
|
||||
}
|
||||
|
||||
export function transformFile(filename, opts, callback) {
|
||||
export function transformFile(filename: string, opts?: Object, callback: Function) {
|
||||
if (isFunction(opts)) {
|
||||
callback = opts;
|
||||
opts = {};
|
||||
@ -48,7 +49,7 @@ export function transformFile(filename, opts, callback) {
|
||||
});
|
||||
}
|
||||
|
||||
export function transformFileSync(filename, opts = {}) {
|
||||
export function transformFileSync(filename: string, opts?: Object = {}) {
|
||||
opts.filename = filename;
|
||||
return transform(fs.readFileSync(filename), opts);
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import "../../polyfill";
|
||||
import sourceMapSupport from "source-map-support";
|
||||
import * as registerCache from "./cache";
|
||||
import resolveRc from "./resolve-rc";
|
||||
import resolveRc from "../../tools/resolve-rc";
|
||||
import extend from "lodash/object/extend";
|
||||
import * as babel from "../node";
|
||||
import each from "lodash/collection/each";
|
||||
@ -44,6 +44,9 @@ var compile = function (filename) {
|
||||
var result;
|
||||
|
||||
var opts = extend({}, transformOpts);
|
||||
|
||||
// this will be done when the file is transformed anyway but we need all
|
||||
// the options so we can generate the cache key
|
||||
resolveRc(filename, opts);
|
||||
|
||||
var cacheKey = `${filename}:${JSON.stringify(opts)}:${babel.version}`;
|
||||
|
||||
@ -1,12 +1,15 @@
|
||||
import convertSourceMap from "convert-source-map";
|
||||
import shebangRegex from "shebang-regex";
|
||||
import isFunction from "lodash/lang/isFunction";
|
||||
import TraversalPath from "../traversal/path";
|
||||
import isFunction from "lodash/lang/isFunction";
|
||||
import isAbsolute from "path-is-absolute";
|
||||
import resolveRc from "../tools/resolve-rc";
|
||||
import sourceMap from "source-map";
|
||||
import transform from "./index";
|
||||
import generate from "../generation";
|
||||
import defaults from "lodash/object/defaults";
|
||||
import includes from "lodash/collection/includes";
|
||||
import traverse from "../traversal";
|
||||
import assign from "lodash/object/assign";
|
||||
import Logger from "./logger";
|
||||
import parse from "../helpers/parse";
|
||||
@ -114,13 +117,14 @@ export default class File {
|
||||
"sourceRoot",
|
||||
"moduleRoot",
|
||||
|
||||
"ignore",
|
||||
"only",
|
||||
|
||||
// legacy
|
||||
"format",
|
||||
"reactCompat",
|
||||
|
||||
// these are used by plugins
|
||||
"ignore",
|
||||
"only",
|
||||
"extensions",
|
||||
"accept"
|
||||
];
|
||||
@ -128,6 +132,10 @@ export default class File {
|
||||
normalizeOptions(opts: Object) {
|
||||
opts = assign({}, opts);
|
||||
|
||||
if (opts.filename && isAbsolute(opts.filename)) {
|
||||
opts = resolveRc(opts.filename, opts);
|
||||
}
|
||||
|
||||
for (var key in opts) {
|
||||
if (key[0] !== "_" && File.validOptions.indexOf(key) < 0) {
|
||||
throw new ReferenceError(`Unknown option: ${key}`);
|
||||
@ -154,6 +162,8 @@ export default class File {
|
||||
modules: "common",
|
||||
compact: "auto",
|
||||
loose: [],
|
||||
ignore: [],
|
||||
only: [],
|
||||
code: true,
|
||||
ast: true
|
||||
});
|
||||
@ -179,6 +189,8 @@ export default class File {
|
||||
opts.optional = util.arrayify(opts.optional);
|
||||
opts.compact = util.booleanify(opts.compact);
|
||||
opts.loose = util.arrayify(opts.loose);
|
||||
opts.ignore = util.arrayify(opts.ignore, util.regexify);
|
||||
opts.only = util.arrayify(opts.only, util.regexify);
|
||||
|
||||
if (includes(opts.loose, "all") || includes(opts.loose, true)) {
|
||||
opts.loose = Object.keys(transform.transformers);
|
||||
@ -214,11 +226,6 @@ export default class File {
|
||||
opts.optional = transform._ensureTransformerNames("optional", opts.optional);
|
||||
opts.loose = transform._ensureTransformerNames("loose", opts.loose);
|
||||
|
||||
if (opts.reactCompat) {
|
||||
opts.optional.push("reactCompat");
|
||||
console.error("The reactCompat option has been moved into the optional transformer `reactCompat`");
|
||||
}
|
||||
|
||||
var ensureEnabled = function (key) {
|
||||
var namespace = transform.transformerNamespaces[key];
|
||||
if (namespace === "es7") opts.experimental = true;
|
||||
@ -409,7 +416,36 @@ export default class File {
|
||||
return this.parseShebang(code);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
parse(code: string) {
|
||||
if (this.shouldIgnore()) {
|
||||
return {
|
||||
code: code,
|
||||
map: null,
|
||||
ast: null
|
||||
};
|
||||
}
|
||||
|
||||
code = this.addCode(code);
|
||||
|
||||
var opts = this.opts;
|
||||
@ -423,13 +459,27 @@ export default class File {
|
||||
});
|
||||
}
|
||||
|
||||
transform(ast) {
|
||||
this.log.debug();
|
||||
|
||||
setAst(ast) {
|
||||
this.path = TraversalPath.get(null, null, ast, ast, "program", this);
|
||||
this.scope = this.path.scope;
|
||||
this.ast = ast;
|
||||
|
||||
traverse(ast, {
|
||||
enter(node, parent, scope) {
|
||||
if (this.isScope()) {
|
||||
for (var key in scope.bindings) {
|
||||
scope.bindings[key].setTypeAnnotation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}, this.scope);
|
||||
}
|
||||
|
||||
transform(ast) {
|
||||
this.log.debug();
|
||||
|
||||
this.setAst(ast);
|
||||
|
||||
this.lastStatements = t.getLastStatements(ast.program);
|
||||
|
||||
var modFormatter = this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
|
||||
|
||||
@ -4,7 +4,7 @@ import object from "../helpers/object";
|
||||
import File from "./file";
|
||||
import each from "lodash/collection/each";
|
||||
|
||||
export default function transform(code: code, opts?: Object) {
|
||||
export default function transform(code: string, opts?: Object) {
|
||||
var file = new File(opts);
|
||||
return file.parse(code);
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ import buildDebug from "debug/node";
|
||||
import cloneDeep from "lodash/lang/cloneDeep";
|
||||
import isBoolean from "lodash/lang/isBoolean";
|
||||
import * as messages from "./messages";
|
||||
import minimatch from "minimatch";
|
||||
import contains from "lodash/collection/contains";
|
||||
import traverse from "./traversal";
|
||||
import isString from "lodash/lang/isString";
|
||||
@ -43,16 +44,21 @@ export function list(val: string): Array<string> {
|
||||
export function regexify(val: any): RegExp {
|
||||
if (!val) return new RegExp(/.^/);
|
||||
if (Array.isArray(val)) val = val.join("|");
|
||||
if (isString(val)) return new RegExp(val);
|
||||
if (isString(val)) return minimatch.makeRe(val, { nocase: true });
|
||||
if (isRegExp(val)) return val;
|
||||
throw new TypeError("illegal type for regexify");
|
||||
}
|
||||
|
||||
export function arrayify(val: any): Array {
|
||||
export function arrayify(val: any, mapFn?: Function): Array {
|
||||
if (!val) return [];
|
||||
if (isBoolean(val)) return [val];
|
||||
if (isString(val)) return list(val);
|
||||
if (Array.isArray(val)) return val;
|
||||
if (isBoolean(val)) return arrayify([val], mapFn);
|
||||
if (isString(val)) return arrayify(list(val), mapFn);
|
||||
|
||||
if (Array.isArray(val)) {
|
||||
if (mapFn) val = val.map(mapFn);
|
||||
return val;
|
||||
}
|
||||
|
||||
throw new TypeError("illegal type for arrayify");
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user