fix: add compatibility checks w/ semver (#424)

* hotfix: `this.load` was added in Rollup `2.60.0`; don't run on older versions

- this is my bad, I didn't realize `this.load` came out _much_ later than `this.resolve`
  - c.f. https://github.com/rollup/rollup/blob/master/CHANGELOG.md#2600

- since we're backward-compatible w/ Rollup `1.26.3`, add in a version check for the type-only fix
  - the type-only fix will error out in Rollup versions less than `2.60.0`, so instead of erroring out, just skip this functionality

- TODO: should move back to using `semver` that was removed in 08d2f5bcad7cb9a9a0974f3707a27d29dd89da7c

* deps: add back `semver` and use for version checking

- reverts the `semver` removal in 08d2f5bcad7cb9a9a0974f3707a27d29dd89da7c
  - add back the TS version range check

- add a Rollup version range check, fixing an old issue
- check `this.load` Rollup version with `semver` instead of string comparison

- add `semver` to `external`s list
  - also remove `resolve` as it's no longer used as of 74f6761ff63863cdb3e92ca1f33640fe6f4fc7db
  - and re-organize the list so that Node built-ins are in one section while deps are in another
    - makes it clearer what we're marking as external

* dx: add a nice warning when `this.load` is not supported

- so it doesn't just silently skip the type-only fix on Rollup versions <2.60.0

* fix(test): add globals for strings that are replaced during build

- `TS_VERSION_RANGE`, `ROLLUP_VERSION_RANGE`, `RPT2_VERSION` were previously only defined during builds, so would cause tests to fail
  - add these vars as (namespaced) globals in the Jest config so that they can be used in tests too
  - if they don't exist on `global` (or if `global` doesn't exist), then use the built strings instead
  - see in-line comments for more details

- also reorder `re` placement to match the usage order in the code
- and fix lint issues (`no-string-literal`)

Co-authored-by: Eugene Zolenko <zolenkoe@gmail.com>
This commit is contained in:
Anton Gilgur 2022-09-20 15:15:10 -04:00 committed by GitHub
parent a1ae42b949
commit 3ef3289fc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 113 additions and 3295 deletions

2
dist/index.d.ts.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAoD,MAAM,QAAQ,CAAC;AAStF,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAMtC,aAAK,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAErC,OAAO,EAAE,WAAW,EAAE,CAAA;AAEtB,QAAA,MAAM,UAAU,EAAE,UAAU,CAAC,WAAW,CAmYvC,CAAC;AAEF,eAAe,UAAU,CAAC"}
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAoD,MAAM,QAAQ,CAAC;AAUtF,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAWtC,aAAK,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAErC,OAAO,EAAE,WAAW,EAAE,CAAA;AAEtB,QAAA,MAAM,UAAU,EAAE,UAAU,CAAC,WAAW,CA8YvC,CAAC;AAEF,eAAe,UAAU,CAAC"}

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,5 @@
const pkg = require("./package.json");
/** @type {import("ts-jest").InitialOptionsTsJest} */
const config = {
// ts-jest settings
@ -5,7 +7,11 @@ const config = {
globals: {
"ts-jest": {
tsconfig: "./tsconfig.test.json",
}
},
// other globals (unrelated to ts-jest) -- these are namespaced so they don't conflict with anything else
"rpt2__TS_VERSION_RANGE": pkg.peerDependencies.typescript,
"rpt2__ROLLUP_VERSION_RANGE": pkg.peerDependencies.rollup,
"rpt2__RPT2_VERSION": pkg.version,
},
// jest settings

120
package-lock.json generated
View File

@ -12,6 +12,7 @@
"@rollup/pluginutils": "^4.1.2",
"find-cache-dir": "^3.3.2",
"fs-extra": "^10.0.0",
"semver": "^7.3.7",
"tslib": "^2.4.0"
},
"devDependencies": {
@ -25,7 +26,7 @@
"@types/lodash": "4.14.161",
"@types/node": "8.0.47",
"@types/object-hash": "1.3.3",
"@types/semver": "7.1.0",
"@types/semver": "7.3.12",
"colors": "1.4.0",
"graphlib": "2.1.8",
"jest": "^28.0.0",
@ -1529,12 +1530,10 @@
}
},
"node_modules/@types/semver": {
"version": "7.1.0",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/node": "*"
}
"version": "7.3.12",
"resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.3.12.tgz",
"integrity": "sha512-WwA1MW0++RfXmCr12xeYOOC5baSC9mSb0ZqCquFzKhcoF4TvHu5MKOuXsncgZcpVFhB1pXd5hZmM0ryAoCp12A==",
"dev": true
},
"node_modules/@types/stack-utils": {
"version": "2.0.1",
@ -4260,21 +4259,6 @@
"integrity": "sha512-Fl7FuabXsJnV5Q1qIOQwx/sagGF18kogb4gpfcG4gjLBWO0WDiiz1ko/ExayuxE7InyQkBLkxRFG5oxY6Uu3Kg==",
"dev": true
},
"node_modules/jest-snapshot/node_modules/semver": {
"version": "7.3.7",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz",
"integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==",
"dev": true,
"dependencies": {
"lru-cache": "^6.0.0"
},
"bin": {
"semver": "bin/semver.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/jest-snapshot/node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
@ -4740,7 +4724,6 @@
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz",
"integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==",
"dev": true,
"dependencies": {
"yallist": "^4.0.0"
},
@ -5207,12 +5190,17 @@
"dev": true
},
"node_modules/semver": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
"dev": true,
"version": "7.3.7",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz",
"integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==",
"dependencies": {
"lru-cache": "^6.0.0"
},
"bin": {
"semver": "bin/semver"
"semver": "bin/semver.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/shebang-command": {
@ -5527,21 +5515,6 @@
}
}
},
"node_modules/ts-jest/node_modules/semver": {
"version": "7.3.7",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz",
"integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==",
"dev": true,
"dependencies": {
"lru-cache": "^6.0.0"
},
"bin": {
"semver": "bin/semver.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/tslib": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz",
@ -5584,6 +5557,15 @@
"node": ">=0.10.0"
}
},
"node_modules/tslint/node_modules/semver": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
"dev": true,
"bin": {
"semver": "bin/semver"
}
},
"node_modules/tslint/node_modules/tslib": {
"version": "1.13.0",
"dev": true,
@ -5768,8 +5750,7 @@
"node_modules/yallist": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==",
"dev": true
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="
},
"node_modules/yargs": {
"version": "17.4.1",
@ -6961,11 +6942,10 @@
}
},
"@types/semver": {
"version": "7.1.0",
"dev": true,
"requires": {
"@types/node": "*"
}
"version": "7.3.12",
"resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.3.12.tgz",
"integrity": "sha512-WwA1MW0++RfXmCr12xeYOOC5baSC9mSb0ZqCquFzKhcoF4TvHu5MKOuXsncgZcpVFhB1pXd5hZmM0ryAoCp12A==",
"dev": true
},
"@types/stack-utils": {
"version": "2.0.1",
@ -8971,15 +8951,6 @@
"integrity": "sha512-Fl7FuabXsJnV5Q1qIOQwx/sagGF18kogb4gpfcG4gjLBWO0WDiiz1ko/ExayuxE7InyQkBLkxRFG5oxY6Uu3Kg==",
"dev": true
},
"semver": {
"version": "7.3.7",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz",
"integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==",
"dev": true,
"requires": {
"lru-cache": "^6.0.0"
}
},
"supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
@ -9325,7 +9296,6 @@
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz",
"integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==",
"dev": true,
"requires": {
"yallist": "^4.0.0"
}
@ -9656,10 +9626,12 @@
"dev": true
},
"semver": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
"dev": true
"version": "7.3.7",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz",
"integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==",
"requires": {
"lru-cache": "^6.0.0"
}
},
"shebang-command": {
"version": "2.0.0",
@ -9879,17 +9851,6 @@
"make-error": "1.x",
"semver": "7.x",
"yargs-parser": "^20.x"
},
"dependencies": {
"semver": {
"version": "7.3.7",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz",
"integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==",
"dev": true,
"requires": {
"lru-cache": "^6.0.0"
}
}
}
},
"tslib": {
@ -9920,6 +9881,12 @@
"version": "1.1.1",
"dev": true
},
"semver": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
"dev": true
},
"tslib": {
"version": "1.13.0",
"dev": true
@ -10051,8 +10018,7 @@
"yallist": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==",
"dev": true
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="
},
"yargs": {
"version": "17.4.1",

View File

@ -35,6 +35,7 @@
"@rollup/pluginutils": "^4.1.2",
"find-cache-dir": "^3.3.2",
"fs-extra": "^10.0.0",
"semver": "^7.3.7",
"tslib": "^2.4.0"
},
"peerDependencies": {
@ -52,7 +53,7 @@
"@types/lodash": "4.14.161",
"@types/node": "8.0.47",
"@types/object-hash": "1.3.3",
"@types/semver": "7.1.0",
"@types/semver": "7.3.12",
"colors": "1.4.0",
"graphlib": "2.1.8",
"jest": "^28.0.0",

View File

@ -8,9 +8,8 @@ export default {
input: "src/index.ts",
external: [
// Node built-ins
"fs",
"fs-extra",
"resolve",
"crypto",
"path",
"constants",
@ -18,6 +17,9 @@ export default {
"util",
"assert",
"os",
// deps
"fs-extra",
"semver",
"@rollup/pluginutils",
],
@ -26,8 +28,9 @@ export default {
({
replaces:
{
"$RPT2_VERSION": pkg.version,
"$TS_VERSION_RANGE": pkg.peerDependencies.typescript,
"$ROLLUP_VERSION_RANGE": pkg.peerDependencies.rollup,
"$RPT2_VERSION": pkg.version,
},
}),
resolve({ jsnext: true, preferBuiltins: true, }),

View File

@ -3,6 +3,7 @@ import * as tsTypes from "typescript";
import { PluginImpl, InputOptions, TransformResult, SourceMap, Plugin } from "rollup";
import { normalizePath as normalize } from "@rollup/pluginutils";
import { blue, red, yellow, green } from "colors/safe";
import { satisfies } from "semver";
import findCacheDir from "find-cache-dir";
import { RollupContext, VerbosityLevel } from "./context";
@ -15,6 +16,11 @@ import { convertDiagnostic, printDiagnostics } from "./diagnostics";
import { TSLIB, TSLIB_VIRTUAL, tslibSource, tslibVersion } from "./tslib";
import { createFilter } from "./get-options-overrides";
// these use globals during testing and are substituted by rollup-plugin-re during builds
const TS_VERSION_RANGE = (global as any)?.rpt2__TS_VERSION_RANGE || "$TS_VERSION_RANGE";
const ROLLUP_VERSION_RANGE = (global as any)?.rpt2__ROLLUP_VERSION_RANGE || "$ROLLUP_VERSION_RANGE";
const RPT2_VERSION = (global as any)?.rpt2__ROLLUP_VERSION_RANGE || "$RPT2_VERSION";
type RPT2Options = Partial<IOptions>;
export { RPT2Options }
@ -22,6 +28,7 @@ export { RPT2Options }
const typescript: PluginImpl<RPT2Options> = (options) =>
{
let watchMode = false;
let supportsThisLoad = false;
let generateRound = 0;
let rollupOptions: InputOptions;
let context: RollupContext;
@ -129,7 +136,17 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
context.info(`tslib version: ${tslibVersion}`);
context.info(`rollup version: ${this.meta.rollupVersion}`);
context.info(`rollup-plugin-typescript2 version: $RPT2_VERSION`);
if (!satisfies(tsModule.version, TS_VERSION_RANGE, { includePrerelease: true }))
context.error(`Installed TypeScript version '${tsModule.version}' is outside of supported range '${TS_VERSION_RANGE}'`);
if (!satisfies(this.meta.rollupVersion, ROLLUP_VERSION_RANGE, { includePrerelease: true }))
context.error(`Installed Rollup version '${this.meta.rollupVersion}' is outside of supported range '${ROLLUP_VERSION_RANGE}'`);
supportsThisLoad = satisfies(this.meta.rollupVersion, ">=2.60.0", { includePrerelease : true }); // this.load is 2.60.0+ only (c.f. https://github.com/rollup/rollup/blob/master/CHANGELOG.md#2600)
if (!supportsThisLoad)
context.warn(() => `${yellow("You are using a Rollup version '<2.60.0'")}. This may result in type-only files being ignored.`);
context.info(`rollup-plugin-typescript2 version: ${RPT2_VERSION}`);
context.debug(() => `plugin options:\n${JSON.stringify(pluginOptions, (key, value) => key === "typescript" ? `version ${(value as typeof tsModule).version}` : value, 4)}`);
context.debug(() => `rollup config:\n${JSON.stringify(rollupOptions, undefined, 4)}`);
context.debug(() => `tsconfig path: ${tsConfigPath}`);
@ -253,7 +270,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
// handle all type-only imports by resolving + loading all of TS's references
// Rollup can't see these otherwise, because they are "emit-less" and produce no JS
if (result.references) {
if (result.references && supportsThisLoad) {
for (const ref of result.references) {
if (ref.endsWith(".d.ts"))
continue;