Skip to content

Commit

Permalink
add @tapjs/tsx plugin
Browse files Browse the repository at this point in the history
Fix: #965
  • Loading branch information
isaacs committed Nov 15, 2023
1 parent 639f3f2 commit dd4f233
Show file tree
Hide file tree
Showing 18 changed files with 519 additions and 3 deletions.
44 changes: 41 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"tap": "^18.0.0-0",
"trivial-deferred": "^2.0.0",
"tshy": "^1.2.2",
"tsx": "^4.1.2",
"typedoc": "^0.25.1",
"typescript": "5.2",
"walk-up-path": "^3.0.1",
Expand Down
9 changes: 9 additions & 0 deletions src/tsx/.tshy/build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "../src",
"target": "es2022",
"module": "nodenext",
"moduleResolution": "nodenext"
}
}
14 changes: 14 additions & 0 deletions src/tsx/.tshy/commonjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./build.json",
"include": [
"../src/**/*.ts",
"../src/**/*.cts",
"../src/**/*.tsx"
],
"exclude": [
".../src/**/*.mts"
],
"compilerOptions": {
"outDir": "../.tshy-build-tmp/commonjs"
}
}
11 changes: 11 additions & 0 deletions src/tsx/.tshy/esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./build.json",
"include": [
"../src/**/*.ts",
"../src/**/*.mts",
"../src/**/*.tsx"
],
"compilerOptions": {
"outDir": "../.tshy-build-tmp/esm"
}
}
55 changes: 55 additions & 0 deletions src/tsx/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Blue Oak Model License

Version 1.0.0

## Purpose

This license gives everyone as much permission to work with
this software as possible, while protecting contributors
from liability.

## Acceptance

In order to receive this license, you must agree to its
rules. The rules of this license are both obligations
under that agreement and conditions to your license.
You must not do anything with this software that triggers
a rule that you cannot or will not follow.

## Copyright

Each contributor licenses you to do everything with this
software that would otherwise infringe that contributor's
copyright in it.

## Notices

You must ensure that everyone who gets a copy of
any part of this software from you, with or without
changes, also gets the text of this license or a link to
<https://blueoakcouncil.org/license/1.0.0>.

## Excuse

If anyone notifies you in writing that you have not
complied with [Notices](#notices), you can keep your
license by taking all practical steps to comply within 30
days after the notice. If you do not do so, your license
ends immediately.

## Patent

Each contributor licenses you to do everything with this
software that would otherwise infringe any patent claims
they can license or become able to license.

## Reliability

No contributor can revoke this license.

## No Liability

**_As far as the law allows, this software comes as is,
without any warranty or condition, and no contributor
will be liable to anyone for any damages related to this
software or this license, under any kind of legal claim._**
5 changes: 5 additions & 0 deletions src/tsx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `@tapjs/tsx`

An alternative plugin to `@tapjs/typescript`. Load typescript
tests using [`tsx`](https://github.com/privatenumber/tsx) instead
of [`ts-node`](https://github.com/TypeStrong/ts-node).
28 changes: 28 additions & 0 deletions src/tsx/loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { fileURLToPath } from 'url'

// check if we need a globalPreload, because node 20 runs
// loaders in separate isolated loaders thread.

const v = process.version
.replace(/[^0-9]/g, ' ')
.trim()
.split(' ')
.map(s => parseInt(s, 10))
const separateLoadersThread = v[0] >= 20

const base = fileURLToPath(import.meta.url)
export const globalPreload = () =>
!separateLoadersThread
? ''
: `
const { createRequire } = getBuiltin('module')
const require = createRequire(${JSON.stringify(base)})
require('@esbuild-kit/cjs')
`

// otherwise, do it here
if (!separateLoadersThread) {
await import('@esbuild-kit/cjs-loader')
}

export * from '@esbuild-kit/esm-loader'
70 changes: 70 additions & 0 deletions src/tsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"name": "@tapjs/tsx",
"version": "1.1.17",
"description": "Alternative to @tapjs/typescript. Load typescript using tsx instead of ts-node.",
"tshy": {
"main": true,
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts",
"./loader": "./src/loader.mts",
"./import": "./src/import.mts"
}
},
"type": "module",
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
},
"./loader": {
"import": {
"types": "./dist/esm/loader.d.mts",
"default": "./dist/esm/loader.mjs"
}
},
"./import": {
"import": {
"types": "./dist/esm/import.d.mts",
"default": "./dist/esm/import.mjs"
}
}
},
"files": [
"dist"
],
"scripts": {
"prepare": "tshy",
"pretest": "npm run prepare",
"presnap": "npm run prepare",
"test": "tap",
"snap": "tap",
"format": "prettier --write . --loglevel warn --ignore-path ../../.prettierignore --cache",
"typedoc": "typedoc --tsconfig tsconfig/esm.json ./src/*.ts"
},
"license": "BlueOak-1.0.0",
"dependencies": {
"tsx": "^4.1.2"
},
"peerDependencies": {
"@tapjs/core": "1.4.5"
},
"tap": {
"typecheck": false
},
"keywords": [
"tapjs plugin"
],
"engines": {
"node": "16 >=16.17.0 || 18 >= 18.6.0 || >=20"
}
}
2 changes: 2 additions & 0 deletions src/tsx/src/import.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//@ts-ignore
export * from 'tsx'
75 changes: 75 additions & 0 deletions src/tsx/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Exports a loader, which does all the work.
// import('tsx')
// import('@tapjs/core')
export const loader = '@tapjs/tsx/loader'
export const importLoader = '@tapjs/tsx/import'
export const preload = true
import type { TapPlugin } from '@tapjs/core'

// the plugin just sets the configs for tsx, doesn't
// add any functionality to the Test class.
const env = (...keys: string[]) => {
for (const key of keys) {
if (process.env[`TAP_${key}`]) {
process.env[key] = process.env[`TAP_${key}`]
}
}
}
let didWarning = false
let didEnv = false

/**
* File types that this plugin adds support for
*/
export const testFileExtensions = ['ts', 'cts', 'mts', 'tsx', 'jsx']

/**
* Plugin function enabling tsx for running typescript tests
*
* The plugin function sets the tsx environment variables based on the tap
* configs, and prints a warning when used along with the `@tapjs/typescript`
* plugin, as this can cause strange conflicts, or at the very least,
* unnecessarily slow down tests by compiling the typescript twice.
*/
export const plugin: TapPlugin<{}> = () => {
if (!didEnv) {
env('TSX_TSCONFIG_PATH', 'TSX_DISABLE_CACHE')
}
const tp = process.env.TAP_PLUGIN || ''
if (!/^!@tapjs\/typescript$/m.test(tp) && !didWarning) {
didWarning = true
console.error(`
@tapjs/tsx may behave strangely when used along with
the @tapjs/typescript default plugin.
Please run: tap plugin rm @tapjs/typescript
`)
}
return {}
}

/**
* Configuration fields added by this plugin.
*
* @group Configuration
*/
export const config = {
/**
* String option. Tell `tsx` where to find your tsconfig.json file.
*
* @group Configuration
*/
'tsx-tsconfig-path': {
type: 'string',
description: `Tell tsx where to find your tsconfig.json file.`,
},
/**
* Flag. Tell `tsx` not to use a cache
*
* @group Configuration
*/
'tsx-disable-cache': {
type: 'boolean',
description: `Tell tsx not to use a cache`,
},
}
4 changes: 4 additions & 0 deletions src/tsx/src/loader.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//@ts-ignore
export * from 'tsx/esm'
//@ts-ignore
import 'tsx/cjs'
22 changes: 22 additions & 0 deletions src/tsx/tap-snapshots/test/index.js.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/index.js > TAP > config 1`] = `
Object {
"esbk-disable-cache": Object {
"description": "Tell @esbuild-kit not to use a cache",
"type": "boolean",
},
"esbk-tsconfig-path": Object {
"description": String(
Tell @esbuild-kit where to find your
tsconfig.json file.
),
"type": "string",
},
}
`
Loading

0 comments on commit dd4f233

Please sign in to comment.