tabby/web/entry.preload.ts

114 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-06-15 21:43:02 +00: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 '../app/src/preload.scss'
2021-06-19 20:52:18 +00:00
// Required before other imports
import './polyfills.buffer'
2021-06-25 19:55:34 +00: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 19:55:34 +00:00
}
customRequire['resolve'] = (() => null) as any
customRequire['main'] = {
paths: [],
}
async function webRequire (url) {
2021-07-08 20:03:41 +00:00
console.log(`>> Loading ${url}`)
2021-06-25 19:55:34 +00: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 20:03:41 +00:00
async function prefetchURL (url) {
console.log(`:: Prefetching ${url}`)
await (await fetch(url)).text()
}
2021-06-29 21:57:04 +00:00
const Tabby = {
2021-06-25 19:55:34 +00:00
registerMock: (name, mod) => {
mocks[name] = mod
},
registerModule: (name, mod) => {
modules[name] = mod
},
2021-07-08 20:03:41 +00:00
resolvePluginInfo: async (url): Promise<any> => {
2021-06-25 19:55:34 +00:00
const pkg = await (await fetch(url + '/package.json')).json()
url += '/' + pkg.main
2021-07-08 20:03:41 +00: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 19:55:34 +00:00
return module
},
2021-07-11 14:31:00 +00:00
loadPlugins: async (urls, progressCallback) => {
2021-07-08 20:03:41 +00:00
const infos: any[] = await Promise.all(urls.map(Tabby.resolvePluginInfo))
2021-07-11 14:31:00 +00:00
progressCallback?.(0, 1)
2021-07-08 20:03:41 +00: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 14:31:00 +00:00
progressCallback?.(infos.indexOf(info), infos.length)
2021-07-08 20:03:41 +00:00
}
2021-07-11 14:31:00 +00:00
progressCallback?.(1, 1)
2021-07-08 20:03:41 +00:00
return pluginModules
},
2021-06-29 21:57:04 +00:00
bootstrap: (...args) => window['bootstrapTabby'](...args),
2021-06-25 19:55:34 +00:00
webRequire,
}
Object.assign(window, {
require: customRequire,
module: {
paths: [],
},
2021-06-29 21:57:04 +00:00
Tabby,
2021-08-24 23:25:40 +00:00
__filename: '',
__dirname: '',
2021-06-25 19:55:34 +00:00
process: {
env: { },
2021-06-29 21:57:04 +00:00
argv: ['tabby'],
2021-06-25 19:55:34 +00:00
platform: 'darwin',
on: () => null,
stdout: {},
stderr: {},
resourcesPath: 'resources',
version: '14.0.0',
2021-07-11 20:59:39 +00:00
versions: {
modules: 0,
},
2021-06-25 19:55:34 +00:00
nextTick: (f, ...args) => setTimeout(() => f(...args)),
cwd: () => '/',
},
global: window,
})