Skip to content

Conversation

@minggangw
Copy link
Member

@minggangw minggangw commented Mar 13, 2025

This patch implements to leverage terser to mangle/compress the generated .js messages when --debug option not added. If debugging the message files, users can run generate-messages:dev to check with the original files, including:

  1. Removing the prettier dependency.
  2. Integrating terser minification with error handling for JavaScript files.
  3. Using a conditional debug flag (isDebug) to determine whether to minify the code.
  4. Don't remove the blank line in the generated files because it will be handled by terser now.

Fix: #1085

@minggangw minggangw requested a review from Copilot March 13, 2025 06:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the JS generation process to leverage terser for compressing and mangling the generated JavaScript messages instead of using prettier.

  • Replaces the dependency on prettier with terser for production builds.
  • Introduces a conditional minification step based on the isDebug flag.

code = await prettier.format(code, { parser: 'babel' });
let result = null;
if (!isDebug && fileName.endsWith('.js')) {
result = await minify(code);
Copy link

Copilot AI Mar 13, 2025

Choose a reason for hiding this comment

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

Consider adding error handling or logging around the terser.minify call to ensure that any minification errors are caught and handled, rather than silently falling back to the unminified code.

Suggested change
result = await minify(code);
try {
result = await minify(code);
} catch (error) {
console.error(`Error minifying ${fileName}:`, error);
result = null;
}

Copilot uses AI. Check for mistakes.
@coveralls
Copy link

coveralls commented Mar 13, 2025

Coverage Status

coverage: 85.355% (+0.04%) from 85.32%
when pulling 2cf0713 on minggangw:fix-1085
into a725c65 on RobotWebTools:develop.

@minggangw minggangw requested a review from Copilot March 13, 2025 06:54
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the code generation process to leverage terser for minifying generated JS code rather than formatting it with prettier. Key changes include:

  • Removing the prettier dependency.
  • Integrating terser minification with error handling for JavaScript files.
  • Using a conditional debug flag (isDebug) to determine whether to minify the code.
Comments suppressed due to low confidence (2)

rosidl_gen/idl_generator.js:45

  • Ensure that 'isDebug' is defined in the current scope or imported appropriately to avoid potential runtime errors during minification.
if (!isDebug && fileName.endsWith('.js')) {

rosidl_gen/idl_generator.js:55

  • [nitpick] Consider assigning the minified code to the original 'code' variable before writing the file to improve clarity and maintainability.
await fse.writeFile(path.join(dir, fileName), result ? result.code : code);

@minggangw minggangw requested a review from Copilot March 13, 2025 07:32
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR leverages terser to mangle and compress the generated JavaScript message files in place of prettier-based formatting. Key changes include:

  • Replacing the prettier formatter with terser for minification.
  • Removing the removeEmptyLines helper function and its invocations.
  • Updating writeGeneratedCode to conditionally minify code when not in debug mode.

@minggangw minggangw merged commit e040133 into RobotWebTools:develop Mar 13, 2025
10 of 11 checks passed
minggangw added a commit that referenced this pull request Mar 13, 2025
This patch implements to leverage [terser](https://www.npmjs.com/package/terser) to mangle/compress the generated `.js`  messages when  `--debug` option not added. If debugging the message files, users can run `generate-messages:dev` to check with the original files, including:

1. Removing the prettier dependency.
2. Integrating terser minification with error handling for JavaScript files.
3. Using a conditional debug flag (isDebug) to determine whether to minify the code.
4. Don't remove the blank line in the generated files because it will be handled by `terser` now.

Fix: #1085
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Leverage terser to mangle/compress the generated js messages

2 participants