Skip to content

Commit

Permalink
chore: add decorators (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
wre232114 authored Dec 2, 2023
1 parent d3246b7 commit 72d0967
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/config/farm-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,35 @@ export default {
};
```

#### `script.decorators`
```ts
export interface DecoratorsConfig {
legacyDecorator: boolean;
decoratorMetadata: boolean;
/**
* The version of the decorator proposal to use. 2021-12 or 2022-03
* @default 2021-12
*/
decoratorVersion: '2021-12' | '2022-03' | null;
/**
* @default []
*/
includes: string[];
/**
* @default ["node_modules/"]
*/
excludes: string[];
}
```
It's recommended to use default decorators configuration of Farm, unless you want to improve performance, you can set `includes` and `excludes`.

Options:
* **legacyDecorator**: default to `true`. Using legacy decorator proposal.
* **decoratorMetadata**: default to `false`. You have to set `legacyDecorator` to `false` if you want to set it to true.
* **decoratorVersion**: default to '2021-12', proposal version. The value is 2021-12 or 2022-03.
* **includes**: default to `[]`. If you want to include modules that are excluded, you can set this option. Regex supported.
* **excludes**: default to `['node_modules/']`. Modules under these paths are ignored when transform decorators. Regex supported

### css

#### `css.modules`
Expand Down
33 changes: 33 additions & 0 deletions docs/features/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,39 @@ export default {

Refer to [Polyfill](/docs/features/polyfill) for more about `presetEnv`.


## 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
By default, Farm won't transform decorators for modules under `node_modules`, refer to [compilation.script.decorators.excludes](/docs/config/farm-config#scriptdecorators).

## Using SWC Plugins
SWC Plugins can be used directly in Farm, for example, we use `swc-plugin-vue-jsx` to compiling vue jsx in Farm:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,37 @@ export default {
};
```

#### `script.decorators`
```ts
export interface DecoratorsConfig {
legacyDecorator: boolean;
decoratorMetadata: boolean;
/**
* 装饰器版本: 2021-12 或者 2022-03
* @default 2021-12
*/
decoratorVersion: '2021-12' | '2022-03' | null;
/**
* @default []
*/
includes: string[];
/**
* @default ["node_modules/"]
*/
excludes: string[];
}
```

建议使用 Farm 默认的装饰器配置,除非你想提高性能,可以设置`includes`和`excludes`。

选项:
* **legacyDecorator**:默认为`true`。使用遗留装饰器提案。
* **decoratorMetadata**:默认为`false`。如果您想将`legacyDecorator`设置为`true`,则必须将其设置为`false`。
* **decoratorVersion**:默认为`2021-12`,提案版本。该值为 2021-12 或 2022-03。
* **包括**:默认为`[]`。如果要包含排除的模块,可以设置此选项。支持正则表达式。
* **排除**:默认为`['node_modules/']`。变换装饰器时,这些路径下的模块将被忽略。支持正则表达式


### css

#### `css.modules`
Expand Down
32 changes: 32 additions & 0 deletions i18n/zh/docusaurus-plugin-content-docs/current/features/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@ export default {

有关`presetEnv`的更多信息,请参阅 [Polyfill](/docs/features/polyfill)

## 装饰器
装饰器默认不启用, 可以通过设置 `compilation.script.parser.tsConfig.decorators``true` 来启用装饰器。

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

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

> Farm 提供了一个装饰器的示例,可以看 https://github.com/farm-fe/farm/tree/main/examples/decorators
默认情况下, Farm 不会转译 `node_modules` 下的装饰器, 参考 [compilation.script.decorators.excludes](/docs/config/farm-config#scriptdecorators).

## 使用 SWC 插件
SWC Plugins可以直接在Farm中使用,例如我们在Farm中使用swc-plugin-vue-jsx来编译vue jsx:

Expand Down

0 comments on commit 72d0967

Please sign in to comment.