Skip to content

Commit

Permalink
frontend: Add sourcemaps for build
Browse files Browse the repository at this point in the history
  • Loading branch information
malinskibeniamin committed Nov 27, 2023
1 parent 51dddae commit 3f48c9d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import checker from 'vite-plugin-checker';
import svgrPlugin from 'vite-plugin-svgr';
import { createHtmlPlugin } from 'vite-plugin-html';
import tsconfigPaths from 'vite-tsconfig-paths';
import muteWarningsPlugin, { warningsToIgnore } from './vite/muteWarningsPlugin';
const ENV_PREFIX = 'REACT_APP_';

// https://vitejs.dev/config/
Expand Down Expand Up @@ -66,6 +67,7 @@ export default defineConfig(({ mode }) => {
tsconfigPaths({
ignoreConfigErrors: true,
}),
muteWarningsPlugin(warningsToIgnore),
],
assetsInclude: ['**/*.md'],
server: {
Expand All @@ -77,6 +79,9 @@ export default defineConfig(({ mode }) => {
},
build: {
outDir: 'build',
sourcemap: true,
// TODO: we need to look at how Vite/Rollup sets source maps vs CRA.
// Can we differentiate between production vs dev build?
},
};
});
54 changes: 54 additions & 0 deletions frontend/vite/muteWarningsPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Plugin } from 'vite';

// Fix for "Can't resolve original location of error" error when using source maps to build
export const warningsToIgnore = [
['SOURCEMAP_ERROR', 'Can\'t resolve original location of error'],
]
/**
* @see https://github.com/vitejs/vite/issues/15012#issuecomment-1825035992
* @param warningsToIgnore
* @returns a Vite plugin that mutes warnings
*/
const muteWarningsPlugin = (warningsToIgnore: string[][]): Plugin => {
const mutedMessages = new Set()
return {
name: 'mute-warnings',
enforce: 'pre',
config: (userConfig) => ({
build: {
rollupOptions: {
onwarn(warning, defaultHandler) {
if (warning.code) {
const muted = warningsToIgnore.find(
([code, message]) =>
code == warning.code && warning.message.includes(message),
)

if (muted) {
mutedMessages.add(muted.join())
return
}
}

if (userConfig.build?.rollupOptions?.onwarn) {
userConfig.build.rollupOptions.onwarn(warning, defaultHandler)
} else {
defaultHandler(warning)
}
},
},
},
}),
closeBundle() {
const diff = warningsToIgnore.filter((x) => !mutedMessages.has(x.join()))
if (diff.length > 0) {
this.warn(
'Some of your muted warnings never appeared during the build process:',
)
diff.forEach((m) => this.warn(`- ${m.join(': ')}`))
}
},
}
}

export default muteWarningsPlugin;

0 comments on commit 3f48c9d

Please sign in to comment.