vscode/extensions/mangle-loader.js
Matt Bierner debcf16fcd
Mangle exported symbols (#182935)
* Mangle exported functions

For #180461

This prototype tries to mangle exported functions, saving a further 440kb from the bundle size

* Fix missing call

* Also try mangling top level exported consts too

* Fixing errors

* Don't run on build files

* Skip a few more manglings and revert change to namespace

* Skip a few more monaco files

* Also mangle consts that shadow types

This increases savings up to 3325

* Also mangle exported classes

* Skip mangling more localization functions for now

* Opt out pfs

* Update build script

* Run find locations task in parallel

This should speed up compile

* Cleanup before close

* Limit workers to avoid hitting memory limit

* Limit pool size

* Skip one more mangling

* Exclude entrypoints from mangling

* Try to fix web build and clean up code

* Exempt a few more projects

* Exempt another file

* Also exempt html

* Skip mangling ext entrypoints

* Use prefix that can't be confused with rpc calls

* Fix max call stack error

* Switch prefixes

* Don't mangle ambient declarations

* Use correct way of checking modifier flags

* Workaround getCombinedModifierFlags not doing what I'd expect

Maybe needs the checker to be enabled too? Just check parent chain instead for now

* Clean up code and add logic showing how enum mangling could work

* Remove a few more skipMangles

Use entrypoints instead

* Fix entrypoint name
2023-06-13 08:18:05 +02:00

63 lines
1.8 KiB
JavaScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// @ts-check
const fs = require('fs');
const webpack = require('webpack');
const fancyLog = require('fancy-log');
const ansiColors = require('ansi-colors');
const { Mangler } = require('../build/lib/mangle/index');
/**
* Map of project paths to mangled file contents
*
* @type {Map<string, Promise<Map<string, { out: string; sourceMap?: string }>>>}
*/
const mangleMap = new Map();
/**
* @param {string} projectPath
*/
function getMangledFileContents(projectPath) {
let entry = mangleMap.get(projectPath);
if (!entry) {
const log = (...data) => fancyLog(ansiColors.blue('[mangler]'), ...data);
log(`Mangling ${projectPath}`);
const ts2tsMangler = new Mangler(projectPath, log);
entry = ts2tsMangler.computeNewFileContents();
mangleMap.set(projectPath, entry);
}
return entry;
}
/**
* @type {webpack.LoaderDefinitionFunction}
*/
module.exports = async function (source, sourceMap, meta) {
if (this.mode !== 'production') {
// Only enable mangling in production builds
return source;
}
const options = this.getOptions();
if (options.disabled) {
// Dynamically disabled
return source;
}
if (source !== fs.readFileSync(this.resourcePath).toString()) {
// File content has changed by previous webpack steps.
// Skip mangling.
return source;
}
const callback = this.async();
const fileContentsMap = await getMangledFileContents(options.configFile);
const newContents = fileContentsMap.get(this.resourcePath);
callback(null, newContents?.out ?? source, sourceMap, meta);
};