Skip to content

Commit

Permalink
feat(js-plugins): support babel plugin & react compiler plugin (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
shulandmimi authored Dec 19, 2024
1 parent 93e7f61 commit 7551a3f
Show file tree
Hide file tree
Showing 20 changed files with 1,443 additions and 205 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-bags-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/js-plugin-babel": patch
---

babel plugin release
5 changes: 5 additions & 0 deletions .changeset/serious-dingos-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/js-plugin-react-compiler": patch
---

release react-compiler plugin
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*]
indent_size = 2
indent_style = space
15 changes: 15 additions & 0 deletions examples/babel-react-compiler/farm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from '@farmfe/core';
import react from '@farmfe/plugin-react';
import { reactCompiler } from '@farmfe/js-plugin-react-compiler';

export default defineConfig({
compilation: {
input: {},
output: {},
presetEnv: false,
minify: false,
mode: 'development',
persistentCache: false,
},
plugins: [reactCompiler(), react()],
});
12 changes: 12 additions & 0 deletions examples/babel-react-compiler/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>react compiler</title>
</head>
<body>
<div id="root"></div>
<script src="./src/index.jsx"></script>
</body>
</html>
31 changes: 31 additions & 0 deletions examples/babel-react-compiler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "babel-react-compiler",
"version": "1.0.0",
"description": "",
"main": "index.js",
"private": true,
"scripts": {
"build": "npx farm build",
"start": "npx farm start",
"dev": "npm run start"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@farmfe/cli": "^1.0.4",
"@farmfe/core": "^1.5.0",
"@farmfe/js-plugin-babel": "workspace:*",
"@farmfe/js-plugin-react-compiler": "workspace:*",
"core-js": "^3.36.1",
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"devDependencies": {
"@farmfe/plugin-react": "^1.2.0",
"@types/node": "^22.7.4",
"@types/react": "19.0.1",
"@types/react-dom": "19.0.2",
"react-refresh": "^0.14.0"
}
}
18 changes: 18 additions & 0 deletions examples/babel-react-compiler/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React, { useCallback, useEffect, useState } from 'react';

export default function App() {
const [count, setCount] = useState(1);

useEffect(() => {
console.log(count);
}, [count]);

const inc = useCallback(() => setCount(v => v + 1), []);

return (
<div>
<h1>hello world {count}</h1>
<button onClick={() => inc()}>inc</button>
</div>
);
}
5 changes: 5 additions & 0 deletions examples/babel-react-compiler/src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import App from './App';

createRoot(document.getElementById('root')).render(<App />);
84 changes: 84 additions & 0 deletions js-plugins/babel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# @farmfe/js-plugin-babel

farm plugin which support use babel

## Install

```shell
npm i @farmfe/js-plugin-babel -D
```

or yarn/pnpm

```shell
pnpm i @farmfe/js-plugin-babel -D
```

## Usage

```ts
// farm.config.ts
import { babel } from "@farmfe/js-plugin-babel";
import react from "@farmfe/plugin-react";

defineConfig({
plugins: [
// transform by compiler, default transform `tsx`, `jsx` files
babel(),
// transform jsx
react(),
],
});
```

## Options

### `filters`

- Type: `{ moduleTypes: ModuleType[], resolvedPaths: string[] }`
- Default:

```ts
{
moduleTypes: ["tsx", "jsx"],
resolvedPaths: []
}
```

Determines which files to transform

For example, files with the `tsx` extension

```ts
{
resolvedPaths: [".tsx$"];
}
```

Or use module types to distinguish

```ts
{
moduleTypes: ["tsx", "jsx"];
}
```

The type comes from the return value of the `load` hook and can be customized (`farm` has some default types `js`, `jsx`, `ts`, `tsx`, `css`, `html`, `asset`, `runtime` that can be used directly)

### `transformOptions`

Babel [transform configuration](https://babeljs.io/docs/options)

### `priority`

- Type: `number`
- Default: `99`

The priority of the farm plugin execution, the higher the priority, the earlier it executes.

### `name`

- Type: `string`
- Default: `js-plugin:babel`

The name of the farm plugin
30 changes: 30 additions & 0 deletions js-plugins/babel/farm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { defineConfig } from "@farmfe/core";
import dts from "@farmfe/js-plugin-dts";

export default defineConfig({
compilation: {
external: ["@farmfe/core", '@babel/core'],
input: {
index: "./src/index.ts",
},
output: {
targetEnv: "library-node",
path: `./dist`,
format: "cjs",
},
partialBundling: {
enforceResources: [
{
name: "index",
test: [".+"],
},
],
},
minify: false,
sourcemap: true,
resolve: {
autoExternalFailedResolve: true,
},
},
plugins: [dts()],
});
50 changes: 50 additions & 0 deletions js-plugins/babel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@farmfe/js-plugin-babel",
"version": "0.0.1",
"description": "",
"scripts": {
"build": "npm run build:cjs",
"build:cjs": "cross-env FARM_FORMAT=cjs farm build",
"dev": "npm run build:cjs -- --watch"
},
"keywords": [
"babel",
"plugin",
"farm",
"farmfe",
"js",
"transform",
"bundler",
"compiler"
],
"author": {
"name": "shulandmimi",
"email": "sshuang141@163.com",
"url": "https://github.com/shulandmimi"
},
"license": "MIT",
"exports": {
"require": "./dist/index.js",
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"dependencies": {
"@babel/core": "^7.26.0"
},
"devDependencies": {
"@farmfe/cli": "^1.0.4",
"@farmfe/core": "^1.5.0",
"@farmfe/js-plugin-dts": "^0.6.4",
"@types/babel__core": "^7.20.5",
"@types/node": "^22.7.4",
"cross-env": "^7.0.3"
},
"peerDependencies": {
"@farmfe/core": "^1.5.0"
},
"files": [
"package.json",
"README.md",
"dist"
]
}
50 changes: 50 additions & 0 deletions js-plugins/babel/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { transformAsync, type TransformOptions } from "@babel/core";
import type { ModuleType, JsPlugin } from "@farmfe/core";

export interface NormalizeFilterParams {
moduleTypes: ModuleType[];
resolvedPaths: string[];
}

export interface BabelOptions {
filters?: Partial<NormalizeFilterParams>;
transformOptions?: TransformOptions;
/**
* the priority of the plugin, large number means first to run
* @default 99
*/
priority: number;
name?: string;
}

const defaultFilters: BabelOptions["filters"] = {
moduleTypes: ["js", "jsx", "ts", "tsx"],
resolvedPaths: [],
};

export function babel(options: BabelOptions = { priority: 99 }): JsPlugin {
return {
name: options.name ?? "js-plugin:babel",
priority: options.priority,

processModule: {
filters: {
moduleTypes: options.filters?.moduleTypes ?? defaultFilters.moduleTypes,
resolvedPaths:
options.filters?.resolvedPaths ?? defaultFilters.resolvedPaths,
},
async executor(param) {
const { content, moduleId } = param;

const result = await transformAsync(content, {
filename: moduleId,
...options.transformOptions,
});

return {
content: result.code,
};
},
},
};
}
21 changes: 21 additions & 0 deletions js-plugins/babel/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"exclude": ["node_modules"],
"include": ["src/**/*"],
"compilerOptions": {
"noUnusedParameters": false,
"noUnusedLocals": false,
"outDir": "dist",
"rootDir": "src",
"lib": ["DOM", "ESNext"],
"noImplicitAny": true,
"sourceMap": true,
"target": "es2020",
"module": "ESNext",
"declaration": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"skipDefaultLibCheck": true,
"skipLibCheck": true,
"moduleResolution": "bundler"
}
}
Loading

0 comments on commit 7551a3f

Please sign in to comment.