Cleanup Jest preprocessor, use cache key
Paul O’Shannessy committed
Sep 22, 2015 at 17:40 UTC
873e3a981696ce978c88e3cad958d1cba1b0a882
1 file changed
+37
-18
scripts/jest/preprocessor.js
+37
-18
@@ -2,6 +2,7 @@
2
3
var path = require('path');
4
5
+var assign = require('object-assign');
6
var babel = require('babel');
7
var coffee = require('coffee-script');
8
@@ -12,6 +13,28 @@ var tsPreprocessor = require('./ts-preprocessor');
13
var moduleMap = require('fbjs/module-map');
14
var babelPluginDEV = require('fbjs-scripts/babel/dev-expression');
15
var babelPluginModules = require('fbjs-scripts/babel/rewrite-modules');
16
+var createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
17
+
18
+// Use require.resolve to be resiliant to file moves, npm updates, etc
19
+var pathToBabel = path.join(require.resolve('babel'), '..', 'package.json');
20
+var pathToModuleMap = require.resolve('fbjs/module-map');
21
+var pathToBabelPluginDev = require.resolve('fbjs-scripts/babel/dev-expression');
22
+var pathToBabelPluginModules = require.resolve('fbjs-scripts/babel/rewrite-modules');
23
+
24
+// TODO: make sure this stays in sync with gulpfile
25
+var babelOptions = {
26
+ nonStandard: true,
27
+ blacklist: [
28
+ 'spec.functionName',
29
+ 'validation.react',
30
+ ],
31
+ optional: [
32
+ 'es7.trailingFunctionCommas',
33
+ ],
34
+ plugins: [babelPluginDEV, babelPluginModules],
35
+ retainLines: true,
36
+ _moduleMap: moduleMap,
37
+};
38
39
module.exports = {
40
process: function(src, filePath) {
@@ -21,24 +44,12 @@ module.exports = {
44
if (filePath.match(/\.ts$/) && !filePath.match(/\.d\.ts$/)) {
45
return tsPreprocessor.compile(src, filePath);
46
}
24
- // TODO: make sure this stays in sync with gulpfile
25
- if (!filePath.match(/\/node_modules\//) &&
26
- !filePath.match(/\/third_party\//)) {
27
- var rv = babel.transform(src, {
28
- nonStandard: true,
29
- blacklist: [
30
- 'spec.functionName',
31
- 'validation.react',
32
- ],
33
- optional: [
34
- 'es7.trailingFunctionCommas',
35
- ],
36
- plugins: [babelPluginDEV, babelPluginModules],
37
- filename: filePath,
38
- retainLines: true,
39
- _moduleMap: moduleMap,
40
- }).code;
41
-
47
+ if (
48
+ !filePath.match(/\/node_modules\//) &&
49
+ !filePath.match(/\/third_party\//)
50
+ ) {
51
+ var rv =
52
+ babel.transform(src, assign({filename: filePath}, babelOptions)).code;
53
// hax to turn fbjs/lib/foo into /path/to/node_modules/fbjs/lib/foo
54
// because jest is slooow with node_modules paths (facebook/jest#465)
55
rv = rv.replace(
@@ -58,4 +69,12 @@ module.exports = {
69
}
70
return src;
71
},
72
+
73
+ getCacheKey: createCacheKeyFunction([
74
+ __filename,
75
+ pathToBabel,
76
+ pathToModuleMap,
77
+ pathToBabelPluginDev,
78
+ pathToBabelPluginModules,
79
+ ]),
80
};