Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SWC support #191

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
leoyli-headsup marked this conversation as resolved.
Show resolved Hide resolved
"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)
}