-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create seperate
testLoader
and testCached
for Node v22 compatibil…
…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
Showing
10 changed files
with
125 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters