From 9af6e1d85c9623a58880ee2be188010f5f9f3eef Mon Sep 17 00:00:00 2001 From: brightwwu Date: Sat, 10 Feb 2024 19:42:51 +0800 Subject: [PATCH 01/11] chore: build plugin success --- .gitignore | 4 +++- Cargo.toml | 2 ++ README.md | 5 ++++- example/farm.config.ts | 2 +- package.json | 2 +- pnpm-lock.yaml | 14 +++++++++++--- src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 7 files changed, 55 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index ece4a24..7436799 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,6 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb -node_modules/ \ No newline at end of file +node_modules/ +*.node +*.farm diff --git a/Cargo.toml b/Cargo.toml index 6ef7175..7390df4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,5 @@ crate-type = ["cdylib", "rlib"] farmfe_core = { version = "*" } farmfe_toolkit_plugin_types = { version = "*" } farmfe_macro_plugin = { version = "*" } +serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1.0" } \ No newline at end of file diff --git a/README.md b/README.md index 137c239..cb1fec7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # rust-plugin-example -Rust Plugins Example for Farm +Rust Plugins Example for Farm. + +## Building Your Rust Plugin + diff --git a/example/farm.config.ts b/example/farm.config.ts index 477cb13..1974d51 100644 --- a/example/farm.config.ts +++ b/example/farm.config.ts @@ -1,5 +1,5 @@ import { defineConfig } from '@farmfe/core'; export default defineConfig({ - plugins: ['@farmfe/plugin-react'] + plugins: ['@farmfe/plugin-react', 'farm-plugin-example'] }); diff --git a/package.json b/package.json index 4d1d034..7c18ee6 100644 --- a/package.json +++ b/package.json @@ -36,4 +36,4 @@ "files": [ "scripts" ] -} +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9002fe1..44196bf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,11 +7,19 @@ settings: devDependencies: '@farmfe/plugin-tools': specifier: latest - version: 0.0.1 + version: 0.0.2 packages: - /@farmfe/plugin-tools@0.0.1: - resolution: {integrity: sha512-Oa4ltAT23ueI18eeA1DT85uutJxqzkvHv5C/CyawwBQYfWJ5otg0KPXBPPwqemw79LI1CpmckSaMvv6Lai/Hkw==} + /@farmfe/plugin-tools@0.0.2: + resolution: {integrity: sha512-46/x6HU5xPIT+AFc9WQjE4wY1czA0gXkw3a62R6uDH7f8H/ixPaw8EHC0raH8DxNu3zdTx+ClH2mKtHPCvSdQg==} + hasBin: true + dependencies: + '@napi-rs/cli': 2.18.0 + dev: true + + /@napi-rs/cli@2.18.0: + resolution: {integrity: sha512-lfSRT7cs3iC4L+kv9suGYQEezn5Nii7Kpu+THsYVI0tA1Vh59LH45p4QADaD7hvIkmOz79eEGtoKQ9nAkAPkzA==} + engines: {node: '>= 10'} hasBin: true dev: true diff --git a/src/lib.rs b/src/lib.rs index 1823011..56b150d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,11 +4,17 @@ use farmfe_core::{config::Config, plugin::Plugin}; use farmfe_macro_plugin::farm_plugin; +#[derive(serde::Deserialize)] +pub struct Options { + pub my_option: Option, +} + #[farm_plugin] pub struct FarmPluginExample {} impl FarmPluginExample { fn new(config: &Config, options: String) -> Self { + let opts: Options = serde_json::from_str(&options).unwrap(); Self {} } } @@ -17,4 +23,31 @@ impl Plugin for FarmPluginExample { fn name(&self) -> &str { "FarmPluginExample" } + + fn priority(&self) -> i32 { + 101 + } + + fn resolve( + &self, + param: &farmfe_core::plugin::PluginResolveHookParam, + _context: &std::sync::Arc, + _hook_context: &farmfe_core::plugin::PluginHookContext, + ) -> farmfe_core::error::Result> { + println!("resolve {:?} from {:?}", param.source, param.importer); + Ok(None) + } + + fn load( + &self, + param: &farmfe_core::plugin::PluginLoadHookParam, + _context: &std::sync::Arc, + _hook_context: &farmfe_core::plugin::PluginHookContext, + ) -> farmfe_core::error::Result> { + println!( + "load path: {:?}, id: {:?}", + param.resolved_path, param.module_id + ); + Ok(None) + } } From 3b654c89f3ac34b2b4a20ee825ab77f2978a487b Mon Sep 17 00:00:00 2001 From: brightwwu Date: Sat, 10 Feb 2024 19:45:04 +0800 Subject: [PATCH 02/11] chore: commit ci --- .github/workflows/ci.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 006ef24..8bf392b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,7 +1,8 @@ name: Cross Build On Linux on: - branches: - - main + pull_request: + branches: + - main jobs: call-rust-build: From 4a57d0bb47a32e1277b2b13e15438398993b79de Mon Sep 17 00:00:00 2001 From: brightwwu Date: Sat, 10 Feb 2024 19:49:09 +0800 Subject: [PATCH 03/11] chore: install pnpm --- .github/workflows/build.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index fedabb8..5a6a462 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -40,7 +40,8 @@ jobs: - uses: actions/setup-node@v4 with: node-version: '18' - cache: 'pnpm' + - name: Install pnpm + run: npm install -g pnpm@8 - name: Install ziglang uses: goto-bus-stop/setup-zig@v1 with: From eb9c2f227c0f064d0717cec4fc8a2c7e0c86d491 Mon Sep 17 00:00:00 2001 From: brightwwu Date: Sun, 11 Feb 2024 19:09:05 +0800 Subject: [PATCH 04/11] chore: test:ci scripts --- .github/workflows/build.yaml | 122 ++++++++++++++++++++++++++--------- .github/workflows/ci.yaml | 16 ++++- 2 files changed, 105 insertions(+), 33 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 5a6a462..123717b 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,57 +1,115 @@ name: Building Rust Binding And Upload Artifacts on: workflow_call -env: - DEBUG: 'napi:*' - MACOSX_DEPLOYMENT_TARGET: '10.13' - jobs: build: + name: Build and Upload Artifacts - ${{ matrix.settings.abi }} + runs-on: ${{ matrix.settings.os }} strategy: fail-fast: false matrix: settings: - - target: x86_64-unknown-linux-gnu + - os: ubuntu-latest + docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian abi: linux-x64-gnu - - target: x86_64-unknown-linux-musl + build: >- + set -e && + unset CC_x86_64_unknown_linux_gnu && + unset CC && + npm run build -- --target x86_64-unknown-linux-gnu + - os: ubuntu-latest + docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine abi: linux-x64-musl - - target: x86_64-apple-darwin - abi: darwin-x64 - - target: x86_64-pc-windows-msvc + build: >- + set -e && + unset CC_x86_64_unknown_linux_musl && + unset CC && + npm run build -- --target x86_64-unknown-linux-musl + - os: windows-latest abi: win32-x64-msvc - - target: i686-pc-windows-msvc + - os: macos-latest + abi: darwin-x64 + + # cross compile + # windows. Note swc plugins is not supported on ia32 and arm64 + - os: windows-latest abi: win32-ia32-msvc - - target: aarch64-pc-windows-msvc + target: i686-pc-windows-msvc + build: | + export CARGO_PROFILE_RELEASE_LTO=false + cargo install cargo-xwin + npm run build -- --target i686-pc-windows-msvc --cargo-flags="--no-default-features" + - os: windows-latest abi: win32-arm64-msvc - - target: aarch64-unknown-linux-gnu - abi: linux-arm64-gnu - - target: aarch64-unknown-linux-musl + target: aarch64-pc-windows-msvc + build: | + export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=256 + export CARGO_PROFILE_RELEASE_LTO=false + cargo install cargo-xwin + npm run build -- --target aarch64-pc-windows-msvc --cargo-flags="--no-default-features" + # linux + - os: ubuntu-latest abi: linux-arm64-musl - - target: aarch64-apple-darwin + target: aarch64-unknown-linux-musl + zig: true + - os: ubuntu-latest + abi: linux-arm64-gnu + target: aarch64-unknown-linux-gnu + zig: true + # macos + - os: macos-latest abi: darwin-arm64 - runs-on: ubuntu-latest + target: aarch64-apple-darwin + zig: false + # - os: ubuntu-latest + # abi: darwin-x64 + # target: x86_64-apple-darwin + # osxcross: true + # zig: true + # - os: ubuntu-latest + # abi: darwin-arm64 + # target: aarch64-apple-darwin + # osxcross: true + # zig: true steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v3 - name: Cache rust artifacts uses: Swatinem/rust-cache@v2 with: shared-key: rust-build-${{ matrix.settings.abi }} + - uses: actions/setup-node@v3 + with: + node-version: 18 + - name: Install Dependencies + run: npm install -g pnpm@8.15.1 && pnpm i --frozen-lockfile - run: rustup target add ${{ matrix.settings.target }} - - uses: actions/setup-node@v4 + if: ${{ matrix.settings.target }} + # Use the v1 of this action + - uses: mbround18/setup-osxcross@v1 + if: ${{ matrix.settings.osxcross }} + # This builds executables & sets env variables for rust to consume. with: - node-version: '18' - - name: Install pnpm - run: npm install -g pnpm@8 - - name: Install ziglang - uses: goto-bus-stop/setup-zig@v1 + osx-version: '12.3' + - uses: goto-bus-stop/setup-zig@v2 + if: ${{ matrix.settings.zig }} + - name: Build in docker + uses: addnab/docker-run-action@v3 + if: ${{ matrix.settings.docker }} with: - version: 0.11.0 - - run: cargo install cargo-xwin - - run: pnpm install - - run: pnpm build --target ${{ matrix.settings.target }} - - uses: actions/upload-artifact@v3 + image: ${{ matrix.settings.docker }} + options: -v ${{ env.HOME }}/.cargo/git:/root/.cargo/git -v ${{ env.HOME }}/.cargo/registry:/root/.cargo/registry -v ${{ github.workspace }}:/build -w /build + run: ${{ matrix.settings.build }} + - name: Default Build + if: ${{ !matrix.settings.docker && !matrix.settings.build }} + run: >- + npm run build -- ${{ matrix.settings.target && format('--target {0}', matrix.settings.target) || '' }} ${{ matrix.settings.zig && '--zig' || '' }} + shell: bash + - name: Build + if: ${{ !matrix.settings.docker && matrix.settings.build }} + run: ${{ matrix.settings.build }} + shell: bash + - name: Upload Plugin + uses: actions/upload-artifact@v3 with: - name: ${{ github.sha }}-${{ matrix.settings.abi }} - path: npm/**/index.farm - if-no-files-found: error - \ No newline at end of file + name: ${{ github.sha }}-${{ matrix.settings.abi }}-plugin + path: npm/${{ matrix.settings.abi }}/index.farm diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8bf392b..254facc 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -6,4 +6,18 @@ on: jobs: call-rust-build: - uses: ./.github/workflows/build.yaml \ No newline at end of file + uses: ./.github/workflows/build.yaml + + test-artifacts: + name: Test Artifacts + needs: [call-rust-build] + runs-on: ubuntu-latest + steps: + - name: Check Artifacts + run: | + for abi in linux-x64-gnu linux-x64-musl darwin-x64 win32-x64-msvc linux-arm64-musl linux-arm64-gnu darwin-arm64 win32-ia32-msvc win32-arm64-msvc + do + mv /tmp/artifacts/${{ github.sha }}-${abi}/* ./npm/${abi} + + test -f ./npm/${abi}/index.farm + done \ No newline at end of file From 0048758d669e715c3e84d56fa7f318d10ee58620 Mon Sep 17 00:00:00 2001 From: brightwwu Date: Mon, 12 Feb 2024 16:53:24 +0800 Subject: [PATCH 05/11] chore: update ci yaml --- .github/workflows/ci.yaml | 4 +++- package.json | 2 +- pnpm-lock.yaml | 8 ++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 254facc..763d18a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,4 +20,6 @@ jobs: mv /tmp/artifacts/${{ github.sha }}-${abi}/* ./npm/${abi} test -f ./npm/${abi}/index.farm - done \ No newline at end of file + done + - name: Test Example + run: cd example && pnpm i && pnpm build \ No newline at end of file diff --git a/package.json b/package.json index 7c18ee6..284f47c 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "type": "module", "license": "MIT", "devDependencies": { - "@farmfe/plugin-tools": "latest" + "@farmfe/plugin-tools": "^0.0.3" }, "napi": { "name": "farm_plugin_example", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 44196bf..68ecfe0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,13 +6,13 @@ settings: devDependencies: '@farmfe/plugin-tools': - specifier: latest - version: 0.0.2 + specifier: ^0.0.3 + version: 0.0.3 packages: - /@farmfe/plugin-tools@0.0.2: - resolution: {integrity: sha512-46/x6HU5xPIT+AFc9WQjE4wY1czA0gXkw3a62R6uDH7f8H/ixPaw8EHC0raH8DxNu3zdTx+ClH2mKtHPCvSdQg==} + /@farmfe/plugin-tools@0.0.3: + resolution: {integrity: sha512-3ZgsNzDrX5s2JgrCcf6pQnwQZK0yJisyT1XvzZEHYHmUk8WC84kM5gIWFg/hR/lVJf8xtcCFxCSFCZcyDsLyCg==} hasBin: true dependencies: '@napi-rs/cli': 2.18.0 From 943c0f8ec4a75998e18d8b7bab04a8a201133f6b Mon Sep 17 00:00:00 2001 From: brightwwu Date: Mon, 12 Feb 2024 17:17:51 +0800 Subject: [PATCH 06/11] chore: update ci scripts --- .github/workflows/build.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 123717b..d4a1617 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -16,7 +16,7 @@ jobs: set -e && unset CC_x86_64_unknown_linux_gnu && unset CC && - npm run build -- --target x86_64-unknown-linux-gnu + npm run build -- --target x86_64-unknown-linux-gnu --abi linux-x64-gnu - os: ubuntu-latest docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine abi: linux-x64-musl @@ -24,7 +24,7 @@ jobs: set -e && unset CC_x86_64_unknown_linux_musl && unset CC && - npm run build -- --target x86_64-unknown-linux-musl + npm run build -- --target x86_64-unknown-linux-musl --abi linux-x64-musl - os: windows-latest abi: win32-x64-msvc - os: macos-latest @@ -38,7 +38,7 @@ jobs: build: | export CARGO_PROFILE_RELEASE_LTO=false cargo install cargo-xwin - npm run build -- --target i686-pc-windows-msvc --cargo-flags="--no-default-features" + npm run build -- --target i686-pc-windows-msvc --abi win32-ia32-msvc --cargo-flags="--no-default-features" - os: windows-latest abi: win32-arm64-msvc target: aarch64-pc-windows-msvc @@ -46,7 +46,7 @@ jobs: export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=256 export CARGO_PROFILE_RELEASE_LTO=false cargo install cargo-xwin - npm run build -- --target aarch64-pc-windows-msvc --cargo-flags="--no-default-features" + npm run build -- --target aarch64-pc-windows-msvc --abi win32-arm64-msvc --cargo-flags="--no-default-features" # linux - os: ubuntu-latest abi: linux-arm64-musl @@ -102,7 +102,7 @@ jobs: - name: Default Build if: ${{ !matrix.settings.docker && !matrix.settings.build }} run: >- - npm run build -- ${{ matrix.settings.target && format('--target {0}', matrix.settings.target) || '' }} ${{ matrix.settings.zig && '--zig' || '' }} + npm run build -- --abi ${{ matrix.settings.abi }} ${{ matrix.settings.target && format('--target {0}', matrix.settings.target) || '' }} ${{ matrix.settings.zig && '--zig' || '' }} shell: bash - name: Build if: ${{ !matrix.settings.docker && matrix.settings.build }} From fd59318a39a90ff1abeeaa35871db1986c132bcf Mon Sep 17 00:00:00 2001 From: brightwwu Date: Mon, 12 Feb 2024 17:39:35 +0800 Subject: [PATCH 07/11] chore: download artifacts --- .github/workflows/ci.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 763d18a..2070c6c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -13,6 +13,10 @@ jobs: needs: [call-rust-build] runs-on: ubuntu-latest steps: + # batch download artifacts + - uses: actions/download-artifact@v3 + with: + path: /tmp/artifacts - name: Check Artifacts run: | for abi in linux-x64-gnu linux-x64-musl darwin-x64 win32-x64-msvc linux-arm64-musl linux-arm64-gnu darwin-arm64 win32-ia32-msvc win32-arm64-msvc From 0a27537fc8453b247f7dc2bb87ac53a4de82ea57 Mon Sep 17 00:00:00 2001 From: brightwwu Date: Mon, 12 Feb 2024 17:49:03 +0800 Subject: [PATCH 08/11] fix: ci tests --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b097bcd..7a15f44 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -31,7 +31,7 @@ jobs: run: | for abi in linux-x64-gnu linux-x64-musl darwin-x64 win32-x64-msvc linux-arm64-musl linux-arm64-gnu darwin-arm64 win32-ia32-msvc win32-arm64-msvc do - mv /tmp/artifacts/${{ github.sha }}-${abi}/* ./npm/${abi} + mv /tmp/artifacts/${{ github.sha }}-${abi}-plugin/* ./npm/${abi} test -f ./npm/${abi}/index.farm done From c06edff940d1a43a9e157cffc7ad224ce5f749ec Mon Sep 17 00:00:00 2001 From: brightwwu Date: Mon, 12 Feb 2024 19:04:01 +0800 Subject: [PATCH 09/11] fix: ci.yaml --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2070c6c..dca95f1 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -21,7 +21,7 @@ jobs: run: | for abi in linux-x64-gnu linux-x64-musl darwin-x64 win32-x64-msvc linux-arm64-musl linux-arm64-gnu darwin-arm64 win32-ia32-msvc win32-arm64-msvc do - mv /tmp/artifacts/${{ github.sha }}-${abi}/* ./npm/${abi} + mv /tmp/artifacts/${{ github.sha }}-${abi}-plugin/* ./npm/${abi} test -f ./npm/${abi}/index.farm done From 9b404cffa5e49277c775e5246f0dd0cb488c591f Mon Sep 17 00:00:00 2001 From: brightwwu Date: Mon, 12 Feb 2024 19:14:52 +0800 Subject: [PATCH 10/11] fix: ci.yaml checkout repo --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index dca95f1..b212370 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -13,6 +13,7 @@ jobs: needs: [call-rust-build] runs-on: ubuntu-latest steps: + - uses: actions/checkout@v3 # batch download artifacts - uses: actions/download-artifact@v3 with: From a96b4805cf9713d4f1975425cba6906717cbe93b Mon Sep 17 00:00:00 2001 From: brightwwu Date: Tue, 13 Feb 2024 10:36:12 +0800 Subject: [PATCH 11/11] chore: set up node in ci.yaml --- .github/workflows/ci.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b212370..b6e8a70 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -26,5 +26,11 @@ jobs: test -f ./npm/${abi}/index.farm done + - name: Setup Node.js 18.x + uses: actions/setup-node@v3 + with: + node-version: 18.x + - name: Install Dependencies + run: npm install -g pnpm@8.15.1 - name: Test Example run: cd example && pnpm i && pnpm build \ No newline at end of file