forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
328 lines (291 loc) · 10.3 KB
/
lib.rs
File metadata and controls
328 lines (291 loc) · 10.3 KB
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
#[cfg(target_os = "macos")]
#[global_allocator]
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;
use serde::{Serialize, Deserialize};
use parcel_css::stylesheet::{StyleSheet, StyleAttribute, ParserOptions, PrinterOptions, PseudoClasses};
use parcel_css::targets::Browsers;
use parcel_css::css_modules::CssModuleExports;
use parcel_css::dependencies::Dependency;
use parcel_css::error::{ParserError, PrinterError};
// ---------------------------------------------
#[cfg(target_arch = "wasm32")]
use serde_wasm_bindgen::{from_value, Serializer};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn transform(config_val: JsValue) -> Result<JsValue, JsValue> {
let config: Config = from_value(config_val).map_err(JsValue::from)?;
let code = unsafe { std::str::from_utf8_unchecked(&config.code) };
let res = compile(code, &config)?;
let serializer = Serializer::new().serialize_maps_as_objects(true);
res.serialize(&serializer).map_err(JsValue::from)
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = "transformStyleAttribute")]
pub fn transform_style_attribute(config_val: JsValue) -> Result<JsValue, JsValue> {
let config: AttrConfig = from_value(config_val).map_err(JsValue::from)?;
let code = unsafe { std::str::from_utf8_unchecked(&config.code) };
let res = compile_attr(code, &config)?;
let serializer = Serializer::new().serialize_maps_as_objects(true);
res.serialize(&serializer).map_err(JsValue::from)
}
// ---------------------------------------------
#[cfg(not(target_arch = "wasm32"))]
use napi_derive::{js_function, module_exports};
#[cfg(not(target_arch = "wasm32"))]
use napi::{CallContext, JsObject, JsUnknown};
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SourceMapJson<'a> {
version: u8,
mappings: String,
sources: &'a Vec<String>,
sources_content: &'a Vec<String>,
names: &'a Vec<String>
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct TransformResult {
#[serde(with = "serde_bytes")]
code: Vec<u8>,
#[serde(with = "serde_bytes")]
map: Option<Vec<u8>>,
exports: Option<CssModuleExports>,
dependencies: Option<Vec<Dependency>>
}
#[cfg(not(target_arch = "wasm32"))]
#[js_function(1)]
fn transform(ctx: CallContext) -> napi::Result<JsUnknown> {
let opts = ctx.get::<JsObject>(0)?;
let config: Config = ctx.env.from_js_value(opts)?;
let code = unsafe { std::str::from_utf8_unchecked(&config.code) };
let res = compile(code, &config);
match res {
Ok(res) => ctx.env.to_js_value(&res),
Err(err) => err.throw(ctx, Some(config.filename), code)
}
}
#[cfg(not(target_arch = "wasm32"))]
#[js_function(1)]
fn transform_style_attribute(ctx: CallContext) -> napi::Result<JsUnknown> {
let opts = ctx.get::<JsObject>(0)?;
let config: AttrConfig = ctx.env.from_js_value(opts)?;
let code = unsafe { std::str::from_utf8_unchecked(&config.code) };
let res = compile_attr(code, &config);
match res {
Ok(res) => ctx.env.to_js_value(&res),
Err(err) => err.throw(ctx, None, code)
}
}
#[cfg(not(target_arch = "wasm32"))]
#[module_exports]
fn init(mut exports: JsObject) -> napi::Result<()> {
exports.create_named_method("transform", transform)?;
exports.create_named_method("transformStyleAttribute", transform_style_attribute)?;
Ok(())
}
// ---------------------------------------------
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Config {
pub filename: String,
#[serde(with = "serde_bytes")]
pub code: Vec<u8>,
pub targets: Option<Browsers>,
pub minify: Option<bool>,
pub source_map: Option<bool>,
pub drafts: Option<Drafts>,
pub css_modules: Option<bool>,
pub analyze_dependencies: Option<bool>,
pub pseudo_classes: Option<OwnedPseudoClasses>
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct OwnedPseudoClasses {
pub hover: Option<String>,
pub active: Option<String>,
pub focus: Option<String>,
pub focus_visible: Option<String>,
pub focus_within: Option<String>
}
impl<'a> Into<PseudoClasses<'a>> for &'a OwnedPseudoClasses {
fn into(self) -> PseudoClasses<'a> {
PseudoClasses {
hover: self.hover.as_deref(),
active: self.active.as_deref(),
focus: self.focus.as_deref(),
focus_visible: self.focus_visible.as_deref(),
focus_within: self.focus_within.as_deref()
}
}
}
#[derive(Serialize, Debug, Deserialize, Default)]
struct Drafts {
nesting: bool
}
fn compile<'i>(code: &'i str, config: &Config) -> Result<TransformResult, CompileError<'i>> {
let options = config.drafts.as_ref();
let mut stylesheet = StyleSheet::parse(config.filename.clone(), &code, ParserOptions {
nesting: match options {
Some(o) => o.nesting,
None => false
},
css_modules: config.css_modules.unwrap_or(false)
})?;
stylesheet.minify(config.targets); // TODO: should this be conditional?
let res = stylesheet.to_css(PrinterOptions {
minify: config.minify.unwrap_or(false),
source_map: config.source_map.unwrap_or(false),
targets: config.targets,
analyze_dependencies: config.analyze_dependencies.unwrap_or(false),
pseudo_classes: config.pseudo_classes.as_ref().map(|p| p.into())
})?;
let map = if let Some(mut source_map) = res.source_map {
source_map.set_source_content(0, code)?;
let mut vlq_output: Vec<u8> = Vec::new();
source_map.write_vlq(&mut vlq_output)?;
let sm = SourceMapJson {
version: 3,
mappings: unsafe { String::from_utf8_unchecked(vlq_output) },
sources: source_map.get_sources(),
sources_content: source_map.get_sources_content(),
names: source_map.get_names()
};
serde_json::to_vec(&sm).ok()
} else {
None
};
Ok(TransformResult {
code: res.code.into_bytes(),
map,
exports: res.exports,
dependencies: res.dependencies
})
}
#[derive(Serialize, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct AttrConfig {
#[serde(with = "serde_bytes")]
pub code: Vec<u8>,
pub targets: Option<Browsers>,
pub minify: Option<bool>,
pub analyze_dependencies: Option<bool>
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct AttrResult {
#[serde(with = "serde_bytes")]
code: Vec<u8>,
dependencies: Option<Vec<Dependency>>
}
fn compile_attr<'i>(code: &'i str, config: &AttrConfig) -> Result<AttrResult, CompileError<'i>> {
let mut attr = StyleAttribute::parse(&code)?;
attr.minify(config.targets); // TODO: should this be conditional?
let res = attr.to_css(PrinterOptions {
minify: config.minify.unwrap_or(false),
source_map: false,
targets: config.targets,
analyze_dependencies: config.analyze_dependencies.unwrap_or(false),
pseudo_classes: None
})?;
Ok(AttrResult {
code: res.code.into_bytes(),
dependencies: res.dependencies
})
}
enum CompileError<'i> {
ParseError(cssparser::ParseError<'i, ParserError<'i>>),
PrinterError(PrinterError),
SourceMapError(parcel_sourcemap::SourceMapError)
}
impl<'i> CompileError<'i> {
fn reason(&self) -> String {
match self {
CompileError::ParseError(e) => {
match &e.kind {
cssparser::ParseErrorKind::Basic(b) => {
use cssparser::BasicParseErrorKind::*;
match b {
AtRuleBodyInvalid => "Invalid at rule body".into(),
EndOfInput => "Unexpected end of input".into(),
AtRuleInvalid(name) => format!("Unknown at rule: @{}", name),
QualifiedRuleInvalid => "Invalid qualified rule".into(),
UnexpectedToken(token) => format!("Unexpected token {:?}", token)
}
},
cssparser::ParseErrorKind::Custom(e) => e.reason()
}
}
CompileError::PrinterError(err) => err.reason(),
_ => "Unknown error".into()
}
}
#[cfg(not(target_arch = "wasm32"))]
fn throw(self, ctx: CallContext, filename: Option<String>, code: &str) -> napi::Result<JsUnknown> {
match &self {
CompileError::ParseError(e) => {
throw_syntax_error(ctx, filename, code, self.reason(), &e.location)
},
CompileError::PrinterError(PrinterError::InvalidComposesSelector(loc)) |
CompileError::PrinterError(PrinterError::InvalidComposesNesting(loc)) => {
throw_syntax_error(ctx, filename, code, self.reason(), loc)
}
_ => Err(self.into())
}
}
}
fn throw_syntax_error(ctx: CallContext, filename: Option<String>, code: &str, message: String, loc: &cssparser::SourceLocation) -> napi::Result<JsUnknown> {
// Generate an error with location information.
let syntax_error = ctx.env.get_global()?
.get_named_property::<napi::JsFunction>("SyntaxError")?;
let reason = ctx.env.create_string_from_std(message)?;
let line = ctx.env.create_int32((loc.line + 1) as i32)?;
let col = ctx.env.create_int32(loc.column as i32)?;
let mut obj = syntax_error.new(&[reason])?;
if let Some(filename) = filename {
let filename = ctx.env.create_string_from_std(filename)?;
obj.set_named_property("fileName", filename)?;
}
let source = ctx.env.create_string(code)?;
obj.set_named_property("source", source)?;
let mut loc = ctx.env.create_object()?;
loc.set_named_property("line", line)?;
loc.set_named_property("column", col)?;
obj.set_named_property("loc", loc)?;
ctx.env.throw(obj)?;
Ok(ctx.env.get_undefined()?.into_unknown())
}
impl<'i> From<cssparser::ParseError<'i, ParserError<'i>>> for CompileError<'i> {
fn from(e: cssparser::ParseError<'i, ParserError<'i>>) -> CompileError<'i> {
CompileError::ParseError(e)
}
}
impl<'i> From<PrinterError> for CompileError<'i> {
fn from(err: PrinterError) -> CompileError<'i> {
CompileError::PrinterError(err)
}
}
impl<'i> From<parcel_sourcemap::SourceMapError> for CompileError<'i> {
fn from(e: parcel_sourcemap::SourceMapError) -> CompileError<'i> {
CompileError::SourceMapError(e)
}
}
#[cfg(not(target_arch = "wasm32"))]
impl<'i> From<CompileError<'i>> for napi::Error {
fn from(e: CompileError) -> napi::Error {
match e {
CompileError::SourceMapError(e) => e.into(),
_ => napi::Error::new(napi::Status::GenericFailure, e.reason())
}
}
}
#[cfg(target_arch = "wasm32")]
impl<'i> From<CompileError<'i>> for wasm_bindgen::JsValue {
fn from(e: CompileError) -> wasm_bindgen::JsValue {
match e {
CompileError::SourceMapError(e) => e.into(),
_ => js_sys::Error::new(&e.reason()).into()
}
}
}