From 8de9cc4c68a11ac9a898300059f221bf98c4094a Mon Sep 17 00:00:00 2001 From: Leo Li <87396507+leoyli-headsup@users.noreply.github.com> Date: Tue, 14 Sep 2021 17:52:39 -0400 Subject: [PATCH] Add SWC support (#191) --- .swcrc | 8 ++++++++ index.js | 6 ++++-- package.json | 5 ++++- scripts/unit-typescript-swc.js | 21 +++++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 .swcrc create mode 100644 scripts/unit-typescript-swc.js diff --git a/.swcrc b/.swcrc new file mode 100644 index 00000000..a2d5b04f --- /dev/null +++ b/.swcrc @@ -0,0 +1,8 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript" + }, + "target": "es2016" + } +} diff --git a/index.js b/index.js index 803474a9..2db02aa6 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/package.json b/package.json index e26d9713..cb591c73 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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", diff --git a/scripts/unit-typescript-swc.js b/scripts/unit-typescript-swc.js new file mode 100644 index 00000000..08d5e691 --- /dev/null +++ b/scripts/unit-typescript-swc.js @@ -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) +}