Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ impl Gen for Statement<'_> {
impl Gen for ExpressionStatement<'_> {
fn r#gen(&self, p: &mut Codegen, _ctx: Context) {
p.print_comments_at(self.span.start);
p.add_source_mapping(self.span);
p.print_indent();
p.start_of_stmt = p.code_len();
p.print_expression(&self.expression);
Expand Down Expand Up @@ -1388,7 +1387,7 @@ impl GenExpr for CallExpression<'_> {
let is_export_default = p.start_of_default_export == p.code_len();
let mut wrap = precedence >= Precedence::New || ctx.intersects(Context::FORBID_CALL);
let pure = self.pure && p.options.print_annotation_comment();
if precedence >= Precedence::Postfix && pure {
if !wrap && pure && precedence >= Precedence::Postfix {
wrap = true;
}

Expand All @@ -1401,7 +1400,6 @@ impl GenExpr for CallExpression<'_> {
} else if is_statement {
p.start_of_stmt = p.code_len();
}
p.add_source_mapping(self.span);
self.callee.print_expr(p, Precedence::Postfix, Context::empty());
if self.optional {
p.print_str("?.");
Expand Down Expand Up @@ -1727,10 +1725,12 @@ impl GenExpr for UnaryExpression<'_> {
let operator = self.operator.as_str();
if self.operator.is_keyword() {
p.print_space_before_identifier();
p.add_source_mapping(self.span);
p.print_str(operator);
p.print_soft_space();
} else {
p.print_space_before_operator(self.operator.into());
p.add_source_mapping(self.span);
p.print_str(operator);
p.prev_op = Some(self.operator.into());
p.prev_op_end = p.code().len();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/sourcemap_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<'a> SourcemapBuilder<'a> {
}

pub fn add_source_mapping(&mut self, output: &[u8], position: u32, name: Option<&str>) {
if matches!(self.last_position, Some(last_position) if last_position == position) {
if self.last_position == Some(position) {
return;
}
let (original_line, original_column) = self.search_original_line_and_column(position);
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ rustc-hash = { workspace = true }
[dev-dependencies]
oxc_parser = { workspace = true }

base64 = { workspace = true }
insta = { workspace = true }
pico-args = { workspace = true }
32 changes: 25 additions & 7 deletions crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![expect(clippy::print_stdout)]
use std::path::Path;
use std::path::{Path, PathBuf};

use base64::{Engine, prelude::BASE64_STANDARD};
use pico_args::Arguments;

use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions, CommentOptions};
use oxc_codegen::{Codegen, CodegenOptions, CodegenReturn, CommentOptions};
use oxc_mangler::MangleOptions;
use oxc_minifier::{CompressOptions, Minifier, MinifierOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
use pico_args::Arguments;

// Instruction:
// create a `test.js`,
Expand All @@ -19,19 +21,34 @@ fn main() -> std::io::Result<()> {
let mangle = args.contains("--mangle");
let nospace = args.contains("--nospace");
let twice = args.contains("--twice");
let sourcemap = args.contains("--sourcemap");
let name = args.free_from_str().unwrap_or_else(|_| "test.js".to_string());

let path = Path::new(&name);
let source_text = std::fs::read_to_string(path)?;
let source_type = SourceType::from_path(path).unwrap();
let source_map_path = sourcemap.then(|| path.to_path_buf());

let mut allocator = Allocator::default();
let printed = minify(&allocator, &source_text, source_type, mangle, nospace);
let ret = minify(&allocator, &source_text, source_type, source_map_path, mangle, nospace);
let printed = ret.code;
println!("{printed}");

if let Some(source_map) = ret.map {
let result = source_map.to_json_string();
let hash = BASE64_STANDARD.encode(format!(
"{}\0{}{}\0{}",
printed.len(),
printed,
result.len(),
result
));
println!("https://evanw.github.io/source-map-visualization/#{hash}");
}

if twice {
allocator.reset();
let printed2 = minify(&allocator, &printed, source_type, mangle, nospace);
let printed2 = minify(&allocator, &printed, source_type, None, mangle, nospace).code;
println!("{printed2}");
println!("same = {}", printed == printed2);
}
Expand All @@ -43,9 +60,10 @@ fn minify(
allocator: &Allocator,
source_text: &str,
source_type: SourceType,
source_map_path: Option<PathBuf>,
mangle: bool,
nospace: bool,
) -> String {
) -> CodegenReturn {
let ret = Parser::new(allocator, source_text, source_type).parse();
let mut program = ret.program;
let options = MinifierOptions {
Expand All @@ -55,11 +73,11 @@ fn minify(
let ret = Minifier::new(options).build(allocator, &mut program);
Codegen::new()
.with_options(CodegenOptions {
source_map_path,
minify: nospace,
comments: CommentOptions::disabled(),
..CodegenOptions::default()
})
.with_scoping(ret.scoping)
.build(&program)
.code
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ expression: snapshot
---
- test.js.map
(0:0) "1;\n" --> (0:0) "1;\n"
(1:0) "__OBJECT__;\n" --> (1:0) "({ 'hello': 'test' });\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right. I assume there should be a mapping for this? Or is the fault in ReplaceGlobalDefines?

(2:0) "2;\n" --> (2:0) "2;\n"
(3:0) "__STRING__;\n" --> (3:0) "'development';\n"
(4:0) "3;\n" --> (4:0) "3;\n"
(5:0) "log(__OBJECT__)" --> (5:0) "log({ 'hello': 'test' })"
(5:15) ";\n" --> (5:24) ";\n"
(6:0) "4;\n" --> (6:0) "4;\n"
(7:0) "log(__STRING__)" --> (7:0) "log('development')"
(7:15) ";\n" --> (7:18) ";\n"
(8:0) "5;\n" --> (8:0) "5;\n"
(9:0) "__OBJECT__." --> (9:0) "({ 'hello': 'test' })."
(9:11) "hello;\n" --> (9:22) "hello;\n"
(10:0) "6;\n" --> (10:0) "6;\n"
(11:0) "log(__MEMBER__)" --> (11:0) "log(xx.yy.zz)"
Expand Down
2 changes: 1 addition & 1 deletion napi/minify/test/minify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('simple', () => {
expect(ret).toStrictEqual({
'code': 'function foo(){var e;e(void 0)}foo();',
'map': {
'mappings': 'AACA,SAAS,KAAM,CAAE,IAAIA,EAAK,SAAc,AAAE,CAAC,KAAK',
'mappings': 'AACA,SAAS,KAAM,CAAE,IAAIA,EAAKA,EAAI,OAAU,AAAE,CAAC,KAAK',
'names': [
'bar',
],
Expand Down
2 changes: 1 addition & 1 deletion napi/transform/test/moduleRunnerTransform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('moduleRunnerTransform', () => {

expect(map).toMatchInlineSnapshot(`
{
"mappings": "AAAA;;;;;;;AAAO,MAAM,IAAI",
"mappings": ";;;;;;;AAAO,MAAM,IAAI",
"names": [],
"sources": [
"index.js",
Expand Down
Loading