Skip to content

Commit 9f98d19

Browse files
authored
feat: add concurrent feature (#297)
1 parent ecc160d commit 9f98d19

File tree

5 files changed

+25
-23
lines changed

5 files changed

+25
-23
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ all-features = true
1414

1515
[features]
1616
bundler = ["swc_bundler", "swc_ecma_transforms_optimization", "swc_graph_analyzer"]
17+
concurrent = ["swc_common/concurrent"]
1718
cjs = ["utils", "visit"]
1819
codegen = ["swc_ecma_codegen", "swc_ecma_codegen_macros", "swc_macros_common"]
1920
compat = ["transforms", "swc_ecma_transforms_compat", "swc_trace_macro", "swc_config", "swc_config_macro"]

src/source_map.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

3-
use std::rc::Rc;
4-
3+
use crate::swc::common::sync::Lrc;
54
use crate::swc::common::FileName;
65
use crate::swc::common::SourceFile;
76
use crate::ModuleSpecifier;
@@ -25,30 +24,30 @@ impl IntoSwcFileName for String {
2524

2625
#[derive(Clone, Default)]
2726
pub struct SourceMap {
28-
inner: Rc<crate::swc::common::SourceMap>,
27+
inner: Lrc<crate::swc::common::SourceMap>,
2928
}
3029

3130
impl SourceMap {
3231
pub fn single(file_name: impl IntoSwcFileName, source: String) -> Self {
3332
let map = Self::default();
3433
map.inner.new_source_file(
35-
Rc::new(IntoSwcFileName::into_file_name(file_name)),
34+
Lrc::new(IntoSwcFileName::into_file_name(file_name)),
3635
source,
3736
);
3837
map
3938
}
4039

41-
pub fn inner(&self) -> &Rc<crate::swc::common::SourceMap> {
40+
pub fn inner(&self) -> &Lrc<crate::swc::common::SourceMap> {
4241
&self.inner
4342
}
4443

4544
pub fn new_source_file(
4645
&self,
4746
file_name: impl IntoSwcFileName,
4847
source: String,
49-
) -> Rc<SourceFile> {
48+
) -> Lrc<SourceFile> {
5049
self.inner.new_source_file(
51-
Rc::new(IntoSwcFileName::into_file_name(file_name)),
50+
Lrc::new(IntoSwcFileName::into_file_name(file_name)),
5251
source,
5352
)
5453
}

src/transpiling/mod.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

33
use std::borrow::Cow;
4-
use std::rc::Rc;
54
use std::sync::Arc;
65

76
use deno_media_type::MediaType;
@@ -14,6 +13,8 @@ use crate::emit;
1413
use crate::swc::ast::Program;
1514
use crate::swc::common::comments::SingleThreadedComments;
1615
use crate::swc::common::errors::Diagnostic as SwcDiagnostic;
16+
use crate::swc::common::sync::Lock;
17+
use crate::swc::common::sync::Lrc;
1718
use crate::swc::ecma_visit::visit_mut_pass;
1819
use crate::swc::parser::error::SyntaxError;
1920
use crate::swc::transforms::fixer;
@@ -39,7 +40,6 @@ use crate::ProgramRef;
3940
use crate::SourceMap;
4041

4142
use deno_error::JsError;
42-
use std::cell::RefCell;
4343

4444
mod jsx_precompile;
4545
mod transforms;
@@ -53,7 +53,7 @@ pub enum TranspileResult {
5353
///
5454
/// This is a performance issue and you should strive to get an `Owned` result.
5555
Cloned(EmittedSourceText),
56-
/// The emit occured consuming the `ParsedSource` without cloning.
56+
/// The emit occurred consuming the `ParsedSource` without cloning.
5757
Owned(EmittedSourceText),
5858
}
5959

@@ -174,8 +174,8 @@ impl Default for TranspileOptions {
174174
impl TranspileOptions {
175175
fn as_tsx_config(&self) -> typescript::TsxConfig {
176176
typescript::TsxConfig {
177-
pragma: Some(Rc::new(self.jsx_factory.clone())),
178-
pragma_frag: Some(Rc::new(self.jsx_fragment_factory.clone())),
177+
pragma: Some(Lrc::new(self.jsx_factory.clone())),
178+
pragma_frag: Some(Lrc::new(self.jsx_fragment_factory.clone())),
179179
}
180180
}
181181

@@ -196,7 +196,7 @@ impl TranspileOptions {
196196
},
197197
// no need for this to be false because we treat all files as modules
198198
no_empty_export: true,
199-
// we don't suport this, so leave it as-is so it errors in v8
199+
// we don't support this, so leave it as-is so it errors in v8
200200
import_export_assign_config:
201201
typescript::TsImportExportAssignConfig::Preserve,
202202
// Do not opt into swc's optimization to inline enum member values
@@ -619,7 +619,7 @@ fn convert_script_module_to_swc_script(
619619

620620
#[derive(Default, Clone)]
621621
struct DiagnosticCollector {
622-
diagnostics_cell: Rc<RefCell<Vec<SwcDiagnostic>>>,
622+
diagnostics: Lrc<Lock<Vec<SwcDiagnostic>>>,
623623
}
624624

625625
impl DiagnosticCollector {
@@ -635,7 +635,8 @@ impl DiagnosticCollector {
635635
impl crate::swc::common::errors::Emitter for DiagnosticCollector {
636636
fn emit(&mut self, db: &crate::swc::common::errors::DiagnosticBuilder<'_>) {
637637
use std::ops::Deref;
638-
self.diagnostics_cell.borrow_mut().push(db.deref().clone());
638+
let mut diagnostics = self.diagnostics.lock();
639+
diagnostics.push(db.deref().clone());
639640
}
640641
}
641642

@@ -720,8 +721,8 @@ pub fn fold_program<'a>(
720721
Some(comments),
721722
#[allow(deprecated)]
722723
react::Options {
723-
pragma: Some(Rc::new(options.jsx_factory.clone())),
724-
pragma_frag: Some(Rc::new(options.jsx_fragment_factory.clone())),
724+
pragma: Some(Lrc::new(options.jsx_factory.clone())),
725+
pragma_frag: Some(Lrc::new(options.jsx_fragment_factory.clone())),
725726
// This will use `Object.assign()` instead of the `_extends` helper
726727
// when spreading props (Note: this property is deprecated)
727728
use_builtins: Some(true),
@@ -755,7 +756,7 @@ pub fn fold_program<'a>(
755756
);
756757

757758
let emitter = DiagnosticCollector::default();
758-
let diagnostics_cell = emitter.diagnostics_cell.clone();
759+
let diagnostics_cell = emitter.diagnostics.clone();
759760
let handler = emitter.into_handler();
760761
let result = crate::swc::common::errors::HANDLER.set(&handler, || {
761762
helpers::HELPERS

src/transpiling/transforms.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ mod test {
288288
use crate::swc::ast::Module;
289289
use crate::swc::codegen::text_writer::JsWriter;
290290
use crate::swc::codegen::Node;
291+
use crate::swc::common::sync::Lrc;
291292
use crate::swc::common::FileName;
292293
use crate::swc::common::SourceMap;
293294
use crate::swc::ecma_visit::Fold;
@@ -298,7 +299,6 @@ mod test {
298299
use crate::swc::parser::TsSyntax;
299300
use crate::ModuleSpecifier;
300301
use pretty_assertions::assert_eq;
301-
use std::rc::Rc;
302302

303303
use super::*;
304304

@@ -498,10 +498,10 @@ mod test {
498498
assert_eq!(output, format!("{}\n", expected_output));
499499
}
500500

501-
fn parse(src: &str) -> (Rc<SourceMap>, Module) {
502-
let source_map = Rc::new(SourceMap::default());
501+
fn parse(src: &str) -> (Lrc<SourceMap>, Module) {
502+
let source_map = Lrc::new(SourceMap::default());
503503
let source_file = source_map.new_source_file(
504-
Rc::new(FileName::Url(
504+
Lrc::new(FileName::Url(
505505
ModuleSpecifier::parse("file:///test.ts").unwrap(),
506506
)),
507507
src.to_string(),
@@ -514,7 +514,7 @@ mod test {
514514
(source_map, parser.parse_module().unwrap())
515515
}
516516

517-
fn print(source_map: Rc<SourceMap>, module: Module) -> String {
517+
fn print(source_map: Lrc<SourceMap>, module: Module) -> String {
518518
let mut buf = vec![];
519519
{
520520
let mut writer =

0 commit comments

Comments
 (0)