mirror of
https://github.com/openglobus/openglobus.git
synced 2025-12-08 19:25:27 +00:00
sourcemap for bundle
This commit is contained in:
parent
fdd705abfe
commit
015c4e3780
@ -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');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user