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

feat: add FASTIFY_AUTOLOAD_TYPESCRIPT environment variable #327

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading