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