Skip to content

Commit

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

* snapshot
  • Loading branch information
hyf0 committed Nov 3, 2023
1 parent efc32ff commit eaebf11
Show file tree
Hide file tree
Showing 29 changed files with 69 additions and 66 deletions.
5 changes: 3 additions & 2 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.11" }
string_wizard = { version = "0.0.12" }
async-trait = "0.1.62"
futures = "0.3.25"
itertools = "0.10.5"
Expand Down
8 changes: 5 additions & 3 deletions crates/rolldown/src/bundler/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ impl RenderKind {
pub struct AstRenderer<'r> {
ctx: AstRenderContext<'r>,
kind: RenderKind,
indentor: String,
}

impl<'r> AstRenderer<'r> {
pub fn new(ctx: AstRenderContext<'r>, kind: RenderKind) -> Self {
Self { ctx, kind }
Self { kind, indentor: ctx.source.guessed_indentor().to_string(), ctx }
}
}

Expand Down Expand Up @@ -137,9 +138,10 @@ impl<'r> AstRenderer<'r> {
let prettify_id = self.ctx.module.resource_id.prettify();
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",
"var {wrap_ref_name} = {commonjs_ref_name}({{\n{}'{prettify_id}'(exports, module) {{\n",
self.indentor,
));
self.ctx.source.append("\n}\n});");
self.ctx.source.append(format!("\n{}}}\n}});", self.indentor));
assert!(!self.ctx.module.is_namespace_referenced());
}
RenderKind::Esm => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
exports.foo = function() {
return 'foo'
}
}
}
});
// bar.js
var require_bar = __commonJS({
'bar.js'(exports, module) {
'bar.js'(exports, module) {
exports.bar = function() {
return 'bar'
}
}
}
});
// entry.js
var import_foo = __toESM(require_foo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
exports.x = 123
}
}
});
// entry.js
var import_foo = __toESM(require_foo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
module.exports = function() {
return 123
}
}
}
});
// entry.js
function nestedScope() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
exports.fn = function() {
return 123
}
}
}
});
// entry.js
var import_foo = __toESM(require_foo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
class Foo {}
module.exports = {Foo};
}
}
});
// entry.js
new (require_foo()).Foo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
exports.bar = 123
}
}
});
// entry.js
var import_foo = __toESM(require_foo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// dir/index.js
var require_dir_index = __commonJS({
'dir/index.js'(exports, module) {
'dir/index.js'(exports, module) {
module.exports = 123
}
}
});
// entry.js
console.log(require_dir_index())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import { __commonJS } from "./_rolldown_runtime.mjs";
// is-main.js
var require_is_main = __commonJS({
'is-main.js'(exports, module) {
'is-main.js'(exports, module) {
module.exports = require.main === module
}
}
});
// entry.js
var require_entry = __commonJS({
'entry.js'(exports, module) {
'entry.js'(exports, module) {
console.log('is main:', require.main === module)
console.log(require_is_main())
console.log('cache:', require.cache);
}
}
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// index.js
var require_require_parent_dir_common_js_index = __commonJS({
'index.js'(exports, module) {
'index.js'(exports, module) {
module.exports = 123
}
}
});
// dir/entry.js
console.log(require_require_parent_dir_common_js_index())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import { __commonJS } from "./_rolldown_runtime.mjs";
// entry.js
var require_entry = __commonJS({
'entry.js'(exports, module) {
'entry.js'(exports, module) {
try {
const supportsColor = require('supports-color');
if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
exports.colors = [];
}
} catch (error) {
}
}
}
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
module.exports = function() {
return 123
}
}
}
});
// entry.js
const fn = require_foo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// cjs.js
var require_cjs = __commonJS({
'cjs.js'(exports, module) {
'cjs.js'(exports, module) {
'use strict'
exports.foo = process
}
}
});
// entry.js
console.log(require_cjs())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
exports.foo = 123
}
}
});
// entry.js
var import_foo = __toESM(require_foo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
exports.foo = 123
}
}
});
// entry.js
var import_foo = __toESM(require_foo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
exports.foo = 123
}
}
});
// entry.js
var import_foo = __toESM(require_foo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
exports.x = 123
}
}
});
// entry.js
var import_foo = __toESM(require_foo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// foo.js
var require_foo = __commonJS({
'foo.js'(exports, module) {
'foo.js'(exports, module) {
exports.x = 123
}
}
});
// entry.js
var import_foo = __toESM(require_foo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS } from "./_rolldown_runtime.mjs";
// shared.js
var require_shared = __commonJS({
'shared.js'(exports, module) {
'shared.js'(exports, module) {
exports.foo = 123
}
}
});
export { require_shared };
```
Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown/tests/fixtures/basic_commonjs/artifacts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ esm_named_class = class esm_named_class {}
});
// commonjs.js
var require_commonjs$1 = __commonJS({
'commonjs.js'(exports, module) {
'commonjs.js'(exports, module) {
var value = (init_esm(), __toCommonJS(esm_ns));
module.exports = value;
}
}
});
// main.js
var import_commonjs = __toESM(require_commonjs$1());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// commonjs.js
var require_commonjs = __commonJS({
'commonjs.js'(exports, module) {
'commonjs.js'(exports, module) {
exports.a = 1
}
}
});
// main.js
var import_commonjs = __toESM(require_commonjs());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// commonjs.js
var require_commonjs = __commonJS({
'commonjs.js'(exports, module) {
'commonjs.js'(exports, module) {
exports.a = 1
}
}
});
// main.js
var import_commonjs = __toESM(require_commonjs());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __reExport, __toESM } from "./_rolldown_runtime.mjs";
// commonjs.js
var require_commonjs = __commonJS({
'commonjs.js'(exports, module) {
'commonjs.js'(exports, module) {
exports.a = 1
}
}
});
// proxy.js
var proxy_ns = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { __commonJS, __reExport, __toESM } from "./_rolldown_runtime.mjs";
// commonjs.js
var require_commonjs = __commonJS({
'commonjs.js'(exports, module) {
'commonjs.js'(exports, module) {
exports.a = 1
}
}
});
// commonjs2.js
var require_commonjs2 = __commonJS({
'commonjs2.js'(exports, module) {
'commonjs2.js'(exports, module) {
exports.a = 2
}
}
});
// proxy.js
var proxy_ns = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// commonjs.js
var require_commonjs = __commonJS({
'commonjs.js'(exports, module) {
'commonjs.js'(exports, module) {
exports.a = 1
}
}
});
// main.js
var import_commonjs = __toESM(require_commonjs());
Expand Down

0 comments on commit eaebf11

Please sign in to comment.