From 7ebefaed6aa451c93cd259893dabccae4e287c92 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 12 Sep 2023 18:09:45 +0200 Subject: [PATCH] feat: add FASTIFY_AUTOLOAD_TYPESCRIPT environment variable --- README.md | 12 ++++++++++++ index.js | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6e28c343..936f0bdb 100644 --- a/README.md +++ b/README.md @@ -292,6 +292,18 @@ Autoload can be customised using the following options: // { country: 'be', language: 'nl' } ``` +## Override Typescript detection per Environment variable + +It is possible to override the automatic detection of a typescript capable runtime per `FASTIFY_AUTOLOAD_TYPESCRIPT` environment variable. If set to a truthy value Autoload will load `.ts` files, expecting that node has a typescript capable loader. + +This is useful for cases, where you want to use Autoload for loading typescript files but detecting the typescript loader fails, because for example you are using a custom loader. + +You can use it like this: + +```sh +FASTIFY_AUTOLOAD_TYPESCRIPT=1 node --loader=my-custom-loader index.ts +``` + ## Plugin Configuration Each plugin can be individually configured using the following module properties: diff --git a/index.js b/index.js index 6173c897..e26192df 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const url = require('node:url') const { readdir } = require('node:fs').promises const pkgUp = require('pkg-up') +const isFastifyAutoloadTypescriptOverride = !!process.env.FASTIFY_AUTOLOAD_TYPESCRIPT const isTsNode = (Symbol.for('ts-node.register.instance') in process) || !!process.env.TS_NODE_DEV const isBabelNode = (process?.execArgv || []).concat(process?.argv || []).some((arg) => arg.indexOf('babel-node') >= 0) @@ -16,7 +17,8 @@ const isSWCNode = typeof process.env._ === 'string' && process.env._.includes('. const isTsm = process._preload_modules && process._preload_modules.includes('tsm') const isEsbuildRegister = process._preload_modules && process._preload_modules.includes('esbuild-register') const isTsx = process._preload_modules && process._preload_modules.toString().includes('tsx') -const typescriptSupport = isTsNode || isVitestEnvironment || isBabelNode || isJestEnvironment || isSWCRegister || isSWCNodeRegister || isSWCNode || isTsm || isTsx || isEsbuildRegister +const typescriptSupport = isFastifyAutoloadTypescriptOverride || isTsNode || isVitestEnvironment || isBabelNode || isJestEnvironment || isSWCRegister || isSWCNodeRegister || isSWCNode || isTsm || isTsx || isEsbuildRegister + const forceESMEnvironment = isVitestEnvironment || false const routeParamPattern = /\/_/ig const routeMixedParamPattern = /__/g