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

chore: build plugin success #1

Merged
merged 11 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
121 changes: 90 additions & 31 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,56 +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 --abi linux-x64-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 --abi linux-x64-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 --abi win32-ia32-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 --abi win32-arm64-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 [email protected] && 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'
cache: 'pnpm'
- 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 -- --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 }}
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

name: ${{ github.sha }}-${{ matrix.settings.abi }}-plugin
path: npm/${{ matrix.settings.abi }}/index.farm
34 changes: 31 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
name: Cross Build On Linux
on:
branches:
- main
pull_request:
branches:
- main

jobs:
call-rust-build:
uses: ./.github/workflows/build.yaml
uses: ./.github/workflows/build.yaml

test-artifacts:
name: Test Artifacts
needs: [call-rust-build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# 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
do
mv /tmp/artifacts/${{ github.sha }}-${abi}-plugin/* ./npm/${abi}

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 [email protected]
- name: Test Example
run: cd example && pnpm i && pnpm build
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

node_modules/
node_modules/
*.node
*.farm
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# rust-plugin-example
Rust Plugins Example for Farm
Rust Plugins Example for Farm.

## Building Your Rust Plugin

2 changes: 1 addition & 1 deletion example/farm.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from '@farmfe/core';

export default defineConfig({
plugins: ['@farmfe/plugin-react']
plugins: ['@farmfe/plugin-react', 'farm-plugin-example']
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"license": "MIT",
"devDependencies": {
"@farmfe/plugin-tools": "latest"
"@farmfe/plugin-tools": "^0.0.3"
},
"napi": {
"name": "farm_plugin_example",
Expand Down Expand Up @@ -36,4 +36,4 @@
"files": [
"scripts"
]
}
}
16 changes: 12 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

#[farm_plugin]
pub struct FarmPluginExample {}

impl FarmPluginExample {
fn new(config: &Config, options: String) -> Self {
let opts: Options = serde_json::from_str(&options).unwrap();
Self {}
}
}
Expand All @@ -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<farmfe_core::context::CompilationContext>,
_hook_context: &farmfe_core::plugin::PluginHookContext,
) -> farmfe_core::error::Result<Option<farmfe_core::plugin::PluginResolveHookResult>> {
println!("resolve {:?} from {:?}", param.source, param.importer);
Ok(None)
}

fn load(
&self,
param: &farmfe_core::plugin::PluginLoadHookParam,
_context: &std::sync::Arc<farmfe_core::context::CompilationContext>,
_hook_context: &farmfe_core::plugin::PluginHookContext,
) -> farmfe_core::error::Result<Option<farmfe_core::plugin::PluginLoadHookResult>> {
println!(
"load path: {:?}, id: {:?}",
param.resolved_path, param.module_id
);
Ok(None)
}
}
Loading