-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (29 loc) · 937 Bytes
/
index.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
const escapeRegExp = require('lodash/escapeRegExp');
const semver = require('semver');
const webpack = require('webpack');
const getReplacePlugin = (module) => {
try {
const modPath = require.resolve(module);
const segment = escapeRegExp(modPath.slice(modPath.indexOf(module)));
return new webpack.NormalModuleReplacementPlugin(
new RegExp(segment, 'i'),
`${__dirname}/shims/${module}.js`,
);
} catch (err) {
return { apply() {} };
}
};
class UnpolyfillPlugin {
constructor({ reactVersion = '16.0.0' }) {
this.reactVersion = reactVersion;
this.replaceContext = getReplacePlugin('create-react-context');
this.replacePolyfill = getReplacePlugin('react-lifecycles-compat');
}
apply(compiler) {
if (semver.gte(this.reactVersion, '16.3.0')) {
this.replaceContext.apply(compiler);
this.replacePolyfill.apply(compiler);
}
}
}
module.exports = UnpolyfillPlugin;