mirror of
https://github.com/zebrajr/react.git
synced 2025-12-07 12:20:38 +01:00
Turns out jest is _incredibly_ slow at resolving require paths like `require('fbjs/lib/foo')`. Like several milliseconds per require. Really adds up when all our files require `invariant` and `warning`. Here's a temporary hack to make things fast again.
Test Plan:
```
npm test src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js
```
has a self-proclaimed runtime of ~8 seconds now instead of ~35 seconds.
69 lines
1.9 KiB
JavaScript
69 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
var path = require('path');
|
|
|
|
var babel = require('babel');
|
|
var coffee = require('coffee-script');
|
|
|
|
var tsPreprocessor = require('./ts-preprocessor');
|
|
|
|
var defaultLibraries = [
|
|
require.resolve('./jest.d.ts'),
|
|
require.resolve('../../src/isomorphic/modern/class/React.d.ts'),
|
|
];
|
|
|
|
var ts = tsPreprocessor(defaultLibraries);
|
|
|
|
// This assumes the module map has been built. This might not be safe.
|
|
// We should consider consuming this from a built fbjs module from npm.
|
|
var moduleMap = require('fbjs/module-map');
|
|
var babelPluginDEV = require('fbjs/scripts/babel/dev-expression');
|
|
var babelPluginModules = require('fbjs/scripts/babel/rewrite-modules');
|
|
|
|
module.exports = {
|
|
process: function(src, filePath) {
|
|
if (filePath.match(/\.coffee$/)) {
|
|
return coffee.compile(src, {'bare': true});
|
|
}
|
|
if (filePath.match(/\.ts$/) && !filePath.match(/\.d\.ts$/)) {
|
|
return ts.compile(src, filePath);
|
|
}
|
|
// TODO: make sure this stays in sync with gulpfile
|
|
if (!filePath.match(/\/node_modules\//) &&
|
|
!filePath.match(/\/third_party\//)) {
|
|
var rv = babel.transform(src, {
|
|
nonStandard: true,
|
|
blacklist: [
|
|
'spec.functionName',
|
|
'validation.react',
|
|
],
|
|
optional: [
|
|
'es7.trailingFunctionCommas',
|
|
],
|
|
plugins: [babelPluginDEV, babelPluginModules],
|
|
filename: filePath,
|
|
retainLines: true,
|
|
_moduleMap: moduleMap,
|
|
}).code;
|
|
|
|
// hax to turn fbjs/lib/foo into /path/to/node_modules/fbjs/lib/foo
|
|
// because jest is slooow with node_modules paths (facebook/jest#465)
|
|
rv = rv.replace(
|
|
/require\('(fbjs\/lib\/.+)'\)/g,
|
|
function(call, arg) {
|
|
return (
|
|
'require(' +
|
|
JSON.stringify(
|
|
path.join(__dirname, '../../node_modules', arg)
|
|
) +
|
|
')'
|
|
);
|
|
}
|
|
);
|
|
|
|
return rv;
|
|
}
|
|
return src;
|
|
},
|
|
};
|