From 178ebad2590eb8e906bc7fa40290f1909af148e2 Mon Sep 17 00:00:00 2001 From: CCherry07 <2405693142@qq.com> Date: Sun, 27 Oct 2024 23:04:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor(rust-plugins):=20=F0=9F=92=A1=20worker?= =?UTF-8?q?=20plugin=20inline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../worker/npm/darwin-arm64/package.json | 4 +- rust-plugins/worker/playground/farm.config.ts | 1 + rust-plugins/worker/playground/src/index.tsx | 2 +- rust-plugins/worker/src/lib.rs | 39 ++++--------------- 4 files changed, 12 insertions(+), 34 deletions(-) diff --git a/rust-plugins/worker/npm/darwin-arm64/package.json b/rust-plugins/worker/npm/darwin-arm64/package.json index 16611a4..8e311fd 100644 --- a/rust-plugins/worker/npm/darwin-arm64/package.json +++ b/rust-plugins/worker/npm/darwin-arm64/package.json @@ -1,6 +1,6 @@ { "name": "farmfe-plugin-worker-darwin-arm64", - "version": "0.1.2", + "version": "0.0.2", "os": [ "darwin" ], @@ -15,4 +15,4 @@ "engines": { "node": ">= 10" } -} +} \ No newline at end of file diff --git a/rust-plugins/worker/playground/farm.config.ts b/rust-plugins/worker/playground/farm.config.ts index 4a50866..b03558b 100644 --- a/rust-plugins/worker/playground/farm.config.ts +++ b/rust-plugins/worker/playground/farm.config.ts @@ -12,6 +12,7 @@ export default defineConfig({ plugins: [ react({ runtime: "automatic" }), farmPlugin({ + isBuild: true, compilerConfig:{ output:{ assetsFilename: '[resourceName].worker.[hash].[ext]', diff --git a/rust-plugins/worker/playground/src/index.tsx b/rust-plugins/worker/playground/src/index.tsx index c6d69ec..47c6f58 100644 --- a/rust-plugins/worker/playground/src/index.tsx +++ b/rust-plugins/worker/playground/src/index.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { createRoot } from 'react-dom/client'; import { Main } from './main'; -import TestWorker from "./worker/test.worker?worker" +import TestWorker from "./worker/test.worker?worker&inline" import './index.css' console.log(TestWorker); diff --git a/rust-plugins/worker/src/lib.rs b/rust-plugins/worker/src/lib.rs index cbc473c..16be6af 100644 --- a/rust-plugins/worker/src/lib.rs +++ b/rust-plugins/worker/src/lib.rs @@ -14,13 +14,12 @@ use farmfe_core::{ }, context::EmitFileParams, module::ModuleType, - plugin::{Plugin, PluginLoadHookResult, PluginResolveHookResult, PluginTransformHookResult}, + plugin::{Plugin, PluginLoadHookResult, PluginTransformHookResult}, resource::ResourceType, serde, serde_json, }; use farmfe_macro_plugin::farm_plugin; use farmfe_toolkit::fs::transform_output_filename; -use farmfe_utils::parse_query; use regress::Regex as JsRegex; use serde::{Deserialize, Serialize}; @@ -167,6 +166,7 @@ fn process_worker(param: ProcessWorkerParam) -> String { if worker_inline_match.is_some() { let content_bytes = worker_cache.get(resolved_path).unwrap(); let content_base64 = general_purpose::STANDARD.encode(content_bytes); + let content_base64_code = format!(r#"const encodedJs = "{}";"#, content_base64); let code = if worker_constructor == "Worker" { let blob_url = if worker_type == "classic" { String::from("") @@ -195,7 +195,7 @@ fn process_worker(param: ProcessWorkerParam) -> String { ); }}{4} }}"#, - content_base64, + content_base64_code, blob_url, worker_constructor, worker_type_option, @@ -214,11 +214,11 @@ fn process_worker(param: ProcessWorkerParam) -> String { r#"{0} export default function WorkerWrapper(options) {{ return new {1}( - "data:text/javascript;base64," + {0}, + "data:text/javascript;base64," + encodedJs, {2} ); }}"#, - content_base64, worker_constructor, worker_type_option + content_base64_code, worker_constructor, worker_type_option ) }; return code; @@ -275,31 +275,9 @@ impl Plugin for FarmfePluginWorker { fn name(&self) -> &str { "FarmfePluginWorker" } - - fn resolve( - &self, - param: &farmfe_core::plugin::PluginResolveHookParam, - _context: &std::sync::Arc, - _hook_context: &farmfe_core::plugin::PluginHookContext, - ) -> farmfe_core::error::Result> { - let id = ¶m.source; - if JsRegex::new(WORKER_OR_SHARED_WORKER_RE) - .unwrap() - .find(id) - .is_some() - { - let (clean_path, query) = ¶m.source.split_once("?").unwrap_or((id, "")); - let query = parse_query(&format!("?{}", query)); - return Ok(Some(PluginResolveHookResult { - resolved_path: clean_path.to_string(), - query, - ..Default::default() - })); - } - - return Ok(None); + fn priority(&self) -> i32 { + 105 } - fn load( &self, param: &farmfe_core::plugin::PluginLoadHookParam, @@ -328,7 +306,6 @@ impl Plugin for FarmfePluginWorker { if param.module_type != ModuleType::Custom("worker".to_string()) { return Ok(None); } - let worker_match = JsRegex::new(WORKER_OR_SHARED_WORKER_RE) .unwrap() .find(¶m.module_id); @@ -345,7 +322,7 @@ impl Plugin for FarmfePluginWorker { worker_cache: &self.worker_cache, context, }); - + return Ok(Some(PluginTransformHookResult { content: code, module_type: Some(ModuleType::Js),