Skip to content

Commit

Permalink
fix(test): add globals for strings that are replaced during build
Browse files Browse the repository at this point in the history
- `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
  • Loading branch information
agilgur5 committed Sep 18, 2022
1 parent dd18e4b commit 4b2e74f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions dist/rollup-plugin-typescript2.cjs.js
Expand Up @@ -27929,6 +27929,10 @@ catch (e) {
throw e;
}

// these use globals during testing and are substituted by rollup-plugin-re during builds
const TS_VERSION_RANGE = (global === null || global === void 0 ? void 0 : global["rpt2__TS_VERSION_RANGE"]) || ">=2.4.0";
const ROLLUP_VERSION_RANGE = (global === null || global === void 0 ? void 0 : global["rpt2__ROLLUP_VERSION_RANGE"]) || ">=1.26.3";
const RPT2_VERSION = (global === null || global === void 0 ? void 0 : global["rpt2__ROLLUP_VERSION_RANGE"]) || "0.34.0";
const typescript = (options) => {
let watchMode = false;
let supportsThisLoad = false;
Expand Down Expand Up @@ -28012,14 +28016,14 @@ const typescript = (options) => {
context.info(`typescript version: ${tsModule.version}`);
context.info(`tslib version: ${tslibVersion}`);
context.info(`rollup version: ${this.meta.rollupVersion}`);
if (!require$$3.satisfies(tsModule.version, ">=2.4.0", { includePrerelease: true }))
context.error(`Installed TypeScript version '${tsModule.version}' is outside of supported range '>=2.4.0'`);
if (!require$$3.satisfies(this.meta.rollupVersion, ">=1.26.3", { includePrerelease: true }))
context.error(`Installed Rollup version '${this.meta.rollupVersion}' is outside of supported range '>=1.26.3'`);
if (!require$$3.satisfies(tsModule.version, TS_VERSION_RANGE, { includePrerelease: true }))
context.error(`Installed TypeScript version '${tsModule.version}' is outside of supported range '${TS_VERSION_RANGE}'`);
if (!require$$3.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 = require$$3.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(() => `${safe.exports.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: 0.34.0`);
context.info(`rollup-plugin-typescript2 version: ${RPT2_VERSION}`);
context.debug(() => `plugin options:\n${JSON.stringify(pluginOptions, (key, value) => key === "typescript" ? `version ${value.version}` : value, 4)}`);
context.debug(() => `rollup config:\n${JSON.stringify(rollupOptions, undefined, 4)}`);
context.debug(() => `tsconfig path: ${tsConfigPath}`);
Expand Down
2 changes: 1 addition & 1 deletion dist/rollup-plugin-typescript2.cjs.js.map

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions dist/rollup-plugin-typescript2.es.js
Expand Up @@ -27900,6 +27900,10 @@ catch (e) {
throw e;
}

// these use globals during testing and are substituted by rollup-plugin-re during builds
const TS_VERSION_RANGE = (global === null || global === void 0 ? void 0 : global["rpt2__TS_VERSION_RANGE"]) || ">=2.4.0";
const ROLLUP_VERSION_RANGE = (global === null || global === void 0 ? void 0 : global["rpt2__ROLLUP_VERSION_RANGE"]) || ">=1.26.3";
const RPT2_VERSION = (global === null || global === void 0 ? void 0 : global["rpt2__ROLLUP_VERSION_RANGE"]) || "0.34.0";
const typescript = (options) => {
let watchMode = false;
let supportsThisLoad = false;
Expand Down Expand Up @@ -27983,14 +27987,14 @@ const typescript = (options) => {
context.info(`typescript version: ${tsModule.version}`);
context.info(`tslib version: ${tslibVersion}`);
context.info(`rollup version: ${this.meta.rollupVersion}`);
if (!satisfies(tsModule.version, ">=2.4.0", { includePrerelease: true }))
context.error(`Installed TypeScript version '${tsModule.version}' is outside of supported range '>=2.4.0'`);
if (!satisfies(this.meta.rollupVersion, ">=1.26.3", { includePrerelease: true }))
context.error(`Installed Rollup version '${this.meta.rollupVersion}' is outside of supported range '>=1.26.3'`);
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(() => `${safe.exports.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: 0.34.0`);
context.info(`rollup-plugin-typescript2 version: ${RPT2_VERSION}`);
context.debug(() => `plugin options:\n${JSON.stringify(pluginOptions, (key, value) => key === "typescript" ? `version ${value.version}` : value, 4)}`);
context.debug(() => `rollup config:\n${JSON.stringify(rollupOptions, undefined, 4)}`);
context.debug(() => `tsconfig path: ${tsConfigPath}`);
Expand Down
2 changes: 1 addition & 1 deletion dist/rollup-plugin-typescript2.es.js.map

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion jest.config.js
@@ -1,11 +1,17 @@
const pkg = require("./package.json");

/** @type {import("ts-jest").InitialOptionsTsJest} */
const config = {
// ts-jest settings
preset: "ts-jest",
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
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.base.js
Expand Up @@ -28,9 +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, }),
Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Expand Up @@ -16,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 }
Expand Down Expand Up @@ -131,17 +136,17 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
context.info(`tslib version: ${tslibVersion}`);
context.info(`rollup version: ${this.meta.rollupVersion}`);

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(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'`);
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.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}`);
Expand Down

0 comments on commit 4b2e74f

Please sign in to comment.