Skip to content

Commit

Permalink
feat(rust): rendering runtime module in snapshot testing (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Nov 28, 2023
1 parent abbc8aa commit 3cb39a2
Show file tree
Hide file tree
Showing 58 changed files with 943 additions and 78 deletions.
5 changes: 5 additions & 0 deletions crates/rolldown/src/bundler/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ impl NormalModule {

#[allow(clippy::needless_pass_by_value)]
pub fn render(&self, ctx: ModuleRenderContext<'_>) -> Option<MagicString<'static>> {
// FIXME: Remove this when we support removing module that rendered nothing
if self.id == ctx.graph.runtime.id() && self.stmt_infos.iter().all(|info| !info.is_included) {
return None;
}

let source = self.ast.source();
// FIXME: should not clone here
let mut source = MagicString::new(source.to_string());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl RuntimeNormalModuleTask {
pub fn run(self) {
let mut builder = NormalModuleBuilder::default();

let source = include_str!("../runtime/index.js").to_string();
let source = include_str!("../runtime/runtime-without-comments.js").to_string();

let (ast, scope, scan_result, symbol, namespace_symbol) = self.make_ast(source);

Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/renderer/impl_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'ast, 'r> AstRenderer<'r> {
if self.current_stmt_info.get().is_included {
self.visit_statement(stmt);
} else {
self.remove_node(stmt.span());
self.ctx.remove_stmt(stmt.span());
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions crates/rolldown/src/bundler/renderer/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ impl<'r> AstRenderContext<'r> {
self.source.remove(span.start, span.end);
}

pub fn remove_stmt(&mut self, span: Span) {
let end = span.end as usize;
let should_delete_line_ending = self.module.ast.source().get(end..=end) == Some("\n");
if should_delete_line_ending {
self.source.remove(span.start, span.end + 1);
} else {
self.source.remove(span.start, span.end);
}
}

pub fn hoisted_module_declaration(&mut self, decl_start: u32, content: String) {
let start = self.first_stmt_start.unwrap_or(decl_start);
self.source.append_left(start, content);
Expand Down
41 changes: 41 additions & 0 deletions crates/rolldown/src/bundler/runtime/runtime-without-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

var __create = Object.create
var __defProp = Object.defineProperty
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
var __getOwnPropNames = Object.getOwnPropertyNames
var __getProtoOf = Object.getPrototypeOf
var __hasOwnProp = Object.prototype.hasOwnProperty
var __esm = (fn, res) => function () {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res
}
var __esmMin = (fn, res) => () => (fn && (res = fn(fn = 0)), res)
var __commonJS = (cb, mod) => function () {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports
}
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports)
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true })
}
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === 'object' || typeof from === 'function')
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i]
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: (k => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
}
return to
}
var __reExport = (target, mod, secondTarget) => (
__copyProps(target, mod, 'default'),
secondTarget && __copyProps(secondTarget, mod, 'default')
)
var __toESM = (mod, isNodeMode, target) => (
target = mod != null ? __create(__getProtoOf(mod)) : {},
__copyProps(
isNodeMode || !mod || !mod.__esModule
? __defProp(target, 'default', { value: mod, enumerable: true })
: target,
mod)
)
var __toCommonJS = mod => __copyProps(__defProp({}, '__esModule', { value: true }), mod)
16 changes: 0 additions & 16 deletions crates/rolldown/src/bundler/stages/bundle_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,25 +305,9 @@ impl<'a> BundleStage<'a> {
self.link_output.modules.len()
];

// FIXME: should remove this when tree shaking is supported
let is_rolldown_test = std::env::var("ROLLDOWN_TEST").is_ok();
if is_rolldown_test {
let runtime_chunk_id = chunks.push(Chunk::new(
Some("_rolldown_runtime".to_string()),
None,
BitSet::new(0),
vec![self.link_output.runtime.id()],
));
module_to_chunk[self.link_output.runtime.id()] = Some(runtime_chunk_id);
}

// 1. Assign modules to corresponding chunks
// 2. Create shared chunks to store modules that belong to multiple chunks.
for module in &self.link_output.modules {
// FIXME: should remove this when tree shaking is supported
if is_rolldown_test && module.id() == self.link_output.runtime.id() {
continue;
}
let bits = &module_to_bits[module.id()];
if let Some(chunk_id) = bits_to_chunk.get(bits).copied() {
chunks[chunk_id].modules.push(module.id());
Expand Down
4 changes: 0 additions & 4 deletions crates/rolldown/tests/common/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ impl Case {
assets.sort_by_key(|c| c.file_name().to_string());
let artifacts = assets
.iter()
// FIXME: should render the runtime module while tree shaking being supported
.filter(|asset| !asset.file_name().contains("rolldown_runtime"))
.flat_map(|asset| {
[
Cow::Owned(format!("## {}\n", asset.file_name())),
Expand All @@ -88,8 +86,6 @@ impl Case {
self.snapshot.append("\n\n## Output Stats\n\n");
let stats = assets
.iter()
// FIXME: should render the runtime module while tree shaking being supported
.filter(|asset| !asset.file_name().contains("rolldown_runtime"))
.flat_map(|asset| match asset {
Output::Chunk(chunk) => {
vec![Cow::Owned(format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@ input_file: crates/rolldown/tests/esbuild/default/common_js_from_es6
## entry_js.mjs

```js
import { __esm, __toCommonJS } from "./_rolldown_runtime.mjs";
// <runtime>
var __defProp = Object.defineProperty
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
var __getOwnPropNames = Object.getOwnPropertyNames
var __hasOwnProp = Object.prototype.hasOwnProperty
var __esm = (fn, res) => function () {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res
}
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === 'object' || typeof from === 'function')
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i]
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: (k => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
}
return to
}
var __toCommonJS = mod => __copyProps(__defProp({}, '__esModule', { value: true }), mod)
// foo.js
function foo$1() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@ input_file: crates/rolldown/tests/esbuild/default/empty_export_clause_bundle_as_
## entry_js.mjs

```js
import { __esm, __toCommonJS } from "./_rolldown_runtime.mjs";
// <runtime>
var __defProp = Object.defineProperty
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
var __getOwnPropNames = Object.getOwnPropertyNames
var __hasOwnProp = Object.prototype.hasOwnProperty
var __esm = (fn, res) => function () {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res
}
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === 'object' || typeof from === 'function')
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i]
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: (k => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
}
return to
}
var __toCommonJS = mod => __copyProps(__defProp({}, '__esModule', { value: true }), mod)
// types.mjs
var types_ns = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,34 @@ input_file: crates/rolldown/tests/esbuild/default/es6_from_common_js
## entry_js.mjs

```js
import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// <runtime>
var __create = Object.create
var __defProp = Object.defineProperty
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
var __getOwnPropNames = Object.getOwnPropertyNames
var __getProtoOf = Object.getPrototypeOf
var __hasOwnProp = Object.prototype.hasOwnProperty
var __commonJS = (cb, mod) => function () {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports
}
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === 'object' || typeof from === 'function')
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i]
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: (k => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
}
return to
}
var __toESM = (mod, isNodeMode, target) => (
target = mod != null ? __create(__getProtoOf(mod)) : {},
__copyProps(
isNodeMode || !mod || !mod.__esModule
? __defProp(target, 'default', { value: mod, enumerable: true })
: target,
mod)
)
// foo.js
var require_foo = __commonJS({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@ input_file: crates/rolldown/tests/esbuild/default/export_forms_common_js
## entry_js.mjs

```js
import { __esm, __toCommonJS } from "./_rolldown_runtime.mjs";
// <runtime>
var __defProp = Object.defineProperty
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
var __getOwnPropNames = Object.getOwnPropertyNames
var __hasOwnProp = Object.prototype.hasOwnProperty
var __esm = (fn, res) => function () {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res
}
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === 'object' || typeof from === 'function')
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i]
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: (k => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
}
return to
}
var __toCommonJS = mod => __copyProps(__defProp({}, '__esModule', { value: true }), mod)
// a.js
var abc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,34 @@ input_file: crates/rolldown/tests/esbuild/default/import_missing_common_js
## entry_js.mjs

```js
import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// <runtime>
var __create = Object.create
var __defProp = Object.defineProperty
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
var __getOwnPropNames = Object.getOwnPropertyNames
var __getProtoOf = Object.getPrototypeOf
var __hasOwnProp = Object.prototype.hasOwnProperty
var __commonJS = (cb, mod) => function () {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports
}
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === 'object' || typeof from === 'function')
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i]
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: (k => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
}
return to
}
var __toESM = (mod, isNodeMode, target) => (
target = mod != null ? __create(__getProtoOf(mod)) : {},
__copyProps(
isNodeMode || !mod || !mod.__esModule
? __defProp(target, 'default', { value: mod, enumerable: true })
: target,
mod)
)
// foo.js
var require_foo = __commonJS({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ input_file: crates/rolldown/tests/esbuild/default/nested_common_js
## entry_js.mjs

```js
import { __commonJS } from "./_rolldown_runtime.mjs";
// <runtime>
var __getOwnPropNames = Object.getOwnPropertyNames
var __commonJS = (cb, mod) => function () {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports
}
// foo.js
var require_foo = __commonJS({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,34 @@ input_file: crates/rolldown/tests/esbuild/default/nested_es6_from_common_js
## entry_js.mjs

```js
import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// <runtime>
var __create = Object.create
var __defProp = Object.defineProperty
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
var __getOwnPropNames = Object.getOwnPropertyNames
var __getProtoOf = Object.getPrototypeOf
var __hasOwnProp = Object.prototype.hasOwnProperty
var __commonJS = (cb, mod) => function () {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports
}
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === 'object' || typeof from === 'function')
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i]
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: (k => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
}
return to
}
var __toESM = (mod, isNodeMode, target) => (
target = mod != null ? __create(__getProtoOf(mod)) : {},
__copyProps(
isNodeMode || !mod || !mod.__esModule
? __defProp(target, 'default', { value: mod, enumerable: true })
: target,
mod)
)
// foo.js
var require_foo = __commonJS({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ input_file: crates/rolldown/tests/esbuild/default/new_expression_common_js
## entry_js.mjs

```js
import { __commonJS } from "./_rolldown_runtime.mjs";
// <runtime>
var __getOwnPropNames = Object.getOwnPropertyNames
var __commonJS = (cb, mod) => function () {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports
}
// foo.js
var require_foo = __commonJS({
Expand Down
5 changes: 5 additions & 0 deletions crates/rolldown/tests/esbuild/default/outbase/artifacts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ input_file: crates/rolldown/tests/esbuild/default/outbase
---
# Assets

## 2.mjs

```js
```
## c_js.mjs

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,34 @@ input_file: crates/rolldown/tests/esbuild/default/re_export_common_js_as_es6
## entry_js.mjs

```js
import { __commonJS, __toESM } from "./_rolldown_runtime.mjs";
// <runtime>
var __create = Object.create
var __defProp = Object.defineProperty
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
var __getOwnPropNames = Object.getOwnPropertyNames
var __getProtoOf = Object.getPrototypeOf
var __hasOwnProp = Object.prototype.hasOwnProperty
var __commonJS = (cb, mod) => function () {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports
}
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === 'object' || typeof from === 'function')
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i]
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: (k => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
}
return to
}
var __toESM = (mod, isNodeMode, target) => (
target = mod != null ? __create(__getProtoOf(mod)) : {},
__copyProps(
isNodeMode || !mod || !mod.__esModule
? __defProp(target, 'default', { value: mod, enumerable: true })
: target,
mod)
)
// foo.js
var require_foo = __commonJS({
Expand Down

0 comments on commit 3cb39a2

Please sign in to comment.