From 7c8c379745e25ccee88337267ab2ae266860954d Mon Sep 17 00:00:00 2001 From: Stephan Schreiber Date: Fri, 17 May 2024 07:52:53 +0200 Subject: [PATCH] style: Typos, minor adjustments --- README.md | 2 +- rollup.config.js | 6 +++--- source/ambient.d.ts | 10 +++++----- source/cjs-hooks.ts | 2 +- source/esm-hooks.ts | 13 +++++++++---- source/index.ts | 2 +- source/transform.cts | 4 ++-- tsconfig.json | 4 ++-- 8 files changed, 24 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 90b8559..75acb7d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # ts-run > The minimalist TypeScript script runner for NodeJS. -Runs TypeScript scripts from the command line as if they were written in plain JavaScript: +Run TypeScript scripts from the command line as if they were written in plain JavaScript: ```sh ts-run ./some-script.ts diff --git a/rollup.config.js b/rollup.config.js index 0199b91..8c8f21a 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -33,7 +33,7 @@ export default defineConfig([ external: /(?:esm|cjs)-hooks.js$/ }, - // This second configuration bundles Sucrase's parser to lib/cjs-transform.cjs + // This second configuration bundles Sucrase's parser to lib/transform.cjs { input: 'source/transform.cts', output: { @@ -54,7 +54,7 @@ export default defineConfig([ terser(), // This plugin fixes https://github.com/alangpierce/sucrase/issues/825 - // by replacing sucrase's computeSourceMap.js with our own. + // by replacing Sucrase's computeSourceMap.js with our own. { name: 'fix-sucrase', async load(id) { @@ -64,7 +64,7 @@ export default defineConfig([ } } ], - // sucrase has MANY circular dependencies :/ + // Sucrase has MANY circular dependencies :/ onLog(level, log, handler) { if (log.code === 'CIRCULAR_DEPENDENCY') return diff --git a/source/ambient.d.ts b/source/ambient.d.ts index d73cce1..28e7833 100644 --- a/source/ambient.d.ts +++ b/source/ambient.d.ts @@ -2,11 +2,6 @@ declare global { namespace NodeJS { type ModuleType = 'commonjs' | 'module' - // In package.json. - interface PackageType { - type?: ModuleType - } - // The data passed to the initialize() hook. interface InitializeHookData { self: string @@ -17,6 +12,11 @@ declare global { _compile(code: string, filename: string): string } } + + // Fields of interest in package.json. + interface PackageJson { + type?: NodeJS.ModuleType + } } declare module 'module' { diff --git a/source/cjs-hooks.ts b/source/cjs-hooks.ts index 221e5dc..5c4e4d6 100644 --- a/source/cjs-hooks.ts +++ b/source/cjs-hooks.ts @@ -30,7 +30,7 @@ function nearestPackageType(file: string, defaultType: NodeJS.ModuleType): NodeJ if (!format) { try { const data = readFileSync(pkgFile, 'utf-8') - const { type } = JSON.parse(data) as NodeJS.PackageType + const { type } = JSON.parse(data) as PackageJson format = type === 'module' || type ==='commonjs' ? type : unknownType diff --git a/source/esm-hooks.ts b/source/esm-hooks.ts index f20bd18..4d80130 100644 --- a/source/esm-hooks.ts +++ b/source/esm-hooks.ts @@ -50,7 +50,12 @@ async function nearestPackageType(file: string): Promise { let format = pkgTypeCache.get(pkgFile) if (!format) { format = await readFile(pkgFile, 'utf-8') - .then(data => (JSON.parse(data) as NodeJS.PackageType).type ?? unknownType) + .then(data => { + const { type } = JSON.parse(data) as PackageJson + return type === 'module' || type === 'commonjs' + ? type + : unknownType + }) .catch(err => { const { code } = err as NodeJS.ErrnoException if (code !== 'ENOENT') @@ -71,7 +76,7 @@ export const load: LoadHook = async (url, context, nextLoad) => { // If this is not a TypeScript file, defer to the next hook in the chain. const { protocol, pathname } = new URL(url) - const ext = /(\.[cm]?ts)$/.exec(pathname) + const [ , ext ] = /(\.[cm]?ts)$/.exec(pathname) ?? [] if (protocol !== 'file:' || !ext) return nextLoad(url, context) @@ -79,9 +84,9 @@ export const load: LoadHook = async (url, context, nextLoad) => { // or the nearest package.json's `type` field. const filePath = fileURLToPath(url) const format: NodeJS.ModuleType = ( - ext[1] === '.ts' + ext === '.ts' ? await nearestPackageType(filePath) - : ext[1] === '.mts' + : ext === '.mts' ? 'module' : 'commonjs' ) diff --git a/source/index.ts b/source/index.ts index b10433b..ffe1a0d 100644 --- a/source/index.ts +++ b/source/index.ts @@ -24,7 +24,7 @@ if ( defaultModuleType = type } - // Install the esm hooks -- those are run in a worker thread. + // Register the esm hooks -- those are run in a worker thread. const self = import.meta.url Module.register('./esm-hooks.js', { parentURL: self, diff --git a/source/transform.cts b/source/transform.cts index 140e88c..6c11065 100644 --- a/source/transform.cts +++ b/source/transform.cts @@ -1,4 +1,4 @@ -import { transform as sucrase, type Transform, type TransformResult } from 'sucrase' +import { transform as parse, type Transform, type TransformResult } from 'sucrase' const transforms: Record = { commonjs: [ 'typescript', 'imports' ], @@ -6,7 +6,7 @@ const transforms: Record = { } export function transform(source: string, format: NodeJS.ModuleType, filePath: string) { - const { code, sourceMap } = sucrase(source, { + const { code, sourceMap } = parse(source, { filePath, transforms: transforms[format], preserveDynamicImport: true, diff --git a/tsconfig.json b/tsconfig.json index ce5636f..680aa0f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,7 +15,6 @@ // Output "target": "ESNext", "module": "ESNext", - "checkJs": true, "noEmit": true, // Libs @@ -34,6 +33,7 @@ "noUnusedParameters": false, "noFallthroughCasesInSwitch": false, "forceConsistentCasingInFileNames": true, - "isolatedModules": true + "isolatedModules": true, + "checkJs": true } }