---
title: "TS/TSX"
locale: "en"
version: "current"
canonicalUrl: "https://farmfe.org/docs/features/script"
publicationStatus: "prerelease"
---

<a id="tstsx"></a>
# TS/TSX


Farm support compiling ` Js/Jsx/Ts/Tsx ` out of box, and compile ` Jsx/Tsx ` to React by default.


```tsx
import Button from "./Button";

function ButtonGroup(props: ButtonProps) {
  return (
    <div>
      {props.buttons.map((b) => (
        <Button>{b}</Button>
      ))}
    </div>
  );
}
```


Farm using SWC to compile scripts, and Farm has set reasonable default configurations for script compilation. Also, you can use ` compilation.script ` to configure how to compile your script file. see [compilation.script](</docs/config/compilation-options#script>) for details.


<a id="configuring-swc-parser"></a>
## Configuring Swc Parser


You can configuring the SWC Parser through ` compilation.script.parser `. Refer to [https://swc.rs/docs/configuration/compilation\#jscparser](<https://swc.rs/docs/configuration/compilation#jscparser>).


For example, if you want to enable decorators in the parser, set ` compilation.script.parser.esConfig.decorators ` (or ` parser.tsConfig.decorators ` for TypeScript modules):


```ts
import { defineConfig } from '@farmfe/core';

export default defineConfig({
  compilation: {
    script: {
      parser: {
        // for .js/.jsx files
        esConfig: {
          decorators: true,
        },
        // for .ts/.tsx files
        tsConfig: {
          decorators: true,
        },
      },
    },
  },
});
```


By default Farm set ` jsx: true ` for ` .jsx|.tsx ` files. Other field are default to SWC's defaults.


<a id="configuring-target"></a>
## Configuring Target


Use ` compilation.script.target ` to configure the JavaScript syntax generation target. Farm can set it automatically from versioned browser [` output.targetEnv `](</docs/config/compilation-options#output-targetenv>) values.



> [!NOTE]
> note
> 
> Set [` output.targetEnv `](</docs/config/compilation-options#output-targetenv>) for common production compatibility presets, or configure ` script.target ` directly when you only need to control generated JavaScript syntax.



This option can be used along with ` compilation.presetEnv ` to gracefully downgrade your project for old browsers. For example, you can set target to ` ES5 ` and enable ` presetEnv `, then your project will be fully downgrade to ES5.


```ts
import { defineConfig } from '@farmfe/core';

export default defineConfig({
  compilation: {
    script: {
      target: "es5",
    },
    presetEnv: true,
  },
});
```


Refer to [Syntax Downgrade and Polyfill](</docs/advanced/polyfill>) for more about ` presetEnv ` and ` target `.


<a id="native-top-level-await"></a>
## Native Top-Level Await


Farm supports native top-level ` await ` via [` compilation.script.nativeTopLevelAwait `](</docs/config/compilation-options#scriptnativetoplevelawait>). When enabled, Farm will preserve ` await ` at the top level of modules instead of transforming it away.


```ts
import { defineConfig } from '@farmfe/core';

export default defineConfig({
  compilation: {
    script: {
      nativeTopLevelAwait: true,
    },
  },
});
```



> [!NOTE]
> note
> 
> Your target environment must support top-level await (e.g. modern browsers, Node.js 14.8+).



<a id="import-not-used-as-values"></a>
## Import Not Used As Values


Use [` compilation.script.importNotUsedAsValues `](</docs/config/compilation-options#scriptimportnotusedasvalues>) to control the behavior of type-only imports, mirroring TypeScript's ` --importsNotUsedAsValues ` flag. Options:


- ` remove ` — Drop import statements that only import types (default)
- ` preserve ` — Keep all import statements


<a id="decorators"></a>
## Decorators


Decorators is disabled by default, you can set ` compilation.script.parser.tsConfig.decorators ` to ` true ` to enable decorators.


```ts
import { defineConfig } from "@farmfe/core";

export default defineConfig({
  compilation: {
    script: {
      parser: {
        tsConfig: {
          // support decorators
          decorators: true,
        },
      },
      // configuring decorators
      decorators: {
        legacyDecorator: true,
        decoratorMetadata: false,
        decoratorVersion: '2021-12',
        includes: ["src/broken.ts"],
        excludes: ['node_modules/'],
      }
    },
  },
});
```


> Farm provide a example for supporting decorators, see [https://github.com/farm-fe/farm/tree/main/examples/decorators](<https://github.com/farm-fe/farm/tree/main/examples/decorators>)
> By default, Farm won't transform decorators for modules under ` node_modules `, refer to [compilation.script.decorators.excludes](</docs/config/compilation-options#scriptdecorators>).


<a id="using-swc-plugins"></a>
## Using SWC Plugins


SWC Plugins can be used directly in Farm, for example, we use ` swc-plugin-vue-jsx ` to compile Vue JSX in Farm. For Vue SFC support, register the current Rust plugin ` @farmfe/plugin-vue `:


```ts
import { defineConfig } from '@farmfe/core';
import vue from '@farmfe/plugin-vue';

export default defineConfig({
  compilation: {
    script: {
      plugins: [
        {
          name: "swc-plugin-vue-jsx",
          options: {
            transformOn: true,
            optimize: true,
          },
          filters: {
            // resolvedPaths: [".+"]
            moduleTypes: ["tsx", "jsx"],
          },
        },
      ],
    },
  },
  plugins: [vue()],
});
```


Refer to [Using Plugins](</docs/using-plugins#using-swc-plugins>) for more details.


<a id="vite-style-importmetaglob"></a>
## Vite-style ` import.meta.glob `


Farm supports literal Vite-style ` import.meta.glob `, see [glob import](<https://vite.dev/guide/features#glob-import>). Its Rust matcher targets the JavaScript glob profiles in Vite 8.3.5; this is not a claim of complete picomatch or native Rolldown compatibility.


The supported options are ` eager `, ` import `, ` query `, ` exhaustive `, ` caseSensitive `, and ` base `, plus the legacy ` as: 'raw' | 'url' ` option. Patterns and options must be static literals; unsupported options and invalid values produce compilation errors.


Negative patterns exclude matches regardless of their position in the pattern array. Dot files and ` node_modules ` are excluded by default; ` exhaustive: true ` includes them during discovery. Extglobs are disabled. File keys are sorted, and import queries are attached to import specifiers rather than object keys. A relative ` base ` resolves from the importer directory and changes the returned keys; an absolute base resolves from the project root.


```ts
const modules = import.meta.glob('./*.js', {
  base: './dir',
  import: 'default',
  query: '?raw',
});
// keys: './foo.js'; import specifier: './dir/foo.js?raw'
```


Development file additions and removals invalidate matching importers and update their glob objects through HMR. Files still follow the configured watcher exclusions. Matcher, filesystem, and raw-read errors are reported without falling back to a different glob dialect. Source and compatibility boundaries are documented in [` farmfe_ecosystem_glob `](<https://github.com/farm-fe/farm/tree/main/rust-ecosystem/glob>).


for example:


```ts
const modules = import.meta.glob("./dir/*.js");
```


The above will be transformed into the following:


```ts
// code produced by Farm
const modules = {
  "./dir/foo.js": () => import("./dir/foo.js"),
  "./dir/bar.js": () => import("./dir/bar.js"),
};
```


Using ` { eager: true } `:


```ts
const modules = import.meta.glob("./dir/*.js", { eager: true });
```


The above will be transformed into the following:


```ts
// code produced by Farm
import * as __glob__0_0 from "./dir/foo.js";
import * as __glob__0_1 from "./dir/bar.js";
const modules = {
  "./dir/foo.js": __glob__0_0,
  "./dir/bar.js": __glob__0_1,
};
```


multiple patterns are supported:


```ts
const modules = import.meta.glob(["./dir/*.js", "./another/*.js"]);
```


negative patterns are also supported:


```ts
const modules = import.meta.glob(["./dir/*.js", "!**/bar.js"]);
```


```ts
// code produced by Farm
const modules = {
  "./dir/foo.js": () => import("./dir/foo.js"),
};
```



> [!NOTE]
> note
> 
> - You should also be aware that all the arguments in the import.meta.glob must be passed as literals. You can NOT use variables or expressions in them.
> - ` import.meta.glob ` transformed by Farm in compile time, it does not exist in runtime.
