1 | ;
|
---|
2 | const {
|
---|
3 | getImportSource,
|
---|
4 | getRequireSource,
|
---|
5 | isPolyfillSource
|
---|
6 | } = require("./utils.cjs");
|
---|
7 | const BABEL_POLYFILL_DEPRECATION = `
|
---|
8 | \`@babel/polyfill\` is deprecated. Please, use required parts of \`core-js\`
|
---|
9 | and \`regenerator-runtime/runtime\` separately`;
|
---|
10 | const NO_DIRECT_POLYFILL_IMPORT = `
|
---|
11 | When setting \`useBuiltIns: 'usage'\`, polyfills are automatically imported when needed.
|
---|
12 | Please remove the direct import of \`SPECIFIER\` or use \`useBuiltIns: 'entry'\` instead.`;
|
---|
13 | module.exports = function ({
|
---|
14 | template
|
---|
15 | }, {
|
---|
16 | regenerator,
|
---|
17 | deprecated,
|
---|
18 | usage
|
---|
19 | }) {
|
---|
20 | return {
|
---|
21 | name: "preset-env/replace-babel-polyfill",
|
---|
22 | visitor: {
|
---|
23 | ImportDeclaration(path) {
|
---|
24 | const src = getImportSource(path);
|
---|
25 | if (usage && isPolyfillSource(src)) {
|
---|
26 | console.warn(NO_DIRECT_POLYFILL_IMPORT.replace("SPECIFIER", src));
|
---|
27 | if (!deprecated) path.remove();
|
---|
28 | } else if (src === "@babel/polyfill") {
|
---|
29 | if (deprecated) {
|
---|
30 | console.warn(BABEL_POLYFILL_DEPRECATION);
|
---|
31 | } else if (regenerator) {
|
---|
32 | path.replaceWithMultiple(template.ast`
|
---|
33 | import "core-js";
|
---|
34 | import "regenerator-runtime/runtime.js";
|
---|
35 | `);
|
---|
36 | } else {
|
---|
37 | path.replaceWith(template.ast`
|
---|
38 | import "core-js";
|
---|
39 | `);
|
---|
40 | }
|
---|
41 | }
|
---|
42 | },
|
---|
43 | Program(path) {
|
---|
44 | path.get("body").forEach(bodyPath => {
|
---|
45 | const src = getRequireSource(bodyPath);
|
---|
46 | if (usage && isPolyfillSource(src)) {
|
---|
47 | console.warn(NO_DIRECT_POLYFILL_IMPORT.replace("SPECIFIER", src));
|
---|
48 | if (!deprecated) bodyPath.remove();
|
---|
49 | } else if (src === "@babel/polyfill") {
|
---|
50 | if (deprecated) {
|
---|
51 | console.warn(BABEL_POLYFILL_DEPRECATION);
|
---|
52 | } else if (regenerator) {
|
---|
53 | bodyPath.replaceWithMultiple(template.ast`
|
---|
54 | require("core-js");
|
---|
55 | require("regenerator-runtime/runtime.js");
|
---|
56 | `);
|
---|
57 | } else {
|
---|
58 | bodyPath.replaceWith(template.ast`
|
---|
59 | require("core-js");
|
---|
60 | `);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | });
|
---|
64 | }
|
---|
65 | }
|
---|
66 | };
|
---|
67 | };
|
---|
68 |
|
---|
69 | //# sourceMappingURL=babel-polyfill.cjs.map
|
---|