TS/TSX
Farm support compiling Js/Jsx/Ts/Tsx out of box, and compile Jsx/Tsx to React by default.
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 for details.
Configuring Swc Parser
You can configuring the SWC Parser through compilation.script.parser. Refer to 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):
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.
Configuring Target
Use compilation.script.target to configure the JavaScript syntax generation target. Farm can set it automatically from versioned browser output.targetEnv values.
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.
import { defineConfig } from '@farmfe/core';
export default defineConfig({
compilation: {
script: {
target: "es5",
},
presetEnv: true,
},
});
Refer to Syntax Downgrade and Polyfill for more about presetEnv and target.
Native Top-Level Await
Farm supports native top-level await via compilation.script.nativeTopLevelAwait. When enabled, Farm will preserve await at the top level of modules instead of transforming it away.
import { defineConfig } from '@farmfe/core';
export default defineConfig({
compilation: {
script: {
nativeTopLevelAwait: true,
},
},
});
Import Not Used As Values
Use compilation.script.importNotUsedAsValues 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
Decorators
Decorators is disabled by default, you can set compilation.script.parser.tsConfig.decorators to true to enable decorators.
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 By default, Farm won't transform decorators for modules under
node_modules, refer to compilation.script.decorators.excludes.
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:
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 for more details.
Vite-style import.meta.glob
Farm supports literal Vite-style import.meta.glob, see 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.
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.
for example:
const modules = import.meta.glob("./dir/*.js");
The above will be transformed into the following:
// code produced by Farm
const modules = {
"./dir/foo.js": () => import("./dir/foo.js"),
"./dir/bar.js": () => import("./dir/bar.js"),
};
Using { eager: true }:
const modules = import.meta.glob("./dir/*.js", { eager: true });
The above will be transformed into the following:
// 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:
const modules = import.meta.glob(["./dir/*.js", "./another/*.js"]);
negative patterns are also supported:
const modules = import.meta.glob(["./dir/*.js", "!**/bar.js"]);
// code produced by Farm
const modules = {
"./dir/foo.js": () => import("./dir/foo.js"),
};
Farm