Skip to content

Commit

Permalink
Add SWC support (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoyli-headsup authored Sep 14, 2021
1 parent 44b7e76 commit 8de9cc4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2016"
}
}
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const pkgUp = require('pkg-up')
const semver = require('semver')

const isTsNode = (Symbol.for('ts-node.register.instance') in process) || !!process.env.TS_NODE_DEV
const isJestEnviroment = process.env.JEST_WORKER_ID !== undefined
const typescriptSupport = isTsNode || isJestEnviroment
const isJestEnvironment = process.env.JEST_WORKER_ID !== undefined
const isSWCRegister = process._preload_modules && process._preload_modules.includes('@swc/register')
const isSWCNode = typeof process.env._ === 'string' && process.env._.includes('.bin/swc-node')
const typescriptSupport = isTsNode || isJestEnvironment || isSWCRegister || isSWCNode

const moduleSupport = semver.satisfies(process.version, '>= 14 || >= 12.17.0 < 13.0.0')
const routeParamPattern = /\/_/ig
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"types": "fastify-autoload.d.ts",
"scripts": {
"lint": "standard | snazzy",
"test": "npm run lint && npm run unit && npm run typescript && npm run typescript:jest && npm run typescript:esm",
"test": "npm run lint && npm run unit && npm run typescript && npm run typescript:jest && npm run typescript:esm && npm run typescript:swc",
"typescript": "tsd",
"typescript:jest": "jest",
"typescript:esm": "node scripts/unit-typescript-esm.js",
"typescript:swc": "node scripts/unit-typescript-swc.js",
"unit": "node scripts/unit.js",
"unit:with-modules": "tap test/commonjs/*.js test/module/*.js test/typescript/*.ts",
"unit:without-modules": "tap test/commonjs/*.js test/typescript/*.ts"
Expand All @@ -36,6 +37,8 @@
},
"homepage": "https://github.com/fastify/fastify-autoload#readme",
"devDependencies": {
"@swc/core": "^1.2.85",
"@swc/register": "^0.1.7",
"@types/jest": "^27.0.1",
"@types/node": "^16.0.0",
"@types/tap": "^15.0.5",
Expand Down
21 changes: 21 additions & 0 deletions scripts/unit-typescript-swc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const { exec } = require('child_process')
const semver = require('semver')

if (semver.satisfies(process.version, '>= 14')) {
const args = [
'tap',
'--node-arg=--require=@swc/register',
'--no-coverage',
'test/typescript/*.ts'
]

const child = exec(args.join(' '), {
shell: true
})

child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
child.once('close', process.exit)
}

0 comments on commit 8de9cc4

Please sign in to comment.