Skip to content

Commit

Permalink
feat: nextjs index pages
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jul 14, 2020
1 parent 77e47fb commit ea97ca0
Show file tree
Hide file tree
Showing 20 changed files with 340 additions and 130 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ dist
.rpt2_cache
*.log
public
.next
.next
out
90 changes: 47 additions & 43 deletions core/loader/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
StoryPackages,
BuildConfiguration,
RunConfiguration,
Pages,
Document,
defDocType,
Pages,
DocType,
docStoryToId,
} from '@component-controls/core';
Expand Down Expand Up @@ -36,8 +36,6 @@ export interface LoadingStore {
stores: (Partial<Pick<LoadingDocStore, 'stories' | 'doc'>> & {
filePath: string;
})[];
getDocs: (type: DocType) => Pages;
getUniquesByField: (field: string) => { [key: string]: number };
}

class Store implements LoadingStore {
Expand All @@ -46,48 +44,54 @@ class Store implements LoadingStore {
packages: LoadingStore['packages'] = {};
config: LoadingStore['config'] = {};
buildConfig: LoadingStore['buildConfig'] = {};
getDocs = (docType: DocType) =>
this.stores
.filter(store => {
if (store?.doc) {
const { type = defDocType } = store.doc;
return type === docType;
}
return false;
})
.map(
store =>
({
...store.doc,
stories:
store.doc && store.stories
? Object.keys(store.stories).map(id =>
//@ts-ignore
docStoryToId(store.doc.title, id),
)
: undefined,
} as Document),
);
getUniquesByField = (field: string) => {
return this.stores.reduce((acc: { [key: string]: number }, store) => {
if (store?.doc) {
//@ts-ignore
const value = store.doc[field];
const values = Array.isArray(value) ? value : [value];
values.forEach(v => {
if (v !== undefined) {
if (typeof acc[v] === 'undefined') {
acc[v] = 0;
}
acc[v] = acc[v] = 1;
}
});
}
return acc;
}, {});
};
}

export const getDocs = (
stores: LoadingStore['stores'],
docType: DocType,
): Pages =>
stores
.filter(store => {
if (store?.doc) {
const { type = defDocType } = store.doc;
return type === docType;
}
return false;
})
.map(
store =>
({
...store.doc,
stories:
store.doc && store.stories
? Object.keys(store.stories).map(id =>
//@ts-ignore
docStoryToId(store.doc.title, id),
)
: undefined,
} as Document),
);
export const getUniquesByField = (
stores: LoadingStore['stores'],
field: string,
): { [key: string]: number } => {
return stores.reduce((acc: { [key: string]: number }, store) => {
if (store?.doc) {
//@ts-ignore
const value = store.doc[field];
const values = Array.isArray(value) ? value : [value];
values.forEach(v => {
if (v !== undefined) {
if (typeof acc[v] === 'undefined') {
acc[v] = 0;
}
acc[v] = acc[v] = 1;
}
});
}
return acc;
}, {});
};
export const store = new Store();

export const reserveStories = (filePaths: string[]) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const withStories = require('@component-controls/nextjs-plugin');
const withStories = require('@component-controls/nextjs-plugin/build');

module.exports = withStories({ configPath: '.config', distDir: 'dist' });
4 changes: 3 additions & 1 deletion examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"license": "MIT",
"scripts": {
"build-sample": "next build",
"build-sample": "next build && next export",
"start": "next -p 9021"
},
"repository": {
Expand All @@ -16,6 +16,8 @@
"dependencies": {
"@component-controls/nextjs-plugin": "^1.8.0",
"@component-controls/pages": "^1.8.0",
"emotion": "^10.0.27",
"emotion-server": "^10.0.27",
"next": "^9.4.4",
"react": "^16.13.1",
"react-dom": "^16.13.1"
Expand Down
50 changes: 50 additions & 0 deletions examples/nextjs/pages/[doctype].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { FC } from 'react';
import { GetStaticProps, GetStaticPaths } from 'next';
import { DocType, defDocType, Pages } from '@component-controls/core';
import { DocumentHomePage } from '@component-controls/app';
import { getDocs } from '@component-controls/loader';
import { Layout } from '@component-controls/nextjs-plugin';

interface PageListProps {
type: DocType;
docId?: string;
}

const DocHomeTemplate: FC<PageListProps> = ({ type = defDocType, docId }) => {
return (
<Layout docId={docId} type={type}>
<DocumentHomePage type={type} />
</Layout>
);
};

export const getStaticPaths: GetStaticPaths = async () => {
const paths: string[] = [];
const store = require('@component-controls/webpack-compile/bundle');
const { pages } = store.buildConfig || {};
if (pages) {
Object.keys(pages).forEach(type => {
const page = pages[type as DocType];
//const docType = type as DocType;
// const docs = getDocs(store.stores, docType);
paths.push(`/${page.basePath}`);
});
}
return { paths, fallback: false };
};

export const getStaticProps: GetStaticProps = async ({ params }) => {
const store = require('@component-controls/webpack-compile/bundle');
const { doctype: basepath } = params as { doctype: string };

const { pages } = store.buildConfig || {};
const doctype = Object.keys(pages).find(key => {
return pages[key].basePath.replace(/\//g, '') === basepath;
}) as DocType;
const docs: Pages = getDocs(store.stores, doctype);
const page = pages[doctype];
const docsPage = docs.find(doc => doc?.route === `/${page.basePath}`);
return { props: { docId: docsPage?.title || null, type: doctype } };
};

export default DocHomeTemplate;
17 changes: 17 additions & 0 deletions examples/nextjs/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import NextApp from 'next/app';
import { CacheProvider } from '@emotion/core';

// Use only { cache } from 'emotion'. Don't use { css }.
import { cache } from 'emotion';

export default class App extends NextApp {
render() {
const { Component, pageProps } = this.props;
return (
<CacheProvider value={cache}>
<Component {...pageProps} />
</CacheProvider>
);
}
}
34 changes: 34 additions & 0 deletions examples/nextjs/pages/_document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import Document, { Head, Main, NextScript } from 'next/document';
import { extractCritical } from 'emotion-server';

export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx);
const styles = extractCritical(initialProps.html);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
<style
data-emotion-css={styles.ids.join(' ')}
dangerouslySetInnerHTML={{ __html: styles.css }}
/>
</>
),
};
}

render() {
return (
<html>
<Head />
<body>
<Main />
<NextScript />
</body>
</html>
);
}
}
30 changes: 30 additions & 0 deletions examples/nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { FC } from 'react';
import { GetStaticProps } from 'next';

import { DocType, defDocType } from '@component-controls/core';
import { DocPage } from '@component-controls/app';
import { Layout } from '@component-controls/nextjs-plugin';
import { LoadingDocStore } from '@component-controls/instrument';

interface PageListProps {
type: DocType;
docId?: string;
}

const HomePage: FC<PageListProps> = ({ type = defDocType, docId }) => {
return (
<Layout docId={docId} type={type}>
<DocPage type={type} />
</Layout>
);
};

export const getStaticProps: GetStaticProps = async () => {
const store = require('@component-controls/webpack-compile/bundle');
const homePage = store.stores.find(
(s: LoadingDocStore) => s.doc?.route === '/',
);
return { props: { docId: homePage?.doc?.title, type: homePage?.doc?.type } };
};

export default HomePage;
26 changes: 13 additions & 13 deletions integrations/gatsby-theme-stories/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
getStoryPath,
DocType,
TabConfiguration,
Pages,
} from '@component-controls/core';
import { getDocs, getUniquesByField } from '@component-controls/loader';

import {
compile,
Expand All @@ -23,7 +25,7 @@ exports.createPages = async (
const { createPage } = actions;
const config: CompileProps = {
webPack: options.webpack,
presets: defaultPresets,
presets: options.presets || defaultPresets,
configPath: options.configPath,
outputFolder:
options.outputFolder || `${path.join(process.cwd(), 'public')}`,
Expand All @@ -39,7 +41,7 @@ exports.createPages = async (
if (!categories.includes(type as DocType)) {
const page = pages[type as DocType];
const docType = type as DocType;
const docs = store.getDocs(docType);
const docs: Pages = getDocs(store.stores, docType);
const tabs: Pick<TabConfiguration, 'route'>[] = page.tabs || [
{ route: undefined },
];
Expand Down Expand Up @@ -86,7 +88,7 @@ exports.createPages = async (
}
});
categories.forEach(catName => {
const cats = store.getUniquesByField(catName);
const cats = getUniquesByField(store.stores, catName);
const catPage = pages[catName as DocType];
const catKeys = Object.keys(cats);
catKeys.forEach(tag => {
Expand All @@ -105,16 +107,14 @@ exports.createPages = async (
},
});
});
if (catKeys.length) {
createPage({
path: `/${catPage.basePath}`,
component: require.resolve(`../src/templates/CategoryList.tsx`),
context: {
type: catName,
docId: null,
},
});
}
createPage({
path: `/${catPage.basePath}`,
component: require.resolve(`../src/templates/DocHome.tsx`),
context: {
type: catName,
docId: null,
},
});
});
}
const homePage = store.stores.find(s => s.doc?.route === '/');
Expand Down
24 changes: 0 additions & 24 deletions integrations/gatsby-theme-stories/src/templates/CategoryList.tsx

This file was deleted.

1 change: 1 addition & 0 deletions integrations/nextjs-plugin/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/build');
3 changes: 2 additions & 1 deletion integrations/nextjs-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"files": [
"dist/",
"package.json",
"README.md"
"README.md",
"build.js"
],
"scripts": {
"build": "yarn cross-env NODE_ENV=production rollup -c",
Expand Down
2 changes: 1 addition & 1 deletion integrations/nextjs-plugin/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config } from '../../rollup-config';

export default config({
input: ['./src/index.ts'],
input: ['./src/index.ts', './src/build.ts'],
});
Loading

0 comments on commit ea97ca0

Please sign in to comment.