Skip to content

Commit

Permalink
feat: basic warning mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Nov 20, 2023
1 parent 40d40c1 commit 7cd5f92
Show file tree
Hide file tree
Showing 26 changed files with 193 additions and 49 deletions.
4 changes: 1 addition & 3 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,4 @@ async-scoped = { version = "0.7.1" }
regex = "1.10.2"
once_cell = "1.18.0"
dashmap = "5.5.3"
miette = { version = "5.10.0", features = ["fancy-no-backtrace"] }
ariadne = "0.3.0"
1 change: 0 additions & 1 deletion crates/rolldown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ string_wizard = { workspace = true }
async-trait = { workspace = true }
smallvec = { workspace = true }
oxc_resolver = { workspace = true }
miette = { workspace = true }
regex = { workspace = true }
once_cell = { workspace = true }

Expand Down
5 changes: 1 addition & 4 deletions crates/rolldown/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use miette::Result;
use rolldown::{Bundler, InputItem, InputOptions};
use std::path::PathBuf;
use sugar_path::SugarPathBuf;

#[tokio::main]
async fn main() -> Result<()> {
async fn main() {
let _guard = rolldown_tracing::try_init_tracing_with_chrome_layer();
let root = PathBuf::from(&std::env::var("CARGO_MANIFEST_DIR").unwrap());
let cwd = root.join("./examples").into_normalize();
Expand All @@ -16,6 +15,4 @@ async fn main() -> Result<()> {

let _outputs = bundler.write(Default::default()).await.unwrap();
// println!("{outputs:#?}");

Ok(())
}
19 changes: 12 additions & 7 deletions crates/rolldown/src/bundler/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ use crate::{
// Rolldown use this alias for outside users.
type BuildResult<T> = Result<T, Vec<BuildError>>;

pub struct RolldownOutput {
pub warnings: Vec<BuildError>,
pub assets: Vec<Output>,
}

pub struct Bundler<T: FileSystem + Default> {
input_options: SharedInputOptions,
plugin_driver: SharedPluginDriver,
Expand Down Expand Up @@ -52,11 +57,11 @@ impl<T: FileSystem + Default + 'static> Bundler<T> {
}
}

pub async fn write(&mut self, output_options: OutputOptions) -> BuildResult<Vec<Output>> {
pub async fn write(&mut self, output_options: OutputOptions) -> BuildResult<RolldownOutput> {
let dir =
self.input_options.cwd.as_path().join(&output_options.dir).to_string_lossy().to_string();

let assets = self.bundle_up(output_options).await?;
let output = self.bundle_up(output_options).await?;

self.fs.create_dir_all(dir.as_path()).unwrap_or_else(|_| {
panic!(
Expand All @@ -65,7 +70,7 @@ impl<T: FileSystem + Default + 'static> Bundler<T> {
self.input_options.cwd.display()
)
});
for chunk in &assets {
for chunk in &output.assets {
let dest = dir.as_path().join(chunk.file_name());
if let Some(p) = dest.parent() {
if !self.fs.exists(p) {
Expand All @@ -77,10 +82,10 @@ impl<T: FileSystem + Default + 'static> Bundler<T> {
});
}

Ok(assets)
Ok(output)
}

pub async fn generate(&mut self, output_options: OutputOptions) -> BuildResult<Vec<Output>> {
pub async fn generate(&mut self, output_options: OutputOptions) -> BuildResult<RolldownOutput> {
self.bundle_up(output_options).await
}

Expand Down Expand Up @@ -128,13 +133,13 @@ impl<T: FileSystem + Default + 'static> Bundler<T> {
}

#[tracing::instrument(skip_all)]
async fn bundle_up(&mut self, output_options: OutputOptions) -> BuildResult<Vec<Output>> {
async fn bundle_up(&mut self, output_options: OutputOptions) -> BuildResult<RolldownOutput> {
tracing::trace!("InputOptions {:#?}", self.input_options);
tracing::trace!("OutputOptions: {output_options:#?}",);
let graph = self.build_result.as_mut().expect("Build should success");
let mut bundle_stage = BundleStage::new(graph, &self.input_options, &output_options);
let assets = bundle_stage.bundle();

Ok(assets)
Ok(RolldownOutput { warnings: std::mem::take(&mut graph.warnings), assets })
}
}
8 changes: 6 additions & 2 deletions crates/rolldown/src/bundler/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use index_vec::IndexVec;
use rolldown_common::{FilePath, ImportKind, ImportRecordId, ModuleId, ResourceId};
use rolldown_error::BuildError;
use rolldown_fs::FileSystem;
use rustc_hash::{FxHashMap, FxHashSet};

Expand Down Expand Up @@ -39,6 +40,7 @@ pub struct ModuleLoaderOutput {
// Entries that user defined + dynamic import entries
pub entries: Vec<(Option<String>, ModuleId)>,
pub runtime: RuntimeModuleBrief,
pub warnings: Vec<BuildError>,
}

impl<T: FileSystem + 'static + Default> ModuleLoader<T> {
Expand Down Expand Up @@ -125,6 +127,7 @@ impl<T: FileSystem + 'static + Default> ModuleLoader<T> {
assert!(!self.input_options.input.is_empty(), "You must supply options.input to rolldown");

let mut errors = BatchedErrors::default();
let mut all_warnings: Vec<BuildError> = Vec::new();

self.intermediate_modules.reserve(user_defined_entries.len() + 1 /* runtime */);

Expand All @@ -149,9 +152,9 @@ impl<T: FileSystem + 'static + Default> ModuleLoader<T> {
resolved_deps,
mut builder,
raw_import_records,
..
warnings,
} = task_result;

all_warnings.extend(warnings);
let import_records = raw_import_records
.into_iter()
.zip(resolved_deps)
Expand Down Expand Up @@ -198,6 +201,7 @@ impl<T: FileSystem + 'static + Default> ModuleLoader<T> {
symbols: self.symbols,
entries,
runtime: runtime_brief.expect("Failed to find runtime module. This should not happen"),
warnings: all_warnings,
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {
async fn run_inner(&mut self) -> BatchedResult<()> {
tracing::trace!("process {:?}", self.path);

let warnings = vec![];
let mut warnings = vec![];

// Run plugin load to get content first, if it is None using read fs as fallback.
let mut source = if let Some(r) =
Expand Down Expand Up @@ -90,7 +90,9 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {
imports,
exports_kind,
repr_name,
warnings: scan_warnings,
} = scan_result;
warnings.extend(scan_warnings);

let builder = NormalModuleBuilder {
id: Some(self.module_id),
Expand Down Expand Up @@ -149,9 +151,10 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {
};
default
}
let source: Arc<str> = source.into();

let source_type = determine_oxc_source_type(self.path.as_path(), self.module_type);
let program = OxcCompiler::parse(source, source_type);
let program = OxcCompiler::parse(Arc::clone(&source), source_type);

let semantic = program.make_semantic(source_type);
let (mut symbol_table, scope) = semantic.into_symbol_table_and_scope_tree();
Expand All @@ -164,6 +167,8 @@ impl<'task, T: FileSystem + Default + 'static> NormalModuleTask<'task, T> {
&mut symbol_for_module,
repr_name.into_owned(),
self.module_type,
&source,
&self.path,
);
let namespace_symbol = scanner.namespace_symbol;
let scan_result = scanner.scan(program.program());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::Arc;

use index_vec::IndexVec;
use oxc::span::SourceType;
use rolldown_common::{ExportsKind, ModuleId, ModuleType, ResourceId, SymbolRef};
use rolldown_common::{ExportsKind, FilePath, ModuleId, ModuleType, ResourceId, SymbolRef};
use rolldown_error::BuildError;
use rolldown_oxc::{OxcCompiler, OxcProgram};

Expand Down Expand Up @@ -49,6 +51,7 @@ impl RuntimeNormalModuleTask {
repr_name,
import_records: _,
exports_kind: _,
warnings: _,
} = scan_result;

builder.id = Some(self.module_id);
Expand Down Expand Up @@ -81,19 +84,23 @@ impl RuntimeNormalModuleTask {
}

fn make_ast(&self, source: String) -> (OxcProgram, AstScope, ScanResult, AstSymbol, SymbolRef) {
let source: Arc<str> = source.into();
let source_type = SourceType::default();
let program = OxcCompiler::parse(source, source_type);
let program = OxcCompiler::parse(Arc::clone(&source), source_type);

let semantic = program.make_semantic(source_type);
let (mut symbol_table, scope) = semantic.into_symbol_table_and_scope_tree();
let ast_scope = AstScope::new(scope, std::mem::take(&mut symbol_table.references));
let mut symbol_for_module = AstSymbol::from_symbol_table(symbol_table);
let facade_path = FilePath::new("runtime");
let scanner = scanner::Scanner::new(
self.module_id,
&ast_scope,
&mut symbol_for_module,
"runtime".to_string(),
ModuleType::EsmMjs,
&source,
&facade_path,
);
let namespace_symbol = scanner.namespace_symbol;
let scan_result = scanner.scan(program.program());
Expand Down
5 changes: 5 additions & 0 deletions crates/rolldown/src/bundler/stages/link_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::ptr::addr_of;

use index_vec::IndexVec;
use rolldown_common::{ExportsKind, ImportKind, ModuleId, StmtInfoId, SymbolRef, WrapKind};
use rolldown_error::BuildError;
use rustc_hash::FxHashSet;

use crate::bundler::{
Expand All @@ -24,6 +25,7 @@ pub struct LinkStageOutput {
pub linking_infos: LinkingInfoVec,
pub symbols: Symbols,
pub runtime: RuntimeModuleBrief,
pub warnings: Vec<BuildError>,
}

#[derive(Debug)]
Expand All @@ -34,6 +36,7 @@ pub struct LinkStage {
pub runtime: RuntimeModuleBrief,
pub sorted_modules: Vec<ModuleId>,
pub linking_infos: LinkingInfoVec,
pub warnings: Vec<BuildError>,
}

impl LinkStage {
Expand All @@ -49,6 +52,7 @@ impl LinkStage {
entries: scan_stage_output.entries,
symbols: scan_stage_output.symbols,
runtime: scan_stage_output.runtime,
warnings: scan_stage_output.warnings,
}
}

Expand Down Expand Up @@ -79,6 +83,7 @@ impl LinkStage {
linking_infos: self.linking_infos,
symbols: self.symbols,
runtime: self.runtime,
warnings: self.warnings,
}
}

Expand Down
5 changes: 3 additions & 2 deletions crates/rolldown/src/bundler/stages/scan_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct ScanStageOutput {
pub entries: Vec<(Option<String>, ModuleId)>,
pub symbols: Symbols,
pub runtime: RuntimeModuleBrief,
pub warnings: Vec<BuildError>,
}

impl<Fs: FileSystem + Default + 'static> ScanStage<Fs> {
Expand Down Expand Up @@ -61,12 +62,12 @@ impl<Fs: FileSystem + Default + 'static> ScanStage<Fs> {

let user_entries = self.resolve_user_defined_entries()?;

let ModuleLoaderOutput { modules, entries, symbols, runtime } =
let ModuleLoaderOutput { modules, entries, symbols, runtime, warnings } =
module_loader.fetch_all_modules(&user_entries).await?;

tracing::debug!("Scan stage finished {modules:#?}");

Ok(ScanStageOutput { modules, entries, symbols, runtime })
Ok(ScanStageOutput { modules, entries, symbols, runtime, warnings })
}

/// Resolve `InputOptions.input`
Expand Down
24 changes: 22 additions & 2 deletions crates/rolldown/src/bundler/visitors/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use index_vec::IndexVec;
use oxc::{
ast::{
Expand All @@ -11,9 +13,10 @@ use oxc::{
span::{Atom, Span},
};
use rolldown_common::{
representative_name, ExportsKind, ImportKind, ImportRecordId, LocalExport, ModuleId, ModuleType,
NamedImport, RawImportRecord, Specifier, StmtInfo, StmtInfos, SymbolRef,
representative_name, ExportsKind, FilePath, ImportKind, ImportRecordId, LocalExport, ModuleId,
ModuleType, NamedImport, RawImportRecord, Specifier, StmtInfo, StmtInfos, SymbolRef,
};
use rolldown_error::BuildError;
use rolldown_oxc::{BindingIdentifierExt, BindingPatternExt};
use rustc_hash::FxHashMap;

Expand All @@ -30,11 +33,14 @@ pub struct ScanResult {
pub export_default_symbol_id: Option<SymbolId>,
pub imports: FxHashMap<Span, ImportRecordId>,
pub exports_kind: ExportsKind,
pub warnings: Vec<BuildError>,
}

pub struct Scanner<'a> {
idx: ModuleId,
source: &'a Arc<str>,
module_type: ModuleType,
file_path: &'a FilePath,
scope: &'a AstScope,
symbol_table: &'a mut AstSymbol,
current_stmt_info: StmtInfo,
Expand All @@ -53,6 +59,8 @@ impl<'ast> Scanner<'ast> {
symbol_table: &'ast mut AstSymbol,
repr_name: String,
module_type: ModuleType,
source: &'ast Arc<str>,
file_path: &'ast FilePath,
) -> Self {
let mut result = ScanResult::default();
let name = format!("{repr_name}_ns");
Expand All @@ -75,6 +83,8 @@ impl<'ast> Scanner<'ast> {
namespace_symbol: namespace_ref,
used_exports_ref: false,
used_module_ref: false,
source,
file_path,
}
}

Expand Down Expand Up @@ -376,6 +386,16 @@ impl<'ast> Visit<'ast> for Scanner<'ast> {
if ident.name == "exports" {
self.used_exports_ref = true;
}
if ident.name == "eval" {
self.result.warnings.push(
BuildError::unsupported_eval(
self.file_path.to_string(),
Arc::clone(self.source),
ident.span,
)
.with_severity_warning(),
);
}
}
_ => {}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) type SharedResolver<T> = Arc<Resolver<T>>;
pub use crate::{
bundler::{
bundle::output::{Output, OutputAsset, OutputChunk, RenderedModule},
bundler::Bundler,
bundler::{Bundler, RolldownOutput},
options::{
file_name_template::FileNameTemplate,
input_options::{External, InputItem, InputOptions},
Expand Down
Loading

0 comments on commit 7cd5f92

Please sign in to comment.