Skip to content

Commit

Permalink
style: Typos, minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Septh committed May 17, 2024
1 parent 61be22c commit 7c8c379
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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) {
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions source/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' {
Expand Down
2 changes: 1 addition & 1 deletion source/cjs-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions source/esm-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ async function nearestPackageType(file: string): Promise<NodeJS.ModuleType> {
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')
Expand All @@ -71,17 +76,17 @@ 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)

// Determine the output format based on the file's extension
// 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'
)
Expand Down
2 changes: 1 addition & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeJS.InitializeHookData>('./esm-hooks.js', {
parentURL: self,
Expand Down
4 changes: 2 additions & 2 deletions source/transform.cts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { transform as sucrase, type Transform, type TransformResult } from 'sucrase'
import { transform as parse, type Transform, type TransformResult } from 'sucrase'

const transforms: Record<NodeJS.ModuleType, Transform[]> = {
commonjs: [ 'typescript', 'imports' ],
module: [ 'typescript' ]
}

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,
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// Output
"target": "ESNext",
"module": "ESNext",
"checkJs": true,
"noEmit": true,

// Libs
Expand All @@ -34,6 +33,7 @@
"noUnusedParameters": false,
"noFallthroughCasesInSwitch": false,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true
"isolatedModules": true,
"checkJs": true
}
}

0 comments on commit 7c8c379

Please sign in to comment.