Browse Source

Reduces bundle size

- Code splits iconv-lite until needed
 - Stubs whatwg/url since we don't need it
 - Stubs debug package as we don't need it
main
Eric Amodio 2 years ago
parent
commit
fd77953e8b
8 changed files with 48 additions and 37 deletions
  1. +10
    -8
      esbuild.mjs
  2. +1
    -0
      package.json
  3. +3
    -0
      patches/debug.js
  4. +1
    -0
      patches/whatwg-url.js
  5. +9
    -5
      src/env/node/git/localGitProvider.ts
  6. +18
    -18
      src/env/node/git/shell.ts
  7. +2
    -1
      tsconfig.json
  8. +4
    -5
      webpack.config.js

+ 10
- 8
esbuild.mjs View File

@ -55,13 +55,12 @@ async function buildExtension(target, mode) {
const alias = {
'@env': path.resolve(__dirname, 'src', 'env', target === 'webworker' ? 'browser' : target),
// Stupid dependency that is used by `http[s]-proxy-agent`
debug: path.resolve(__dirname, 'patches', 'debug.js'),
// This dependency is very large, and isn't needed for our use-case
tr46: path.resolve(__dirname, 'patches', 'tr46.js'),
// Stupid dependency that is used by `http-proxy-agent`
debug:
target === 'webworker'
? path.resolve(__dirname, 'node_modules', 'debug', 'src', 'browser.js')
: path.resolve(__dirname, 'node_modules', 'debug', 'src', 'node.js'),
// This dependency is unnecessary for our use-case
'whatwg-url': path.resolve(__dirname, 'patches', 'whatwg-url.js'),
};
if (target === 'webworker') {
@ -84,11 +83,11 @@ async function buildExtension(target, mode) {
logLevel: 'info',
mainFields: target === 'webworker' ? ['browser', 'module', 'main'] : ['module', 'main'],
metafile: true,
minify: mode === 'production' ? true : false,
minify: mode === 'production',
outdir: out,
platform: target === 'webworker' ? 'browser' : target,
sourcemap: mode === 'production' ? false : true,
// splitting: target === 'webworker' ? false : true,
sourcemap: mode !== 'production',
// splitting: target !== 'webworker',
// chunkNames: 'feature-[name]-[hash]',
target: ['es2022', 'chrome102', 'node16.14.2'],
treeShaking: true,
@ -97,6 +96,9 @@ async function buildExtension(target, mode) {
plugins: plugins,
});
if (!fs.existsSync(path.join('dist', 'meta'))) {
fs.mkdirSync(path.join('dist', 'meta'));
}
fs.writeFileSync(
path.join('dist', 'meta', `gitlens${target === 'webworker' ? '.browser' : ''}.json`),
JSON.stringify(result.metafile),

+ 1
- 0
package.json View File

@ -13416,6 +13416,7 @@
"webpack-require-from": "1.8.6"
},
"resolutions": {
"iconv-lite": "0.6.3",
"node-fetch": "2.6.9",
"semver-regex": "4.0.5"
}

+ 3
- 0
patches/debug.js View File

@ -0,0 +1,3 @@
export default function () {
return function () {};
}

+ 1
- 0
patches/whatwg-url.js View File

@ -0,0 +1 @@
exports.URL = require('url').URL;

+ 9
- 5
src/env/node/git/localGitProvider.ts View File

@ -2,7 +2,6 @@ import { readdir, realpath } from 'fs';
import { homedir, hostname, userInfo } from 'os';
import { resolve as resolvePath } from 'path';
import { env as process_env } from 'process';
import { encodingExists } from 'iconv-lite';
import type { CancellationToken, Event, TextDocument, WorkspaceFolder } from 'vscode';
import { Disposable, env, EventEmitter, extensions, FileType, Range, Uri, window, workspace } from 'vscode';
import { md5 } from '@env/crypto';
@ -2353,12 +2352,13 @@ export class LocalGitProvider implements GitProvider, Disposable {
}
}
const encoding = await getEncoding(uri);
const promise = this.getDiffForFileCore(
uri.repoPath,
uri.fsPath,
ref1,
ref2,
{ encoding: getEncoding(uri) },
{ encoding: encoding },
doc,
key,
scope,
@ -2441,12 +2441,13 @@ export class LocalGitProvider implements GitProvider, Disposable {
}
}
const encoding = await getEncoding(uri);
const promise = this.getDiffForFileContentsCore(
uri.repoPath,
uri.fsPath,
ref,
contents,
{ encoding: getEncoding(uri) },
{ encoding: encoding },
doc,
key,
scope,
@ -4861,7 +4862,10 @@ export class LocalGitProvider implements GitProvider, Disposable {
}
}
function getEncoding(uri: Uri): string {
async function getEncoding(uri: Uri): Promise<string> {
const encoding = configuration.getAny<string>('files.encoding', uri);
return encoding != null && encodingExists(encoding) ? encoding : 'utf8';
if (encoding == null || encoding === 'utf8') return 'utf8';
const encodingExists = (await import(/* webpackChunkName: "encoding" */ 'iconv-lite')).encodingExists;
return encodingExists(encoding) ? encoding : 'utf8';
}

+ 18
- 18
src/env/node/git/shell.ts View File

@ -4,7 +4,6 @@ import type { Stats } from 'fs';
import { exists, existsSync, statSync } from 'fs';
import { join as joinPaths } from 'path';
import * as process from 'process';
import { decode } from 'iconv-lite';
import type { CancellationToken } from 'vscode';
import { Logger } from '../../../system/logger';
@ -218,7 +217,7 @@ export function run(
let killed = false;
return new Promise<T>((resolve, reject) => {
const proc = execFile(command, args, opts, (error: ExecException | null, stdout, stderr) => {
const proc = execFile(command, args, opts, async (error: ExecException | null, stdout, stderr) => {
if (killed) return;
if (options?.exitCodeOnly) {
@ -232,17 +231,17 @@ export function run(
error.message = `Command output exceeded the allocated stdout buffer. Set 'options.maxBuffer' to a larger value than ${opts.maxBuffer} bytes`;
}
reject(
new RunError(
error,
encoding === 'utf8' || encoding === 'binary' || encoding === 'buffer';
? stdout
: decode(Buffer.from(stdout, 'binary'), encoding),
encoding === 'utf8' || encoding === 'binary' || encoding === 'buffer';
? stderr
: decode(Buffer.from(stderr, 'binary'), encoding),
),
);
let stdoutDecoded;
let stderrDecoded;
if (encoding === 'utf8' || encoding === 'binary' || encoding === 'buffer') {
stdoutDecoded = stdout;
stderrDecoded = stderr;
} else {
const decode = (await import(/* webpackChunkName: "encoding" */ 'iconv-lite')).decode;
stdoutDecoded = decode(Buffer.from(stdout, 'binary'), encoding);
stderrDecoded = decode(Buffer.from(stderr, 'binary'), encoding);
}
reject(new RunError(error, stdoutDecoded, stderrDecoded));
return;
}
@ -251,11 +250,12 @@ export function run(
Logger.warn(`Warning(${command} ${args.join(' ')}): ${stderr}`);
}
resolve(
encoding === 'utf8' || encoding === 'binary' || encoding === 'buffer'
? (stdout as T)
: (decode(Buffer.from(stdout, 'binary'), encoding) as T),
);
if (encoding === 'utf8' || encoding === 'binary' || encoding === 'buffer') {
resolve(stdout as T);
} else {
const decode = (await import(/* webpackChunkName: "encoding" */ 'iconv-lite')).decode;
resolve(decode(Buffer.from(stdout, 'binary'), encoding) as T);
}
});
options?.cancellation?.onCancellationRequested(() => {

+ 2
- 1
tsconfig.json View File

@ -3,7 +3,8 @@
"compilerOptions": {
"paths": {
"@env/*": ["src/env/node/*"]
}
},
"tsBuildInfoFile": "tsconfig.tsbuildinfo"
},
"include": ["src/**/*"],
"exclude": ["src/test/**/*", "src/webviews/apps/**/*", "src/env/browser/**/*"]

+ 4
- 5
webpack.config.js View File

@ -241,13 +241,12 @@ function getExtensionConfig(target, mode, env) {
resolve: {
alias: {
'@env': path.resolve(__dirname, 'src', 'env', target === 'webworker' ? 'browser' : target),
// Stupid dependency that is used by `http[s]-proxy-agent`
debug: path.resolve(__dirname, 'patches', 'debug.js'),
// This dependency is very large, and isn't needed for our use-case
tr46: path.resolve(__dirname, 'patches', 'tr46.js'),
// Stupid dependency that is used by `http-proxy-agent`
debug:
target === 'webworker'
? path.resolve(__dirname, 'node_modules', 'debug', 'src', 'browser.js')
: path.resolve(__dirname, 'node_modules', 'debug', 'src', 'node.js'),
// This dependency is unnecessary for our use-case
'whatwg-url': path.resolve(__dirname, 'patches', 'whatwg-url.js'),
},
fallback:
target === 'webworker'

||||||
x
 
000:0
Loading…
Cancel
Save