chore: merge blocksuite source code (#9213)

This commit is contained in:
Mirone
2024-12-20 15:38:06 +08:00
committed by GitHub
parent 2c9ef916f4
commit 30200ff86d
2031 changed files with 238888 additions and 229 deletions

View File

@@ -0,0 +1,65 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import path from 'node:path';
import { init, parse } from 'es-module-lexer';
import MagicString from 'magic-string';
import micromatch from 'micromatch';
import type { Plugin } from 'vite';
const isMatch = micromatch.isMatch;
export function fineTuneHmr({
include,
exclude,
}: {
include: string[];
exclude: string[];
}): Plugin {
let root = '';
const plugin: Plugin = {
name: 'add-hot-for-pure-exports',
apply: 'serve',
configResolved(config) {
root = config.root;
},
async configureServer() {
await init;
},
transform: (code, id) => {
// only handle js/ts files
const includeGlob = include.map(i => path.resolve(root, i));
const excludeGlob = exclude.map(i => path.resolve(root, i));
const isInScope = isMatch(id, includeGlob) && !isMatch(id, excludeGlob);
if (!isInScope) return;
if (!(id.endsWith('.js') || id.endsWith('.ts'))) return;
// only handle files which not contains Lit elements
if (code.includes('import.meta.hot')) return;
const [imports, exports] = parse(code, id);
if (exports.length === 0 && imports.length > 0) {
const modules = imports.map(i => i.n);
const modulesEndsWithTs = modules
.filter(Boolean)
.map(m => m!.replace(/\.js$/, '.ts'));
const preamble = `
if (import.meta.hot) {
import.meta.hot.accept(${JSON.stringify(
modulesEndsWithTs
)}, data => {
// some update logic
});
}
`;
const s = new MagicString(code);
s.prepend(preamble + '\n');
return {
code: s.toString(),
map: s.generateMap({ hires: true, source: id, includeContent: true }),
};
}
return;
},
};
return plugin;
}

View File

@@ -0,0 +1,38 @@
import path from 'node:path';
import {
hmrPlugin as wcHmrPlugin,
presets,
} from 'vite-plugin-web-components-hmr';
import { fineTuneHmr } from './fine-tune.js';
const customLitPath = path.resolve(
__dirname,
'../../../blocks/src/_legacy/index.js'
);
const include = ['../blocks/src/**/*'];
const exclude = ['**/*/node_modules/**/*'];
// https://vitejs.dev/config/
export const hmrPlugin = process.env.WC_HMR
? [
wcHmrPlugin({
include,
exclude,
presets: [presets.lit],
decorators: [{ name: 'customElement', import: 'lit/decorators.js' }],
baseClasses: [
{
name: 'ShadowlessElement',
import: customLitPath,
},
],
}),
fineTuneHmr({
include,
exclude,
}),
]
: [];