From 7f8ded9851245beb89935702579c3e7fb3653703 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Wed, 20 Mar 2019 19:47:30 -0500 Subject: [PATCH] Tweak preset-env corejs/useBuiltIns warning and error messages (#9716) --- .../babel-preset-env/src/normalize-options.js | 21 ++++++++++++++++--- .../corejs-without-usebuiltins/stdout.txt | 2 +- .../entry-no-corejs-no-import/stdout.txt | 7 ++++++- .../stdout.txt | 7 ++++++- .../entry-no-corejs-uglify/stdout.txt | 7 ++++++- .../debug-fixtures/entry-no-corejs/stdout.txt | 7 ++++++- .../usage-no-corejs-none/stdout.txt | 7 ++++++- .../debug-fixtures/usage-no-corejs/stdout.txt | 7 ++++++- .../test/normalize-options.spec.js | 20 ++++++++++++++++++ 9 files changed, 75 insertions(+), 10 deletions(-) diff --git a/packages/babel-preset-env/src/normalize-options.js b/packages/babel-preset-env/src/normalize-options.js index 0320da9574..026ac96e83 100644 --- a/packages/babel-preset-env/src/normalize-options.js +++ b/packages/babel-preset-env/src/normalize-options.js @@ -204,7 +204,17 @@ export function normalizeCoreJSOption( if (useBuiltIns && corejs === undefined) { rawVersion = 2; console.log( - "\nWith `useBuiltIns` option, required direct setting of `corejs` option\n", + "\nWe noticed you're using the `useBuiltIns` option without declaring a " + + "core-js version. Currently, we assume version 2.x when no version " + + "is passed. Since this default version will likely change in future " + + "versions of Babel, we recommend explicitly setting the core-js version " + + "you are using via the `corejs` option.\n" + + "\nYou should also be sure that the version you pass to the `corejs` " + + "option matches the version specified in your `package.json`'s " + + "`dependencies` section. If it doesn't, you need to run one of the " + + "following commands:\n\n" + + " npm install --save core-js@2 npm install --save core-js@3\n" + + " yarn add core-js@2 yarn add core-js@3\n", ); } else if (typeof corejs === "object" && corejs !== null) { rawVersion = corejs.version; @@ -216,11 +226,16 @@ export function normalizeCoreJSOption( const version = rawVersion ? coerce(String(rawVersion)) : false; if (!useBuiltIns && version) { - console.log("\n`corejs` option required only with `useBuiltIns` option\n"); + console.log( + "\nThe `corejs` option only has an effect when the `useBuiltIns` option is not `false`\n", + ); } if (useBuiltIns && (!version || version.major < 2 || version.major > 3)) { - throw new RangeError("Supported only core-js@2 and core-js@3."); + throw new RangeError( + "Invalid Option: The version passed to `corejs` is invalid. Currently, " + + "only core-js@2 and core-js@3 are supported.", + ); } return { version, proposals }; diff --git a/packages/babel-preset-env/test/debug-fixtures/corejs-without-usebuiltins/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/corejs-without-usebuiltins/stdout.txt index 019f66f3f5..a912835c5a 100644 --- a/packages/babel-preset-env/test/debug-fixtures/corejs-without-usebuiltins/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/corejs-without-usebuiltins/stdout.txt @@ -1,4 +1,4 @@ -`corejs` option required only with `useBuiltIns` option +The `corejs` option only has an effect when the `useBuiltIns` option is not `false` @babel/preset-env: `DEBUG` option diff --git a/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-no-import/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-no-import/stdout.txt index 4987bd443c..d16a002045 100644 --- a/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-no-import/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-no-import/stdout.txt @@ -1,4 +1,9 @@ -With `useBuiltIns` option, required direct setting of `corejs` option +We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option. + +You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands: + + npm install --save core-js@2 npm install --save core-js@3 + yarn add core-js@2 yarn add core-js@3 @babel/preset-env: `DEBUG` option diff --git a/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-shippedProposals/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-shippedProposals/stdout.txt index dc07d391aa..e338c6d932 100644 --- a/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-shippedProposals/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-shippedProposals/stdout.txt @@ -1,4 +1,9 @@ -With `useBuiltIns` option, required direct setting of `corejs` option +We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option. + +You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands: + + npm install --save core-js@2 npm install --save core-js@3 + yarn add core-js@2 yarn add core-js@3 @babel/preset-env: `DEBUG` option diff --git a/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-uglify/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-uglify/stdout.txt index d5b0639d7c..31c42932de 100644 --- a/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-uglify/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs-uglify/stdout.txt @@ -1,4 +1,9 @@ -With `useBuiltIns` option, required direct setting of `corejs` option +We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option. + +You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands: + + npm install --save core-js@2 npm install --save core-js@3 + yarn add core-js@2 yarn add core-js@3 The uglify target has been deprecated. Set the top level diff --git a/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs/stdout.txt index f9bd620ce4..5d7438003c 100644 --- a/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/entry-no-corejs/stdout.txt @@ -1,4 +1,9 @@ -With `useBuiltIns` option, required direct setting of `corejs` option +We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option. + +You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands: + + npm install --save core-js@2 npm install --save core-js@3 + yarn add core-js@2 yarn add core-js@3 @babel/preset-env: `DEBUG` option diff --git a/packages/babel-preset-env/test/debug-fixtures/usage-no-corejs-none/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/usage-no-corejs-none/stdout.txt index 44fbb60277..13cb1284bd 100644 --- a/packages/babel-preset-env/test/debug-fixtures/usage-no-corejs-none/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/usage-no-corejs-none/stdout.txt @@ -1,4 +1,9 @@ -With `useBuiltIns` option, required direct setting of `corejs` option +We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option. + +You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands: + + npm install --save core-js@2 npm install --save core-js@3 + yarn add core-js@2 yarn add core-js@3 @babel/preset-env: `DEBUG` option diff --git a/packages/babel-preset-env/test/debug-fixtures/usage-no-corejs/stdout.txt b/packages/babel-preset-env/test/debug-fixtures/usage-no-corejs/stdout.txt index f0f6376575..197bbf297e 100644 --- a/packages/babel-preset-env/test/debug-fixtures/usage-no-corejs/stdout.txt +++ b/packages/babel-preset-env/test/debug-fixtures/usage-no-corejs/stdout.txt @@ -1,4 +1,9 @@ -With `useBuiltIns` option, required direct setting of `corejs` option +We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option. + +You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands: + + npm install --save core-js@2 npm install --save core-js@3 + yarn add core-js@2 yarn add core-js@3 @babel/preset-env: `DEBUG` option diff --git a/packages/babel-preset-env/test/normalize-options.spec.js b/packages/babel-preset-env/test/normalize-options.spec.js index 81817d06c6..eefb69e45f 100644 --- a/packages/babel-preset-env/test/normalize-options.spec.js +++ b/packages/babel-preset-env/test/normalize-options.spec.js @@ -64,6 +64,26 @@ describe("normalize-options", () => { ).toThrowError(/were found in both/); }, ); + + it("should not throw if corejs version is valid", () => { + [2, 2.1, 3, 3.5].forEach(corejs => { + ["entry", "usage"].forEach(useBuiltIns => { + expect(() => + normalizeOptions.default({ useBuiltIns, corejs }), + ).not.toThrowError(); + }); + }); + }); + + it("should throw if corejs version is invalid", () => { + [1, 1.2, 4, 4.5].forEach(corejs => { + ["entry", "usage"].forEach(useBuiltIns => { + expect(() => + normalizeOptions.default({ useBuiltIns, corejs }), + ).toThrowError(/The version passed to `corejs` is invalid./); + }); + }); + }); }); describe("Config format validation", () => {