gen-filenames.ts 3.3 KB

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