-
Notifications
You must be signed in to change notification settings - Fork 3
/
Plugin.js
75 lines (65 loc) · 2.68 KB
/
Plugin.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
'use strict';
const ConstDependency = require('webpack/lib/dependencies/ConstDependency');
/**
* Resolves module paths.
* @param {Object} expr The expression.
* @param {Number} argIndex Which argument to resolve, defaults to the first arg.
*/
function resolveArgument(expr, argIndex) {
if (typeof argIndex === 'undefined') {
argIndex = 0;
}
if (!expr.arguments.length || !expr.arguments[argIndex]) {
return;
}
this.applyPluginsBailResult('call require:commonjs:item', expr,
this.evaluateExpression(expr.arguments[argIndex]));
}
/**
* Adds support for Jest API.
* e.g. jest.dontMock('./foo') => jest.dontMock(1);
* @constructor
*/
class JestpackPlugin {
/**
* Resolves Jest API `moduleName` arguments and updates the Webpack runtime.
* @param {Object} compiler The Webpack compiler.
*/
apply(compiler) {
compiler.parser.plugin('call jest.dontMock', resolveArgument);
compiler.parser.plugin('call jest.mock', resolveArgument);
compiler.parser.plugin('call jest.genMockFromModule', resolveArgument);
compiler.parser.plugin('call jest.setMock', resolveArgument);
compiler.parser.plugin('call jest._registerManualMock', function(expr) {
resolveArgument.call(this, expr);
resolveArgument.call(this, expr, 1);
});
compiler.parser.plugin('call require.requireActual', resolveArgument);
compiler.parser.plugin('expression require.requireActual', function(expr) {
var dep = new ConstDependency('__webpack_require__.requireActual', expr.range);
dep.loc = expr.loc;
this.state.current.addDependency(dep);
return true;
});
compiler.plugin('compilation', compilation => {
// Replace `__webpack_require__` with `require` as defined by Jest's JSDom env.
compilation.mainTemplate.plugin('module-require', () => {
return 'jest._webpackRequire';
});
// Make all references to the module cache global so 'jestpack/ModuleLoader'
// can manage the object *and the reference*.
compilation.mainTemplate.plugin('local-vars', source => {
return source.replace('var installedModules', 'window.installedModules');
});
// Provide `__webpack_require__` to 'jestpack/ModuleLoader'.
compilation.mainTemplate.plugin('startup', source => {
return [
'',
'jest._setupWebpackRequire(__webpack_require__);',
source
].join('\n');
});
});
}
}
module.exports = JestpackPlugin;