sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

[1.22 Question] Unable to run heft build --production when using ES2020 in tsconfig. Possible solution.

Open kbeverforden opened this issue 10 months ago • 3 comments

We switched to ES2020 from the default ES5 a long time ago and it worked fine until trying to use heft build --production in v1.22. Now we get hundreds of these errors without a file or line number. I determined it was from Terser when minifying the javascript. I wanted to bring this up in case others are having the same issue and to see if my solution is correct and discuss the side effects.

JS_Parse_Error [SyntaxError]: Unexpected token punc «{», expected punc «,»
    at js_error (C:\RespositoriesGit\CP\clientportalsharepoint\node_modules\terser\dist\bundle.min.js:536:11)
    at croak (C:\RespositoriesGit\CP\clientportalsharepoint\node_modules\terser\dist\bundle.min.js:1264:9)
    at token_error (C:\RespositoriesGit\CP\clientportalsharepoint\node_modules\terser\dist\bundle.min.js:1272:9)
    at expect_token (C:\RespositoriesGit\CP\clientportalsharepoint\node_modules\terser\dist\bundle.min.js:1285:9)
    at expect (C:\RespositoriesGit\CP\clientportalsharepoint\node_modules\terser\dist\bundle.min.js:1288:36)
    at call_args (C:\RespositoriesGit\CP\clientportalsharepoint\node_modules\terser\dist\bundle.min.js:3368:17)
    at subscripts (C:\RespositoriesGit\CP\clientportalsharepoint\node_modules\terser\dist\bundle.min.js:3278:30)
    at expr_atom (C:\RespositoriesGit\CP\clientportalsharepoint\node_modules\terser\dist\bundle.min.js:2529:20)
    at maybe_unary (C:\RespositoriesGit\CP\clientportalsharepoint\node_modules\terser\dist\bundle.min.js:3389:19)
    at expr_ops (C:\RespositoriesGit\CP\clientportalsharepoint\node_modules\terser\dist\bundle.min.js:3464:28) {
  filename: 'code',
  line: 2,
  col: 9,
  pos: 28
}

I fixed it by adding the following code to my webpack patch file. It clears the minimizer and adds it back using ES2020. Since this fixes the issue, my assumption is that the existing minimizer targets something below ES2020 and can't handle anything newer.

Questions:

  • Is this issue expected when not using the default ES5? Could it be caused by another setting in combination with ES2020?
  • Is this the best solution?
  • Will this break anything since it wipes out the existing minimizer? I published our project to a test site and didn't see any issues but I need to do a lot more testing.

Side effects:

  • It now takes 4+ minutes to build the project instead of 1 minute in previous SPFx versions.
  • VS Code/Node JS uses 15 GB of memory and 100% CPU on my computer when building. This is because parallel is set to true. It can be set to a number to use a specific number of CPUs but it will take longer than 4 minutes to build.

Initial solutions that didn't work:

  • Adding "comma-dangle": ["warn", "never"] to eslint to find all unnecessary trailing commas and removing them

Solution that worked:

const TerserPlugin = require('terser-webpack-plugin');

module.exports = function (generatedConfiguration)
{
    // Remove existing minimizers
    if (generatedConfiguration?.optimization?.minimizer)
        generatedConfiguration.optimization.minimizer = [];

    // Add new TerserPlugin with custom config
    if (!generatedConfiguration.optimization)
        generatedConfiguration.optimization = {};

    if (!generatedConfiguration.optimization.minimizer)
        generatedConfiguration.optimization.minimizer = [];

    generatedConfiguration.optimization.minimizer.push(
        new TerserPlugin({
            terserOptions: {

                ecma: 2020, // Options: 5, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022

                parse: {
                    bare_returns: false,
                    html5_comments: false
                },

                format: {
                    comments: false,
                    ecma: 2020,
                    ascii_only: true
                },

                compress: {
                    ecma: 2020
                },

                mangle: {
                    safari10: true,
                    properties: false,
                },

                keep_classnames: false,
            },

            parallel: true,
            extractComments: false

        })
    );

    return generatedConfiguration;
};

kbeverforden avatar Jan 07 '26 13:01 kbeverforden

@kbeverforden, Thank you for bringing this issue to our attention. We’ve reached out to the engineering team and shared the details. We’ll update you as soon as we get confirmation or next steps.

Ashlesha-MSFT avatar Jan 12 '26 05:01 Ashlesha-MSFT

Hey there. So, at the moment, we can't say that ES2020 is on a supported path just yet (although it will be in the future). However, things ought to still work. We actually are using the latest version of terser via heft's @rushstack/module-minifier, so it should be able to handle this syntax. It may be the case that there's some transpiration of a node_module needed.

I tried to reproduce the errors with the React webpart, but wasn't able to. Are you able to put together a minimal repro? I'm thinking one of your npm dependencies may be highlighting the issue.

As for the performance -- we can look into it -- most likely a terser or terser config issue.

nick-pape avatar Jan 15 '26 15:01 nick-pape

Since the error doesn't have a file or line number I can't tell what's causing it to create a repro. I can confirm that as soon as I take out the changes in my patch file that changes it to ES2020 that it won't build the project. I just tested it again now to confirm. Since I also can't reproduce the issue in a new blank project after changing it to ES2020 I will go ahead and close this ticket. I mainly just wanted to bring it to your attention in case other people start seeing the same thing as more people use 1.22.

kbeverforden avatar Jan 23 '26 14:01 kbeverforden