From 1e6d9932266423935824990329d157415171717f Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 14 Sep 2021 00:00:06 -0400 Subject: [PATCH 1/2] add swc support --- .swcrc | 8 ++++++++ index.js | 6 ++++-- package.json | 3 +++ scripts/unit-typescript-swc.js | 21 +++++++++++++++++++++ 4 files changed, 36 insertions(+), 2 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..59bc1197 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "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) +} From 0e325de2cac634c06714e6c00e5550377c50cb5b Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 14 Sep 2021 11:17:46 -0400 Subject: [PATCH 2/2] add unit test to CI --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 59bc1197..cb591c73 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "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",