entry.preload.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import 'source-sans-pro/source-sans-pro.css'
  2. import 'source-code-pro/source-code-pro.css'
  3. import '@fortawesome/fontawesome-free/css/solid.css'
  4. import '@fortawesome/fontawesome-free/css/brands.css'
  5. import '@fortawesome/fontawesome-free/css/regular.css'
  6. import '@fortawesome/fontawesome-free/css/fontawesome.css'
  7. import '../app/src/preload.scss'
  8. // Required before other imports
  9. import './polyfills.buffer'
  10. const mocks = {}
  11. const modules = {}
  12. const customRequire = path => {
  13. if (mocks[path]) {
  14. console.log(':: mock', path)
  15. return mocks[path]
  16. }
  17. if (modules[path]) {
  18. return modules[path]
  19. }
  20. throw new Error(`Attempted to require ${path}`)
  21. }
  22. customRequire['resolve'] = (() => null) as any
  23. customRequire['main'] = {
  24. paths: [],
  25. }
  26. async function webRequire (url) {
  27. console.log(`>> Loading ${url}`)
  28. const e = document.createElement('script')
  29. window['module'] = { exports: {} } as any
  30. window['exports'] = window['module'].exports
  31. await new Promise(resolve => {
  32. e.onload = resolve
  33. e.src = url
  34. document.querySelector('head').appendChild(e)
  35. })
  36. return window['module'].exports
  37. }
  38. async function prefetchURL (url) {
  39. console.log(`:: Prefetching ${url}`)
  40. await (await fetch(url)).text()
  41. }
  42. const Tabby = {
  43. registerMock: (name, mod) => {
  44. mocks[name] = mod
  45. },
  46. registerModule: (name, mod) => {
  47. modules[name] = mod
  48. },
  49. resolvePluginInfo: async (url): Promise<any> => {
  50. const pkg = await (await fetch(url + '/package.json')).json()
  51. url += '/' + pkg.main
  52. return { ...pkg, url }
  53. },
  54. registerPluginModule: (packageName, module) => {
  55. Tabby.registerModule(`resources/builtin-plugins/${packageName}`, module)
  56. Tabby.registerModule(packageName, module)
  57. },
  58. loadPlugin: async (url) => {
  59. const info = await Tabby.resolvePluginInfo(url)
  60. const module = await webRequire(info.url)
  61. Tabby.registerPluginModule(info.name, module)
  62. return module
  63. },
  64. loadPlugins: async (urls, progressCallback) => {
  65. const infos: any[] = await Promise.all(urls.map(Tabby.resolvePluginInfo))
  66. progressCallback?.(0, 1)
  67. await Promise.all(infos.map(x => prefetchURL(x.url)))
  68. const pluginModules = []
  69. for (const info of infos) {
  70. const module = await webRequire(info.url)
  71. Tabby.registerPluginModule(info.name, module)
  72. pluginModules.push(module)
  73. progressCallback?.(infos.indexOf(info), infos.length)
  74. }
  75. progressCallback?.(1, 1)
  76. return pluginModules
  77. },
  78. bootstrap: (...args) => window['bootstrapTabby'](...args),
  79. webRequire,
  80. }
  81. Object.assign(window, {
  82. require: customRequire,
  83. module: {
  84. paths: [],
  85. },
  86. Tabby,
  87. __filename: '',
  88. __dirname: '',
  89. process: {
  90. env: { },
  91. argv: ['tabby'],
  92. platform: 'darwin',
  93. on: () => null,
  94. stdout: {},
  95. stderr: {},
  96. resourcesPath: 'resources',
  97. version: '14.0.0',
  98. versions: {
  99. modules: 0,
  100. },
  101. nextTick: (f, ...args) => setTimeout(() => f(...args)),
  102. cwd: () => '/',
  103. },
  104. global: window,
  105. })