Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/mangle-loader.js
3285 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
// @ts-check
6
7
const fs = require('fs');
8
const webpack = require('webpack');
9
const fancyLog = require('fancy-log');
10
const ansiColors = require('ansi-colors');
11
const { Mangler } = require('../build/lib/mangle/index');
12
13
/**
14
* Map of project paths to mangled file contents
15
*
16
* @type {Map<string, Promise<Map<string, { out: string; sourceMap?: string }>>>}
17
*/
18
const mangleMap = new Map();
19
20
/**
21
* @param {string} projectPath
22
*/
23
function getMangledFileContents(projectPath) {
24
let entry = mangleMap.get(projectPath);
25
if (!entry) {
26
const log = (...data) => fancyLog(ansiColors.blue('[mangler]'), ...data);
27
log(`Mangling ${projectPath}`);
28
const ts2tsMangler = new Mangler(projectPath, log, { mangleExports: true, manglePrivateFields: true });
29
entry = ts2tsMangler.computeNewFileContents();
30
mangleMap.set(projectPath, entry);
31
}
32
33
return entry;
34
}
35
36
/**
37
* @type {webpack.LoaderDefinitionFunction}
38
*/
39
module.exports = async function (source, sourceMap, meta) {
40
if (this.mode !== 'production') {
41
// Only enable mangling in production builds
42
return source;
43
}
44
if (true) {
45
// disable mangling for now, SEE https://github.com/microsoft/vscode/issues/204692
46
return source;
47
}
48
const options = this.getOptions();
49
if (options.disabled) {
50
// Dynamically disabled
51
return source;
52
}
53
54
if (source !== fs.readFileSync(this.resourcePath).toString()) {
55
// File content has changed by previous webpack steps.
56
// Skip mangling.
57
return source;
58
}
59
60
const callback = this.async();
61
62
const fileContentsMap = await getMangledFileContents(options.configFile);
63
64
const newContents = fileContentsMap.get(this.resourcePath);
65
callback(null, newContents?.out ?? source, sourceMap, meta);
66
};
67
68