Skip to content

Commit

Permalink
feat: correct indent for generated code (#160)
Browse files Browse the repository at this point in the history
* feat: add indentor for inserted code

* snapshot

* feat: correct indent for generated code
  • Loading branch information
hyf0 authored Nov 4, 2023
1 parent eaebf11 commit 21a70c2
Show file tree
Hide file tree
Showing 64 changed files with 210 additions and 199 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ serde_json = "1.0.87"
insta = "1.21.0"
testing_macros = "0.2.7"
scoped-tls = "1.0.1"
string_wizard = { version = "0.0.12" }
string_wizard = { version = "0.0.14" }
async-trait = "0.1.62"
futures = "0.3.25"
itertools = "0.10.5"
Expand Down
24 changes: 17 additions & 7 deletions crates/rolldown/src/bundler/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use oxc::{
use rolldown_common::{SymbolRef, WrapKind};
use rolldown_oxc::BindingIdentifierExt;
use rustc_hash::FxHashMap;
use string_wizard::MagicString;
use string_wizard::{IndentOptions, MagicString};

use super::{
chunk_graph::ChunkGraph,
Expand Down Expand Up @@ -109,16 +109,21 @@ impl<'r> AstRenderer<'r> {

match &mut self.kind {
RenderKind::WrappedEsm(info) => {
let mut exclude_indent = vec![];
info.hoisted_functions.iter().for_each(|f| {
// Improve: multiply functions should separate by "\n"
self.ctx.source.relocate(f.start, f.end, 0);
self.ctx.source.append_right(0, "\n");
self.ctx.source.append_left(f.end, "\n");
exclude_indent.push([f.start, f.end]);
});
self.ctx.source.indent_with(IndentOptions {
exclude: exclude_indent,
indentor: Some(&self.indentor.repeat(2)),
});
if !info.hoisted_vars.is_empty() {
self.ctx.source.append_right(0, format!("var {};\n", info.hoisted_vars.join(",")));
}

if let Some(s) = self.ctx.generate_namespace_variable_declaration() {
if let Some(s) = self.generate_namespace_variable_declaration() {
self.ctx.source.append_right(0, s);
}

Expand All @@ -127,15 +132,20 @@ impl<'r> AstRenderer<'r> {
self.ctx.source.append_right(
0,
format!(
"var {wrap_ref_name} = {esm_ref_name}({{\n'{}'() {{\n",
"var {wrap_ref_name} = {esm_ref_name}({{\n{}'{}'() {{\n",
self.indentor,
self.ctx.module.resource_id.prettify(),
),
);
self.ctx.source.append("\n}\n});");
self.ctx.source.append(format!("\n{}}}\n}});", self.indentor));
}
RenderKind::Cjs => {
let wrap_ref_name = self.ctx.wrap_symbol_name.unwrap();
let prettify_id = self.ctx.module.resource_id.prettify();
self.ctx.source.indent_with(IndentOptions {
indentor: Some(&self.indentor.repeat(2)),
..Default::default()
});
let commonjs_ref_name = self.ctx.canonical_name_for_runtime("__commonJS");
self.ctx.source.prepend(format!(
"var {wrap_ref_name} = {commonjs_ref_name}({{\n{}'{prettify_id}'(exports, module) {{\n",
Expand All @@ -145,7 +155,7 @@ impl<'r> AstRenderer<'r> {
assert!(!self.ctx.module.is_namespace_referenced());
}
RenderKind::Esm => {
if let Some(s) = self.ctx.generate_namespace_variable_declaration() {
if let Some(s) = self.generate_namespace_variable_declaration() {
self.ctx.source.prepend(s);
}
}
Expand Down
54 changes: 28 additions & 26 deletions crates/rolldown/src/bundler/renderer/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,6 @@ impl<'r> AstRenderContext<'r> {
self.source.append_left(start, content);
}

pub fn generate_namespace_variable_declaration(&mut self) -> Option<String> {
if self.module.is_namespace_referenced() {
let ns_name = self.canonical_name_for(self.module.namespace_symbol);
let exports: String = self
.linking_info
.sorted_exports()
.map(|(exported_name, resolved_export)| {
let canonical_ref = self.graph.symbols.par_canonical_ref_for(resolved_export.symbol_ref);
let symbol = self.graph.symbols.get(canonical_ref);
let return_expr = if let Some(ns_alias) = &symbol.namespace_alias {
let canonical_ns_name = &self.canonical_names[&ns_alias.namespace_ref];
format!("{canonical_ns_name}.{exported_name}",)
} else {
let canonical_name = self.canonical_names.get(&canonical_ref).unwrap();
format!("{canonical_name}",)
};
format!(" get {exported_name}() {{ return {return_expr} }}",)
})
.collect::<Vec<_>>()
.join(",\n");
Some(format!("var {ns_name} = {{\n{exports}\n}};\n",))
} else {
None
}
}

pub fn generate_import_commonjs_module(
&self,
importee: &NormalModule,
Expand Down Expand Up @@ -105,6 +79,34 @@ impl<'r> AstRenderer<'r> {
);
}

pub fn generate_namespace_variable_declaration(&mut self) -> Option<String> {
if self.ctx.module.is_namespace_referenced() {
let ns_name = self.ctx.canonical_name_for(self.ctx.module.namespace_symbol);
let exports: String = self
.ctx
.linking_info
.sorted_exports()
.map(|(exported_name, resolved_export)| {
let canonical_ref =
self.ctx.graph.symbols.par_canonical_ref_for(resolved_export.symbol_ref);
let symbol = self.ctx.graph.symbols.get(canonical_ref);
let return_expr = if let Some(ns_alias) = &symbol.namespace_alias {
let canonical_ns_name = &self.ctx.canonical_names[&ns_alias.namespace_ref];
format!("{canonical_ns_name}.{exported_name}",)
} else {
let canonical_name = self.ctx.canonical_names.get(&canonical_ref).unwrap();
format!("{canonical_name}",)
};
format!("{}get {exported_name}() {{ return {return_expr} }}", self.indentor)
})
.collect::<Vec<_>>()
.join(",\n");
Some(format!("var {ns_name} = {{\n{exports}\n}};\n",))
} else {
None
}
}

pub fn remove_node(&mut self, span: Span) {
self.ctx.source.remove(span.start, span.end);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ function foo$1() {
return 'foo'
}
var foo_ns = {
get foo() { return foo$1 }
get foo() { return foo$1 }
};
var init_foo = __esm({
'foo.js'() {
'foo.js'() {
}
}
});
// bar.js
function bar$1() {
return 'bar'
}
var bar_ns = {
get bar() { return bar$1 }
get bar() { return bar$1 }
};
var init_bar = __esm({
'bar.js'() {
'bar.js'() {
}
}
});
// entry.js
const {foo} = (init_foo(), __toCommonJS(foo_ns))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var types_ns = {
};
var init_types = __esm({
'types.mjs'() {
'types.mjs'() {
}
}
});
// entry.js
console.log((init_types(), __toCommonJS(types_ns)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
exports.foo = function() {
return 'foo'
}
exports.foo = function() {
return 'foo'
}
}
});
// bar.js
var require_bar = __commonJS({
'bar.js'(exports, module) {
exports.bar = function() {
return 'bar'
}
exports.bar = function() {
return 'bar'
}
}
});
// entry.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,111 +13,111 @@ import { __esm, __toCommonJS } from "./_rolldown_runtime.mjs";
// a.js
var abc;
var a_ns = {
get abc() { return abc }
get abc() { return abc }
};
var init_a = __esm({
'a.js'() {
abc = undefined
}
'a.js'() {
abc = undefined
}
});
// b.js
var xyz;
var b_ns = {
get xyz() { return xyz }
get xyz() { return xyz }
};
var init_b = __esm({
'b.js'() {
xyz = null
}
'b.js'() {
xyz = null
}
});
// commonjs.js
function Fn() {}
var commonjs_default,v,l,c,Class;
var commonjs_ns = {
get Class() { return Class },
get Fn() { return Fn },
get abc() { return abc },
get b() { return b_ns },
get c() { return c },
get default() { return commonjs_default },
get l() { return l },
get v() { return v }
get Class() { return Class },
get Fn() { return Fn },
get abc() { return abc },
get b() { return b_ns },
get c() { return c },
get default() { return commonjs_default },
get l() { return l },
get v() { return v }
};
var init_commonjs = __esm({
'commonjs.js'() {
commonjs_default = 123
v = 234
l = 234
c = 234
'commonjs.js'() {
commonjs_default = 123
v = 234
l = 234
c = 234
Class = class Class {}
Class = class Class {}
}
}
});
// c.js
var c_default;
var c_ns = {
get default() { return c_default }
get default() { return c_default }
};
var init_c = __esm({
'c.js'() {
c_default = class {}
'c.js'() {
c_default = class {}
}
}
});
// d.js
var Foo;
var d_ns = {
get default() { return Foo }
get default() { return Foo }
};
var init_d = __esm({
'd.js'() {
Foo = class Foo {}
'd.js'() {
Foo = class Foo {}
Foo.prop = 123
}
}
});
// e.js
function e_default() {}
var e_ns = {
get default() { return e_default }
get default() { return e_default }
};
var init_e = __esm({
'e.js'() {
'e.js'() {
}
}
});
// f.js
function foo$1() {}
var f_ns = {
get default() { return foo$1 }
get default() { return foo$1 }
};
var init_f = __esm({
'f.js'() {
foo$1.prop = 123
}
'f.js'() {
foo$1.prop = 123
}
});
// g.js
async function g_default() {}
var g_ns = {
get default() { return g_default }
get default() { return g_default }
};
var init_g = __esm({
'g.js'() {
'g.js'() {
}
}
});
// h.js
async function foo() {}
var h_ns = {
get default() { return foo }
get default() { return foo }
};
var init_h = __esm({
'h.js'() {
foo.prop = 123
}
'h.js'() {
foo.prop = 123
}
});
// entry.js
init_commonjs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ input_file: crates/rolldown/tests/esbuild/default/export_forms_es6
const abc = undefined
// b.js
var b_ns = {
get xyz() { return xyz }
get xyz() { return xyz }
};
const xyz = null
// entry.js
Expand Down
Loading

0 comments on commit 21a70c2

Please sign in to comment.