-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjest-webpack.js
92 lines (80 loc) · 2.38 KB
/
jest-webpack.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
#!/usr/bin/env node
function versionInfo() {
return `jest-webpack ${require('./package.json').version}\n` +
`jest ${require('jest/package.json').version}\n` +
`webpack ${require('webpack/package.json').version}`;
}
function tryRequire(attempts) {
attempts = Array.from(arguments);
var err;
for (var i = 0; i < attempts.length; i++) {
var fn = attempts[i];
try {
var exports = fn();
return exports.default || exports;
}
catch (_) {err = _;}
}
throw err;
}
function run(argv, webpackConfig) {
var jestArgvPortion, webpackArgvPortion;
var webpackArgIndex =
~(~argv.lastIndexOf('--') || ~argv.lastIndexOf('--webpack'));
if (webpackArgIndex === -1) {
jestArgvPortion = argv;
webpackArgvPortion = [];
}
else {
jestArgvPortion = argv.slice(0, webpackArgIndex);
webpackArgvPortion = argv.slice(webpackArgIndex);
}
if (!webpackConfig) {
var webpackYargs = require('yargs/yargs')([]);
tryRequire(
function() {return require('webpack/bin/config-yargs');},
function() {return require('webpack-cli/bin/config-yargs');}
)(webpackYargs);
var webpackArgv = webpackYargs.parse(webpackArgvPortion);
webpackConfig = tryRequire(
function() {return require('webpack/bin/convert-argv');},
function() {return require('webpack-cli/bin/convert-argv');}
)(
webpackYargs, webpackArgv
);
}
if (typeof webpackConfig.then === 'function') {
webpackConfig.then(function(config) {run(argv, config);})
.catch(function(err) {
console.error(err.stack || err);
process.exit();
});
return;
}
if (Array.isArray(webpackConfig)) {
console.error(
'jest-webpack does not support webpack\'s multi-compiler array ' +
'configuration at this time.'
);
process.exit(1);
}
var main;
if (require('webpack/package.json').version.startsWith('1')) {
main = require('./webpack-1/jest-webpack.js');
}
else {
main = require('./src/jest-webpack.js');
}
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'test';
}
main(argv, webpackConfig);
}
if (process.mainModule === module) {
const jestWebpackYargs = require('yargs/yargs')(process.argv)
.reset()
.usage(`${versionInfo()}\n\nUsage: jest-webpack [jest options] [--[webpack] [webpack options]]`);
jestWebpackYargs.version(false).parse();
run(process.argv.slice(2));
}
module.exports = run;