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