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: rust plugin usage #1269

Merged
merged 35 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
81074c7
feat: rust plugin usage
callqh Apr 30, 2024
fbf7349
fix: calling the plugin as a function
callqh Apr 30, 2024
a9daa84
fix: plugin file cache
callqh Apr 30, 2024
84438d0
chore: update template
callqh Apr 30, 2024
338bd3b
fix: example
callqh Apr 30, 2024
b7822cd
chore: lock
callqh Apr 30, 2024
a59c148
Merge branch 'main' into feature/plugin
callqh Apr 30, 2024
ff02417
fix: plugin export
callqh Apr 30, 2024
e3bbe35
chore: changeset
callqh Apr 30, 2024
439e170
fix: add files
callqh Apr 30, 2024
adcc92a
fix: export plugin
callqh May 1, 2024
3ff8009
fix: plugin
callqh May 20, 2024
b01b725
Merge branch 'main' into feature/plugin
callqh May 20, 2024
47e2860
fix: update rust plugin template
callqh May 20, 2024
9552b05
Merge branch 'main' into feature/plugin
callqh May 20, 2024
d73ea62
fix: error
callqh May 20, 2024
9e8eba9
fix: extract constants
callqh May 20, 2024
dac3faf
fix: deps
callqh May 21, 2024
d1e4beb
Merge branch 'main' into feature/plugin
callqh May 21, 2024
ed3c428
Merge branch 'main' into feature/plugin
callqh May 21, 2024
2e16973
fix: error
callqh May 21, 2024
6d9fbdc
Merge branch 'main' into feature/plugin
callqh May 21, 2024
e3bb2c1
Merge branch 'main' into feature/plugin
callqh May 22, 2024
3cffbe2
Merge branch 'main' into feature/plugin
callqh Jun 14, 2024
e7286e5
Merge branch 'main' into feature/plugin
callqh Jun 14, 2024
16d3f93
Merge branch 'main' into feature/plugin
callqh Jun 24, 2024
018cc60
Merge branch 'main' into feature/plugin
wre232114 Jun 29, 2024
31a07a5
fix: normalize path
callqh Jul 5, 2024
d377b06
Merge branch 'main' into feature/plugin
callqh Jul 5, 2024
3e4638b
Merge branch 'main' into feature/plugin
callqh Jul 8, 2024
d1820b8
fix: rust plugin
callqh Jul 8, 2024
6f62afa
fix: export default
callqh Jul 8, 2024
677cf91
fix: exports
callqh Jul 8, 2024
4e9d6fa
Merge branch 'main' into feature/plugin
callqh Jul 8, 2024
013a2be
fix: deps
callqh Jul 8, 2024
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
8 changes: 8 additions & 0 deletions .changeset/proud-snakes-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'create-farm-plugin': patch
'@farmfe/plugin-react': patch
'@farmfe/plugin-sass': patch
'@farmfe/core': patch
---

feat: normalize usage of rust plugins and js plugins
5 changes: 4 additions & 1 deletion crates/core/src/config/persistent_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ impl PersistentCacheConfig {
let mut content = String::new();

for dep in &cloned_obj.build_dependencies {
if !PathBuf::from(dep).exists() || !PathBuf::from(dep).is_file() {
if !PathBuf::from(dep).exists()
|| !PathBuf::from(dep).is_file()
|| dep.ends_with(".farm")
{
content.push_str(dep);
} else {
let c = std::fs::read_to_string(dep).unwrap();
Expand Down
1 change: 1 addition & 0 deletions examples/external/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@farmfe/cli": "^1.0.1",
"@farmfe/core": "^1.1.1",
"@types/jquery": "^3.5.29",
"jquery": "^3.7.1",
"typescript": "^5.4.3"
}
}
16 changes: 11 additions & 5 deletions examples/vite-adapter-react/farm.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { defineConfig } from '@farmfe/core';
import Pages from 'vite-plugin-pages';
import react from '@farmfe/plugin-react'

export default defineConfig({
plugins: ['@farmfe/plugin-react'],
vitePlugins:[
Pages({
resolver:'react'
}),
plugins: [
react({ runtime: 'automatic', refresh: true})
],
compilation:{
persistentCache: false
},
vitePlugins: [
Pages({
resolver: 'react'
})
]
});
4 changes: 2 additions & 2 deletions examples/vite-adapter-react/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<title>${title}$</title>
</head>
<body>
<div id="root"></div>
<script src="./src/index.tsx"></script>
</body>
</html>
</html>
1 change: 1 addition & 0 deletions examples/vite-adapter-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@farmfe/plugin-react": "workspace:*",
"@types/react": "18",
"@types/react-dom": "18",
"farm-plugin-remove-console": "^0.1.4",
"react-refresh": "^0.14.0",
"vite-plugin-pages": "^0.32.0"
},
Expand Down
1 change: 1 addition & 0 deletions examples/vite-adapter-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const container = document.querySelector('#root');
const root = createRoot(container);

function App() {
console.log('this is a react app');
return <Suspense fallback={<p>Loading...</p>}>{useRoutes(routes)}</Suspense>;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export const CUSTOM_KEYS = {
external_record: 'external.record',
runtime_isolate: 'runtime.isolate'
};

export const FARM_RUST_PLUGIN_FUNCTION_ENTRY = 'func.js';
29 changes: 23 additions & 6 deletions packages/core/src/plugin/rust/rustPluginResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import { createRequire } from 'node:module';
import path from 'node:path';
import { pathToFileURL } from 'node:url';

export type RustPlugin = string | [string, Record<string, unknown>];
export type RustPlugin = string | [string, any];

type RustPluginPathObject = {
binary: string;
options: Record<string, any>;
};

type RustPluginFunction = (
options?: Record<string, any>
) => [string, typeof options];
/**
* Resolve the binary plugin file, return [filePath, jsonStringifiedOptions]
* @param plugin rust plugin config
Expand All @@ -18,7 +26,7 @@ export async function rustPluginResolver(
if (typeof plugin === 'string') {
pluginPath = plugin;
} else if (Array.isArray(plugin) && plugin.length === 2) {
[pluginPath, options] = [plugin[0], JSON.stringify(plugin[1])];
[pluginPath, options] = [plugin[0], JSON.stringify(plugin[1]) ?? '{}'];
} else {
throw new Error(
'Invalid config: [plugins]. A rust plugin must be a string, or [string, Record<string, any>]'
Expand All @@ -39,13 +47,22 @@ export async function rustPluginResolver(
} else {
pluginPath = await import(pluginPath).then((m) => m.default);
}
// console.log(pluginPath);

// Calling the plugin as a function
if (typeof pluginPath === 'function') {
const [_path, _options] = (pluginPath as RustPluginFunction)();
options = JSON.stringify({
..._options,
...JSON.parse(options)
});
pluginPath = _path;
}

// The entry js file should return { binary: string, options: Record<string, any> } when it's not string
if (typeof pluginPath !== 'string') {
const { binary, options: pluginOptions } = pluginPath as {
binary: string;
options: Record<string, any>;
};
const { binary, options: pluginOptions } =
pluginPath as RustPluginPathObject;
options = JSON.stringify({
...pluginOptions,
...JSON.parse(options)
Expand Down
4 changes: 0 additions & 4 deletions packages/create-farm-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,6 @@ function replaceNamePlaceholders(
name: '<FARM-RUST-PLUGIN-NPM-NAME>',
replace: () => options.pluginName
},
{
name: '<FARM-RUST-PLUGIN-NPM-NAME>',
replace: () => options.pluginName
},
{
name: '<FARM-RUST-PLUGIN-CARGO-NAME>',
replace: () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/create-farm-plugin/templates/rust/options.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IPluginOptions {

}
9 changes: 5 additions & 4 deletions packages/create-farm-plugin/templates/rust/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
},
"exports": {
".": {
"import": "./scripts/index.js",
"require": "./scripts/index.js",
"types": "./scripts/index.d.ts"
"import": "./scripts/func.js",
"types": "./scripts/index.d.ts",
"default": "./scripts/index.js"
},
"./package.json": "./package.json"
},
Expand All @@ -37,6 +37,7 @@
"prepublishOnly": "farm-plugin-tools prepublish"
},
"files": [
"scripts"
"scripts",
"options.d.ts"
]
}
3 changes: 3 additions & 0 deletions packages/create-farm-plugin/templates/rust/scripts/func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import binPath from "./index.js";

export default (options)=>[binPath,options];
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const binPath: string;
import type { IPluginOptions } from '../options.d';
declare const binPath: (options?: IPluginOptions) => [string, IPluginOptions];
export default binPath;
Loading
Loading