/
babel-pipeline.js
278 lines (232 loc) 路 8.27 KB
/
babel-pipeline.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
'use strict';
const fs = require('fs');
const path = require('path');
const babel = require('@babel/core');
const concordance = require('concordance');
const convertSourceMap = require('convert-source-map');
const findUp = require('find-up');
const isPlainObject = require('is-plain-object');
const md5Hex = require('md5-hex');
const packageHash = require('package-hash');
const pkgConf = require('pkg-conf');
const stripBomBuf = require('strip-bom-buf');
const writeFileAtomic = require('write-file-atomic');
const pkg = require('../package.json');
function getSourceMap(filePath, code) {
let sourceMap = convertSourceMap.fromSource(code);
if (!sourceMap) {
const dirPath = path.dirname(filePath);
sourceMap = convertSourceMap.fromMapFileSource(code, dirPath);
}
return sourceMap ? sourceMap.toObject() : undefined;
}
function hasValidKeys(conf) {
return Object.keys(conf).every(key => key === 'extensions' || key === 'testOptions');
}
function isValidExtensions(extensions) {
return Array.isArray(extensions) && extensions.every(ext => typeof ext === 'string' && ext !== '');
}
function validate(conf) {
if (conf === false) {
return null;
}
const defaultOptions = {babelrc: true, configFile: true};
if (conf === undefined) {
return {testOptions: defaultOptions};
}
if (
!isPlainObject(conf) ||
!hasValidKeys(conf) ||
(conf.testOptions !== undefined && !isPlainObject(conf.testOptions)) ||
(conf.extensions !== undefined && !isValidExtensions(conf.extensions))
) {
throw new Error(`Unexpected Babel configuration for AVA. See https://github.com/avajs/ava/blob/v${pkg.version}/docs/recipes/babel.md for allowed values.`);
}
return {
extensions: conf.extensions,
testOptions: {...defaultOptions, ...conf.testOptions}
};
}
function enableParserPlugins(api) {
api.assertVersion(7);
return {
name: 'ava-babel-pipeline-enable-parser-plugins',
manipulateOptions(_, parserOpts) {
parserOpts.plugins.push(
'asyncGenerators',
'bigInt',
'classPrivateProperties',
'classProperties',
'dynamicImport',
'numericSeparator',
'objectRestSpread',
'optionalCatchBinding'
);
}
};
}
// Compare actual values rather than file paths, which should be
// more reliable.
function makeValueChecker(ref) {
const expected = require(ref);
return ({value}) => value === expected || value === expected.default;
}
// Resolved paths are used to create the config item, rather than the plugin
// function itself, so Babel can print better error messages.
// See <https://github.com/babel/babel/issues/7921>.
function createConfigItem(ref, type, options = {}) {
return babel.createConfigItem([require.resolve(ref), options], {type});
}
// Assume the stage-4 preset is wanted if there are `userOptions`, but there is
// no declaration of a stage-` preset that comes with `false` for its options.
//
// Ideally we'd detect the stage-4 preset anywhere in the configuration
// hierarchy, but Babel's loadPartialConfig() does not return disabled presets.
// See <https://github.com/babel/babel/issues/7920>.
function wantsStage4(userOptions, projectDir) {
if (!userOptions) {
return false;
}
if (!userOptions.testOptions.presets) {
return true;
}
const stage4 = require('../stage-4');
return userOptions.testOptions.presets.every(arr => {
if (!Array.isArray(arr)) {
return true; // There aren't any preset options.
}
const [ref, options] = arr;
// Require the preset given the aliasing `ava/stage-4` does towards
// `@ava/babel-preset-stage-4`.
const resolved = require(babel.resolvePreset(ref, projectDir));
return resolved !== stage4 || options !== false;
});
}
function hashPartialTestConfig({babelrc, config, options: {plugins, presets}}, projectDir, pluginAndPresetHashes) {
const inputs = [];
if (babelrc) {
inputs.push(babelrc);
const filename = path.basename(babelrc);
if (filename === 'package.json') {
inputs.push(JSON.stringify(pkgConf.sync('babel', {cwd: path.dirname(filename)})));
} else {
inputs.push(stripBomBuf(fs.readFileSync(babelrc)));
}
}
if (config) {
inputs.push(config, stripBomBuf(fs.readFileSync(config)));
}
for (const item of [...plugins, ...presets]) {
if (!item.file) {
continue;
}
const {file: {resolved: filename}} = item;
if (pluginAndPresetHashes.has(filename)) {
inputs.push(pluginAndPresetHashes.get(filename));
continue;
}
const [firstComponent] = path.relative(projectDir, filename).split(path.sep);
let hash;
if (firstComponent === 'node_modules') {
hash = packageHash.sync(findUp.sync('package.json', {cwd: path.dirname(filename)}));
} else {
hash = md5Hex(stripBomBuf(fs.readFileSync(filename)));
}
pluginAndPresetHashes.set(filename, hash);
inputs.push(hash);
}
return md5Hex(inputs);
}
function build(projectDir, cacheDir, userOptions, compileEnhancements) {
if (!userOptions && !compileEnhancements) {
return null;
}
// Note that Babel ignores empty string values, even for NODE_ENV. Here
// default to 'test' unless NODE_ENV is defined, in which case fall back to
// Babel's default of 'development' if it's empty.
const envName = process.env.BABEL_ENV || ('NODE_ENV' in process.env ? process.env.NODE_ENV : 'test') || 'development';
// Prepare inputs for caching seeds. Compute a seed based on the Node.js
// version and the project directory. Dependency hashes may vary based on the
// Node.js version, e.g. with the @ava/stage-4 Babel preset. Certain plugins
// and presets are provided as absolute paths, which wouldn't necessarily
// be valid if the project directory changes. Also include `envName`, so
// options can be cached even if users change BABEL_ENV or NODE_ENV between
// runs.
const seedInputs = [
process.versions.node,
packageHash.sync(require.resolve('../package.json')),
projectDir,
envName,
concordance.serialize(concordance.describe(userOptions))
];
const partialCacheKey = md5Hex(seedInputs);
const pluginAndPresetHashes = new Map();
const ensureStage4 = wantsStage4(userOptions, projectDir);
const containsStage4 = makeValueChecker('../stage-4');
const containsTransformTestFiles = makeValueChecker('@ava/babel-preset-transform-test-files');
const loadOptions = filename => {
const partialTestConfig = babel.loadPartialConfig({
babelrc: false,
babelrcRoots: [projectDir],
configFile: false,
sourceMaps: true,
...userOptions && userOptions.testOptions,
cwd: projectDir,
envName,
filename,
sourceFileName: path.relative(projectDir, filename),
sourceRoot: projectDir
});
if (!partialTestConfig) {
return {hash: '', options: null};
}
const {options: testOptions} = partialTestConfig;
testOptions.plugins.push(enableParserPlugins);
if (ensureStage4 && !testOptions.presets.some(containsStage4)) {
// Apply last.
testOptions.presets.unshift(createConfigItem('../stage-4', 'preset'));
}
if (compileEnhancements && !testOptions.presets.some(containsTransformTestFiles)) {
// Apply first.
testOptions.presets.push(createConfigItem('@ava/babel-preset-transform-test-files', 'preset', {powerAssert: true}));
}
const hash = hashPartialTestConfig(partialTestConfig, projectDir, pluginAndPresetHashes);
const options = babel.loadOptions(testOptions);
return {hash, options};
};
return filename => {
const {hash: optionsHash, options} = loadOptions(filename);
if (!options) {
return null;
}
const contents = stripBomBuf(fs.readFileSync(filename));
const ext = path.extname(filename);
const hash = md5Hex([partialCacheKey, contents, optionsHash]);
const cachePath = path.join(cacheDir, `${hash}${ext}`);
if (fs.existsSync(cachePath)) {
return cachePath;
}
const inputCode = contents.toString('utf8');
const inputSourceMap = getSourceMap(filename, inputCode);
if (inputSourceMap) {
options.inputSourceMap = inputSourceMap;
}
const {code, map} = babel.transformSync(inputCode, options);
if (map) {
// Save source map
const mapPath = `${cachePath}.map`;
writeFileAtomic.sync(mapPath, JSON.stringify(map));
// Append source map comment to transformed code so that other libraries
// (like nyc) can find the source map.
const comment = convertSourceMap.generateMapFileComment(mapPath);
writeFileAtomic.sync(cachePath, `${code}\n${comment}`);
} else {
writeFileAtomic.sync(cachePath, code);
}
return cachePath;
};
}
module.exports = {
validate,
build
};