fileTransformer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createElectronCompilerHost = exports.createTransformer = exports.hasDep = exports.isElectronCompileUsed = exports.NODE_MODULES_PATTERN = void 0;
  4. const builder_util_1 = require("builder-util");
  5. const promises_1 = require("fs/promises");
  6. const path = require("path");
  7. /** @internal */
  8. exports.NODE_MODULES_PATTERN = `${path.sep}node_modules${path.sep}`;
  9. /** @internal */
  10. function isElectronCompileUsed(info) {
  11. if (info.config.electronCompile != null) {
  12. return info.config.electronCompile;
  13. }
  14. // if in devDependencies - it means that babel is used for precompilation or for some reason user decided to not use electron-compile for production
  15. return hasDep("electron-compile", info);
  16. }
  17. exports.isElectronCompileUsed = isElectronCompileUsed;
  18. /** @internal */
  19. function hasDep(name, info) {
  20. const deps = info.metadata.dependencies;
  21. return deps != null && name in deps;
  22. }
  23. exports.hasDep = hasDep;
  24. /** @internal */
  25. function createTransformer(srcDir, configuration, extraMetadata, extraTransformer) {
  26. const mainPackageJson = path.join(srcDir, "package.json");
  27. const isRemovePackageScripts = configuration.removePackageScripts !== false;
  28. const isRemovePackageKeywords = configuration.removePackageKeywords !== false;
  29. const packageJson = path.sep + "package.json";
  30. return file => {
  31. if (file === mainPackageJson) {
  32. return modifyMainPackageJson(file, extraMetadata, isRemovePackageScripts, isRemovePackageKeywords);
  33. }
  34. if (file.endsWith(packageJson) && file.includes(exports.NODE_MODULES_PATTERN)) {
  35. return (0, promises_1.readFile)(file, "utf-8")
  36. .then(it => cleanupPackageJson(JSON.parse(it), {
  37. isMain: false,
  38. isRemovePackageScripts,
  39. isRemovePackageKeywords,
  40. }))
  41. .catch((e) => builder_util_1.log.warn(e));
  42. }
  43. else if (extraTransformer != null) {
  44. return extraTransformer(file);
  45. }
  46. else {
  47. return null;
  48. }
  49. };
  50. }
  51. exports.createTransformer = createTransformer;
  52. /** @internal */
  53. function createElectronCompilerHost(projectDir, cacheDir) {
  54. const electronCompilePath = path.join(projectDir, "node_modules", "electron-compile", "lib");
  55. return require(path.join(electronCompilePath, "config-parser")).createCompilerHostFromProjectRoot(projectDir, cacheDir);
  56. }
  57. exports.createElectronCompilerHost = createElectronCompilerHost;
  58. const ignoredPackageMetadataProperties = new Set(["dist", "gitHead", "build", "jspm", "ava", "xo", "nyc", "eslintConfig", "contributors", "bundleDependencies", "tags"]);
  59. function cleanupPackageJson(data, options) {
  60. const deps = data.dependencies;
  61. // https://github.com/electron-userland/electron-builder/issues/507#issuecomment-312772099
  62. const isRemoveBabel = deps != null && typeof deps === "object" && !Object.getOwnPropertyNames(deps).some(it => it.startsWith("babel"));
  63. try {
  64. let changed = false;
  65. for (const prop of Object.getOwnPropertyNames(data)) {
  66. // removing devDependencies from package.json breaks levelup in electron, so, remove it only from main package.json
  67. if (prop[0] === "_" ||
  68. ignoredPackageMetadataProperties.has(prop) ||
  69. (options.isRemovePackageScripts && prop === "scripts") ||
  70. (options.isRemovePackageKeywords && prop === "keywords") ||
  71. (options.isMain && prop === "devDependencies") ||
  72. (!options.isMain && prop === "bugs") ||
  73. (isRemoveBabel && prop === "babel")) {
  74. delete data[prop];
  75. changed = true;
  76. }
  77. }
  78. if (changed) {
  79. return JSON.stringify(data, null, 2);
  80. }
  81. }
  82. catch (e) {
  83. (0, builder_util_1.debug)(e);
  84. }
  85. return null;
  86. }
  87. async function modifyMainPackageJson(file, extraMetadata, isRemovePackageScripts, isRemovePackageKeywords) {
  88. const mainPackageData = JSON.parse(await (0, promises_1.readFile)(file, "utf-8"));
  89. if (extraMetadata != null) {
  90. (0, builder_util_1.deepAssign)(mainPackageData, extraMetadata);
  91. }
  92. // https://github.com/electron-userland/electron-builder/issues/1212
  93. const serializedDataIfChanged = cleanupPackageJson(mainPackageData, {
  94. isMain: true,
  95. isRemovePackageScripts,
  96. isRemovePackageKeywords,
  97. });
  98. if (serializedDataIfChanged != null) {
  99. return serializedDataIfChanged;
  100. }
  101. else if (extraMetadata != null) {
  102. return JSON.stringify(mainPackageData, null, 2);
  103. }
  104. return null;
  105. }
  106. //# sourceMappingURL=fileTransformer.js.map