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

Update for V5 (bis) #373

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:

jobs:
test:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v4.1.0
with:
license-check: true
lint: true
8 changes: 0 additions & 8 deletions .swcrc
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this config?

Btw, we must pass env variable SWCRC=true to load it.

This file was deleted.

7 changes: 4 additions & 3 deletions .taprc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ts: true
jsx: false
flow: false
# vim: set filetype=yaml :
timeout: 60
plugin:
- "!@tapjs/typescript"
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ const fastifyAutoload = async function autoload (fastify, options) {
async function getPackageType (cwd) {
const directories = cwd.split(sep)

/* c8 ignore start */
// required for paths that begin with the sep, such as linux root
directories[0] = /* istanbul ignore next - OS specific */ directories[0] !== '' ? directories[0] : sep

// ignore because OS specific evaluation
directories[0] = directories[0] !== '' ? directories[0] : sep
/* c8 ignore stop */
while (directories.length > 0) {
const filePath = join(...directories, 'package.json')

Expand Down
53 changes: 25 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
{
"name": "@fastify/autoload",
"version": "5.8.2",
"version": "5.8.1",
"description": "Require all plugins in a directory",
"main": "index.js",
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard | snazzy",
"lint:fix": "standard --fix | snazzy",
"test": "npm run unit && npm run typescript && npm run typescript:jest && npm run typescript:esm && npm run typescript:swc && npm run typescript:swc-node-register && npm run typescript:tsm && npm run typescript:tsx && npm run typescript:vitest && npm run typescript:esbuild",
"test": "npm run typescript && npm run typescript:jest && npm run typescript:swc-node-register && npm run typescript:tsm && npm run typescript:tsx && npm run typescript:vitest && npm run typescript:esbuild && npm run unit",
"typescript": "tsd",
"typescript:jest": "jest",
"typescript:esm": "node scripts/unit-typescript-esm.js",
"typescript:swc": "node scripts/unit-typescript-swc.js",
"typescript:swc-node-register": "node scripts/unit-typescript-swc-node-register.js",
"typescript:tsm": "node scripts/unit-typescript-tsm.js",
"typescript:tsx": "node scripts/unit-typescript-tsx.js",
"typescript:esbuild": "node scripts/unit-typescript-esbuild.js",
"typescript:vitest": "vitest run",
"typescript:vitest:dev": "vitest",
"unit": "node scripts/unit.js",
"unit:with-modules": "tap test/issues/*/test.js test/commonjs/*.js test/module/*.js test/typescript/*.ts"
"unit:with-modules": "tap test/issues/*/test.js test/commonjs/*.js test/module/*.js"
},
"repository": {
"type": "git",
Expand All @@ -43,33 +42,31 @@
"url": "https://github.com/fastify/fastify-autoload/issues"
},
"homepage": "https://github.com/fastify/fastify-autoload#readme",
"dependencies": {},
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
"@fastify/url-data": "^5.0.0",
"@swc-node/register": "^1.5.1",
"@swc/core": "^1.2.129",
"@swc/register": "^0.1.9",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"@types/jest": "^29.0.0",
"@types/node": "^20.1.0",
"@types/tap": "^15.0.5",
"esbuild": "^0.19.2",
"esbuild-register": "^3.4.1",
"fastify": "^4.0.0-rc.2",
"fastify-plugin": "^4.0.0",
"jest": "^28.1.3",
"@fastify/pre-commit": "^2.1.0",
"@fastify/url-data": "^5.4.0",
"@swc-node/register": "^1.9.1",
"@swc/core": "^1.4.13",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.7",
"@types/tap": "^15.0.11",
"esbuild": "^0.20.2",
"esbuild-register": "^3.5.0",
"fastify": "^4.26.2",
"fastify-plugin": "^4.5.1",
"jest": "^29.7.0",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tap": "^16.0.0",
"ts-jest": "^28.0.7",
"ts-node": "^10.4.0",
"standard": "^17.1.0",
"tap": "^18.7.2",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"tsd": "^0.29.0",
"tsm": "^2.2.1",
"tsx": "^3.7.1",
"typescript": "^5.0.2",
"vite": "^5.0.0",
"vitest": "^0.34.1"
"tsd": "^0.31.0",
"tsm": "^2.3.0",
"tsx": "^4.7.2",
"typescript": "^5.4.5",
"vite": "^5.2.8",
"vitest": "^1.5.0"
},
"standard": {
"ignore": [
Expand Down
45 changes: 24 additions & 21 deletions runtime.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
'use strict'
// TODO: change x ?? (x = y) to x ??= y when next major

// runtime cache
const cache = {}

let processArgv
function checkProcessArgv (moduleName) {
/* istanbul ignore next - nullish needed for non Node.js runtime */
processArgv ?? (processArgv = (process.execArgv ?? []).concat(process.argv ?? []))
/* c8 ignore start */
// nullish needed for non Node.js runtime
processArgv ??= (process.execArgv ?? []).concat(process.argv ?? [])
/* c8 ignore stop */
return processArgv.some((arg) => arg.indexOf(moduleName) >= 0)
}

let preloadModules
function checkPreloadModules (moduleName) {
/* istanbul ignore next - nullish needed for non Node.js runtime */
preloadModules ?? (preloadModules = (process._preload_modules ?? []))
/* c8 ignore start */
// nullish needed for non Node.js runtime
preloadModules ??= (process._preload_modules ?? [])
/* c8 ignore stop */
return preloadModules.includes(moduleName)
}

let preloadModulesString
function checkPreloadModulesString (moduleName) {
preloadModulesString ?? (preloadModulesString = preloadModules.toString())
preloadModulesString ??= preloadModules.toString()
return preloadModulesString.includes(moduleName)
}

Expand All @@ -35,69 +38,69 @@ const runtime = {}
Object.defineProperties(runtime, {
tsNode: {
get () {
cache.tsNode ?? (cache.tsNode = (
cache.tsNode ??= (
// --require tsnode/register
(Symbol.for('ts-node.register.instance') in process) ||
// --loader ts-node/esm
checkProcessArgv('ts-node/esm') ||
// ts-node-dev
!!process.env.TS_NODE_DEV
))
)
return cache.tsNode
}
},
babelNode: {
get () {
cache.babelNode ?? (cache.babelNode = checkProcessArgv('babel-node'))
cache.babelNode ??= checkProcessArgv('babel-node')
return cache.babelNode
}
},
vitest: {
get () {
cache.vitest ?? (cache.vitest = (
cache.vitest ??= (
checkEnvVariable('VITEST', 'true') ||
checkEnvVariable('VITEST_WORKER_ID')
))
)
return cache.vitest
}
},
jest: {
get () {
cache.jest ?? (cache.jest = checkEnvVariable('JEST_WORKER_ID'))
cache.jest ??= checkEnvVariable('JEST_WORKER_ID')
return cache.jest
}
},
swc: {
get () {
cache.swc ?? (cache.swc = (
cache.swc ??= (
checkPreloadModules('@swc/register') ||
checkPreloadModules('@swc-node/register') ||
checkProcessArgv('.bin/swc-node')
))
)
return cache.swc
}
},
tsm: {
get () {
cache.tsm ?? (cache.tsm = checkPreloadModules('tsm'))
cache.tsm ??= checkPreloadModules('tsm')
return cache.tsm
}
},
esbuild: {
get () {
cache.esbuild ?? (cache.esbuild = checkPreloadModules('esbuild-register'))
cache.esbuild ??= checkPreloadModules('esbuild-register')
return cache.esbuild
}
},
tsx: {
get () {
cache.tsx ?? (cache.tsx = checkPreloadModulesString('tsx'))
cache.tsx ??= checkPreloadModulesString('tsx')
return cache.tsx
}
},
supportTypeScript: {
get () {
cache.supportTypeScript ?? (cache.supportTypeScript = (
cache.supportTypeScript ??= (
checkEnvVariable('FASTIFY_AUTOLOAD_TYPESCRIPT') ||
runtime.tsNode ||
runtime.vitest ||
Expand All @@ -107,17 +110,17 @@ Object.defineProperties(runtime, {
runtime.tsm ||
runtime.tsx ||
runtime.esbuild
))
)
return cache.supportTypeScript
}
},
forceESM: {
get () {
cache.forceESM ?? (cache.forceESM = (
cache.forceESM ??= (
checkProcessArgv('ts-node/esm') ||
runtime.vitest ||
false
))
)
return cache.forceESM
}
}
Expand Down
6 changes: 2 additions & 4 deletions scripts/unit-typescript-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
const { exec } = require('node:child_process')

const args = [
'tap',
'--node-arg=--loader=ts-node/esm',
'--node-arg=--experimental-specifier-resolution=node',
'--no-coverage',
'node',
'--loader=ts-node/esm',
'test/typescript-esm/*.ts'
]

Expand Down
5 changes: 2 additions & 3 deletions scripts/unit-typescript-swc-node-register.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
const { exec } = require('node:child_process')

const args = [
'tap',
'--node-arg=--require=@swc-node/register',
'--no-coverage',
'node',
'--require=@swc-node/register',
'test/typescript/*.ts'
]

Expand Down
18 changes: 0 additions & 18 deletions scripts/unit-typescript-swc.js

This file was deleted.

6 changes: 2 additions & 4 deletions scripts/unit-typescript-tsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
const { exec } = require('node:child_process')

const args = [
'tap',
'--no-ts',
'--node-arg=--require=tsm',
'--no-coverage',
'node',
'--require=tsm',
'test/typescript/*.ts'
]

Expand Down
3 changes: 1 addition & 2 deletions test/commonjs/autohooks/routes/.autohooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = async function (app, opts, next) {
module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('root')
next()
})
}
3 changes: 1 addition & 2 deletions test/commonjs/autohooks/routes/child/.autohooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = async function (app, opts, next) {
module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('child')
next()
})
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = async function (app, opts, next) {
module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('grandchild')
next()
})
}
2 changes: 1 addition & 1 deletion test/commonjs/autohooks/routes/child/grandchild/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = async function (app, opts, next) {
module.exports = async function (app, opts) {
app.get('/', async function (req, reply) {
reply.status(200).send({ hooked: req.hooked })
})
Expand Down
2 changes: 1 addition & 1 deletion test/commonjs/autohooks/routes/child/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = async function (app, opts, next) {
module.exports = async function (app, opts) {
app.get('/', async function (req, reply) {
reply.status(200).send({ hooked: req.hooked })
})
Expand Down
2 changes: 1 addition & 1 deletion test/commonjs/autohooks/routes/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = async function (app, opts, next) {
module.exports = async function (app, opts) {
app.get('/', async function (req, reply) {
reply.status(200).send({ hooked: req.hooked })
})
Expand Down
2 changes: 1 addition & 1 deletion test/commonjs/autohooks/routes/sibling/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = async function (app, opts, next) {
module.exports = async function (app, opts) {
app.get('/', async function (req, reply) {
reply.status(200).send({ hooked: req.hooked })
})
Expand Down
2 changes: 1 addition & 1 deletion test/commonjs/basic/index/bar/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = async (server, opts, next) => {
module.exports = async (server, opts) => {
server.get('/', async (request, reply) => {
reply.status(200).send({ works: true })
})
Expand Down
2 changes: 1 addition & 1 deletion test/commonjs/basic/index/ignored.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// This module should be ignored because an index.js file is present in the same directory

module.exports = async (server, opts, next) => {
module.exports = async (server, opts) => {
server.get('/ignored', async (request, reply) => {
reply.status(200).send({ works: true })
})
Expand Down
Loading
Loading