mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 13:25:12 +00:00
chore: merge blocksuite source code (#9213)
This commit is contained in:
65
blocksuite/playground/scripts/hmr-plugin/fine-tune.ts
Normal file
65
blocksuite/playground/scripts/hmr-plugin/fine-tune.ts
Normal 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;
|
||||
}
|
||||
38
blocksuite/playground/scripts/hmr-plugin/index.ts
Normal file
38
blocksuite/playground/scripts/hmr-plugin/index.ts
Normal 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,
|
||||
}),
|
||||
]
|
||||
: [];
|
||||
Reference in New Issue
Block a user