From 4144cc3028a036fc7ce55b794c20bc6e8647522f Mon Sep 17 00:00:00 2001 From: sanyuan <494130947@qq.com> Date: Mon, 5 Sep 2022 20:27:58 +0800 Subject: [PATCH] feat: support page data virtual module --- package.json | 2 +- playground/.island/config.ts | 16 ++++++++++++++++ src/client/app/data.ts | 5 +++++ src/client/app/index.ts | 2 ++ src/client/theme/layout/DocLayout/index.tsx | 3 +++ src/client/type.d.ts | 5 +++++ src/node/config.ts | 6 ++++++ src/node/index.ts | 1 + src/node/plugin-island/index.ts | 20 +++++++++++++++++--- src/node/plugin-routes/RouteService.ts | 6 ++---- src/node/plugin-svgr/index.ts | 1 - src/node/tsconfig.json | 3 ++- 12 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 playground/.island/config.ts create mode 100644 src/client/app/data.ts create mode 100644 src/node/index.ts diff --git a/package.json b/package.json index f25ca5b1..2acb235e 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "island", + "name": "island-ssg", "version": "1.0.0", "description": "", "main": "index.js", diff --git a/playground/.island/config.ts b/playground/.island/config.ts new file mode 100644 index 00000000..4fc90b88 --- /dev/null +++ b/playground/.island/config.ts @@ -0,0 +1,16 @@ +import { defineConfig } from '../../dist/node/index'; + +export default defineConfig({ + lang: 'zh-CN', + themeConfig: { + nav: [ + { text: '架构', link: '/arch/why', activeMatch: '^/$|^/arch/' }, + { + text: '构建实现', + link: '/build/1-pre-bundle/esbuild', + activeMatch: '^/build/' + }, + { text: '生态', link: '/eco/plugin-vue', activeMatch: '^/eco/' } + ] + } +}); diff --git a/src/client/app/data.ts b/src/client/app/data.ts new file mode 100644 index 00000000..e4dda0dd --- /dev/null +++ b/src/client/app/data.ts @@ -0,0 +1,5 @@ +import data from 'island:page-data'; + +export const usePageData = () => { + return data; +}; diff --git a/src/client/app/index.ts b/src/client/app/index.ts index e69de29b..a2e5196f 100644 --- a/src/client/app/index.ts +++ b/src/client/app/index.ts @@ -0,0 +1,2 @@ +export { Content } from './Content'; +export { usePageData } from './data'; diff --git a/src/client/theme/layout/DocLayout/index.tsx b/src/client/theme/layout/DocLayout/index.tsx index a5a54416..576a129f 100644 --- a/src/client/theme/layout/DocLayout/index.tsx +++ b/src/client/theme/layout/DocLayout/index.tsx @@ -4,6 +4,9 @@ import styles from './index.module.scss'; import { Content } from 'island:client/Content'; import { Aside } from '../../components/Aside/index'; import { DocFooter } from '../../components/DocFooter/index'; +import { usePageData } from 'island:client/data'; + +console.log(usePageData()); export function DocLayout() { return ( diff --git a/src/client/type.d.ts b/src/client/type.d.ts index 361eafd6..a900ad7e 100644 --- a/src/client/type.d.ts +++ b/src/client/type.d.ts @@ -10,10 +10,15 @@ declare module 'island:islands' { export default Record>; } +declare module 'island:page-data' { + export default any; +} + declare module 'island:client*' { import { ComponentType } from 'react'; export const Content: ComponentType; + export const usePageData: () => any; } declare module 'virtual:routes' { diff --git a/src/node/config.ts b/src/node/config.ts index dd0422c2..82c6a667 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -92,3 +92,9 @@ export async function resolveConfig( return siteConfig; } + +export function defineConfig( + config: UserConfig +) { + return config; +} diff --git a/src/node/index.ts b/src/node/index.ts new file mode 100644 index 00000000..b8bb5aca --- /dev/null +++ b/src/node/index.ts @@ -0,0 +1 @@ +export { defineConfig } from './config'; diff --git a/src/node/plugin-island/index.ts b/src/node/plugin-island/index.ts index c0575be7..5e23a811 100644 --- a/src/node/plugin-island/index.ts +++ b/src/node/plugin-island/index.ts @@ -12,14 +12,19 @@ import fs from 'fs-extra'; import { join } from 'path'; import { SiteConfig } from '../../shared/types'; +export const PAGE_DATA_ID = 'island:page-data'; + /** - * The plugin for island framework + * The plugin for island framework: + * 1. Handle module alias + * 2. Response page data + * 3. Generate html template for development */ export function pluginIsland(config: SiteConfig): Plugin { + const { pageData } = config; return { name: 'island:vite-plugin-internal', config(c) { - console.log(join(c.root!, ROUTE_PATH)); return { resolve: { alias: { @@ -36,7 +41,16 @@ export function pluginIsland(config: SiteConfig): Plugin { } }; }, - + resolveId(id) { + if (id === PAGE_DATA_ID) { + return '\0' + PAGE_DATA_ID; + } + }, + load(id) { + if (id === '\0' + PAGE_DATA_ID) { + return `export default ${JSON.stringify(pageData)}`; + } + }, transformIndexHtml(html) { if (isProduction()) { return html; diff --git a/src/node/plugin-routes/RouteService.ts b/src/node/plugin-routes/RouteService.ts index c72c8ab5..a0b7003f 100644 --- a/src/node/plugin-routes/RouteService.ts +++ b/src/node/plugin-routes/RouteService.ts @@ -20,9 +20,8 @@ export class RouteService { #routeData: RouteMeta[] = []; constructor( private scanDir: string, - private extensions: string[] - ) // private root: string - {} + private extensions: string[] // private root: string + ) {} async init() { const files = fastGlob.sync(`**/*.{${this.extensions.join(',')}}`, { @@ -48,7 +47,6 @@ export class RouteService { } generateRoutesCode() { - console.log(this.#routeData); return ` ${isProduction() ? '' : `import loadable from '@loadable/component'`}; import React from 'react'; diff --git a/src/node/plugin-svgr/index.ts b/src/node/plugin-svgr/index.ts index 4ff54fe3..8b1614ed 100644 --- a/src/node/plugin-svgr/index.ts +++ b/src/node/plugin-svgr/index.ts @@ -24,7 +24,6 @@ export function pluginSvgr(options: SvgrOptions = {}): Plugin { ); let componentCode = svgrResult; if (defaultExport === 'url') { - // 加上 Vite 默认的 `export default 资源路径` componentCode += code; componentCode = svgrResult.replace( 'export default ReactComponent', diff --git a/src/node/tsconfig.json b/src/node/tsconfig.json index d5118276..7a26b258 100644 --- a/src/node/tsconfig.json +++ b/src/node/tsconfig.json @@ -3,6 +3,7 @@ "compilerOptions": { "target": "ES2022", "module": "commonjs", - "outDir": "../../dist/" + "outDir": "../../dist/node", + "declaration": true } }