@fastify/autoload
Advanced tools
Comparing version 5.2.0 to 5.3.0
25
index.js
@@ -8,3 +8,2 @@ 'use strict' | ||
const isTsNode = (Symbol.for('ts-node.register.instance') in process) || !!process.env.TS_NODE_DEV | ||
const isJestEnvironment = process.env.JEST_WORKER_ID !== undefined | ||
@@ -16,3 +15,2 @@ const isSWCRegister = process._preload_modules && process._preload_modules.includes('@swc/register') | ||
const isTsx = process._preload_modules && process._preload_modules.toString().includes('tsx') | ||
const typescriptSupport = isTsNode || isJestEnvironment || isSWCRegister || isSWCNodeRegister || isSWCNode || isTsm || isTsx | ||
const routeParamPattern = /\/_/ig | ||
@@ -167,4 +165,8 @@ const routeMixedParamPattern = /__/g | ||
const type = getScriptType(file, options.packageType) | ||
if (type === 'typescript' && !typescriptSupport) { | ||
throw new Error(`@fastify/autoload cannot import hooks plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`) | ||
if (tsFileNoTsSupport()) { | ||
try { | ||
require('ts-node').register() | ||
} catch { | ||
throw new Error(`@fastify/autoload cannot import hooks plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`) | ||
} | ||
} | ||
@@ -221,4 +223,8 @@ | ||
const type = getScriptType(file, options.packageType) | ||
if (type === 'typescript' && !typescriptSupport) { | ||
throw new Error(`@fastify/autoload cannot import plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`) | ||
if (tsFileNoTsSupport()) { | ||
try { | ||
require('ts-node').register() | ||
} catch { | ||
throw new Error(`@fastify/autoload cannot import plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`) | ||
} | ||
} | ||
@@ -350,2 +356,9 @@ | ||
function tsFileNoTsSupport () { | ||
const isTsNode = (Symbol.for('ts-node.register.instance') in process) || !!process.env.TS_NODE_DEV | ||
const typescriptSupport = isTsNode || isJestEnvironment || isSWCRegister || isSWCNodeRegister || isSWCNode || isTsm || isTsx | ||
// return isTsNode && !typescriptSupport | ||
return isTsNode === false && typescriptSupport === false | ||
} | ||
// do not create a new context, do not encapsulate | ||
@@ -352,0 +365,0 @@ // same as fastify-plugin |
{ | ||
"name": "@fastify/autoload", | ||
"version": "5.2.0", | ||
"version": "5.3.0", | ||
"description": "Require all plugins in a directory", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"lint:fix": "standard --fix | snazzy", | ||
"test": "npm run lint && 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", | ||
"test": "npm run lint && 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 vitest", | ||
"typescript": "tsd", | ||
@@ -21,3 +21,4 @@ "typescript:jest": "jest", | ||
"unit:with-modules": "tap test/commonjs/*.js test/module/*.js test/typescript/*.ts", | ||
"unit:without-modules": "tap test/commonjs/*.js test/typescript/*.ts" | ||
"unit:without-modules": "tap test/commonjs/*.js test/typescript/*.ts", | ||
"vitest": "vitest run" | ||
}, | ||
@@ -57,2 +58,3 @@ "repository": { | ||
"jest": "^28.1.3", | ||
"semver": "^7.3.7", | ||
"snazzy": "^9.0.0", | ||
@@ -64,6 +66,8 @@ "standard": "^17.0.0", | ||
"ts-node-dev": "^2.0.0", | ||
"tsd": "^0.22.0", | ||
"tsd": "^0.23.0", | ||
"tsm": "^2.2.1", | ||
"tsx": "^3.7.1", | ||
"typescript": "^4.5.4" | ||
"typescript": "^4.5.4", | ||
"vite": "^3.0.9", | ||
"vitest": "^0.22.1" | ||
}, | ||
@@ -70,0 +74,0 @@ "dependencies": { |
@@ -6,2 +6,3 @@ 'use strict' | ||
const semver = require('semver') | ||
const { join } = require('path') | ||
@@ -32,7 +33,17 @@ const moduleSupport = semver.satisfies(process.version, '>= 14 || >= 12.17.0 < 13.0.0') | ||
app3.register(require('./ts-error/app')) | ||
const mockedAutoLoad = t.mock('../../', { | ||
'ts-node': { | ||
register: function () { | ||
throw new Error('ts-node doesn\'t exist because it is mocked') | ||
} | ||
} | ||
}) | ||
app3.register(mockedAutoLoad, { | ||
dir: join(__dirname, 'ts-node/routes') | ||
}) | ||
app3.ready(function (err) { | ||
t.type(err, Error) | ||
t.match(err.message, /cannot import plugin.*typescript/i) | ||
t.match(err.message, /cannot import plugin.*typescript/i, 't.match error.message for app 3') | ||
}) | ||
@@ -39,0 +50,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
113328
228
3310
24