Skip to content

Commit

Permalink
feat: add minify configuration to BundleOptions (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmmease authored Oct 23, 2023
1 parent 18d2422 commit 8599742
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions js/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export interface BundleOptions {
* provide a way to use "in-memory" resources instead of fetching them
* remotely. */
load?: FetchCacher["load"];
/** Minify compiled code, default false. */
minify?: boolean;
/** Should the emitted bundle be an ES module or an IIFE script. The default
* is `"module"` to output a ESM module. */
type?: "module" | "classic";
Expand Down Expand Up @@ -166,6 +168,7 @@ export async function bundle(
compilerOptions,
importMap,
load,
minify,
type,
} = options;

Expand All @@ -183,6 +186,7 @@ export async function bundle(
type,
processImportMapInput(importMap),
compilerOptions,
minify ?? false,
);
return {
code: result.code,
Expand Down
24 changes: 23 additions & 1 deletion rs-lib/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct BundleOptions {
pub bundle_type: BundleType,
pub emit_options: EmitOptions,
pub emit_ignore_directives: bool,
pub minify: bool,
}

pub struct TranspileOptions {
Expand Down Expand Up @@ -203,7 +204,7 @@ pub fn bundle_graph(
let mut srcmap = Vec::new();
{
let cfg = swc::codegen::Config {
minify: false,
minify: options.minify,
ascii_only: false,
target: deno_ast::ES_VERSION,
omit_last_semi: false,
Expand Down Expand Up @@ -401,6 +402,7 @@ export const b = "b";
bundle_type: crate::BundleType::Module,
emit_ignore_directives: false,
emit_options: Default::default(),
minify: false,
},
)
.unwrap();
Expand All @@ -412,6 +414,25 @@ export { b as b };
"#,
output.code.split_once("//# sourceMappingURL").unwrap().0
);

let minified_output = bundle_graph(
&graph,
BundleOptions {
bundle_type: crate::BundleType::Module,
emit_ignore_directives: false,
emit_options: Default::default(),
minify: true,
},
)
.unwrap();
assert_eq!(
r#"import"https://example.com/external.ts";const b="b";export{b as b};"#,
minified_output
.code
.split_once("//# sourceMappingURL")
.unwrap()
.0
);
}

#[tokio::test]
Expand All @@ -434,6 +455,7 @@ export { b as b };
bundle_type: crate::BundleType::Module,
emit_ignore_directives: false,
emit_options: Default::default(),
minify: false,
},
)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/bundle/minified_json_import.js

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

12 changes: 12 additions & 0 deletions tests/bundle_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ Deno.test({
),
});

Deno.test({
name: "minified json import",
fn: testBundle(
resolveFixture("json_import.ts"),
{ minify: true },
async ({ outputFileUrl }) => {
const output = await runModule(outputFileUrl);
assertStringIncludes(output, "with space");
},
),
});

Deno.test({
name: "circular",
fn: testBundle(
Expand Down
2 changes: 2 additions & 0 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ pub async fn bundle(
maybe_bundle_type: Option<String>,
maybe_import_map: JsValue,
maybe_compiler_options: JsValue,
minify: bool,
) -> Result<JsValue, JsValue> {
// todo(dsherret): eliminate all the duplicate `.map_err`s
let compiler_options: CompilerOptions = serde_wasm_bindgen::from_value::<
Expand Down Expand Up @@ -206,6 +207,7 @@ pub async fn bundle(
bundle_type,
emit_options,
emit_ignore_directives: false,
minify,
},
)
.await
Expand Down

0 comments on commit 8599742

Please sign in to comment.