mangle-loader.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. const fs = require('fs');
  7. const webpack = require('webpack');
  8. const fancyLog = require('fancy-log');
  9. const ansiColors = require('ansi-colors');
  10. const { Mangler } = require('../build/lib/mangle/index');
  11. /**
  12. * Map of project paths to mangled file contents
  13. *
  14. * @type {Map<string, Promise<Map<string, { out: string; sourceMap?: string }>>>}
  15. */
  16. const mangleMap = new Map();
  17. /**
  18. * @param {string} projectPath
  19. */
  20. function getMangledFileContents(projectPath) {
  21. let entry = mangleMap.get(projectPath);
  22. if (!entry) {
  23. const log = (...data) => fancyLog(ansiColors.blue('[mangler]'), ...data);
  24. log(`Mangling ${projectPath}`);
  25. const ts2tsMangler = new Mangler(projectPath, log, { mangleExports: true, manglePrivateFields: true });
  26. entry = ts2tsMangler.computeNewFileContents();
  27. mangleMap.set(projectPath, entry);
  28. }
  29. return entry;
  30. }
  31. /**
  32. * @type {webpack.LoaderDefinitionFunction}
  33. */
  34. module.exports = async function (source, sourceMap, meta) {
  35. if (this.mode !== 'production') {
  36. // Only enable mangling in production builds
  37. return source;
  38. }
  39. if (true) {
  40. // disable mangling for now, SEE https://github.com/microsoft/vscode/issues/204692
  41. return source;
  42. }
  43. const options = this.getOptions();
  44. if (options.disabled) {
  45. // Dynamically disabled
  46. return source;
  47. }
  48. if (source !== fs.readFileSync(this.resourcePath).toString()) {
  49. // File content has changed by previous webpack steps.
  50. // Skip mangling.
  51. return source;
  52. }
  53. const callback = this.async();
  54. const fileContentsMap = await getMangledFileContents(options.configFile);
  55. const newContents = fileContentsMap.get(this.resourcePath);
  56. callback(null, newContents?.out ?? source, sourceMap, meta);
  57. };