From 9eb2d6611b99c7532e849f6326ecaef6c4702406 Mon Sep 17 00:00:00 2001
From: silverwind <me@silverwind.io>
Date: Mon, 5 Jun 2023 21:28:46 +0200
Subject: [PATCH 1/4] Enable all webpack sourcemaps in development

---
 webpack.config.js | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/webpack.config.js b/webpack.config.js
index ae71a0599bce6..e1904cf2da90e 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -162,10 +162,12 @@ export default {
     }),
     new SourceMapDevToolPlugin({
       filename: '[file].[contenthash:8].map',
-      include: [
-        'js/index.js',
-        'css/index.css',
-      ],
+      ...(isProduction && {
+        include: [
+          'js/index.js',
+          'css/index.css',
+        ],
+      }),
     }),
     new MonacoWebpackPlugin({
       filename: 'js/monaco-[name].[contenthash:8].worker.js',

From 53f7e6b0c6ad9fee548f15332b5b8a55f6fcaa1a Mon Sep 17 00:00:00 2001
From: silverwind <me@silverwind.io>
Date: Mon, 5 Jun 2023 22:05:13 +0200
Subject: [PATCH 2/4] use all or nothing approach

---
 .../doc/installation/from-source.en-us.md      |  2 ++
 webpack.config.js                              | 18 +++++++-----------
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/docs/content/doc/installation/from-source.en-us.md b/docs/content/doc/installation/from-source.en-us.md
index e0be7f2f14463..dfded3d9fd1b4 100644
--- a/docs/content/doc/installation/from-source.en-us.md
+++ b/docs/content/doc/installation/from-source.en-us.md
@@ -132,6 +132,8 @@ If pre-built frontend files are present it is possible to only build the backend
 TAGS="bindata" make backend
 ```
 
+Webpack source maps are by default enabled in development builds and disabled in production builds. They can be enabled by setting the `ENABLE_SOURCEMAP=true` environment variable.
+
 ## Test
 
 After following the steps above, a `gitea` binary will be available in the working directory.
diff --git a/webpack.config.js b/webpack.config.js
index e1904cf2da90e..886942da75e49 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -10,6 +10,7 @@ import {parse, dirname} from 'node:path';
 import webpack from 'webpack';
 import {fileURLToPath} from 'node:url';
 import {readFileSync} from 'node:fs';
+import {env} from 'node:process';
 
 const {EsbuildPlugin} = EsBuildLoader;
 const {SourceMapDevToolPlugin, DefinePlugin} = webpack;
@@ -25,7 +26,8 @@ for (const path of glob('web_src/css/themes/*.css')) {
   themes[parse(path).name] = [path];
 }
 
-const isProduction = process.env.NODE_ENV !== 'development';
+const isProduction = env.NODE_ENV !== 'development';
+const sourceMapEnabled = env.ENABLE_SOURCEMAP === 'true' ? true : !isProduction;
 
 const filterCssImport = (url, ...args) => {
   const cssFile = args[1] || args[0]; // resourcePath is 2nd argument for url and 3rd for import
@@ -122,7 +124,7 @@ export default {
           {
             loader: 'css-loader',
             options: {
-              sourceMap: true,
+              sourceMap: sourceMapEnabled,
               url: {filter: filterCssImport},
               import: {filter: filterCssImport},
             },
@@ -160,15 +162,9 @@ export default {
       filename: 'css/[name].css',
       chunkFilename: 'css/[name].[contenthash:8].css',
     }),
-    new SourceMapDevToolPlugin({
+    sourceMapEnabled && (new SourceMapDevToolPlugin({
       filename: '[file].[contenthash:8].map',
-      ...(isProduction && {
-        include: [
-          'js/index.js',
-          'css/index.css',
-        ],
-      }),
-    }),
+    })),
     new MonacoWebpackPlugin({
       filename: 'js/monaco-[name].[contenthash:8].worker.js',
     }),
@@ -197,7 +193,7 @@ export default {
       emitError: true,
       allow: '(Apache-2.0 OR BSD-2-Clause OR BSD-3-Clause OR MIT OR ISC OR CPAL-1.0 OR Unlicense OR EPL-1.0 OR EPL-2.0)',
     }) : new AddAssetPlugin('js/licenses.txt', `Licenses are disabled during development`),
-  ],
+  ].filter(Boolean),
   performance: {
     hints: false,
     maxEntrypointSize: Infinity,

From eafc46b08e4fda1b1b25af86241874c34ba4c83d Mon Sep 17 00:00:00 2001
From: silverwind <me@silverwind.io>
Date: Mon, 5 Jun 2023 22:10:20 +0200
Subject: [PATCH 3/4] simplify condition

---
 webpack.config.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/webpack.config.js b/webpack.config.js
index 886942da75e49..345744e0de074 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -27,7 +27,7 @@ for (const path of glob('web_src/css/themes/*.css')) {
 }
 
 const isProduction = env.NODE_ENV !== 'development';
-const sourceMapEnabled = env.ENABLE_SOURCEMAP === 'true' ? true : !isProduction;
+const sourceMapEnabled = env.ENABLE_SOURCEMAP === 'true' || !isProduction;
 
 const filterCssImport = (url, ...args) => {
   const cssFile = args[1] || args[0]; // resourcePath is 2nd argument for url and 3rd for import

From 59da3da50a5d8a42c6a0f6e2c4eaa70c3a647f89 Mon Sep 17 00:00:00 2001
From: silverwind <me@silverwind.io>
Date: Mon, 5 Jun 2023 22:13:58 +0200
Subject: [PATCH 4/4] better condition

---
 webpack.config.js | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/webpack.config.js b/webpack.config.js
index 345744e0de074..a810de536c84e 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -27,7 +27,13 @@ for (const path of glob('web_src/css/themes/*.css')) {
 }
 
 const isProduction = env.NODE_ENV !== 'development';
-const sourceMapEnabled = env.ENABLE_SOURCEMAP === 'true' || !isProduction;
+
+let sourceMapEnabled;
+if ('ENABLE_SOURCEMAP' in env) {
+  sourceMapEnabled = env.ENABLE_SOURCEMAP === 'true';
+} else {
+  sourceMapEnabled = !isProduction;
+}
 
 const filterCssImport = (url, ...args) => {
   const cssFile = args[1] || args[0]; // resourcePath is 2nd argument for url and 3rd for import