1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-08-10 22:11:50 +02:00

Chore: Desktop: Decrease source map size (#12679)

This commit is contained in:
Henry Heino
2025-07-07 08:06:33 -07:00
committed by GitHub
parent b4a57a10aa
commit 36910a2a9b

View File

@@ -1,9 +1,12 @@
import { filename, toForwardSlashes } from '@joplin/utils/path';
import * as esbuild from 'esbuild';
import { existsSync } from 'fs';
import { existsSync, readFileSync } from 'fs';
import { writeFile } from 'fs/promises';
import { dirname, join, relative } from 'path';
const baseDir = dirname(__dirname);
const baseNodeModules = join(baseDir, 'node_modules');
// Note: Roughly based on js-draw's use of esbuild:
// https://github.com/personalizedrefrigerator/js-draw/blob/6fe6d6821402a08a8d17f15a8f48d95e5d7b084f/packages/build-tool/src/BundledFile.ts#L64
const makeBuildContext = (entryPoint: string, renderer: boolean, computeFileSizeStats: boolean) => {
@@ -28,8 +31,6 @@ const makeBuildContext = (entryPoint: string, renderer: boolean, computeFileSize
name: 'joplin--relative-imports-for-externals',
setup: build => {
const externalRegex = /^(.*\.node|sqlite3|electron|@electron\/remote\/.*|electron\/.*|@mapbox\/node-pre-gyp|jsdom)$/;
const baseDir = dirname(__dirname);
const baseNodeModules = join(baseDir, 'node_modules');
build.onResolve({ filter: externalRegex }, args => {
// Electron packages don't need relative requires
if (args.path === 'electron' || args.path.startsWith('electron/')) {
@@ -66,8 +67,6 @@ const makeBuildContext = (entryPoint: string, renderer: boolean, computeFileSize
// Rewrite imports to prefer .js files to .ts. Otherwise, certain files are duplicated in the final bundle
name: 'joplin--prefer-js-imports',
setup: build => {
const baseDir = dirname(__dirname);
const baseNodeModules = join(baseDir, 'node_modules');
// Rewrite all relative imports
build.onResolve({ filter: /^\./ }, args => {
try {
@@ -90,6 +89,31 @@ const makeBuildContext = (entryPoint: string, renderer: boolean, computeFileSize
});
},
},
{
name: 'joplin--smaller-source-map-size',
setup: build => {
// Exclude dependencies from node_modules. This significantly reduces the size of the
// source map, improving startup performance.
//
// See https://github.com/evanw/esbuild/issues/1685#issuecomment-944916409
// and https://github.com/evanw/esbuild/issues/4130
const emptyMapData = Buffer.from(
JSON.stringify({ version: 3, sources: [null], mappings: 'AAAA' }),
'utf-8',
).toString('base64');
const emptyMapUrl = `data:application/json;base64,${emptyMapData}`;
build.onLoad({ filter: /node_modules.*js$/ }, args => {
return {
contents: [
readFileSync(args.path, 'utf8'),
`//# sourceMappingURL=${emptyMapUrl}`,
].join('\n'),
loader: 'default',
};
});
},
},
],
});
};