Skip to main content

Library/Fn/Worker/
mod.rs

1//! Web Worker compilation support
2//!
3//! This module provides:
4//! - Worker file detection and classification
5//! - Worker bootstrap code generation
6//! - Module handling for worker contexts
7
8#[path = "Detect.rs"]
9pub mod Detect;
10#[path = "Bootstrap.rs"]
11pub mod Bootstrap;
12#[path = "Compile.rs"]
13pub mod Compile;
14
15pub use Detect::WorkerDetector;
16pub use Bootstrap::WorkerBootstrap;
17pub use Compile::WorkerCompiler;
18
19/// Configuration for worker compilation
20#[derive(Debug, Clone, Default)]
21pub struct WorkerConfig {
22	/// Enable worker compilation
23	pub enabled:bool,
24	/// Output directory for worker bundles
25	pub output_dir:String,
26	/// Whether to inline dependencies
27	pub inline_dependencies:bool,
28	/// Worker type: "classic" or "module"
29	pub worker_type:WorkerType,
30	/// Additional scripts to include in worker bootstrap
31	pub bootstrap_scripts:Vec<String>,
32	/// Whether to generate source maps for workers
33	pub source_maps:bool,
34}
35
36impl WorkerConfig {
37	pub fn new() -> Self {
38		Self {
39			enabled:true,
40			output_dir:"out/workers".to_string(),
41			inline_dependencies:true,
42			worker_type:WorkerType::Module,
43			bootstrap_scripts:Vec::new(),
44			source_maps:true,
45		}
46	}
47}
48
49/// Type of web worker
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
51pub enum WorkerType {
52	/// Classic worker (shared worker context)
53	Classic,
54	/// Module worker (ES modules)
55	#[default]
56	Module,
57}
58
59impl WorkerType {
60	pub fn as_str(&self) -> &'static str {
61		match self {
62			WorkerType::Classic => "classic",
63			WorkerType::Module => "module",
64		}
65	}
66}
67
68/// Information about a detected worker file
69#[derive(Debug, Clone)]
70pub struct WorkerInfo {
71	/// Path to the worker source file
72	pub source_path:String,
73	/// Path to the output worker bundle
74	pub output_path:String,
75	/// Name of the worker (derived from filename)
76	pub name:String,
77	/// The type of worker
78	pub worker_type:WorkerType,
79	/// Dependencies that need to be bundled
80	pub dependencies:Vec<String>,
81	/// Whether this is a shared worker
82	pub is_shared:bool,
83}
84
85impl WorkerInfo {
86	pub fn new(source_path:impl Into<String>, worker_type:WorkerType) -> Self {
87		let source_path = source_path.into();
88		let name = std::path::Path::new(&source_path)
89			.file_stem()
90			.and_then(|s| s.to_str())
91			.unwrap_or("worker")
92			.to_string();
93
94		Self {
95			source_path:source_path.clone(),
96			output_path:source_path,
97			name,
98			worker_type,
99			dependencies:Vec::new(),
100			is_shared:false,
101		}
102	}
103}