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

swc.minify with sourcemap issue #10108

Open
Skrypt opened this issue Feb 26, 2025 · 9 comments
Open

swc.minify with sourcemap issue #10108

Skrypt opened this issue Feb 26, 2025 · 9 comments

Comments

@Skrypt
Copy link

Skrypt commented Feb 26, 2025

Describe the bug

I am using swc.minify() API method to minify files.
Though, the sourcemap generated is different when used under Windows or under Linux.
Also, the .map files generates with \r\n newlines on Windows and \n newlines on Linux.
I needed to add code to rewrite the files with \n which is an ugly patch for now.
But the main issue is that the .map files are generated with different output when minified under Windows vs Linux.

Input code

You can find the code used here: https://github.com/OrchardCMS/OrchardCore/blob/main/.scripts/assets-manager/min.mjs

Config

    {
        compress: true,
        sourceMap: true,
    }

Playground link (or link to the minimal reproduction)

OrchardCMS/OrchardCore#17533

SWC Info output

No response

Expected behavior

Should output the same from Windows or Linux

Actual behavior

No response

Version

1.11.1

Additional context

No response

@GiveMe-A-Name
Copy link
Contributor

Maybe you can replace all \r\n to \n when minify files to make sure source map consistency.

@Skrypt
Copy link
Author

Skrypt commented Feb 27, 2025

Yes, that's what I did and it works. Though, as you can see on the CI there is one file that compiles with a different sourcemap "hash" when compiled with Windows or Linux. This one I can't fix.

I'm not sure if the issue comes from swc or from fs to be honest for the \r\n issue. Though, I'd expect that swc outputs the same sourcemap with both. I will debug this and report back what I found.

Thanks for the answer.

@Skrypt
Copy link
Author

Skrypt commented Feb 27, 2025

So when I say a different hash it is this part of the sourcemap which is different:

"mappings":"IAsDIA,cArDJC,OAAOC,EAAE" ....

And after debugging the output.map, the issue for the \r\n issue is not fs:

Image

So something in swc when generating these sourcemaps is using something like "newline" while it should explicitly be \n.

@kdy1
Copy link
Member

kdy1 commented Feb 27, 2025

If the original file contains \r\n, it's expected for the .map files to have them.
You need to adjust the configuration for git clone instead.

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2025
@kdy1
Copy link
Member

kdy1 commented Feb 27, 2025

While you are cloning files, git will adjust line endings depending on the configuration.

@Skrypt
Copy link
Author

Skrypt commented Feb 27, 2025

We did that and it did not work. Because these are "litteral" \r\n that are written as text in a .map file as opposite than the actual encoded \r\n chars.

@kdy1 kdy1 reopened this Feb 27, 2025
@kdy1
Copy link
Member

kdy1 commented Feb 27, 2025

Can you reproduce it in playground or provide a minimal reproduction?

@Skrypt
Copy link
Author

Skrypt commented Feb 27, 2025

Well playground would compile only on Linux I'd guess. Best repro is pulling that branch and running yarn build -n shortcodes both on Windows and Linux to see the difference.

@Skrypt
Copy link
Author

Skrypt commented Feb 27, 2025

Solution was to use:

    let reader = await fs.readFile(file, "utf8");

    await swc.minify(reader.replace(/(\r?\n|\r)/gm, "\n"), {  // Here is the main fix
        compress: true,
        sourceMap: mode === "production",
     }).then((output) => {
         const minifiedTarget = path.join(dest, path.parse(target).name + ".min.js");

         fs.outputFile(minifiedTarget, output.code);
         console.log(`Minified (${chalk.gray("from")}, ${chalk.cyan("to")})`, chalk.gray(file), chalk.cyan(minifiedTarget));

         if (mode === "production" && output.map) {
             const mappedTarget = path.join(dest, path.parse(target).name + ".map");
             const normalized = output.map.replace(/(?:\\[rn])+/g, "\\n");  // Still needed to keep this one
             fs.outputFile(mappedTarget, normalized + "\n");
             console.log(`Mapped (${chalk.gray("from")}, ${chalk.cyan("to")})`, chalk.gray(file), chalk.cyan(mappedTarget));
         }
    });

So, the fs.readFile on Windows will return a reader that has \r\n chars in it. I needed to convert these to \n so that the .map file generated be consistent between different OS.

Maybe an option on SWC compiler to convert these string to normalized X newline string of your choice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants