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