diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/webgpu/tests/cts/checkout/src/common/tools/setup-ts-in-node.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | dom/webgpu/tests/cts/checkout/src/common/tools/setup-ts-in-node.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/dom/webgpu/tests/cts/checkout/src/common/tools/setup-ts-in-node.js b/dom/webgpu/tests/cts/checkout/src/common/tools/setup-ts-in-node.js new file mode 100644 index 0000000000..89e91e8c9d --- /dev/null +++ b/dom/webgpu/tests/cts/checkout/src/common/tools/setup-ts-in-node.js @@ -0,0 +1,51 @@ +const path = require('path'); + +// Automatically transpile .ts imports +require('ts-node').register({ + // Specify the project file so ts-node doesn't try to find it itself based on the CWD. + project: path.resolve(__dirname, '../../../tsconfig.json'), + compilerOptions: { + module: 'commonjs', + }, + transpileOnly: true, +}); +const Module = require('module'); + +// Redirect imports of .js files to .ts files +const resolveFilename = Module._resolveFilename; +Module._resolveFilename = (request, parentModule, isMain) => { + do { + if (request.startsWith('.') && parentModule.filename.endsWith('.ts')) { + // Required for browser (because it needs the actual correct file path and + // can't do any kind of file resolution). + if (request.endsWith('/index.js')) { + throw new Error( + "Avoid the name `index.js`; we don't have Node-style path resolution: " + request + ); + } + + // Import of Node addon modules are valid and should pass through. + if (request.endsWith('.node')) { + break; + } + + if (!request.endsWith('.js')) { + throw new Error('All relative imports must end in .js: ' + request); + } + + try { + const tsRequest = request.substring(0, request.length - '.js'.length) + '.ts'; + return resolveFilename.call(this, tsRequest, parentModule, isMain); + } catch (ex) { + // If the .ts file doesn't exist, try .js instead. + break; + } + } + } while (0); + + return resolveFilename.call(this, request, parentModule, isMain); +}; + +process.on('unhandledRejection', ex => { + throw ex; +}); |