diff --git a/.gitignore b/.gitignore index e173a04..5e8b6ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ node_modules/ +/bin /lib -zz* -*.tgz -.np-config.json diff --git a/package.json b/package.json index d4ae800..6fda13f 100644 --- a/package.json +++ b/package.json @@ -35,26 +35,23 @@ }, "type": "module", "bin": { - "ts-run": "./lib/index.js" + "ts-run": "./bin/index.js" }, "exports": { - ".": { - "types": "./register.d.ts", - "default": "./lib/index.js" - }, - "./register": { - "types": "./register.d.ts", - "default": "./lib/index.js" - } + "types": "./lib/register.d.ts", + "default": "./lib/register.js" }, "imports": { "#package.json": "./package.json" }, "scripts": { - "prebuild": "rimraf lib", + "prebuild": "rimraf bin lib", "build": "rollup --config", "watch": "npm run build -- --watch", - "test": "node test/check-node-21.js && node ./lib/index.js test/01.argv.test.ts && node --import=./lib/index.js --test test/**/*.test.{ts,mts,cts}" + "pretest": "npm run build", + "test": "npm run testOnly", + "testOnly": "node test/check-node-21.js && node --import=./lib/register.js --test test/**/*.test.{ts,mts,cts}", + "prepublishOnly": "npm run build" }, "devDependencies": { "@rollup/plugin-commonjs": "^28.0.0", @@ -70,9 +67,9 @@ "typescript": "^5.5.2" }, "files": [ + "./bin", "./lib", - "!*.map", - "register.d.ts" + "!*.map" ], "publishConfig": { "access": "public" diff --git a/register.d.ts b/register.d.ts deleted file mode 100644 index e991dba..0000000 --- a/register.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -/** - * This module does not export anything. It can only be imported for its side-effect - * (which is registering the custom loaders). - * - * `import "@septh/ts-run"` - */ -export {} diff --git a/rollup.config.js b/rollup.config.js index ea1b78c..b4a79dd 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -2,36 +2,70 @@ import path from 'node:path' import { readFile } from 'fs/promises' import { nodeExternals } from 'rollup-plugin-node-externals' import { nodeResolve } from '@rollup/plugin-node-resolve' -import commonsJS from '@rollup/plugin-commonjs' +import _commonsJS from '@rollup/plugin-commonjs' import { sucrase } from 'rollup-plugin-fast-typescript' -import terser from '@rollup/plugin-terser' +import _terser from '@rollup/plugin-terser' import { defineConfig } from 'rollup' +/** + * @template T + * @param {{ default: T }} f + * @see {@link https://github.com/rollup/plugins/issues/1541#issuecomment-1837153165} + */ +const fix = (f) => /** @type {T} */(f) +const commonJS = fix(_commonsJS) +const terser = fix(_terser) + export default defineConfig([ + + // bin/index.js + { + input: 'source/bin/index.ts', + output: { + file: 'bin/index.js', + format: 'esm', + sourcemap: true, + sourcemapExcludeSources: true + }, + plugins: [ + nodeExternals(), + sucrase() + ], + external: /\/register.js$/ + }, + + // lib/register.js, lib/esm-hooks.js { input: [ - 'source/index.ts', - 'source/esm-hooks.ts' + 'source/lib/register.ts', + 'source/lib/esm-hooks.ts' ], output: { dir: 'lib', format: 'esm', - generatedCode: 'es2015', sourcemap: true, sourcemapExcludeSources: true }, plugins: [ nodeExternals(), - sucrase() + sucrase(), + { + name: 'emit-esm-declaration-file', + async generateBundle() { + this.emitFile({ + type: 'asset', + fileName: 'register.d.ts', + source: await readFile('./source/lib/register.d.ts') + }) + } + } ], - // Have to use Rollup's external option as rollup-plugin-node-externals - // only applies to Node builtins and npm dependencies. - external: [ './transform.cjs' ] + external: /\/transform.cjs$/ }, - // This second configuration bundles Sucrase's parser to lib/transform.cjs + // lib/transform.cjs { - input: 'source/transform.cts', + input: 'source/lib/transform.cts', output: { file: 'lib/transform.cjs', format: 'commonjs', @@ -45,7 +79,7 @@ export default defineConfig([ plugins: [ nodeExternals(), nodeResolve(), - commonsJS(), + commonJS(), sucrase(), terser(), @@ -55,7 +89,7 @@ export default defineConfig([ name: 'fix-sucrase', async load(id) { return id.endsWith('computeSourceMap.js') - ? readFile(path.resolve('./source/fix-sucrase/computeSourceMap.js'), 'utf-8') + ? readFile(path.resolve('./source/lib/fix-sucrase/computeSourceMap.js'), 'utf-8') : null } } diff --git a/source/bin/index.ts b/source/bin/index.ts new file mode 100644 index 0000000..931e910 --- /dev/null +++ b/source/bin/index.ts @@ -0,0 +1,27 @@ +#!/usr/bin/env node +import { createRequire } from 'node:module' +import { resolve } from 'node:path' +import { pathToFileURL } from 'node:url' + +const { argv } = process +if (argv.length >= 3 && !argv[2].startsWith('-')) { + + // Get the name of the real