-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Open
Description
Describe the bug
When enabling the merge_imports option in SWC's compression transform, imports are being reordered incorrectly and placed after their usage in the code. This breaks the expected execution order and make output not aesthetically pleasing.
Input code
import { v1 } from 'a';
import { v2 } from 'b';
import { v3 } from 'b';
import { v4 } from 'c';
console.log(v1, v2, v3, v4);Link to the code that reproduces this issue
SWC Info output
No response
Expected behavior
import { v1 } from 'a';
import { v2, v3 } from 'b';
import { v4 } from 'c';
console.log(v1, v2, v3, v4);Actual behavior
import { v1 } from 'a';
import { v4 } from 'c';
console.log(v1, v2, v3, v4);
import { v2, v3 } from "b";Version
latest
Additional context
Problem Analysis
- Semantic Breaking: The transformation changes the semantics of the program by altering the execution order. The import from module
'b'is placed after imports from'a'and'c', which changes the original import order. - Imports After Usage: The merged import
import { v2, v3 } from "b"is placed after theconsole.logstatement where these variables are used. This is not aesthetically pleasing.