Skip to content

Commit

Permalink
feat: add FASTIFY_AUTOLOAD_TYPESCRIPT environment variable (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Sep 14, 2023
1 parent 2679e50 commit 2afec63
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 2afec63

Please sign in to comment.