Skip to content

Commit

Permalink
feat: throw error when targets are used with regenerator
Browse files Browse the repository at this point in the history
- the polyfill-regenerator plugin actually does not support targets and
  does not use them
  - it instead detects whether the `regeneratorRuntime` variable has
    been injected by `transform-regenerator`, which already supports
    targets
  - so targets are not needed twice in that sense

- instead of silently ignoring the targets option, error out when it is
  given as it is an unsupported option
  - this should hopefully make it easier for developers to realize
    what's going on when debugging issues with this (as I did)
  - but it shouldn't error if top-level targets are configured, as those
    aren't specific to this plugin
    - caveat: if a user specifies both and they are the same, there's
      currently no way to tell, so it won't error in that case
      - but will catch the common mistake

- add a test for this new error as well as a basic "harness" based on
  the one in `define-polyfill-provider`
  - remove `targets` from any fixtures that had them for this polyfill
    so that this test passes
    - and because they didn't do anything previously anyway
- also add a test that ensures that top-level targets still work
  - and change some fixtures to use top-level targets instead, ensuring
    that those fixture tests still pass as well
  • Loading branch information
agilgur5 committed Apr 8, 2022
1 parent 5636de3 commit e1760f0
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 26 deletions.
15 changes: 14 additions & 1 deletion packages/babel-plugin-polyfill-regenerator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ type Options = {
};
};

export default defineProvider<Options>(({ debug }, options) => {
export default defineProvider<Options>(({ debug, targets, babel }, options) => {
if (!shallowEqual(targets, babel.targets())) {
throw new Error(
"This plugin does not use the targets option. Only preset-env's targets" +
" or top-level targets need to be configured for this plugin to work." +
" See https://github.com/babel/babel-polyfills/issues/36 for more" +
" details",
);
}

const { [runtimeCompat]: { useBabelRuntime } = { useBabelRuntime: "" } } =
options;

Expand Down Expand Up @@ -39,3 +48,7 @@ export default defineProvider<Options>(({ debug }, options) => {

const isRegenerator = meta =>
meta.kind === "global" && meta.name === "regeneratorRuntime";

function shallowEqual(obj1: any, obj2: any) {
return JSON.stringify(obj1) === JSON.stringify(obj2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
[
"@@/polyfill-regenerator",
{
"method": "usage-global",
"targets": {
"node": 6
}
"method": "usage-global"
}
]
],
Expand All @@ -21,4 +18,4 @@
}
]
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
}
]
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
}
]
]
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
{
"targets": {
"chrome": 70
},
"plugins": [
[
"@@/polyfill-regenerator",
{
"method": "usage-pure",
"targets": {
"chrome": 70
}
"method": "usage-pure"
}
]
],
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"targets": {
"chrome": 70
}
"modules": false
}
]
]
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
{
"targets": {
"chrome": 40
},
"plugins": [
[
"@@/polyfill-regenerator",
{
"method": "usage-pure",
"targets": {
"chrome": 40
}
"method": "usage-pure"
}
]
],
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"targets": {
"chrome": 40
}
"modules": false
}
]
]
}
}
39 changes: 39 additions & 0 deletions packages/babel-plugin-polyfill-regenerator/test/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as babel from "@babel/core";
import polyfillRegenerator from "../lib";

function transform(code, babelOpts, pluginOpts) {
return babel.transformSync(code, {
configFile: false,
...babelOpts,
plugins: [[polyfillRegenerator, pluginOpts]],
});
}

describe("targets", () => {
it("is not supported", () => {
expect(() =>
transform(
"code",
{},
{
method: "usage-pure",
targets: { chrome: 50 },
},
),
).toThrow(/targets/);
});
});

describe("top-level targets", () => {
it("is supported", () => {
expect(() =>
transform(
"code",
{ targets: { chrome: 50 } },
{
method: "usage-pure",
},
),
).not.toThrow(/targets/);
});
});

0 comments on commit e1760f0

Please sign in to comment.