gen-filenames.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import * as cp from 'node:child_process';
  2. import * as fs from 'fs-extra';
  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. const webpackTargetsWithDeps = await Promise.all(webpackTargets.map(async webpackTarget => {
  46. const tmpDir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'electron-filenames-'));
  47. const child = cp.spawn('node', [
  48. './node_modules/webpack-cli/bin/cli.js',
  49. '--config', `./build/webpack/${webpackTarget.config}`,
  50. '--stats', 'errors-only',
  51. '--output-path', tmpDir,
  52. '--output-filename', `${webpackTarget.name}.measure.js`,
  53. '--env', 'PRINT_WEBPACK_GRAPH'
  54. ], {
  55. cwd: path.resolve(__dirname, '..')
  56. });
  57. let output = '';
  58. child.stdout.on('data', chunk => {
  59. output += chunk.toString();
  60. });
  61. child.stderr.on('data', chunk => console.error(chunk.toString()));
  62. await new Promise<void>((resolve, reject) => child.on('exit', (code) => {
  63. if (code !== 0) {
  64. console.error(output);
  65. return reject(new Error(`Failed to list webpack dependencies for entry: ${webpackTarget.name}`));
  66. }
  67. resolve();
  68. }));
  69. const webpackTargetWithDeps = {
  70. ...webpackTarget,
  71. dependencies: (JSON.parse(output) as string[])
  72. // Remove whitespace
  73. .map(line => line.trim())
  74. // Get the relative path
  75. .map(line => path.relative(rootPath, line).replace(/\\/g, '/'))
  76. // Only care about files in //electron
  77. .filter(line => !line.startsWith('..'))
  78. // Only care about our own files
  79. .filter(line => !line.startsWith('node_modules'))
  80. // All webpack builds depend on the tsconfig and package json files
  81. .concat(['tsconfig.json', 'tsconfig.electron.json', 'package.json', ...typingFiles])
  82. // Make the generated list easier to read
  83. .sort()
  84. };
  85. await fs.remove(tmpDir);
  86. return webpackTargetWithDeps;
  87. }));
  88. fs.writeFileSync(
  89. gniPath,
  90. `# THIS FILE IS AUTO-GENERATED, PLEASE DO NOT EDIT BY HAND
  91. auto_filenames = {
  92. api_docs = [
  93. ${allDocs.map(doc => ` "${doc}",`).join('\n')}
  94. ]
  95. ${webpackTargetsWithDeps.map(target => ` ${target.name} = [
  96. ${target.dependencies.map(dep => ` "${dep}",`).join('\n')}
  97. ]`).join('\n\n')}
  98. }
  99. `);
  100. };
  101. if (require.main === module) {
  102. main().catch((err) => {
  103. console.error(err);
  104. process.exit(1);
  105. });
  106. }