gen-filenames.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const cp = require('child_process')
  2. const fs = require('fs-extra')
  3. const os = require('os')
  4. const path = require('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 main = async () => {
  14. const webpackTargets = [
  15. {
  16. name: 'sandbox_bundle_deps',
  17. config: 'webpack.config.sandboxed_renderer.js'
  18. },
  19. {
  20. name: 'isolated_bundle_deps',
  21. config: 'webpack.config.isolated_renderer.js'
  22. },
  23. {
  24. name: 'content_script_bundle_deps',
  25. config: 'webpack.config.content_script.js'
  26. },
  27. {
  28. name: 'browser_bundle_deps',
  29. config: 'webpack.config.browser.js'
  30. },
  31. {
  32. name: 'renderer_bundle_deps',
  33. config: 'webpack.config.renderer.js'
  34. },
  35. {
  36. name: 'worker_bundle_deps',
  37. config: 'webpack.config.worker.js'
  38. }
  39. ]
  40. await Promise.all(webpackTargets.map(async webpackTarget => {
  41. const tmpDir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'electron-filenames-'))
  42. const child = cp.spawn('node', [
  43. 'build/webpack/get-outputs.js',
  44. `./${webpackTarget.config}`,
  45. path.resolve(tmpDir, `${webpackTarget.name}.measure.js`)
  46. ], {
  47. cwd: path.resolve(__dirname, '..')
  48. })
  49. let output = ''
  50. child.stdout.on('data', chunk => {
  51. output += chunk.toString()
  52. })
  53. child.stderr.on('data', chunk => console.error(chunk.toString()))
  54. await new Promise((resolve, reject) => child.on('exit', (code) => {
  55. if (code !== 0) {
  56. console.error(output)
  57. return reject(new Error(`Failed to list webpack dependencies for entry: ${webpackTarget.name}`))
  58. }
  59. resolve()
  60. }))
  61. webpackTarget.dependencies = JSON.parse(output)
  62. // Remove whitespace
  63. .map(line => line.trim())
  64. // Get the relative path
  65. .map(line => path.relative(rootPath, line).replace(/\\/g, '/'))
  66. // Only care about files in //electron
  67. .filter(line => !line.startsWith('..'))
  68. // Only care about our own files
  69. .filter(line => !line.startsWith('node_modules'))
  70. // All webpack builds depend on the tsconfig and package json files
  71. .concat(['tsconfig.json', 'tsconfig.electron.json', 'package.json'])
  72. // Make the generated list easier to read
  73. .sort()
  74. await fs.remove(tmpDir)
  75. }))
  76. fs.writeFileSync(
  77. gniPath,
  78. `# THIS FILE IS AUTO-GENERATED, PLEASE DO NOT EDIT BY HAND
  79. auto_filenames = {
  80. api_docs = [
  81. ${allDocs.map(doc => ` "${doc}",`).join('\n')}
  82. ]
  83. ${webpackTargets.map(target => ` ${target.name} = [
  84. ${target.dependencies.map(dep => ` "${dep}",`).join('\n')}
  85. ]`).join('\n\n')}
  86. }
  87. `)
  88. }
  89. if (process.mainModule === module) {
  90. main().catch((err) => {
  91. console.error(err)
  92. process.exit(1)
  93. })
  94. }