forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-config-chain.js
179 lines (146 loc) · 3.89 KB
/
build-config-chain.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import type Logger from "../logger";
import resolve from "../../../helpers/resolve";
import json5 from "json5";
import isAbsolute from "path-is-absolute";
import pathExists from "path-exists";
import path from "path";
import fs from "fs";
let existsCache = {};
let jsonCache = {};
const BABELIGNORE_FILENAME = ".babelignore";
const BABELRC_FILENAME = ".babelrc";
const PACKAGE_FILENAME = "package.json";
function exists(filename) {
let cached = existsCache[filename];
if (cached == null) {
return existsCache[filename] = pathExists.sync(filename);
} else {
return cached;
}
}
export default function buildConfigChain(opts: Object = {}, log?: Logger) {
let filename = opts.filename;
let builder = new ConfigChainBuilder(log);
// resolve all .babelrc files
if (opts.babelrc !== false) {
builder.findConfigs(filename);
}
builder.mergeConfig({
options: opts,
alias: "base",
dirname: filename && path.dirname(filename)
});
return builder.configs;
}
class ConfigChainBuilder {
constructor(log?: Logger) {
this.resolvedConfigs = [];
this.configs = [];
this.log = log;
}
findConfigs(loc) {
if (!loc) return;
if (!isAbsolute(loc)) {
loc = path.join(process.cwd(), loc);
}
let foundConfig = false;
let foundIgnore = false;
while (loc !== (loc = path.dirname(loc))) {
if (!foundConfig) {
let configLoc = path.join(loc, BABELRC_FILENAME);
if (exists(configLoc)) {
this.addConfig(configLoc);
foundConfig = true;
}
let pkgLoc = path.join(loc, PACKAGE_FILENAME);
if (!foundConfig && exists(pkgLoc)) {
foundConfig = this.addConfig(pkgLoc, "babel", JSON);
}
}
if (!foundIgnore) {
let ignoreLoc = path.join(loc, BABELIGNORE_FILENAME);
if (exists(ignoreLoc)) {
this.addIgnoreConfig(ignoreLoc);
foundIgnore = true;
}
}
if (foundIgnore && foundConfig) return;
}
}
addIgnoreConfig(loc) {
let file = fs.readFileSync(loc, "utf8");
let lines = file.split("\n");
lines = lines
.map((line) => line.replace(/#(.*?)$/, "").trim())
.filter((line) => !!line);
if (lines.length) {
this.mergeConfig({
options: { ignore: lines },
alias: loc,
dirname: path.dirname(loc)
});
}
}
addConfig(loc: string, key?: string, json = json5): boolean {
if (this.resolvedConfigs.indexOf(loc) >= 0) {
return false;
}
this.resolvedConfigs.push(loc);
let content = fs.readFileSync(loc, "utf8");
let options;
try {
options = jsonCache[content] = jsonCache[content] || json.parse(content);
if (key) options = options[key];
} catch (err) {
err.message = `${loc}: Error while parsing JSON - ${err.message}`;
throw err;
}
this.mergeConfig({
options,
alias: loc,
dirname: path.dirname(loc)
});
return !!options;
}
mergeConfig({
options,
alias,
loc,
dirname
}) {
if (!options) {
return false;
}
options = Object.assign({}, options);
dirname = dirname || process.cwd();
loc = loc || alias;
// add extends clause
if (options.extends) {
let extendsLoc = resolve(options.extends, dirname);
if (extendsLoc) {
this.addConfig(extendsLoc);
} else {
if (this.log) this.log.error(`Couldn't resolve extends clause of ${options.extends} in ${alias}`);
}
delete options.extends;
}
this.configs.push({
options,
alias,
loc,
dirname
});
// env
let envOpts;
let envKey = process.env.BABEL_ENV || process.env.NODE_ENV || "development";
if (options.env) {
envOpts = options.env[envKey];
delete options.env;
}
this.mergeConfig({
options: envOpts,
alias: `${alias}.env.${envKey}`,
dirname: dirname
});
}
}