Skip to content

Commit

Permalink
Merge 50e259f into 1ca7f63
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Sep 21, 2023
2 parents 1ca7f63 + 50e259f commit 5da9f15
Show file tree
Hide file tree
Showing 20 changed files with 653 additions and 939 deletions.
50 changes: 9 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/turbopack-core/src/issue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ pub struct IssueSource {

#[turbo_tasks::value_impl]
impl IssueSource {
#[turbo_tasks::function]
pub fn new(source: Vc<Box<dyn Source>>, start: SourcePos, end: SourcePos) -> Result<Vc<Self>> {
Ok(Self::cell(IssueSource { source, start, end }))
}

#[turbo_tasks::function]
pub async fn from_byte_offset(
source: Vc<Box<dyn Source>>,
Expand Down
3 changes: 2 additions & 1 deletion crates/turbopack-core/src/source_pos.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use turbo_tasks::trace::TraceRawVcs;
use turbo_tasks::{trace::TraceRawVcs, TaskInput};
use turbo_tasks_hash::DeterministicHash;

/// LINE FEED (LF), one of the basic JS line terminators.
Expand All @@ -16,6 +16,7 @@ const U8_CR: u8 = 0x0D;
Clone,
PartialOrd,
Ord,
TaskInput,
TraceRawVcs,
Serialize,
Deserialize,
Expand Down
17 changes: 8 additions & 9 deletions crates/turbopack-css/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ anyhow = { workspace = true }
async-trait = { workspace = true }
indexmap = { workspace = true }
indoc = { workspace = true }
lightningcss = { version = "1.0.0-alpha.45", features = [
"serde",
"visitor",
"into_owned",
] }
once_cell = { workspace = true }
parcel_selectors = "0.26.0"
regex = { workspace = true }
serde = { workspace = true }
turbo-tasks = { workspace = true }
Expand All @@ -24,17 +30,10 @@ turbopack-core = { workspace = true }
turbopack-ecmascript = { workspace = true }
turbopack-swc-utils = { workspace = true }

parcel_sourcemap = "2.1.1"
smallvec = "1.11.0"
swc_core = { workspace = true, features = [
"ecma_ast",
"css_ast",
"css_ast_serde",
"css_codegen",
"css_parser",
"css_utils",
"css_visit",
"css_visit_path",
"css_compat",
"css_modules",
"common",
"common_concurrent",
] }
Expand Down
119 changes: 40 additions & 79 deletions crates/turbopack-css/src/asset.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
use anyhow::Result;
use swc_core::{
common::{Globals, GLOBALS},
css::{
ast::{AtRule, AtRulePrelude, Rule},
codegen::{writer::basic::BasicCssWriter, CodeGenerator, Emit},
visit::{VisitMutWith, VisitMutWithPath},
},
};
use turbo_tasks::{TryJoinIterExt, Value, ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
Expand All @@ -25,12 +17,11 @@ use turbopack_core::{
use crate::{
chunk::{CssChunk, CssChunkItem, CssChunkItemContent, CssChunkPlaceable, CssImport},
code_gen::CodeGenerateable,
parse::{parse_css, ParseCss, ParseCssResult, ParseCssResultSourceMap},
path_visitor::ApplyVisitors,
references::{
analyze_css_stylesheet, compose::CssModuleComposeReference, import::ImportAssetReference,
process::{
finalize_css, parse_css, process_css_with_placeholder, CssWithPlaceholderResult,
FinalCssResult, ParseCss, ParseCssResult, ProcessCss,
},
transform::CssInputTransforms,
references::{compose::CssModuleComposeReference, import::ImportAssetReference},
CssModuleAssetType,
};

Expand All @@ -44,7 +35,6 @@ fn modifier() -> Vc<String> {
pub struct CssModuleAsset {
source: Vc<Box<dyn Source>>,
asset_context: Vc<Box<dyn AssetContext>>,
transforms: Vc<CssInputTransforms>,
ty: CssModuleAssetType,
}

Expand All @@ -55,13 +45,11 @@ impl CssModuleAsset {
pub fn new(
source: Vc<Box<dyn Source>>,
asset_context: Vc<Box<dyn AssetContext>>,
transforms: Vc<CssInputTransforms>,
ty: CssModuleAssetType,
) -> Vc<Self> {
Self::cell(CssModuleAsset {
source,
asset_context,
transforms,
ty,
})
}
Expand All @@ -76,8 +64,29 @@ impl CssModuleAsset {
#[turbo_tasks::value_impl]
impl ParseCss for CssModuleAsset {
#[turbo_tasks::function]
fn parse_css(&self) -> Vc<ParseCssResult> {
parse_css(self.source, self.ty, self.transforms)
async fn parse_css(self: Vc<Self>) -> Result<Vc<ParseCssResult>> {
let this = self.await?;
Ok(parse_css(this.source, Vc::upcast(self), this.ty))
}
}

#[turbo_tasks::value_impl]
impl ProcessCss for CssModuleAsset {
#[turbo_tasks::function]
async fn get_css_with_placeholder(self: Vc<Self>) -> Result<Vc<CssWithPlaceholderResult>> {
let parse_result = self.parse_css();

Ok(process_css_with_placeholder(parse_result))
}

#[turbo_tasks::function]
async fn finalize_css(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<FinalCssResult>> {
let process_result = self.get_css_with_placeholder();

Ok(finalize_css(process_result, chunking_context))
}
}

Expand All @@ -90,14 +99,14 @@ impl Module for CssModuleAsset {

#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<ModuleReferences>> {
let this = self.await?;
let result = self.parse_css().await?;
// TODO: include CSS source map
Ok(analyze_css_stylesheet(
this.source,
Vc::upcast(self),
this.ty,
this.transforms,
))

match &*result {
ParseCssResult::Ok { references, .. } => Ok(references.clone()),
ParseCssResult::Unparseable => Ok(ModuleReferences::empty()),
ParseCssResult::NotFound => Ok(ModuleReferences::empty()),
}
}
}

Expand Down Expand Up @@ -230,72 +239,24 @@ impl CssChunkItem for CssModuleChunkItem {
let code_gens = code_gens.into_iter().try_join().await?;
let code_gens = code_gens.iter().map(|cg| &**cg).collect::<Vec<_>>();
// TOOD use interval tree with references into "code_gens"
let mut visitors = Vec::new();
let mut root_visitors = Vec::new();
for code_gen in code_gens {
for import in &code_gen.imports {
imports.push(import.clone());
}

for (path, visitor) in code_gen.visitors.iter() {
if path.is_empty() {
root_visitors.push(&**visitor);
} else {
visitors.push((path, &**visitor));
}
}
}

let parsed = self.module.parse_css().await?;
let result = self.module.finalize_css(chunking_context).await?;

if let ParseCssResult::Ok {
stylesheet,
if let FinalCssResult::Ok {
output_code,
source_map,
..
} = &*parsed
} = &*result
{
let mut stylesheet = stylesheet.clone();

let globals = Globals::new();
GLOBALS.set(&globals, || {
if !visitors.is_empty() {
stylesheet.visit_mut_with_path(
&mut ApplyVisitors::new(visitors),
&mut Default::default(),
);
}
for visitor in root_visitors {
stylesheet.visit_mut_with(&mut visitor.create());
}
});

// remove imports
stylesheet.rules.retain(|r| {
!matches!(
r,
&Rule::AtRule(box AtRule {
prelude: Some(box AtRulePrelude::ImportPrelude(_)),
..
})
)
});

let mut code_string = String::new();
let mut srcmap = vec![];

let mut code_gen = CodeGenerator::new(
BasicCssWriter::new(&mut code_string, Some(&mut srcmap), Default::default()),
Default::default(),
);

code_gen.emit(&stylesheet)?;

let srcmap = ParseCssResultSourceMap::new(source_map.clone(), srcmap).cell();

Ok(CssChunkItemContent {
inner_code: code_string.into(),
inner_code: output_code.to_owned().into(),
imports,
source_map: Some(srcmap),
source_map: Some(source_map.clone()),
}
.into())
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-css/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use writer::expand_imports;
use self::{single_item_chunk::chunk::SingleItemCssChunk, source_map::CssChunkSourceMapAsset};
use crate::{
embed::{CssEmbed, CssEmbeddable},
parse::ParseCssResultSourceMap,
process::ParseCssResultSourceMap,
util::stringify_js,
ImportAssetReference,
};
Expand Down
Loading

0 comments on commit 5da9f15

Please sign in to comment.