Skip to content

Commit

Permalink
feat: add footer intro outro
Browse files Browse the repository at this point in the history
  • Loading branch information
PengBoUESTC committed Mar 21, 2024
1 parent 71362a4 commit 20de8dc
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions crates/rolldown/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ impl Chunk {
if let Some(banner) = &output_options.banner {
concat_source.prepend_source(Box::new(RawSource::new(banner.to_string())))
}
// add footer
if let Some(footer) = &output_options.footer {
concat_source.add_source(Box::new(RawSource::new(footer.to_string())))
}
if let Some(exports) = self.render_exports(graph, output_options) {
concat_source.add_source(Box::new(RawSource::new(exports)));
}
Expand Down
3 changes: 3 additions & 0 deletions crates/rolldown/src/options/normalized_output_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ pub struct NormalizedOutputOptions {
pub format: OutputFormat,
pub sourcemap: SourceMapType,
pub banner: Option<String>,
pub footer: Option<String>,
pub intro: Option<String>,
pub outro: Option<String>,
}
3 changes: 3 additions & 0 deletions crates/rolldown/src/options/output_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub struct OutputOptions {
pub format: Option<OutputFormat>,
pub sourcemap: Option<SourceMapType>,
pub banner: Option<String>,
pub footer: Option<String>,
pub intro: Option<String>,
pub outro: Option<String>,
}

// impl Default for OutputOptions {
Expand Down
3 changes: 3 additions & 0 deletions crates/rolldown/src/utils/normalize_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ pub fn normalize_options(
.into(),
dir: "dist".to_string(),
banner: raw_output.banner,
footer: raw_output.footer,
intro: raw_output.intro,
outro: raw_output.outro,
format: raw_output.format.unwrap_or(crate::OutputFormat::Esm),
sourcemap: raw_output.sourcemap.unwrap_or(SourceMapType::Hidden),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct BindingOutputOptions {
// extend: boolean;
// externalLiveBindings: boolean;
// footer: () => string | Promise<string>;
pub footer: Option<String>,
#[napi(ts_type = "'es' | 'cjs'")]
pub format: Option<String>,
// freeze: boolean;
Expand All @@ -36,12 +37,14 @@ pub struct BindingOutputOptions {
// inlineDynamicImports: boolean;
// interop: GetInterop;
// intro: () => string | Promise<string>;
pub intro: Option<String>,
// manualChunks: ManualChunksOption;
// minifyInternalExports: boolean;
// name: string | undefined;
// namespaceToStringTag: boolean;
// noConflict: boolean;
// outro: () => string | Promise<string>;
pub outro: Option<String>,
// paths: OptionsPaths;
pub plugins: Vec<PluginOptions>,
// preferConst: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub fn normalize_binding_options(
dir: output_options.dir,
sourcemap: output_options.sourcemap.map(Into::into),
banner: output_options.banner,
footer: output_options.footer,
intro: output_options.intro,
outro: output_options.outro,
..Default::default()
};

Expand Down
3 changes: 3 additions & 0 deletions packages/rolldown/src/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export interface BindingOutputOptions {
banner?: string
dir?: string
exports?: 'default' | 'named' | 'none' | 'auto'
footer?: string
format?: 'es' | 'cjs'
intro?: string
outro?: string
plugins: Array<PluginOptions>
sourcemap?: 'file' | 'inline' | 'hidden'
}
Expand Down
8 changes: 7 additions & 1 deletion packages/rolldown/src/options/output-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export interface OutputOptions {
exports?: RollupOutputOptions['exports']
sourcemap?: RollupOutputOptions['sourcemap']
banner?: string
footer?: string
intro?: string
outro?: string
}

function normalizeFormat(
Expand Down Expand Up @@ -43,13 +46,16 @@ function normalizeSourcemap(
export function normalizeOutputOptions(
opts: OutputOptions,
): BindingOutputOptions {
const { dir, format, exports, sourcemap, banner } = opts
const { dir, format, exports, sourcemap, banner, footer, intro, outro } = opts
return {
dir: dir,
format: normalizeFormat(format),
exports,
sourcemap: normalizeSourcemap(sourcemap),
plugins: [],
banner,
footer,
intro,
outro,
}
}
2 changes: 1 addition & 1 deletion packages/rolldown/test/cases/output/banner/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const config: RollupOptions = {
external: [/external/, 'external-a'],
output: {
banner: '// banner test\n',
}
},
}

export default {
Expand Down
2 changes: 0 additions & 2 deletions packages/rolldown/test/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ function normalizedOptions(caseRoot: string, config?: RollupOptions) {
throw new Error(`The ${caseRoot} output shouldn't be array`)
}
const output = config?.output ?? {}
const dir = output.dir ?? path.join(caseRoot, 'dist')
console.log(dir)
return {
input: config?.input ?? path.join(caseRoot, 'main.js'),
output: {
Expand Down

0 comments on commit 20de8dc

Please sign in to comment.