Introduction
Hello everyone, this is part 3 of my series. Below are the earlier series:
Most of you may have faced issues in production after deployment, but since the files are minified, it is very difficult to debug or find the error. In this series, we will see how to generate a source map and use it for debugging. I had the issue and I tried to find out the solution. There is a very good article on sourcemap and debugging by Waldek Mastykarz, which is as follows:
This article is good and will give you a lot of information, but the problem is that it applies to an older webpack version. Webpack 4+ uses Terser plugin to minify. In this article, we will learn how to generate a sourcemap for a newer webpack version. For an older one, you can refer to the Waldek article.
SPFx Source Map Generation (applies to Webpack 4+)
To enable sourcemap generation for --ship/production version, we need to add the below code in gulpfile.js before “build.initialize(gulp);”
- //When you pass --ship in gulp it sets process.env.NODE_ENV to production so we are checking that.
- build.configureWebpack.mergeConfig({
- additionalConfiguration: (generatedConfiguration) => {
- var envmode = "";
- for (var i = 0; i < generatedConfiguration.plugins.length; i++) {
- var plugin = generatedConfiguration.plugins[i];
- if (plugin instanceof webpack.DefinePlugin) {
- envmode = plugin.definitions["process.env.NODE_ENV"];
- break;
- }
- }
- if (envmode && envmode == '"production"') {
- //setting the webpack configuration to generate sourcemap
- generatedConfiguration.devtool = 'source-map';
- //setting the terser plugin configuration to enable sourcemap.
- const TerserPlugin = require('terser-webpack-plugin');
- //removing the existing terser plugin object
- generatedConfiguration.optimization.minimizer.pop();
- //adding the new object with source map true
- generatedConfiguration.optimization.minimizer.push(new TerserPlugin({
- sourceMap: true, // Must be set to true if using source-maps in production
- }));
- }
- return generatedConfiguration;
- }
- });





Join the conversation! Your thoughts help the community grow.