Skip to content

Commit

Permalink
feat: support page data virtual module
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Sep 5, 2022
1 parent 64be402 commit 4144cc3
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "island",
"name": "island-ssg",
"version": "1.0.0",
"description": "",
"main": "index.js",
Expand Down
16 changes: 16 additions & 0 deletions playground/.island/config.ts
Original file line number Diff line number Diff line change
@@ -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/' }
]
}
});
5 changes: 5 additions & 0 deletions src/client/app/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import data from 'island:page-data';

export const usePageData = () => {
return data;
};
2 changes: 2 additions & 0 deletions src/client/app/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Content } from './Content';
export { usePageData } from './data';
3 changes: 3 additions & 0 deletions src/client/theme/layout/DocLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
5 changes: 5 additions & 0 deletions src/client/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ declare module 'island:islands' {
export default Record<string, ComponentType<any>>;
}

declare module 'island:page-data' {
export default any;
}

declare module 'island:client*' {
import { ComponentType } from 'react';

export const Content: ComponentType<any>;
export const usePageData: () => any;
}

declare module 'virtual:routes' {
Expand Down
6 changes: 6 additions & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ export async function resolveConfig(

return siteConfig;
}

export function defineConfig<ThemeConfig = any>(
config: UserConfig<ThemeConfig>
) {
return config;
}
1 change: 1 addition & 0 deletions src/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { defineConfig } from './config';
20 changes: 17 additions & 3 deletions src/node/plugin-island/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/node/plugin-routes/RouteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(',')}}`, {
Expand All @@ -48,7 +47,6 @@ export class RouteService {
}

generateRoutesCode() {
console.log(this.#routeData);
return `
${isProduction() ? '' : `import loadable from '@loadable/component'`};
import React from 'react';
Expand Down
1 change: 0 additions & 1 deletion src/node/plugin-svgr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion src/node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"outDir": "../../dist/"
"outDir": "../../dist/node",
"declaration": true
}
}

0 comments on commit 4144cc3

Please sign in to comment.