-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
entry-plugin.js
139 lines (125 loc) 路 3.8 KB
/
entry-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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// @flow
import corejs3Polyfills from "core-js-compat/data.json";
import corejsEntries from "core-js-compat/entries.json";
import getModulesListForTargetVersion from "core-js-compat/get-modules-list-for-target-version";
import { filterItems } from "@babel/helper-compilation-targets";
import {
has,
intersection,
createImport,
getImportSource,
getRequireSource,
getModulePath,
} from "../../utils";
import { logEntryPolyfills } from "../../debug";
import type { InternalPluginOptions } from "../../types";
import type { NodePath } from "@babel/traverse";
function isBabelPolyfillSource(source) {
return source === "@babel/polyfill" || source === "babel-polyfill";
}
function isCoreJSSource(source) {
if (typeof source === "string") {
source = source
.replace(/\\/g, "/")
.replace(/(\/(index)?)?(\.js)?$/i, "")
.toLowerCase();
}
return has(corejsEntries, source) && corejsEntries[source];
}
const BABEL_POLYFILL_DEPRECATION = `
\`@babel/polyfill\` is deprecated. Please, use required parts of \`core-js\`
and \`regenerator-runtime/runtime\` separately`;
export default function (
_: any,
{ corejs, include, exclude, polyfillTargets, debug }: InternalPluginOptions,
) {
const polyfills = filterItems(
corejs3Polyfills,
include,
exclude,
polyfillTargets,
null,
);
const available = new Set(getModulesListForTargetVersion(corejs.version));
function shouldReplace(source, modules) {
if (!modules) return false;
if (
// Don't replace an import with itself to avoid an infinite loop
modules.length === 1 &&
polyfills.has(modules[0]) &&
available.has(modules[0]) &&
getModulePath(modules[0]) === source
) {
return false;
}
return true;
}
const isPolyfillImport = {
ImportDeclaration(path: NodePath) {
const source = getImportSource(path);
if (!source) return;
if (isBabelPolyfillSource(source)) {
console.warn(BABEL_POLYFILL_DEPRECATION);
} else {
const modules = isCoreJSSource(source);
if (shouldReplace(source, modules)) {
this.replaceBySeparateModulesImport(path, modules);
}
}
},
Program: {
enter(path: NodePath) {
path.get("body").forEach(bodyPath => {
const source = getRequireSource(bodyPath);
if (!source) return;
if (isBabelPolyfillSource(source)) {
console.warn(BABEL_POLYFILL_DEPRECATION);
} else {
const modules = isCoreJSSource(source);
if (shouldReplace(source, modules)) {
this.replaceBySeparateModulesImport(bodyPath, modules);
}
}
});
},
exit(path: NodePath) {
const filtered = intersection(polyfills, this.polyfillsSet, available);
const reversed = Array.from(filtered).reverse();
for (const module of reversed) {
// Program:exit could be called multiple times.
// Avoid injecting the polyfills twice.
if (!this.injectedPolyfills.has(module)) {
createImport(path, module);
}
}
filtered.forEach(module => this.injectedPolyfills.add(module));
},
},
};
return {
name: "corejs3-entry",
visitor: isPolyfillImport,
pre() {
this.injectedPolyfills = new Set();
this.polyfillsSet = new Set();
this.replaceBySeparateModulesImport = function (path, modules) {
for (const module of modules) {
this.polyfillsSet.add(module);
}
path.remove();
};
},
post() {
if (debug) {
logEntryPolyfills(
"core-js",
this.injectedPolyfills.size > 0,
this.injectedPolyfills,
this.file.opts.filename,
polyfillTargets,
corejs3Polyfills,
);
}
},
};
}