gen-filenames.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import * as cp from 'node:child_process';
  2. import * as fs from 'node:fs';
  3. import * as os from 'node:os';
  4. import * as path from 'node:path';
  5. const rootPath = path.resolve(__dirname, '..');
  6. const gniPath = path.resolve(__dirname, '../filenames.auto.gni');
  7. const allDocs = fs.readdirSync(path.resolve(__dirname, '../docs/api'))
  8. .map(doc => `docs/api/${doc}`)
  9. .concat(
  10. fs.readdirSync(path.resolve(__dirname, '../docs/api/structures'))
  11. .map(doc => `docs/api/structures/${doc}`)
  12. );
  13. const typingFiles = fs.readdirSync(path.resolve(__dirname, '../typings')).map(child => `typings/${child}`);
  14. const main = async () => {
  15. const webpackTargets = [
  16. {
  17. name: 'sandbox_bundle_deps',
  18. config: 'webpack.config.sandboxed_renderer.js'
  19. },
  20. {
  21. name: 'isolated_bundle_deps',
  22. config: 'webpack.config.isolated_renderer.js'
  23. },
  24. {
  25. name: 'browser_bundle_deps',
  26. config: 'webpack.config.browser.js'
  27. },
  28. {
  29. name: 'renderer_bundle_deps',
  30. config: 'webpack.config.renderer.js'
  31. },
  32. {
  33. name: 'worker_bundle_deps',
  34. config: 'webpack.config.worker.js'
  35. },
  36. {
  37. name: 'node_bundle_deps',
  38. config: 'webpack.config.node.js'
  39. },
  40. {
  41. name: 'utility_bundle_deps',
  42. config: 'webpack.config.utility.js'
  43. },
  44. {
  45. name: 'preload_realm_bundle_deps',
  46. config: 'webpack.config.preload_realm.js'
  47. }
  48. ];
  49. const webpackTargetsWithDeps = await Promise.all(webpackTargets.map(async webpackTarget => {
  50. const tmpDir = await fs.promises.mkdtemp(path.resolve(os.tmpdir(), 'electron-filenames-'));
  51. const child = cp.spawn('node', [
  52. './node_modules/webpack-cli/bin/cli.js',
  53. '--config', `./build/webpack/${webpackTarget.config}`,
  54. '--stats', 'errors-only',
  55. '--output-path', tmpDir,
  56. '--output-filename', `${webpackTarget.name}.measure.js`,
  57. '--env', 'PRINT_WEBPACK_GRAPH'
  58. ], {
  59. cwd: path.resolve(__dirname, '..')
  60. });
  61. let output = '';
  62. child.stdout.on('data', chunk => {
  63. output += chunk.toString();
  64. });
  65. child.stderr.on('data', chunk => console.error(chunk.toString()));
  66. await new Promise<void>((resolve, reject) => child.on('exit', (code) => {
  67. if (code !== 0) {
  68. console.error(output);
  69. return reject(new Error(`Failed to list webpack dependencies for entry: ${webpackTarget.name}`));
  70. }
  71. resolve();
  72. }));
  73. const webpackTargetWithDeps = {
  74. ...webpackTarget,
  75. dependencies: (JSON.parse(output) as string[])
  76. // Remove whitespace
  77. .map(line => line.trim())
  78. // Get the relative path
  79. .map(line => path.relative(rootPath, line).replace(/\\/g, '/'))
  80. // Only care about files in //electron
  81. .filter(line => !line.startsWith('..'))
  82. // Only care about our own files
  83. .filter(line => !line.startsWith('node_modules'))
  84. // All webpack builds depend on the tsconfig and package json files
  85. .concat(['tsconfig.json', 'tsconfig.electron.json', 'package.json', ...typingFiles])
  86. // Make the generated list easier to read
  87. .sort()
  88. };
  89. await fs.promises.rm(tmpDir, { force: true, recursive: true });
  90. return webpackTargetWithDeps;
  91. }));
  92. fs.writeFileSync(
  93. gniPath,
  94. `# THIS FILE IS AUTO-GENERATED, PLEASE DO NOT EDIT BY HAND
  95. auto_filenames = {
  96. api_docs = [
  97. ${allDocs.map(doc => ` "${doc}",`).join('\n')}
  98. ]
  99. ${webpackTargetsWithDeps.map(target => ` ${target.name} = [
  100. ${target.dependencies.map(dep => ` "${dep}",`).join('\n')}
  101. ]`).join('\n\n')}
  102. }
  103. `);
  104. };
  105. if (require.main === module) {
  106. main().catch((err) => {
  107. console.error(err);
  108. process.exit(1);
  109. });
  110. }