tabby/web/entry.preload.ts

85 lines
2.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 'ngx-toastr/toastr.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 originalRequire = window['require']
const customRequire = path => {
if (mocks[path]) {
console.log(':: mock', path)
return mocks[path]
}
if (modules[path]) {
return modules[path]
}
return originalRequire(path)
}
customRequire['resolve'] = (() => null) as any
customRequire['main'] = {
paths: [],
}
async function webRequire (url) {
console.log(`Loading ${url}`)
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-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
},
loadPlugin: async (url) => {
const pkg = await (await fetch(url + '/package.json')).json()
url += '/' + pkg.main
const module = await webRequire(url)
2021-06-29 21:57:04 +00:00
Tabby.registerModule(`resources/builtin-plugins/${pkg.name}`, module)
Tabby.registerModule(pkg.name, module)
2021-06-25 19:55:34 +00:00
return module
},
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-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',
nextTick: (f, ...args) => setTimeout(() => f(...args)),
cwd: () => '/',
},
global: window,
})