Get rid of warnings for dual esm and cjs packages of the same version (#1890)
* Get rid of warnings for dual esm and cjs packages of the same version * Add command line flag for suppressing ESM CJS warnings when all packages are of the same version
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { detectPackage } from './versionDetect.js';
|
||||
import { detectPackage, POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG } from './versionDetect.js';
|
||||
|
||||
describe('detectPackage', (): void => {
|
||||
const PKG = '@polkadot/util';
|
||||
@@ -70,3 +70,41 @@ The following conflicting packages were found:
|
||||
spy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('detectPackageEsmCjsNoWarnings', (): void => {
|
||||
const PKG = '@polkadot/wasm-crypto';
|
||||
const VER1 = '9.8.0-beta.45';
|
||||
const PATH = '/Users/jaco/Projects/polkadot-js/api/node_modules/@polkadot/api/node_modules/@polkadot/wasm-crypto';
|
||||
|
||||
it('should not log when there are concurrent esm and cjs versions of the same package with the same version number and warnings are disabled', (): void => {
|
||||
const spy = jest.spyOn(console, 'warn');
|
||||
const pkgEsm = { name: PKG, path: PATH, type: 'esm', version: VER1 };
|
||||
const pkgCjs = { name: PKG, path: `${PATH}/cjs`, type: 'cjs', version: VER1 };
|
||||
|
||||
process.env[POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG] = '1';
|
||||
detectPackage(pkgEsm, false, []);
|
||||
detectPackage(pkgCjs, false, []);
|
||||
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
spy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('detectPackageEsmCjs', (): void => {
|
||||
const PKG = '@polkadot/wasm-crypto-wasm';
|
||||
const VER1 = '9.8.0-beta.45';
|
||||
const PATH = '/Users/jaco/Projects/polkadot-js/api/node_modules/@polkadot/api/node_modules/@polkadot/wasm-crypto-wasm';
|
||||
|
||||
it('should log when there are concurrent esm and cjs versions of the same package with the same version number and warnings are not disabled', (): void => {
|
||||
const spy = jest.spyOn(console, 'warn');
|
||||
const pkgEsm = { name: PKG, path: PATH, type: 'esm', version: VER1 };
|
||||
const pkgCjs = { name: PKG, path: `${PATH}/cjs`, type: 'cjs', version: VER1 };
|
||||
|
||||
process.env[POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG] = undefined;
|
||||
detectPackage(pkgEsm, false, []);
|
||||
detectPackage(pkgCjs, false, []);
|
||||
|
||||
expect(spy).toHaveBeenCalled();
|
||||
spy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -26,6 +26,8 @@ type FnString = () => string | undefined;
|
||||
|
||||
const DEDUPE = 'Either remove and explicitly install matching versions or dedupe using your package manager.\nThe following conflicting packages were found:';
|
||||
|
||||
export const POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG = 'POLKADOTJS_DISABLE_ESM_CJS_WARNING';
|
||||
|
||||
/** @internal */
|
||||
function getEntry (name: string): VersionPath[] {
|
||||
const _global = xglobal as PjsGlobal;
|
||||
@@ -105,7 +107,7 @@ function warn <T extends { version: string }> (pre: string, all: T[], fmt: (vers
|
||||
/**
|
||||
* @name detectPackage
|
||||
* @summary Checks that a specific package is only imported once
|
||||
* @description A `@polkadot/*` version detection utility, checking for one occurence of a package in addition to checking for ddependency versions.
|
||||
* @description A `@polkadot/*` version detection utility, checking for one occurrence of a package in addition to checking for dependency versions.
|
||||
*/
|
||||
export function detectPackage ({ name, path, type, version }: PackageInfo, pathOrFn?: FnString | string | false | null, deps: PackageInfo[] = []): void {
|
||||
if (!name.startsWith('@polkadot')) {
|
||||
@@ -116,7 +118,14 @@ export function detectPackage ({ name, path, type, version }: PackageInfo, pathO
|
||||
|
||||
entry.push({ path: getPath(path, pathOrFn), type, version });
|
||||
|
||||
if (entry.length !== 1) {
|
||||
// if we have more than one entry at DIFFERENT version types then warn. If there is more than one entry at the same
|
||||
// version and ESM/CJS dual warnings are disabled, then do not display warnings
|
||||
const entriesSameVersion = entry.every((e) => e.version === version);
|
||||
const esmCjsWarningDisabled = POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG in process.env && process.env[POLKADOTJS_DISABLE_ESM_CJS_WARNING_FLAG] === '1';
|
||||
const multipleEntries = entry.length !== 1;
|
||||
const disableWarnings = esmCjsWarningDisabled && entriesSameVersion;
|
||||
|
||||
if (multipleEntries && !disableWarnings) {
|
||||
warn(`${name} has multiple versions, ensure that there is only one installed.`, entry, formatVersion);
|
||||
} else {
|
||||
const mismatches = deps.filter((d) => d && d.version !== version);
|
||||
|
||||
Reference in New Issue
Block a user