feat: plugin system with isolated bundles (#2660)

This commit is contained in:
Himself65
2023-06-02 16:28:47 +08:00
committed by GitHub
parent f9079bb681
commit 94d20f1bdc
19 changed files with 147 additions and 28 deletions

View File

@@ -1,9 +1,12 @@
#!/usr/bin/env zx
import 'zx/globals';
import { spawnSync } from 'node:child_process';
import { resolve } from 'node:path';
import * as esbuild from 'esbuild';
import { config } from './common.mjs';
import { config, rootDir } from './common.mjs';
const NODE_ENV =
process.env.NODE_ENV === 'development' ? 'development' : 'production';
@@ -17,6 +20,12 @@ async function buildLayers() {
const common = config();
await esbuild.build(common.preload);
console.log('build plugins');
spawnSync('yarn', ['build'], {
cwd: resolve(rootDir, './plugins/bookmark-block'),
stdio: 'inherit',
});
await esbuild.build({
...common.main,
define: {

View File

@@ -2,7 +2,10 @@ import { resolve } from 'node:path';
import { fileURLToPath } from 'url';
export const root = fileURLToPath(new URL('..', import.meta.url));
export const electronDir = fileURLToPath(new URL('..', import.meta.url));
export const rootDir = resolve(electronDir, '..', '..');
export const NODE_MAJOR_VERSION = 18;
// hard-coded for now:
@@ -33,10 +36,13 @@ export const config = () => {
return {
main: {
entryPoints: [
resolve(root, './layers/main/src/index.ts'),
resolve(root, './layers/main/src/workers/merge-update.worker.ts'),
resolve(electronDir, './layers/main/src/index.ts'),
resolve(
electronDir,
'./layers/main/src/workers/merge-update.worker.ts'
),
],
outdir: resolve(root, './dist/layers/main'),
outdir: resolve(electronDir, './dist/layers/main'),
bundle: true,
target: `node${NODE_MAJOR_VERSION}`,
platform: 'node',
@@ -50,8 +56,8 @@ export const config = () => {
treeShaking: true,
},
preload: {
entryPoints: [resolve(root, './layers/preload/src/index.ts')],
outdir: resolve(root, './dist/layers/preload'),
entryPoints: [resolve(electronDir, './layers/preload/src/index.ts')],
outdir: resolve(electronDir, './dist/layers/preload'),
bundle: true,
target: `node${NODE_MAJOR_VERSION}`,
platform: 'node',

View File

@@ -1,12 +1,12 @@
/* eslint-disable no-async-promise-executor */
import { spawn } from 'node:child_process';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import path, { resolve } from 'node:path';
import electronPath from 'electron';
import * as esbuild from 'esbuild';
import { config, root } from './common.mjs';
import { config, electronDir, rootDir } from './common.mjs';
// this means we don't spawn electron windows, mainly for testing
const watchMode = process.argv.includes('--watch');
@@ -21,7 +21,10 @@ const stderrFilterPatterns = [
// these are set before calling `config`, so we have a chance to override them
try {
const devJson = readFileSync(path.resolve(root, './dev.json'), 'utf-8');
const devJson = readFileSync(
path.resolve(electronDir, './dev.json'),
'utf-8'
);
const devEnv = JSON.parse(devJson);
Object.assign(process.env, devEnv);
} catch (err) {
@@ -65,7 +68,18 @@ function spawnOrReloadElectron() {
const common = config();
function watchPreload() {
function watchPlugins() {
const cp = spawn('yarn', ['dev'], {
cwd: resolve(rootDir, './plugins/bookmark-block'),
stdio: 'inherit',
});
process.once('beforeExit', () => {
cp.kill();
});
}
async function watchPreload() {
return new Promise(async resolve => {
let initialBuild = false;
const preloadBuild = await esbuild.context({
@@ -122,6 +136,7 @@ async function watchMain() {
}
async function main() {
watchPlugins();
await watchMain();
await watchPreload();