VS2019 bundle and minification causing errors with ES6 code and won't minify

Viewed 120

I posted a question a few days ago about an issue that I have with minifying code that has ES6 code in it, and one commenter said that the problem is with the minification. The code that is an issue is fat arrow functions and async/await.

I don't really want to have to convert the async/await code back to promises and having to long hand the fat arrow functions

Is there any way to get the VS2019 bundling and Minification to get along with ES6 code and if not, what can I use for bundling and minification? I tried one from the VisualStudio Marketplace, but that threw errors with the keyword const and won't be supported in VS2022.

1 Answers

The simplest way in a non-node.js environment seems to be using the TypeScript NuGet package, to convert ES6 to ES5, then bundle/minify that:

  • install NuGet package Microsoft.TypeScript.MSBuild
  • add a tsconfig.json file in your project root: { "compilerOptions": { "noImplicitAny": false, "noEmitOnError": true, "removeComments": false, "sourceMap": true, "target": "es5", "outDir": "bin/js" }, "include": [ "Content/ts/**/*" ], "compileOnSave": true }
  • add/move your ES6 JS code to .ts files
  • the .ts file(s) from include folder will be transpiled to ES5 .js file(s) in outDir
  • add the files from outDir to your bundle(s) as any other ES5 JS file

More details here: https://docs.microsoft.com/en-us/visualstudio/javascript/compile-typescript-code-nuget?source=recommendations&view=vs-2019

Related