File size: 2,472 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{Vc, fxindexmap};
use turbopack_core::diagnostics::{Diagnostic, DiagnosticPayload};

/// A structure that keeps track of whether a particular Next.js feature is
/// enabled for the telemetry.
///
/// The original implementation code can be found
/// [here](https://github.com/vercel/next.js/blob/9da305fe320b89ee2f8c3cfb7ecbf48856368913/packages/next/src/build/webpack-config.ts#L2516).
#[turbo_tasks::value(shared)]
pub struct NextFeatureTelemetry {
    pub event_name: RcStr,
    pub feature_name: RcStr,
    pub enabled: bool,
}

impl NextFeatureTelemetry {
    pub fn new(feature_name: RcStr, enabled: bool) -> Self {
        NextFeatureTelemetry {
            event_name: rcstr!("EVENT_BUILD_FEATURE_USAGE"),
            feature_name,
            enabled,
        }
    }
}

#[turbo_tasks::value_impl]
impl Diagnostic for NextFeatureTelemetry {
    #[turbo_tasks::function]
    fn category(&self) -> Vc<RcStr> {
        Vc::cell(rcstr!("NextFeatureTelemetry_category_tbd"))
    }

    #[turbo_tasks::function]
    fn name(&self) -> Vc<RcStr> {
        Vc::cell(self.event_name.clone())
    }

    #[turbo_tasks::function]
    fn payload(&self) -> Vc<DiagnosticPayload> {
        Vc::cell(fxindexmap! {
            self.feature_name.clone() =>
            self.enabled.to_string().into(),
        })
    }
}

/// A struct represent telemetry event for the feature usage,
/// referred as `importing` a certain module. (i.e importing @next/image)
#[turbo_tasks::value(shared)]
pub struct ModuleFeatureTelemetry {
    pub event_name: RcStr,
    pub feature_name: RcStr,
    pub invocation_count: usize,
}

impl ModuleFeatureTelemetry {
    pub fn new(feature_name: RcStr, invocation_count: usize) -> Self {
        ModuleFeatureTelemetry {
            event_name: rcstr!("EVENT_BUILD_FEATURE_USAGE"),
            feature_name,
            invocation_count,
        }
    }
}

#[turbo_tasks::value_impl]
impl Diagnostic for ModuleFeatureTelemetry {
    #[turbo_tasks::function]
    fn category(&self) -> Vc<RcStr> {
        Vc::cell(rcstr!("ModuleFeatureTelemetry_category_tbd"))
    }

    #[turbo_tasks::function]
    fn name(&self) -> Vc<RcStr> {
        Vc::cell(self.event_name.clone())
    }

    #[turbo_tasks::function]
    fn payload(&self) -> Vc<DiagnosticPayload> {
        Vc::cell(fxindexmap! {
            self.feature_name.clone() =>
            self.invocation_count.to_string().into(),
        })
    }
}