vize_atelier_sfc 0.65.0

Atelier SFC - The Single File Component workshop for Vize
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! SFC type definitions.
//!
//! Zero-copy design using borrowed strings for maximum parsing performance.

use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use vize_carton::{FxHashMap, String};

// Re-export from vize_relief to avoid duplication
pub use vize_atelier_core::options::{BindingMetadata, BindingType};

/// SFC Descriptor - parsed result of a .vue file
/// Uses Cow<str> for zero-copy parsing with optional ownership
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SfcDescriptor<'a> {
    /// Filename
    #[serde(borrow)]
    pub filename: Cow<'a, str>,

    /// Source code
    #[serde(borrow)]
    pub source: Cow<'a, str>,

    /// Template block
    pub template: Option<SfcTemplateBlock<'a>>,

    /// Script block (options API or <script> without setup)
    pub script: Option<SfcScriptBlock<'a>>,

    /// Script setup block
    pub script_setup: Option<SfcScriptBlock<'a>>,

    /// Style blocks
    pub styles: Vec<SfcStyleBlock<'a>>,

    /// Custom blocks
    pub custom_blocks: Vec<SfcCustomBlock<'a>>,

    /// CSS variables from <style> v-bind
    #[serde(borrow)]
    pub css_vars: Vec<Cow<'a, str>>,

    /// Whether the SFC uses slots
    #[serde(default)]
    pub slotted: bool,

    /// Whether the component should inherit attrs
    #[serde(default)]
    pub should_force_reload: bool,
}

impl<'a> Default for SfcDescriptor<'a> {
    fn default() -> Self {
        Self {
            filename: Cow::Borrowed(""),
            source: Cow::Borrowed(""),
            template: None,
            script: None,
            script_setup: None,
            styles: Vec::new(),
            custom_blocks: Vec::new(),
            css_vars: Vec::new(),
            slotted: false,
            should_force_reload: false,
        }
    }
}

impl<'a> SfcDescriptor<'a> {
    /// Convert to owned version (for serialization or storage)
    pub fn into_owned(self) -> SfcDescriptor<'static> {
        SfcDescriptor {
            filename: Cow::Owned(self.filename.into_owned()),
            source: Cow::Owned(self.source.into_owned()),
            template: self.template.map(|t| t.into_owned()),
            script: self.script.map(|s| s.into_owned()),
            script_setup: self.script_setup.map(|s| s.into_owned()),
            styles: self.styles.into_iter().map(|s| s.into_owned()).collect(),
            custom_blocks: self
                .custom_blocks
                .into_iter()
                .map(|c| c.into_owned())
                .collect(),
            css_vars: self
                .css_vars
                .into_iter()
                .map(|s| Cow::Owned(s.into_owned()))
                .collect(),
            slotted: self.slotted,
            should_force_reload: self.should_force_reload,
        }
    }

    /// Compute hash of the template block content.
    /// Returns None if there is no template block.
    pub fn template_hash(&self) -> Option<vize_carton::String> {
        self.template
            .as_ref()
            .map(|t| vize_carton::hash::content_hash(&t.content))
    }

    /// Compute hash of all style blocks content.
    /// Returns None if there are no style blocks.
    pub fn style_hash(&self) -> Option<vize_carton::String> {
        if self.styles.is_empty() {
            return None;
        }
        let mut combined = vize_carton::String::default();
        for style in &self.styles {
            combined.push_str(&style.content);
            combined.push('\0'); // Separator
        }
        Some(vize_carton::hash::content_hash(&combined))
    }

    /// Compute hash of the script blocks (script + script_setup) content.
    /// Returns None if there are no script blocks.
    pub fn script_hash(&self) -> Option<vize_carton::String> {
        let script_content = self.script.as_ref().map(|s| s.content.as_ref());
        let script_setup_content = self.script_setup.as_ref().map(|s| s.content.as_ref());

        match (script_content, script_setup_content) {
            (None, None) => None,
            (Some(s), None) => Some(vize_carton::hash::content_hash(s)),
            (None, Some(ss)) => Some(vize_carton::hash::content_hash(ss)),
            (Some(s), Some(ss)) => {
                let mut combined = vize_carton::String::with_capacity(s.len() + ss.len() + 1);
                combined.push_str(s);
                combined.push('\0');
                combined.push_str(ss);
                Some(vize_carton::hash::content_hash(&combined))
            }
        }
    }
}

/// Template block
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SfcTemplateBlock<'a> {
    /// Block content
    #[serde(borrow)]
    pub content: Cow<'a, str>,

    /// Block location in source
    pub loc: BlockLocation,

    /// Template language (default: html)
    #[serde(default, borrow)]
    pub lang: Option<Cow<'a, str>>,

    /// Source attribute for external template
    #[serde(default, borrow)]
    pub src: Option<Cow<'a, str>>,

    /// Additional attributes
    #[serde(default)]
    pub attrs: FxHashMap<Cow<'a, str>, Cow<'a, str>>,
}

impl<'a> SfcTemplateBlock<'a> {
    /// Convert to owned version
    pub fn into_owned(self) -> SfcTemplateBlock<'static> {
        SfcTemplateBlock {
            content: Cow::Owned(self.content.into_owned()),
            loc: self.loc,
            lang: self.lang.map(|s| Cow::Owned(s.into_owned())),
            src: self.src.map(|s| Cow::Owned(s.into_owned())),
            attrs: self
                .attrs
                .into_iter()
                .map(|(k, v)| (Cow::Owned(k.into_owned()), Cow::Owned(v.into_owned())))
                .collect(),
        }
    }
}

/// Script block
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SfcScriptBlock<'a> {
    /// Block content
    #[serde(borrow)]
    pub content: Cow<'a, str>,

    /// Block location in source
    pub loc: BlockLocation,

    /// Script language (js/ts)
    #[serde(default, borrow)]
    pub lang: Option<Cow<'a, str>>,

    /// Source attribute for external script
    #[serde(default, borrow)]
    pub src: Option<Cow<'a, str>>,

    /// Whether this is script setup
    #[serde(default)]
    pub setup: bool,

    /// Additional attributes
    #[serde(default)]
    pub attrs: FxHashMap<Cow<'a, str>, Cow<'a, str>>,

    /// Binding metadata (filled after analysis)
    #[serde(default)]
    pub bindings: Option<BindingMetadata>,
}

impl<'a> SfcScriptBlock<'a> {
    /// Convert to owned version
    pub fn into_owned(self) -> SfcScriptBlock<'static> {
        SfcScriptBlock {
            content: Cow::Owned(self.content.into_owned()),
            loc: self.loc,
            lang: self.lang.map(|s| Cow::Owned(s.into_owned())),
            src: self.src.map(|s| Cow::Owned(s.into_owned())),
            setup: self.setup,
            attrs: self
                .attrs
                .into_iter()
                .map(|(k, v)| (Cow::Owned(k.into_owned()), Cow::Owned(v.into_owned())))
                .collect(),
            bindings: self.bindings,
        }
    }
}

/// Style block
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SfcStyleBlock<'a> {
    /// Block content
    #[serde(borrow)]
    pub content: Cow<'a, str>,

    /// Block location in source
    pub loc: BlockLocation,

    /// Style language (css/scss/less/etc)
    #[serde(default, borrow)]
    pub lang: Option<Cow<'a, str>>,

    /// Source attribute for external style
    #[serde(default, borrow)]
    pub src: Option<Cow<'a, str>>,

    /// Whether the style is scoped
    #[serde(default)]
    pub scoped: bool,

    /// Whether the style is a CSS module
    #[serde(default, borrow)]
    pub module: Option<Cow<'a, str>>,

    /// Additional attributes
    #[serde(default)]
    pub attrs: FxHashMap<Cow<'a, str>, Cow<'a, str>>,
}

impl<'a> SfcStyleBlock<'a> {
    /// Convert to owned version
    pub fn into_owned(self) -> SfcStyleBlock<'static> {
        SfcStyleBlock {
            content: Cow::Owned(self.content.into_owned()),
            loc: self.loc,
            lang: self.lang.map(|s| Cow::Owned(s.into_owned())),
            src: self.src.map(|s| Cow::Owned(s.into_owned())),
            scoped: self.scoped,
            module: self.module.map(|s| Cow::Owned(s.into_owned())),
            attrs: self
                .attrs
                .into_iter()
                .map(|(k, v)| (Cow::Owned(k.into_owned()), Cow::Owned(v.into_owned())))
                .collect(),
        }
    }
}

/// Custom block (e.g., <i18n>, <docs>)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SfcCustomBlock<'a> {
    /// Block type/tag name
    #[serde(rename = "type", borrow)]
    pub block_type: Cow<'a, str>,

    /// Block content
    #[serde(borrow)]
    pub content: Cow<'a, str>,

    /// Block location in source
    pub loc: BlockLocation,

    /// Additional attributes
    #[serde(default)]
    pub attrs: FxHashMap<Cow<'a, str>, Cow<'a, str>>,
}

impl<'a> SfcCustomBlock<'a> {
    /// Convert to owned version
    pub fn into_owned(self) -> SfcCustomBlock<'static> {
        SfcCustomBlock {
            block_type: Cow::Owned(self.block_type.into_owned()),
            content: Cow::Owned(self.content.into_owned()),
            loc: self.loc,
            attrs: self
                .attrs
                .into_iter()
                .map(|(k, v)| (Cow::Owned(k.into_owned()), Cow::Owned(v.into_owned())))
                .collect(),
        }
    }
}

/// Location information for a block
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BlockLocation {
    /// Start offset of content in source (after opening tag)
    pub start: usize,

    /// End offset of content in source (before closing tag)
    pub end: usize,

    /// Start offset of opening tag (e.g., `<template>`)
    #[serde(default)]
    pub tag_start: usize,

    /// End offset of closing tag (e.g., `</template>`)
    #[serde(default)]
    pub tag_end: usize,

    /// Start line (1-based)
    pub start_line: usize,

    /// Start column (1-based)
    pub start_column: usize,

    /// End line (1-based)
    pub end_line: usize,

    /// End column (1-based)
    pub end_column: usize,
}

/// Parse options for SFC
#[derive(Debug, Clone, Default)]
pub struct SfcParseOptions {
    /// Filename
    pub filename: String,

    /// Source map generation
    pub source_map: bool,

    /// Pad line numbers for blocks
    pub pad: PadOption,

    /// Ignore empty blocks
    pub ignore_empty: bool,

    /// Compiler options for template
    pub template_parse_options: Option<vize_atelier_core::options::ParserOptions>,
}

/// Padding option for source map alignment
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum PadOption {
    /// No padding
    #[default]
    None,
    /// Pad with newlines
    Line,
    /// Pad with spaces
    Space,
}

/// SFC compilation options
#[derive(Debug, Clone, Default)]
pub struct SfcCompileOptions {
    /// SFC parse options
    pub parse: SfcParseOptions,

    /// Script compile options
    pub script: ScriptCompileOptions,

    /// Template compile options
    pub template: TemplateCompileOptions,

    /// Style compile options
    pub style: StyleCompileOptions,

    /// Whether to compile the SFC in Vapor mode
    pub vapor: bool,

    /// External scope ID (8-char hex, without "data-v-" prefix).
    /// When provided, this scope ID is used instead of generating one from the filename.
    /// This ensures consistency with the JS-side scope ID generation (SHA-256).
    pub scope_id: Option<String>,
}

/// Script compile options
#[derive(Debug, Clone, Default)]
pub struct ScriptCompileOptions {
    /// ID for scoped CSS
    pub id: Option<String>,

    /// Whether inline template
    pub inline_template: bool,

    /// Whether to use TypeScript
    pub is_ts: bool,

    /// Reactive transform
    pub reactive_props_destructure: bool,

    /// Props destructure
    pub props_destructure: PropsDestructure,

    /// Define model options
    pub define_model: bool,
}

/// Props destructure mode
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum PropsDestructure {
    /// Disabled (error)
    #[default]
    False,
    /// Enabled
    True,
    /// Error on use
    Error,
}

/// Template compile options
#[derive(Debug, Clone, Default)]
pub struct TemplateCompileOptions {
    /// ID for scoped CSS
    pub id: Option<String>,

    /// Whether SSR mode
    pub ssr: bool,

    /// SSR CSS vars
    pub ssr_css_vars: Option<String>,

    /// Scoped
    pub scoped: bool,

    /// Is prod mode
    pub is_prod: bool,

    /// Whether TypeScript mode
    pub is_ts: bool,

    /// Whether the template targets a custom renderer instead of the DOM.
    pub custom_renderer: bool,

    /// Compiler options
    pub compiler_options: Option<vize_atelier_dom::DomCompilerOptions>,
}

/// Style compile options
#[derive(Debug, Clone, Default)]
pub struct StyleCompileOptions {
    /// ID for scoped CSS
    pub id: String,

    /// Whether scoped
    pub scoped: bool,

    /// Whether trim
    pub trim: bool,

    /// Source map
    pub source_map: bool,

    /// Preprocessor language
    pub preprocessor_lang: Option<String>,

    /// Custom data attributes to add
    pub data_attrs: Vec<String>,
}

/// SFC compilation result
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SfcCompileResult {
    /// Compiled JavaScript code
    pub code: String,

    /// Compiled CSS (from all style blocks)
    pub css: Option<String>,

    /// Source map
    pub map: Option<serde_json::Value>,

    /// Errors
    pub errors: Vec<SfcError>,

    /// Warnings
    pub warnings: Vec<SfcError>,

    /// Binding metadata
    pub bindings: Option<BindingMetadata>,
}

/// SFC error/warning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SfcError {
    /// Error message
    pub message: String,

    /// Error code
    #[serde(default)]
    pub code: Option<String>,

    /// Location
    #[serde(default)]
    pub loc: Option<BlockLocation>,
}

impl From<vize_atelier_core::CompilerError> for SfcError {
    fn from(err: vize_atelier_core::CompilerError) -> Self {
        let mut code = vize_carton::String::default();
        use std::fmt::Write as _;
        let _ = write!(&mut code, "{:?}", err.code);
        Self {
            message: err.message,
            code: Some(code),
            loc: None,
        }
    }
}