sourcemap for bundle

This commit is contained in:
pavletto 2025-05-02 10:48:26 +04:00
parent fdd705abfe
commit 015c4e3780
2 changed files with 32 additions and 14 deletions

View File

@ -1,26 +1,43 @@
import { readFile, writeFile } from 'fs/promises';
import path from 'node:path';
import { minify } from 'terser';
export default function forceTerserPlugin({ filePath }) {
const mapPath = `${filePath}.map`;
const fileName = path.basename(filePath);
const mapFileName = path.basename(mapPath);
return {
name: 'vite-plugin-force-terser',
apply: 'build',
closeBundle: async () => {
const code = await readFile(filePath, 'utf-8');
async closeBundle() {
const [code, prevMap] = await Promise.all([
readFile(filePath, 'utf-8'),
readFile(mapPath, 'utf-8')
]);
const result = await minify(code, {
compress: true,
mangle: true,
format: {
comments: false
const result = await minify(
{ [fileName]: code },
{
compress: true,
mangle: true,
format: { comments: false },
sourceMap: {
content: prevMap,
filename: fileName,
url: mapFileName
}
}
});
);
if (result.code) {
await writeFile(filePath, result.code, 'utf-8');
console.log(`${filePath} minified with terser`);
if (result.code && result.map) {
await Promise.all([
writeFile(filePath, result.code, 'utf-8'),
writeFile(mapPath, result.map, 'utf-8'),
]);
console.log(`${fileName} and ${mapFileName} regenerated with Terser`);
} else {
console.warn('⚠ terser did not return any code');
console.warn('⚠ terser did not return code or map');
}
}
};

View File

@ -26,11 +26,12 @@ export default function ({mode}: { mode: 'development' | 'production' }) {
},
emptyOutDir: true,
outDir: path.resolve(__dirname, './lib'),
sourcemap: isDev,
sourcemap: true,
rollupOptions: {
output: {
entryFileNames: `og.[format].js`,
assetFileNames: `[name][extname]`
assetFileNames: `[name][extname]`,
sourcemapExcludeSources: true,
},
plugins: [
// doesn't work for esm modules