New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn when the version specified in transform-runtime doesn't match #10325
base: master
Are you sure you want to change the base?
Changes from all commits
9c9bf17
ca20aad
fa5c79a
1ec175c
d247044
3dbae24
bb0a147
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,57 @@ export function hasMinVersion(minVersion, runtimeVersion) { | |
); | ||
} | ||
|
||
export function verifyRuntimeVersion( | ||
options, | ||
moduleName, | ||
runtimeVersion, | ||
actualRuntimeVersion, | ||
) { | ||
if (!actualRuntimeVersion) return; | ||
|
||
const minActualVersion = semver.minVersion(actualRuntimeVersion).version; | ||
const minVersion = semver.minVersion(runtimeVersion).version; | ||
|
||
if (minActualVersion === minVersion) return; | ||
|
||
const fixedOptions = JSON.stringify( | ||
{ | ||
plugins: [ | ||
[ | ||
"@babel/plugin-transform-runtime", | ||
{ ...options, version: minActualVersion }, | ||
], | ||
], | ||
}, | ||
null, | ||
2, | ||
).replace(/^/gm, " ".repeat(2)); | ||
|
||
if (semver.gt(minActualVersion, minVersion)) { | ||
console.warn( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be very hesitant to add new logging to a plugin in a non-major if it isn't opt-in. Some tools will treat any stderr output as an indication that something in the build has failed. |
||
`The installed version of "${moduleName}" (${actualRuntimeVersion}) is greater ` + | ||
`than the version specified in "@babel/transform-runtime"'s options ` + | ||
`(${options.version}). This difference won't cause any problem in runtime ` + | ||
`functionality, but it will make your compiled code larger since Babel ` + | ||
`can't rely on some new or modified helpers to be present in the installed ` + | ||
`"${moduleName}". For this reason, some helpers will be inlined in your code ` + | ||
`as if you weren't using "@babel/plugin-transform-runtime", leadng to bigger bundles.\n` + | ||
`To fix this problem, you can specify the correct version in ` + | ||
`"@babel/transform-runtime"'s options:\n\n${fixedOptions}`, | ||
); | ||
} else { | ||
console.warn( | ||
`The installed version of "${moduleName}" (${actualRuntimeVersion}) is lower ` + | ||
`than the version specified in "@babel/transform-runtime"'s options ` + | ||
`(${options.version}). For this reason, Babel will assume that some helpers ` + | ||
`are supported by the installed "${moduleName}" version, even if they ` + | ||
`might not actually be present.\n` + | ||
`To fix this problem, you must specify the correct version in ` + | ||
`"@babel/transform-runtime"'s options:\n\n${fixedOptions}`, | ||
); | ||
} | ||
} | ||
|
||
// Note: We can't use NodePath#couldBeBaseType because it doesn't support arrays. | ||
// Even if we added support for arrays, this package needs to be compatible with | ||
// ^7.0.0 so we can't rely on it. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
import resolve from "resolve"; | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import { addDefault, isModule } from "@babel/helper-module-imports"; | ||
import { types as t } from "@babel/core"; | ||
|
||
import getCoreJS2Definitions from "./runtime-corejs2-definitions"; | ||
import getCoreJS3Definitions from "./runtime-corejs3-definitions"; | ||
import { typeAnnotationToString } from "./helpers"; | ||
import { typeAnnotationToString, verifyRuntimeVersion } from "./helpers"; | ||
|
||
function resolveAbsoluteRuntime(moduleName: string, dirname: string) { | ||
try { | ||
|
@@ -27,6 +28,30 @@ function resolveAbsoluteRuntime(moduleName: string, dirname: string) { | |
} | ||
} | ||
|
||
function resolveRuntimeVersion( | ||
moduleName: string, | ||
modulePath: string, | ||
dirname: string, | ||
) { | ||
try { | ||
const runtimeDir = path.dirname( | ||
resolve.sync(`${modulePath}/package.json`, { | ||
basedir: dirname, | ||
}), | ||
); | ||
const pkgFilename = path.resolve(runtimeDir, "../../../package.json"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I'm not sure this is what I'd recommend for verifying this. Could we consider looking for the |
||
const pkg = JSON.parse(fs.readFileSync(pkgFilename, "utf8")); | ||
|
||
return ( | ||
pkg.dependencies?.[moduleName] || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is stage 3 now 🙏 |
||
pkg.devDependencies?.[moduleName] || | ||
pkg.peerDependencies?.[moduleName] | ||
); | ||
} catch {} | ||
|
||
return null; | ||
} | ||
|
||
function supportsStaticESM(caller) { | ||
return !!(caller && caller.supportsStaticESM); | ||
} | ||
|
@@ -189,6 +214,13 @@ export default declare((api, options, dirname) => { | |
); | ||
} | ||
|
||
verifyRuntimeVersion( | ||
options, | ||
moduleName, | ||
runtimeVersion, | ||
resolveRuntimeVersion(moduleName, modulePath, dirname), | ||
); | ||
|
||
return { | ||
name: "transform-runtime", | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"validateLogs": true, | ||
"plugins": [["transform-runtime", { "version": "7.2.0" }]] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dependencies": { | ||
"@babel/runtime": "^7.3.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
The installed version of "@babel/runtime" (^7.3.0) is greater than the version specified in "@babel/transform-runtime"'s options (7.2.0). This difference won't cause any problem in runtime functionality, but it will make your compiled code larger since Babel can't rely on some new or modified helpers to be present in the installed "@babel/runtime". For this reason, some helpers will be inlined in your code as if you weren't using "@babel/plugin-transform-runtime", leadng to bigger bundles. | ||
To fix this problem, you can specify the correct version in "@babel/transform-runtime"'s options: | ||
|
||
{ | ||
"plugins": [ | ||
[ | ||
"@babel/plugin-transform-runtime", | ||
{ | ||
"version": "7.3.0" | ||
} | ||
] | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"validateLogs": true, | ||
"plugins": [["@babel/plugin-transform-runtime", { "version": "7.5.0" }]] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dependencies": { | ||
"@babel/runtime": "^7.3.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
The installed version of "@babel/runtime" (^7.3.0) is lower than the version specified in "@babel/transform-runtime"'s options (7.5.0). For this reason, Babel will assume that some helpers are supported by the installed "@babel/runtime" version, even if they might not actually be present. | ||
To fix this problem, you must specify the correct version in "@babel/transform-runtime"'s options: | ||
|
||
{ | ||
"plugins": [ | ||
[ | ||
"@babel/plugin-transform-runtime", | ||
{ | ||
"version": "7.3.0" | ||
} | ||
] | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Treating semver ranges as anything other than ranges is asking for trouble I think. Avoiding logic like this is why I lean toward
intersects
for things inbabel/packages/babel-core/src/transformation/file/file.js
Line 191 in f0c2364
What we really want to know in this context is whether every possible version in
actualRuntimeVersion
is contained withinruntimeVersion
, I think? As in, every possible version of the runtime that could be installed by this package.json is valid given the semver range passed to the Babel plugin.This current logic would pass if for instance we had
since the minimum for both is
7.2.0
.I'm not sure if the semver module actually exposes and API for doing the comparison we need to do here, but we can certainly check.