mirror of
https://github.com/lobehub/lobehub.git
synced 2026-04-02 14:29:23 +07:00
* ✨ feat(electron): add codemods to convert dynamic imports to static Add multiple modifiers for Electron build workflow: - dynamicToStatic: Convert dynamicElement() to static imports - nextDynamicToStatic: Convert next/dynamic (ssr: false) to static - wrapChildrenWithClientOnly: Wrap layout children with ClientOnly + Loading fallback - settingsContentToStatic: Handle SettingsContent componentMap pattern - removeSuspense: Remove Suspense wrappers from components - routes: Delete loading.tsx files and (mobile) directory Also add fallback prop support to ClientOnly component for better UX during hydration. * ✨ feat(electron): enhance settingsContentToStatic with business features support - Introduced a new function to check if business features are enabled via environment variables. - Updated import generation functions to conditionally include business-related imports based on the new feature flag. - Improved regex patterns for better matching of dynamic imports. - Added logging to indicate when business features are active, enhancing debugging and user awareness. Signed-off-by: Innei <tukon479@gmail.com> --------- Signed-off-by: Innei <tukon479@gmail.com>
74 lines
2.6 KiB
TypeScript
74 lines
2.6 KiB
TypeScript
import { Lang, parse } from '@ast-grep/napi';
|
|
import path from 'node:path';
|
|
|
|
import { invariant, isDirectRun, runStandalone, updateFile } from './utils.mjs';
|
|
|
|
export const wrapChildrenWithClientOnly = async (TEMP_DIR: string) => {
|
|
const layoutPath = path.join(TEMP_DIR, 'src/app/[variants]/layout.tsx');
|
|
|
|
console.log(' Wrapping children with ClientOnly in layout.tsx...');
|
|
|
|
await updateFile({
|
|
assertAfter: (code) => {
|
|
const hasClientOnlyImport = /import ClientOnly from ["']@\/components\/client\/ClientOnly["']/.test(code);
|
|
const hasLoadingImport = /import Loading from ["']@\/components\/Loading\/BrandTextLoading["']/.test(code);
|
|
const hasClientOnlyWrapper = /<ClientOnly fallback={<Loading/.test(code);
|
|
return hasClientOnlyImport && hasLoadingImport && hasClientOnlyWrapper;
|
|
},
|
|
filePath: layoutPath,
|
|
name: 'wrapChildrenWithClientOnly',
|
|
transformer: (code) => {
|
|
const ast = parse(Lang.Tsx, code);
|
|
const root = ast.root();
|
|
|
|
let result = code;
|
|
|
|
const hasClientOnlyImport = /import ClientOnly from ["']@\/components\/client\/ClientOnly["']/.test(code);
|
|
const hasLoadingImport = /import Loading from ["']@\/components\/Loading\/BrandTextLoading["']/.test(code);
|
|
|
|
const lastImport = root.findAll({
|
|
rule: {
|
|
kind: 'import_statement',
|
|
},
|
|
}).at(-1);
|
|
|
|
invariant(lastImport, '[wrapChildrenWithClientOnly] No import statements found in layout.tsx');
|
|
|
|
const insertPos = lastImport!.range().end.index;
|
|
let importsToAdd = '';
|
|
|
|
if (!hasClientOnlyImport) {
|
|
importsToAdd += "\nimport ClientOnly from '@/components/client/ClientOnly';";
|
|
}
|
|
if (!hasLoadingImport) {
|
|
importsToAdd += "\nimport Loading from '@/components/Loading/BrandTextLoading';";
|
|
}
|
|
|
|
if (importsToAdd) {
|
|
result = result.slice(0, insertPos) + importsToAdd + result.slice(insertPos);
|
|
}
|
|
|
|
const authProviderPattern = /<AuthProvider>\s*{children}\s*<\/AuthProvider>/;
|
|
invariant(
|
|
authProviderPattern.test(result),
|
|
'[wrapChildrenWithClientOnly] Pattern <AuthProvider>{children}</AuthProvider> not found in layout.tsx',
|
|
);
|
|
|
|
result = result.replace(
|
|
authProviderPattern,
|
|
`<AuthProvider>
|
|
<ClientOnly fallback={<Loading />}>{children}</ClientOnly>
|
|
</AuthProvider>`,
|
|
);
|
|
|
|
return result;
|
|
},
|
|
});
|
|
};
|
|
|
|
if (isDirectRun(import.meta.url)) {
|
|
await runStandalone('wrapChildrenWithClientOnly', wrapChildrenWithClientOnly, [
|
|
{ lang: Lang.Tsx, path: 'src/app/[variants]/layout.tsx' },
|
|
]);
|
|
}
|