entry.preload.ts 3.2 KB

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