1
1
mirror of https://github.com/Eugeny/tabby.git synced 2024-12-04 19:35:13 +03:00
tabby/web/entry.preload.ts

115 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-06-16 00:43:02 +03:00
import 'source-sans-pro/source-sans-pro.css'
import 'source-code-pro/source-code-pro.css'
import '@fortawesome/fontawesome-free/css/solid.css'
import '@fortawesome/fontawesome-free/css/brands.css'
import '@fortawesome/fontawesome-free/css/regular.css'
import '@fortawesome/fontawesome-free/css/fontawesome.css'
import 'ngx-toastr/toastr.css'
import '../app/src/preload.scss'
2021-06-19 23:52:18 +03:00
// Required before other imports
import './polyfills.buffer'
2021-06-25 22:55:34 +03:00
const mocks = {}
const modules = {}
const customRequire = path => {
if (mocks[path]) {
console.log(':: mock', path)
return mocks[path]
}
if (modules[path]) {
return modules[path]
}
throw new Error(`Attempted to require ${path}`)
2021-06-25 22:55:34 +03:00
}
customRequire['resolve'] = (() => null) as any
customRequire['main'] = {
paths: [],
}
async function webRequire (url) {
2021-07-08 23:03:41 +03:00
console.log(`>> Loading ${url}`)
2021-06-25 22:55:34 +03:00
const e = document.createElement('script')
window['module'] = { exports: {} } as any
window['exports'] = window['module'].exports
await new Promise(resolve => {
e.onload = resolve
e.src = url
document.querySelector('head').appendChild(e)
})
return window['module'].exports
}
2021-07-08 23:03:41 +03:00
async function prefetchURL (url) {
console.log(`:: Prefetching ${url}`)
await (await fetch(url)).text()
}
2021-06-30 00:57:04 +03:00
const Tabby = {
2021-06-25 22:55:34 +03:00
registerMock: (name, mod) => {
mocks[name] = mod
},
registerModule: (name, mod) => {
modules[name] = mod
},
2021-07-08 23:03:41 +03:00
resolvePluginInfo: async (url): Promise<any> => {
2021-06-25 22:55:34 +03:00
const pkg = await (await fetch(url + '/package.json')).json()
url += '/' + pkg.main
2021-07-08 23:03:41 +03:00
return { ...pkg, url }
},
registerPluginModule: (packageName, module) => {
Tabby.registerModule(`resources/builtin-plugins/${packageName}`, module)
Tabby.registerModule(packageName, module)
},
loadPlugin: async (url) => {
const info = await Tabby.resolvePluginInfo(url)
const module = await webRequire(info.url)
Tabby.registerPluginModule(info.name, module)
2021-06-25 22:55:34 +03:00
return module
},
2021-07-11 17:31:00 +03:00
loadPlugins: async (urls, progressCallback) => {
2021-07-08 23:03:41 +03:00
const infos: any[] = await Promise.all(urls.map(Tabby.resolvePluginInfo))
2021-07-11 17:31:00 +03:00
progressCallback?.(0, 1)
2021-07-08 23:03:41 +03:00
await Promise.all(infos.map(x => prefetchURL(x.url)))
const pluginModules = []
for (const info of infos) {
const module = await webRequire(info.url)
Tabby.registerPluginModule(info.name, module)
pluginModules.push(module)
2021-07-11 17:31:00 +03:00
progressCallback?.(infos.indexOf(info), infos.length)
2021-07-08 23:03:41 +03:00
}
2021-07-11 17:31:00 +03:00
progressCallback?.(1, 1)
2021-07-08 23:03:41 +03:00
return pluginModules
},
2021-06-30 00:57:04 +03:00
bootstrap: (...args) => window['bootstrapTabby'](...args),
2021-06-25 22:55:34 +03:00
webRequire,
}
Object.assign(window, {
require: customRequire,
module: {
paths: [],
},
2021-06-30 00:57:04 +03:00
Tabby,
2021-08-25 02:25:40 +03:00
__filename: '',
__dirname: '',
2021-06-25 22:55:34 +03:00
process: {
env: { },
2021-06-30 00:57:04 +03:00
argv: ['tabby'],
2021-06-25 22:55:34 +03:00
platform: 'darwin',
on: () => null,
stdout: {},
stderr: {},
resourcesPath: 'resources',
version: '14.0.0',
2021-07-11 23:59:39 +03:00
versions: {
modules: 0,
},
2021-06-25 22:55:34 +03:00
nextTick: (f, ...args) => setTimeout(() => f(...args)),
cwd: () => '/',
},
global: window,
})