mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
refactor(infra): remove old plugin system (#5411)
plugin system need redesign
This commit is contained in:
@@ -181,16 +181,6 @@ try {
|
||||
})
|
||||
);
|
||||
|
||||
// Build:plugins
|
||||
await awaitChildProcess(
|
||||
spawn('yarn', ['build:plugins'], {
|
||||
cwd,
|
||||
stdio: 'inherit',
|
||||
shell: true,
|
||||
env: process.env,
|
||||
})
|
||||
);
|
||||
|
||||
// Start webpack
|
||||
await awaitChildProcess(
|
||||
spawn(
|
||||
|
||||
@@ -11,20 +11,10 @@ const infraFilePath = resolve(
|
||||
'infra',
|
||||
'vite.config.ts'
|
||||
);
|
||||
const pluginInfraFilePath = resolve(
|
||||
projectRoot,
|
||||
'packages',
|
||||
'plugin-infra',
|
||||
'vite.config.ts'
|
||||
);
|
||||
|
||||
export const buildInfra = async () => {
|
||||
await build({
|
||||
configFile: infraFilePath,
|
||||
});
|
||||
await build({
|
||||
configFile: pluginInfraFilePath,
|
||||
});
|
||||
};
|
||||
|
||||
export const watchInfra = async () => {
|
||||
@@ -33,9 +23,4 @@ export const watchInfra = async () => {
|
||||
shell: true,
|
||||
stdio: 'inherit',
|
||||
});
|
||||
spawn('vite', ['build', '--watch'], {
|
||||
cwd: resolve(projectRoot, 'packages/plugin-infra'),
|
||||
shell: true,
|
||||
stdio: 'inherit',
|
||||
});
|
||||
};
|
||||
|
||||
@@ -23,10 +23,7 @@
|
||||
"y-provider",
|
||||
"debug",
|
||||
"storage",
|
||||
"infra",
|
||||
"plugin-cli",
|
||||
"sdk",
|
||||
"plugin"
|
||||
"infra"
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
{
|
||||
"name": "@affine/plugin-cli",
|
||||
"type": "module",
|
||||
"version": "0.11.0",
|
||||
"bin": {
|
||||
"af": "./src/af.mjs"
|
||||
},
|
||||
"files": [
|
||||
"src",
|
||||
"tsconfig.json"
|
||||
],
|
||||
"dependencies": {
|
||||
"@plugxjs/vite-plugin": "0.1.0",
|
||||
"@swc/core": "^1.3.93",
|
||||
"@toeverything/infra": "workspace:^",
|
||||
"@vanilla-extract/rollup-plugin": "^1.3.0",
|
||||
"@vitejs/plugin-vue": "^5.0.0",
|
||||
"rollup": "^4.0.0",
|
||||
"rollup-plugin-swc3": "^0.10.2",
|
||||
"ts-node": "^10.9.1",
|
||||
"vue": "^3.3.4"
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const child = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
'--loader',
|
||||
'ts-node/esm/transpile-only',
|
||||
fileURLToPath(new URL('./af.ts', import.meta.url)),
|
||||
...process.argv.slice(2),
|
||||
],
|
||||
{ stdio: 'inherit' }
|
||||
);
|
||||
|
||||
if (child.status) process.exit(child.status);
|
||||
@@ -1,180 +0,0 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { parseArgs } from 'node:util';
|
||||
|
||||
import { plugx } from '@plugxjs/vite-plugin';
|
||||
import {
|
||||
packageJsonInputSchema,
|
||||
packageJsonOutputSchema,
|
||||
} from '@toeverything/infra/type';
|
||||
import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
|
||||
import react from '@vitejs/plugin-react-swc';
|
||||
import vue from '@vitejs/plugin-vue';
|
||||
import { build, type PluginOption } from 'vite';
|
||||
import type { z } from 'zod';
|
||||
|
||||
const args = process.argv.splice(2);
|
||||
|
||||
const result = parseArgs({
|
||||
args,
|
||||
allowPositionals: true,
|
||||
});
|
||||
|
||||
const plugin = process.cwd().split(path.sep).pop();
|
||||
if (!plugin) {
|
||||
throw new Error('plugin name not found');
|
||||
}
|
||||
|
||||
const command = result.positionals[0];
|
||||
|
||||
const isWatch = (() => {
|
||||
switch (command) {
|
||||
case 'dev': {
|
||||
return true;
|
||||
}
|
||||
case 'build': {
|
||||
return false;
|
||||
}
|
||||
default: {
|
||||
throw new Error('invalid command');
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
const external = [
|
||||
// built-in packages
|
||||
/^@affine/,
|
||||
/^@blocksuite/,
|
||||
/^@toeverything/,
|
||||
|
||||
// react
|
||||
'react',
|
||||
/^react\//,
|
||||
/^react-dom/,
|
||||
|
||||
// store
|
||||
/^jotai/,
|
||||
|
||||
// utils
|
||||
'swr',
|
||||
|
||||
// css
|
||||
/^@vanilla-extract/,
|
||||
];
|
||||
|
||||
const pluginDir = process.cwd();
|
||||
|
||||
const packageJsonFile = path.resolve(pluginDir, 'package.json');
|
||||
|
||||
const json: z.infer<typeof packageJsonInputSchema> = await readFile(
|
||||
packageJsonFile,
|
||||
{
|
||||
encoding: 'utf-8',
|
||||
}
|
||||
)
|
||||
.then(text => JSON.parse(text))
|
||||
.then(async json => {
|
||||
const result = await packageJsonInputSchema.safeParseAsync(json);
|
||||
if (result.success) {
|
||||
return json;
|
||||
} else {
|
||||
throw new Error('invalid package.json', result.error);
|
||||
}
|
||||
});
|
||||
|
||||
type Metadata = {
|
||||
assets: Set<string>;
|
||||
};
|
||||
|
||||
const metadata: Metadata = {
|
||||
assets: new Set(),
|
||||
};
|
||||
|
||||
const projectRoot = fileURLToPath(new URL('../../..', import.meta.url));
|
||||
const outDir = path.resolve(
|
||||
projectRoot,
|
||||
'packages/frontend/core/public/plugins'
|
||||
);
|
||||
|
||||
const coreOutDir = path.resolve(outDir, plugin);
|
||||
|
||||
const coreEntry = path.resolve(pluginDir, json.affinePlugin.entry.core);
|
||||
|
||||
const generatePackageJson: PluginOption = {
|
||||
name: 'generate-package.json',
|
||||
async generateBundle() {
|
||||
const packageJson = {
|
||||
name: json.name,
|
||||
version: json.version,
|
||||
description: json.description,
|
||||
affinePlugin: {
|
||||
release: json.affinePlugin.release,
|
||||
entry: {
|
||||
core: 'index.js',
|
||||
},
|
||||
assets: [...metadata.assets],
|
||||
},
|
||||
} satisfies z.infer<typeof packageJsonOutputSchema>;
|
||||
packageJsonOutputSchema.parse(packageJson);
|
||||
this.emitFile({
|
||||
type: 'asset',
|
||||
fileName: 'package.json',
|
||||
source: JSON.stringify(packageJson, null, 2),
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
// step 1: generate core bundle
|
||||
await build({
|
||||
build: {
|
||||
watch: isWatch ? {} : undefined,
|
||||
minify: false,
|
||||
target: 'esnext',
|
||||
outDir: coreOutDir,
|
||||
emptyOutDir: true,
|
||||
lib: {
|
||||
entry: coreEntry,
|
||||
fileName: 'index',
|
||||
formats: ['es'],
|
||||
},
|
||||
rollupOptions: {
|
||||
output: {
|
||||
assetFileNames: chunkInfo => {
|
||||
if (chunkInfo.name) {
|
||||
metadata.assets.add(chunkInfo.name);
|
||||
return chunkInfo.name;
|
||||
} else {
|
||||
throw new Error('no name');
|
||||
}
|
||||
},
|
||||
chunkFileNames: chunkInfo => {
|
||||
if (chunkInfo.name) {
|
||||
const hash = createHash('md5')
|
||||
.update(
|
||||
Object.values(chunkInfo.moduleIds)
|
||||
.map(m => m)
|
||||
.join()
|
||||
)
|
||||
.digest('hex')
|
||||
.substring(0, 6);
|
||||
return `${chunkInfo.name}-${hash}.mjs`;
|
||||
} else {
|
||||
throw new Error('no name');
|
||||
}
|
||||
},
|
||||
},
|
||||
external,
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
vanillaExtractPlugin(),
|
||||
vue(),
|
||||
react(),
|
||||
plugx({
|
||||
staticJsonSuffix: '.json',
|
||||
}),
|
||||
generatePackageJson,
|
||||
],
|
||||
});
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"outDir": "lib"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [
|
||||
{
|
||||
"path": "../../packages/common/infra"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user