Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object contents are incorrectly removed on minify #7635

Closed
thebanjomatic opened this issue Jul 6, 2023 · 1 comment · Fixed by #7638
Closed

Object contents are incorrectly removed on minify #7635

thebanjomatic opened this issue Jul 6, 2023 · 1 comment · Fixed by #7638
Assignees
Labels
Milestone

Comments

@thebanjomatic
Copy link

thebanjomatic commented Jul 6, 2023

Describe the bug

When running minify / minifySync contents of objects declared at the global scope are being removed unless you reference that variable directly elsewhere in the source. If you only use part of the object (reference only some properties), it inlines those properties and drops the entire top-level variable declaration which seems broken.

Input code

const {minifySync} = require('@swc/core');

const options = {compress: true, mangle: true, module: false};
const source = `const index = {foo: 5, bar: 10};`;
const useIndex = `console.log(index);`
const usePartOfIndex = `console.log(index.foo);`

console.log('test1:', minifySync(source, options).code);
console.log('test2:', minifySync(`${source}\n${useIndex}`, options).code);
console.log('test3:', minifySync(`${source}\n${usePartOfIndex}`, options).code);

Config

{
  "compress": true,
  "mangle": true,
}

Playground link

No response

Expected behavior

The output Terser produces in this case matches my expectations. Since we have module: false, top-level variables should not be mangled, or removed since they are in the global scope. As a result, each of the 3 test cases has the full object definition for index, and references that variable for the console.log statements:

test1: const index={foo:5,bar:10};
test2: const index={foo:5,bar:10};console.log(index);
test3: const index={foo:5,bar:10};console.log(index.foo);

Actual behavior

The code currently produced by SWC varies based on how/if the index variable is used ranging from the variable not being declared at all, being fully declared, or being an empty object.

test1: const index={};
test2: const index={foo:5,bar:10};console.log(index);
test3: console.log(5);

Version

1.3.68

Additional context

The behavior when setting module: true is a lot more consistent because in that context the top-level variables are only in module scope and aren't externally visible outside of the module.

What we get in that case is the following result which matches expectations for when setting module: true:

test1: 
test2: console.log({foo:5,bar:10});
test3: console.log(5);

or if you export const index:

test1: export const index={foo:5,bar:10};
test2: export const index={foo:5,bar:10};console.log(index);
test3: export const index={foo:5,bar:10};console.log(index.foo);
@thebanjomatic thebanjomatic changed the title Object contents are removed on minify Object contents are incorrectly removed on minify Jul 6, 2023
@kdy1 kdy1 self-assigned this Jul 7, 2023
@kdy1 kdy1 added this to the Planned milestone Jul 7, 2023
@kdy1 kdy1 closed this as completed in #7638 Jul 7, 2023
kdy1 added a commit that referenced this issue Jul 7, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.69 Jul 13, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Aug 12, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Aug 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants