-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreprocessor.js
71 lines (51 loc) · 1.85 KB
/
preprocessor.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
'use strict';
var pkginfo = require('pkginfo');
var path = require('path');
// package.json key for webpack config
var JEST_WEBPACK_PKGCONFIG_KEY = 'jest-webpack-config';
// Using 'lib/webpack.web.js'
var webpackPackageJson = require('webpack/package.json');
var webpack = require('webpack/' + webpackPackageJson.web);
var MemoryFileSystem = require('memory-fs');
var EnhancedResolve = require('enhanced-resolve');
var SyncNodeJsInputFileSystem = EnhancedResolve.SyncNodeJsInputFileSystem;
// Store FS among preprocess calls
var memoryFs = new MemoryFileSystem();
var syncFs = new SyncNodeJsInputFileSystem();
// Will try load config for webpack
var retvieveWebpackConfig = function (filename) {
var jestWebpackCfg = {};
try {
var pkgrelpath = pkginfo.find(filename);
var filedir = path.dirname(filename);
var pkgpath = path.join(filedir, pkgrelpath);
var pkgdirname = path.dirname(pkgpath);
var pkg = require(pkgpath);
var jestWebpackConfigRelPath = pkg[JEST_WEBPACK_PKGCONFIG_KEY];
var jestWebpackConfigPath = path.join(pkgdirname, jestWebpackConfigRelPath);
jestWebpackCfg = require(jestWebpackConfigPath);
} catch (e) {
// probably not found. We can ignore it silently
}
return jestWebpackCfg;
};
module.exports = {
process: function (src, filename) {
if (filename.indexOf('__tests__') !== -1) {
return src;
}
var options = retvieveWebpackConfig(filename);
options.entry = filename;
options.target = 'node';
options.output = options.output || {};
options.output.path = '/';
options.output.libraryTarget = 'commonjs2';
options.output.filename = filename;
options.outputFileSystem = memoryFs;
options.inputFileSystem = syncFs;
webpack(options).run();
var content = '' + memoryFs.readFileSync(filename);
memoryFs.unlinkSync(filename);
return content;
}
};