Skip to content

Commit

Permalink
Create seperate testLoader and testCached for Node v22 compatibil…
Browse files Browse the repository at this point in the history
…ity with JSON assertions (#1149)

* Add test loader

* Change exec to testCached

* cleanup pathing for execution

* bump version

* lint

* fix failing test

* Fix rootEsm tests for JSON compat with 22
  • Loading branch information
TarikGul authored Nov 10, 2024
1 parent 6f7f893 commit fe50319
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.82.1

- Create seperate testLoader and testCached for Node v22 compatibility with JSON assertions


## 0.81.1

- Duplicate .d.ts files into cjs
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.81.2",
"version": "0.82.0",
"versions": {
"git": "0.81.2",
"npm": "0.81.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.81.2",
"version": "0.82.0",
"main": "./index.js",
"exports": {
"./globals.d.ts": "./src/globals.d.ts"
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.81.2",
"version": "0.82.0",
"main": "./index.js",
"exports": {
"./globals.d.ts": "./src/globals.d.ts"
Expand Down
13 changes: 13 additions & 0 deletions packages/dev-ts/src/testCached.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2017-2024 @polkadot/dev-ts authors & contributors
// SPDX-License-Identifier: Apache-2.0

// Adapted from: https://nodejs.org/api/esm.html#esm_transpiler_loader
//
// NOTE: This assumes the loader implementation for Node.js >= 18

import { loaderOptions } from './common.js';

loaderOptions.isCached = true;

export { resolve } from './resolver.js';
export { load } from './testLoader.js';
92 changes: 92 additions & 0 deletions packages/dev-ts/src/testLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2017-2024 @polkadot/dev-ts authors & contributors
// SPDX-License-Identifier: Apache-2.0

import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import ts from 'typescript';

import { EXT_TS_REGEX, loaderOptions } from './common.js';

interface Loaded {
format: 'commonjs' | 'module';
shortCircuit?: boolean;
source: string;
}

type NexLoad = (url: string, context: Record<string, unknown>) => Promise<Loaded>;

/**
* Load all TypeScript files, compile via tsc on-the-fly
**/
export async function load (url: string, context: Record<string, unknown>, nextLoad: NexLoad): Promise<Loaded> {
if (EXT_TS_REGEX.test(url)) {
// used the chained loaders to retrieve
const { source } = await nextLoad(url, {
...context,
format: 'module'
});

// This ensures there is support for Node v22 while also maintaining backwards compatibility for testing.
const modifiedSrc = Buffer.from(source.toString().replace(/assert\s*\{\s*type:\s*'json'\s*\}/g, 'with { type: \'json\' }'), 'utf-8');

// we use a hash of the source to determine caching
const sourceHash = `//# sourceHash=${crypto.createHash('sha256').update(modifiedSrc as unknown as string).digest('hex')}`;
const compiledFile = url.includes('/src/')
? fileURLToPath(
url
.replace(/\.tsx?$/, '.js')
.replace('/src/', '/build-loader/')
)
: null;

if (loaderOptions.isCached && compiledFile && fs.existsSync(compiledFile)) {
const compiled = fs.readFileSync(compiledFile, 'utf-8');

if (compiled.includes(sourceHash)) {
return {
format: 'module',
source: compiled
};
}
}

// compile via typescript
const { outputText } = ts.transpileModule(modifiedSrc.toString(), {
compilerOptions: {
...(
url.endsWith('.tsx')
? { jsx: ts.JsxEmit.ReactJSX }
: {}
),
esModuleInterop: true,
importHelpers: true,
inlineSourceMap: true,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.NodeNext,
skipLibCheck: true,
// Aligns with packages/dev/scripts/polkadot-dev-build-ts & packages/dev/config/tsconfig
target: ts.ScriptTarget.ES2022
},
fileName: fileURLToPath(url)
});

if (loaderOptions.isCached && compiledFile) {
const compiledDir = path.dirname(compiledFile);

if (!fs.existsSync(compiledDir)) {
fs.mkdirSync(compiledDir, { recursive: true });
}

fs.writeFileSync(compiledFile, `${outputText}\n${sourceHash}`, 'utf-8');
}

return {
format: 'module',
source: outputText
};
}

return nextLoad(url, context);
}
6 changes: 3 additions & 3 deletions packages/dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.81.2",
"version": "0.82.0",
"bin": {
"polkadot-ci-ghact-build": "./scripts/polkadot-ci-ghact-build.mjs",
"polkadot-ci-ghact-docs": "./scripts/polkadot-ci-ghact-docs.mjs",
Expand Down Expand Up @@ -50,8 +50,8 @@
},
"dependencies": {
"@eslint/js": "^8.56.0",
"@polkadot/dev-test": "^0.81.2",
"@polkadot/dev-ts": "^0.81.2",
"@polkadot/dev-test": "^0.82.0",
"@polkadot/dev-ts": "^0.82.0",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-dynamic-import-vars": "^2.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/dev/scripts/polkadot-dev-run-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ try {
: `@polkadot/dev-test/${testEnv}`
);

execNodeTs(allFlags, nodeFlags, false, isDev ? './packages/dev-ts/build/cached.js' : undefined);
execNodeTs(allFlags, nodeFlags, false, isDev ? './packages/dev-ts/build/testCached.js' : '@polkadot/dev-ts/testCached');
} catch {
process.exit(1);
}
14 changes: 4 additions & 10 deletions packages/dev/src/rootEsm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@

/// <reference types="@polkadot/dev-test/globals.d.ts" />

import type * as testRoot from './root.js';

import fs from 'node:fs';
import path from 'node:path';

// NOTE We don't use ts-expect-error here since the build folder may or may
// not exist (so the error may or may not be there)
//
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore This should only run against the compiled ouput, where this should exist
import * as testRootBuild from '../build/root.js';
import * as testRoot from './root.js';
import { runTests } from './rootTests.js';

runTests(testRootBuild as unknown as typeof testRoot);
runTests(testRoot);

describe('as-built output checks', (): void => {
const buildRoot = path.join(process.cwd(), 'packages/dev/build');
Expand Down Expand Up @@ -94,7 +87,8 @@ describe('as-built output checks', (): void => {
jsIdx[type].includes(
type === 'cjs'
? 'require("@polkadot/dev/rootJs/testJson.json")'
: "import testJson from '@polkadot/dev/rootJs/testJson.json' assert { type: 'json' };"
// eslint-disable-next-line no-useless-escape
: "import testJson from '@polkadot/dev/rootJs/testJson.json' assert { type: \'json\' };"
)
).toBe(true);
})
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ __metadata:
languageName: node
linkType: hard

"@polkadot/dev-test@npm:^0.81.2, @polkadot/dev-test@workspace:packages/dev-test":
"@polkadot/dev-test@npm:^0.82.0, @polkadot/dev-test@workspace:packages/dev-test":
version: 0.0.0-use.local
resolution: "@polkadot/dev-test@workspace:packages/dev-test"
dependencies:
Expand All @@ -443,7 +443,7 @@ __metadata:
languageName: unknown
linkType: soft

"@polkadot/dev-ts@npm:^0.81.2, @polkadot/dev-ts@workspace:packages/dev-ts":
"@polkadot/dev-ts@npm:^0.82.0, @polkadot/dev-ts@workspace:packages/dev-ts":
version: 0.0.0-use.local
resolution: "@polkadot/dev-ts@workspace:packages/dev-ts"
dependencies:
Expand All @@ -458,8 +458,8 @@ __metadata:
resolution: "@polkadot/dev@workspace:packages/dev"
dependencies:
"@eslint/js": "npm:^8.56.0"
"@polkadot/dev-test": "npm:^0.81.2"
"@polkadot/dev-ts": "npm:^0.81.2"
"@polkadot/dev-test": "npm:^0.82.0"
"@polkadot/dev-ts": "npm:^0.82.0"
"@rollup/plugin-alias": "npm:^5.1.0"
"@rollup/plugin-commonjs": "npm:^25.0.7"
"@rollup/plugin-dynamic-import-vars": "npm:^2.1.2"
Expand Down

0 comments on commit fe50319

Please sign in to comment.