Skip to content

Commit

Permalink
feat: restrict typings and refactor RenderedModule (rolldown#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Mar 9, 2024
1 parent f81f87d commit 1cc0fbe
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 128 deletions.
9 changes: 2 additions & 7 deletions crates/rolldown/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod de_conflict;
pub mod render_chunk;
mod render_chunk_exports;
mod render_chunk_imports;

use index_vec::IndexVec;
use rolldown_common::ChunkId;

Expand Down Expand Up @@ -106,13 +107,7 @@ impl Chunk {
);
Some((
m.resource_id.expect_file().to_string(),
RenderedModule {
original_length: m.source.len().try_into().unwrap(),
rendered_length: rendered_content
.as_ref()
.map(|c| c.code.len() as u32)
.unwrap_or_default(),
},
RenderedModule { code: None },
rendered_content,
if output_options.sourcemap.is_hidden() {
None
Expand Down
10 changes: 3 additions & 7 deletions crates/rolldown_binding/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export interface RenderedChunk {
moduleIds: Array<string>
exports: Array<string>
fileName: string
modules: Record<string, RenderedModule>
modules: Record<string, BindingRenderedModule>
}
export interface SourceMap {
mappings: string
Expand All @@ -95,12 +95,8 @@ export interface SourceMap {
sources: Array<string>
sourcesContent: Array<string>
}
export interface RenderedModule {
export interface BindingRenderedModule {
code?: string
removedExports: Array<string>
renderedExports: Array<string>
originalLength: number
renderedLength: number
}
export interface OutputChunk {
isEntry: boolean
Expand All @@ -109,7 +105,7 @@ export interface OutputChunk {
moduleIds: Array<string>
exports: Array<string>
fileName: string
modules: Record<string, RenderedModule>
modules: Record<string, BindingRenderedModule>
code: string
}
export interface OutputAsset {
Expand Down
3 changes: 2 additions & 1 deletion crates/rolldown_binding/src/options/plugin/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ pub struct RenderedChunk {
pub exports: Vec<String>,
// RenderedChunk
pub file_name: String,
pub modules: HashMap<String, crate::output::RenderedModule>,
#[serde(skip)]
pub modules: HashMap<String, crate::output::BindingRenderedModule>,
}

impl From<rolldown_common::RenderedChunk> for RenderedChunk {
Expand Down
33 changes: 14 additions & 19 deletions crates/rolldown_binding/src/output.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
use std::collections::HashMap;
use std::{collections::HashMap, fmt::Debug};

use derivative::Derivative;
use napi_derive::napi;
use serde::Deserialize;

#[napi_derive::napi(object)]
#[derive(Deserialize, Default, Derivative)]
#[serde(rename_all = "camelCase")]
#[derivative(Debug)]
pub struct RenderedModule {
#[napi(object)]
pub struct BindingRenderedModule {
pub code: Option<String>,
pub removed_exports: Vec<String>,
pub rendered_exports: Vec<String>,
pub original_length: u32,
pub rendered_length: u32,
}

impl From<rolldown_common::RenderedModule> for RenderedModule {
impl Debug for BindingRenderedModule {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BindingRenderedModule").field("code", &"...").finish()
}
}

impl From<rolldown_common::RenderedModule> for BindingRenderedModule {
fn from(value: rolldown_common::RenderedModule) -> Self {
Self {
code: None,
original_length: value.original_length,
rendered_length: value.rendered_length,
removed_exports: vec![],
rendered_exports: vec![],
}
Self { code: value.code }
}
}

Expand All @@ -40,7 +34,8 @@ pub struct OutputChunk {
pub exports: Vec<String>,
// RenderedChunk
pub file_name: String,
pub modules: HashMap<String, RenderedModule>,
#[serde(skip_deserializing)]
pub modules: HashMap<String, BindingRenderedModule>,
// OutputChunk
pub code: String,
}
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown_common/src/types/output_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustc_hash::FxHashMap;

use super::rendered_module::RenderedModule;

#[allow(clippy::zero_sized_map_values)]
#[derive(Debug, Clone)]
pub struct OutputChunk {
// PreRenderedChunk
Expand Down
4 changes: 1 addition & 3 deletions crates/rolldown_common/src/types/rendered_module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#[derive(Debug, Clone)]
pub struct RenderedModule {
// The code of the module is omit at now.
pub original_length: u32,
pub rendered_length: u32,
pub code: Option<String>,
}
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ lint-node:
yarn lint-filename
yarn lint
yarn prettier:ci
yarn typecheck

lint:
just lint-rust
Expand Down
1 change: 1 addition & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"devDependencies": {
"@types/node": "^20.11.25",
"type-fest": "^4.12.0",
"typescript": "^5.4.2",
"unbuild": "^2.0.0",
"vitest": "^1.3.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RolldownOutput } from './objects/rolldown-output'
import type { InputOptions, RolldownPlugin } from './options/input-options'
import type { OutputOptions } from './options/output-options'
import { RolldownOutput } from './utils'

export { rolldown, experimental_scan } from './rolldown'

Expand All @@ -15,5 +15,5 @@ export type {
InputOptions,
OutputOptions,
RolldownPlugin as Plugin,
RolldownOutput as RollupOutput,
RolldownOutput as RollupOutput
}
5 changes: 5 additions & 0 deletions packages/node/src/objects/output-bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RolldownOutputAsset, RolldownOutputChunk } from './rolldown-output';

export interface OutputBundle {
[fileName: string]: RolldownOutputAsset | RolldownOutputChunk;
}
1 change: 1 addition & 0 deletions packages/node/src/objects/rendered-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface RenderedModule { }
43 changes: 43 additions & 0 deletions packages/node/src/objects/rolldown-output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { OutputAsset, OutputChunk, RollupOutput } from '../rollup'
import { HasProperty, IsPropertiesEqual, IsPropertyEqual, TypeAssert } from '../utils/type-assert';
import { RenderedModule } from './rendered-module';

export interface RolldownOutputAsset {
type: 'asset';
fileName: string;
source: string | Uint8Array;
}

function _assertRolldownOutputAsset() {
type _ = TypeAssert<IsPropertiesEqual<RolldownOutputAsset, OutputAsset>>;
}

export interface RolldownOutputChunk {
type: 'chunk';
code: string;
isEntry: boolean;
exports: string[];
fileName: string;
modules: {
[id: string]: RenderedModule;
};
facadeModuleId: string | null;
isDynamicEntry: boolean;
moduleIds: string[];
}

function _assertRolldownOutputChunk() {
type _ = TypeAssert<IsPropertiesEqual<Omit<RolldownOutputChunk, 'modules'>, OutputChunk>>;
}

export interface RolldownOutput {
output: [
RolldownOutputChunk,
...(RolldownOutputChunk | RolldownOutputAsset)[],
]
}


function _assertRolldownOutput() {
type _ = TypeAssert<HasProperty<RolldownOutput, 'output'>>;
}
5 changes: 3 additions & 2 deletions packages/node/src/options/create-build-plugin-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function writeBundle(hook: Plugin['writeBundle']) {
}
return async (outputs: Outputs) => {
try {
// TODO outputOptions
// @ts-expect-error: FIXME: hyf0
await hook.call({} as any, {} as any, transformToOutputBundle(outputs))
} catch (error) {
console.error(error)
Expand All @@ -51,10 +51,10 @@ function generateBundle(hook: Plugin['generateBundle']) {
}
return async (outputs: Outputs, isWrite: boolean) => {
try {
// TODO outputOptions
await hook.call(
{} as any,
{} as any,
// @ts-expect-error: FIXME: hyf0
transformToOutputBundle(outputs),
isWrite,
)
Expand Down Expand Up @@ -127,6 +127,7 @@ function renderChunk(hook: Plugin['renderChunk']) {
const value = await hook.call(
{} as any,
code,
// @ts-expect-error: FIXME: hyf0
renderedChunk,
{} as any,
{} as any,
Expand Down
30 changes: 7 additions & 23 deletions packages/node/src/rolldown-build.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Bundler } from '@rolldown/node-binding'
import { normalizeOutputOptions, OutputOptions } from './options/output-options'
import type { RollupBuild, SerializedTimings } from './rollup-types'
import { transformToRollupOutput, RolldownOutput, unimplemented } from './utils'
import { transformToRollupOutput, unimplemented } from './utils'
import { RolldownOutput } from './objects/rolldown-output'
import { HasProperty, TypeAssert } from './utils/type-assert'

export class RolldownBuild implements Omit<RollupBuild, 'generate' | 'write'> {
export class RolldownBuild {
#bundler: Bundler
constructor(bundler: Bundler) {
this.#bundler = bundler
}

closed = false

async generate(outputOptions: OutputOptions = {}): Promise<RolldownOutput> {
const bindingOptions = normalizeOutputOptions(outputOptions)
const output = await this.#bundler.write(bindingOptions)
Expand All @@ -22,23 +21,8 @@ export class RolldownBuild implements Omit<RollupBuild, 'generate' | 'write'> {
const output = await this.#bundler.write(bindingOptions)
return transformToRollupOutput(output) as RolldownOutput
}
}

async close() {
this.closed = true
}

// -- unimplemented

get cache(): undefined {
throw unimplemented()
return unimplemented()
}
get watchFiles(): string[] {
throw unimplemented()
return unimplemented()
}
get getTimings(): () => SerializedTimings {
throw unimplemented()
return unimplemented()
}
function _assert() {
type _ = TypeAssert<HasProperty<RolldownBuild, 'generate' | 'write'>>
}
3 changes: 0 additions & 3 deletions packages/node/src/rollup-types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export type {
RollupBuild,
RollupOutput,
OutputOptions,
SerializedTimings,
InputOptions,
Plugin,
OutputPlugin,
Expand Down
9 changes: 8 additions & 1 deletion packages/node/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ export function unimplemented(info?: string): never {
throw new Error('unimplemented')
}

export function noop(..._args: any[]) {}
export function unreachable(info?: string): never {
if (info) {
throw new Error(`unreachable: ${info}`)
}
throw new Error('unreachable')
}

export function noop(..._args: any[]) { }
Loading

0 comments on commit 1cc0fbe

Please sign in to comment.