File size: 6,693 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
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
use std::path::PathBuf;

use anyhow::Context;
use napi::bindgen_prelude::*;
use next_build::{
    BuildOptions as NextBuildOptions,
    build_options::{BuildContext, DefineEnv},
};
use next_core::next_config::{Rewrite, Rewrites, RouteHas};

use crate::next_api::project::NapiDefineEnv;

#[napi(object, object_to_js = false)]
#[derive(Debug)]
pub struct NextBuildContext {
    // Added by Next.js for next build --turbopack specifically.
    /// The root directory of the workspace.
    pub root: Option<String>,

    /// The project's directory.
    pub dir: Option<String>,

    /// next.config.js's distDir. Current there's some early stage setup
    /// requires this Before construct a context to read next.config.js,
    /// which we passes separately here.
    pub dist_dir: Option<String>,

    /// The build ID.
    pub build_id: Option<String>,

    /// The rewrites, as computed by Next.js.
    pub rewrites: Option<NapiRewrites>,
    // TODO(alexkirsz) These are detected directly by Turbopack for now.
    // pub app_dir: Option<String>,
    // pub pages_dir: Option<String>,
    // TODO(alexkirsz) These are used to generate route types.
    // pub original_rewrites: Option<Rewrites>,
    // pub original_redirects: Option<Vec<Redirect>>,
    pub define_env: NapiDefineEnv,
}

impl TryFrom<NextBuildContext> for NextBuildOptions {
    type Error = napi::Error;

    fn try_from(value: NextBuildContext) -> Result<Self> {
        Ok(Self {
            dir: value.dir.map(PathBuf::try_from).transpose()?,
            root: value.root.map(PathBuf::try_from).transpose()?,
            log_level: None,
            show_all: true,
            log_detail: true,
            full_stats: true,
            memory_limit: None,
            dist_dir: value.dist_dir,
            build_context: Some(BuildContext {
                build_id: value
                    .build_id
                    .context("NextBuildContext must provide a build ID")?,
                rewrites: value
                    .rewrites
                    .context("NextBuildContext must provide rewrites")?
                    .into(),
            }),
            define_env: value.define_env.into(),
        })
    }
}

impl From<NapiDefineEnv> for DefineEnv {
    fn from(val: NapiDefineEnv) -> Self {
        DefineEnv {
            client: val
                .client
                .into_iter()
                .map(|var| (var.name, var.value))
                .collect(),
            edge: val
                .edge
                .into_iter()
                .map(|var| (var.name, var.value))
                .collect(),
            nodejs: val
                .nodejs
                .into_iter()
                .map(|var| (var.name, var.value))
                .collect(),
        }
    }
}

/// Keep in sync with [`next_core::next_config::Rewrites`]
#[napi(object, object_to_js = false)]
#[derive(Debug)]
pub struct NapiRewrites {
    pub fallback: Vec<NapiRewrite>,
    pub after_files: Vec<NapiRewrite>,
    pub before_files: Vec<NapiRewrite>,
}

impl From<NapiRewrites> for Rewrites {
    fn from(val: NapiRewrites) -> Self {
        Rewrites {
            fallback: val
                .fallback
                .into_iter()
                .map(|rewrite| rewrite.into())
                .collect(),
            after_files: val
                .after_files
                .into_iter()
                .map(|rewrite| rewrite.into())
                .collect(),
            before_files: val
                .before_files
                .into_iter()
                .map(|rewrite| rewrite.into())
                .collect(),
        }
    }
}

/// Keep in sync with [`next_core::next_config::Rewrite`]
#[napi(object, object_to_js = false)]
#[derive(Debug)]
pub struct NapiRewrite {
    pub source: String,
    pub destination: String,
    pub base_path: Option<bool>,
    pub locale: Option<bool>,
    pub has: Option<Vec<NapiRouteHas>>,
    pub missing: Option<Vec<NapiRouteHas>>,
}

impl From<NapiRewrite> for Rewrite {
    fn from(val: NapiRewrite) -> Self {
        Rewrite {
            source: val.source,
            destination: val.destination,
            base_path: val.base_path,
            locale: val.locale,
            has: val
                .has
                .map(|has| has.into_iter().map(|has| has.into()).collect()),
            missing: val
                .missing
                .map(|missing| missing.into_iter().map(|missing| missing.into()).collect()),
        }
    }
}

/// Keep in sync with [`next_core::next_config::RouteHas`]
#[derive(Debug)]
pub enum NapiRouteHas {
    Header { key: String, value: Option<String> },
    Query { key: String, value: Option<String> },
    Cookie { key: String, value: Option<String> },
    Host { value: String },
}

impl FromNapiValue for NapiRouteHas {
    unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
        let object = unsafe { Object::from_napi_value(env, napi_val)? };
        let type_ = object.get_named_property::<String>("type")?;
        Ok(match type_.as_str() {
            "header" => NapiRouteHas::Header {
                key: object.get_named_property("key")?,
                value: object.get_named_property("value")?,
            },
            "query" => NapiRouteHas::Query {
                key: object.get_named_property("key")?,
                value: object.get_named_property("value")?,
            },
            "cookie" => NapiRouteHas::Cookie {
                key: object.get_named_property("key")?,
                value: object.get_named_property("value")?,
            },
            "host" => NapiRouteHas::Host {
                value: object.get_named_property("value")?,
            },
            _ => {
                return Err(napi::Error::new(
                    Status::GenericFailure,
                    format!("invalid type for RouteHas: {type_}"),
                ));
            }
        })
    }
}

impl From<NapiRouteHas> for RouteHas {
    fn from(val: NapiRouteHas) -> Self {
        match val {
            NapiRouteHas::Header { key, value } => RouteHas::Header {
                key: key.into(),
                value: value.map(From::from),
            },
            NapiRouteHas::Query { key, value } => RouteHas::Query {
                key: key.into(),
                value: value.map(From::from),
            },
            NapiRouteHas::Cookie { key, value } => RouteHas::Cookie {
                key: key.into(),
                value: value.map(From::from),
            },
            NapiRouteHas::Host { value } => RouteHas::Host {
                value: value.into(),
            },
        }
    }
}