Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 70d40cc

Browse files
authored
feat: add initial gatsby integration (#630)
* chore(docz-example-gatsby): add initial structure * fix(docz-example-gatsby): entry find on layout * chore: add Link component as prop of theme * chore: enable eslint on vscode config * chore: update dependencies * chore: update dependencies * fix(docz-core): correct websocket url on template * chore(docz-core): change entries data format * chore: some adjusts to fit with gatsby * feat(docz-example-gatsby): add gatsby poc * feat(gatsby-theme-docz): add initial version * fix(gatsby-theme-docz): remove build script * fix(gatsby-theme-docz): add plugin to compile theme files * chore: update dependencies * fix(docz-theme-default): use tippy for tooltip * chore: update dependencies * fix(docz-theme-default): add hash on menu heading * fix(docz): add shouldComponentUpdate for Theme * fix(gatsby-theme-docz): json parse database * chore(gatsby-theme-docz): remove some dependency * fix(docz-theme-default): extend sidebar to the bottom of the page (#609) * fix(docz-core): public path
1 parent 2d63e83 commit 70d40cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+6334
-2259
lines changed

.vscode/launch.json

+26-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version": "0.2.0",
33
"configurations": [
44
{
5-
"name": "docz dev",
5+
"name": "Docz dev",
66
"type": "node",
77
"request": "launch",
88
"protocol": "inspector",
@@ -14,7 +14,7 @@
1414
"sourceMaps": false
1515
},
1616
{
17-
"name": "docz build",
17+
"name": "Docz build",
1818
"type": "node",
1919
"request": "launch",
2020
"protocol": "inspector",
@@ -23,6 +23,30 @@
2323
"args": ["build"],
2424
"stopOnEntry": false,
2525
"sourceMaps": false
26+
},
27+
{
28+
"name": "Gatsby develop",
29+
"type": "node",
30+
"request": "launch",
31+
"protocol": "inspector",
32+
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby",
33+
"cwd": "${workspaceFolder}/examples/gatsby",
34+
"args": ["develop"],
35+
"stopOnEntry": false,
36+
"runtimeArgs": ["--nolazy"],
37+
"sourceMaps": false
38+
},
39+
{
40+
"name": "Gatsby build",
41+
"type": "node",
42+
"request": "launch",
43+
"protocol": "inspector",
44+
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby",
45+
"cwd": "${workspaceFolder}/examples/gatsby",
46+
"args": ["build"],
47+
"stopOnEntry": false,
48+
"runtimeArgs": ["--nolazy"],
49+
"sourceMaps": false
2650
}
2751
]
2852
}

.vscode/settings.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"eslint.enable": false,
32
"javascript.validate.enable": false,
43
"typescript.tsdk": "node_modules/typescript/lib"
54
}

core/docz-core/package.json

+21-20
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
"@babel/core": "7.2.2",
2424
"@babel/polyfill": "7.2.5",
2525
"@babel/runtime": "^7.3.1",
26-
"@mdx-js/loader": "^0.16.6",
26+
"@mdx-js/loader": "^0.17.0",
2727
"@sindresorhus/slugify": "^0.8.0",
2828
"@svgr/webpack": "^4.1.0",
2929
"babel-loader": "^8.0.5",
3030
"babel-plugin-export-metadata": "^0.13.4",
31-
"babel-plugin-named-asset-import": "^0.3.0",
32-
"babel-preset-react-app": "^7.0.0",
31+
"babel-plugin-named-asset-import": "^0.3.1",
32+
"babel-preset-react-app": "^7.0.1",
3333
"cache-loader": "^2.0.1",
3434
"chalk": "^2.4.2",
35-
"chokidar": "^2.0.4",
35+
"chokidar": "^2.1.1",
3636
"common-tags": "^1.8.0",
3737
"deepmerge": "^3.1.0",
3838
"detect-port": "^1.3.0",
@@ -49,19 +49,20 @@
4949
"get-pkg-repo": "^4.1.0",
5050
"html-minifier": "^3.5.21",
5151
"humanize-string": "^1.0.2",
52+
"immer": "^2.0.0",
5253
"load-cfg": "^0.13.3",
5354
"lodash": "^4.17.11",
5455
"mini-html-webpack-plugin": "^0.2.3",
5556
"p-reduce": "^1.0.0",
5657
"pretty-error": "^2.1.1",
57-
"react-dev-utils": "^7.0.1",
58-
"react-docgen": "^2.21.0",
58+
"react-dev-utils": "^7.0.3",
59+
"react-docgen": "^3.0.0",
5960
"react-docgen-actual-name-handler": "0.13.5",
6061
"react-docgen-external-proptypes-handler": "^1.0.2",
6162
"react-docgen-imported-proptype-handler": "^1.0.4",
6263
"react-docgen-typescript": "^1.12.3",
63-
"react-hot-loader": "^4.6.3",
64-
"recast": "^0.16.2",
64+
"react-hot-loader": "^4.6.5",
65+
"recast": "^0.17.3",
6566
"rehype-docz": "^0.13.5",
6667
"rehype-slug": "^2.0.2",
6768
"remark-docz": "^0.13.3",
@@ -70,32 +71,32 @@
7071
"resolve": "^1.10.0",
7172
"signale": "^1.3.0",
7273
"source-map-loader": "^0.2.4",
73-
"terser-webpack-plugin": "^1.2.1",
74-
"thread-loader": "^2.1.1",
74+
"terser-webpack-plugin": "^1.2.2",
75+
"thread-loader": "^2.1.2",
7576
"titleize": "^1.0.1",
76-
"typescript": "^3.2.4",
77+
"typescript": "^3.3.3",
7778
"url-loader": "^1.1.2",
78-
"webpack": "^4.29.0",
79-
"webpack-bundle-analyzer": "^3.0.3",
80-
"webpack-chain": "^5.1.0",
79+
"webpack": "^4.29.3",
80+
"webpack-bundle-analyzer": "^3.0.4",
81+
"webpack-chain": "^5.2.0",
8182
"webpack-dev-server": "^3.1.14",
8283
"webpack-hot-client": "^4.1.1",
8384
"webpack-manifest-plugin": "^2.0.4",
8485
"webpackbar": "^3.1.5",
85-
"ws": "^6.1.2",
86-
"yargs": "^12.0.5"
86+
"ws": "^6.1.3",
87+
"yargs": "^13.1.0"
8788
},
8889
"devDependencies": {
8990
"@types/chokidar": "^1.7.5",
90-
"@types/express": "^4.16.0",
91+
"@types/express": "^4.16.1",
9192
"@types/html-minifier": "^3.5.2",
9293
"@types/p-reduce": "^1.0.0",
9394
"@types/resolve": "^0.0.8",
94-
"@types/webpack": "^4.4.23",
95+
"@types/webpack": "^4.4.24",
9596
"@types/webpack-chain": "^5.0.0",
96-
"@types/webpack-dev-server": "^3.1.1",
97+
"@types/webpack-dev-server": "^3.1.2",
9798
"@types/ws": "^6.0.1",
98-
"@types/yargs": "^12.0.5",
99+
"@types/yargs": "^12.0.8",
99100
"cpy": "^7.0.1"
100101
}
101102
}

core/docz-core/src/bundler/devserver.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import { ServerHooks } from '../lib/Bundler'
1010

1111
export const devServerConfig = (hooks: ServerHooks, args: Args) => {
1212
const srcPath = path.resolve(paths.root, args.src)
13-
const publicDirPath = path.resolve(paths.root, args.public)
14-
const publicDir = path.join(paths.root, publicDirPath)
13+
const publicDir = path.resolve(paths.root, args.public)
1514
const nonExistentDir = path.resolve(__dirname, 'non-existent')
1615

1716
return {

core/docz-core/src/bundler/plugins.ts

-3
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,12 @@ export const injections = (config: Config, args: Args, env: Env) => {
5454
const { stringify } = JSON
5555
const base = paths.servedPath(args.base)
5656
const plugin = require('webpack/lib/DefinePlugin')
57-
const websocketUrl = `ws://${args.websocketHost}:${args.websocketPort}`
58-
const isDev = env !== 'production'
5957

6058
config.plugin('injections').use(plugin, [
6159
{
6260
...getClientEnvironment(base).stringified,
6361
NODE_ENV: stringify(env),
6462
DOCZ_BASE_URL: args.hashRouter ? stringify('/') : stringify(base),
65-
DOCZ_WEBSOCKET_URL: stringify(isDev && websocketUrl),
6663
DOCZ_HASH_ROUTER: stringify(args.hashRouter || false),
6764
},
6865
])

core/docz-core/src/config/docz.ts

+37-22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Arguments } from 'yargs'
33
import { omit } from 'lodash/fp'
44
import { load, loadFrom } from 'load-cfg'
55
import detectPort from 'detect-port'
6+
import merge from 'lodash/merge'
67

78
import * as paths from '../config/paths'
89
import { BabelRC } from '../config/babel'
@@ -15,33 +16,47 @@ const htmlContext = {
1516
favicon: 'https://cdn-std.dprcdn.net/files/acc_649651/LUKiMl',
1617
}
1718

18-
export const parseConfig = async (argv: Arguments<Argv>): Promise<Config> => {
19-
const port = await detectPort(argv.port)
20-
const websocketPort = await detectPort(argv.websocketPort)
19+
export const doczRcBaseConfig = {
20+
htmlContext,
21+
themeConfig: {},
22+
docgenConfig: {},
23+
modifyBundlerConfig: (config: any) => config,
24+
modifyBabelRc: (babelrc: BabelRC) => babelrc,
25+
onCreateWebpackChain: () => null,
26+
menu: [],
27+
plugins: [],
28+
mdPlugins: [],
29+
hastPlugins: [],
30+
ignore: [
31+
'readme.md',
32+
'changelog.md',
33+
'code_of_conduct.md',
34+
'contributing.md',
35+
'license.md',
36+
],
37+
}
38+
39+
export const getBaseConfig = (
40+
argv: Arguments<Argv>,
41+
custom?: Partial<Config>
42+
): Config => {
2143
const initial = omit<Arguments<Argv>, any>(toOmit, argv)
44+
const base = { ...initial, ...doczRcBaseConfig }
45+
return merge(base, custom) as Config
46+
}
2247

23-
const defaultConfig: any = {
24-
...initial,
48+
export const parseConfig = async (
49+
argv: Arguments<Argv>,
50+
custom?: Partial<Config>
51+
): Promise<Config> => {
52+
const port = await detectPort(argv.port)
53+
const websocketPort = await detectPort(argv.websocketPort)
54+
const defaultConfig = getBaseConfig(argv, {
2555
port,
2656
websocketPort,
2757
htmlContext,
28-
menu: [],
29-
plugins: [],
30-
mdPlugins: [],
31-
hastPlugins: [],
32-
ignore: [
33-
'readme.md',
34-
'changelog.md',
35-
'code_of_conduct.md',
36-
'contributing.md',
37-
'license.md',
38-
],
39-
themeConfig: {},
40-
docgenConfig: {},
41-
modifyBundlerConfig: (config: any) => config,
42-
modifyBabelRc: (babelrc: BabelRC) => babelrc,
43-
onCreateWebpackChain: () => null,
44-
}
58+
...custom,
59+
})
4560

4661
const config = argv.config
4762
? loadFrom<Config>(path.resolve(argv.config), defaultConfig)

core/docz-core/src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
export { cli } from './cli'
33

44
/** config exports */
5-
export { parseConfig } from './config/docz'
6-
export { Config } from './config/argv'
5+
export { parseConfig, getBaseConfig } from './config/docz'
6+
export { Config, setArgs } from './config/argv'
77
export { BabelRC } from './config/babel'
88

9+
/** states */
10+
import * as states from './states'
11+
export { states }
12+
913
/** lib exports */
1014
export { Plugin, createPlugin } from './lib/Plugin'
1115
export { DataServer } from './lib/DataServer'

core/docz-core/src/lib/DataServer.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as fs from 'fs-extra'
2-
import { isFunction } from 'lodash/fp'
2+
import { get, isFunction } from 'lodash/fp'
33

44
import * as paths from '../config/paths'
55

66
export interface Params {
7-
state: Record<string, any>
7+
getState: () => Record<string, any>
88
setState: (key: string, val: any) => void
99
}
1010

@@ -39,13 +39,14 @@ export class DataServer {
3939

4040
public async start(): Promise<void> {
4141
const setState = (key: string, val: any) => this.setState(key, val)
42+
const getState = () => this.getState()
4243

4344
await Promise.all(
4445
Array.from(this.states).map(async state => {
4546
if (!isFunction(state.start)) return
4647
return state.start({
4748
setState,
48-
state: this.mapToObject(this.state),
49+
getState,
4950
})
5051
})
5152
)
@@ -62,11 +63,18 @@ export class DataServer {
6263
return () => this.listeners.clear()
6364
}
6465

66+
public getState(): Map<string, any> {
67+
return this.mapToObject(this.state)
68+
}
69+
6570
private setState(key: string, val: any): void {
66-
this.state.set(key, val)
71+
const prev = get(key, this.getState())
72+
const next = typeof val === 'function' ? val(prev) : val
73+
74+
this.state.set(key, next)
6775
this.writeDbFile()
6876
this.listeners.forEach(listener => {
69-
listener({ type: `state.${key}`, payload: val })
77+
listener({ type: `state.${key}`, payload: next })
7078
})
7179
}
7280

core/docz-core/src/lib/Entries.ts

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const writeAppFiles = async (config: Config, dev: boolean): Promise<void> => {
4141
theme,
4242
isProd: !dev,
4343
wrapper: config.wrapper,
44-
hashRouter: config.hashRouter,
4544
websocketUrl,
4645
})
4746

core/docz-core/src/states/entries.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import * as path from 'path'
22
import chokidar from 'chokidar'
33
import equal from 'fast-deep-equal'
4+
import { get } from 'lodash/fp'
45
import { touch, compiled } from 'docz-utils/lib/fs'
56

7+
import { mapToArray } from './props'
68
import { Params, State } from '../lib/DataServer'
79
import { Entries, EntryMap, fromTemplates } from '../lib/Entries'
810
import { Config } from '../config/argv'
@@ -16,12 +18,12 @@ const writeImports = async (map: EntryMap): Promise<void> => {
1618
}
1719

1820
const updateEntries = (entries: Entries) => async (p: Params) => {
19-
const old = p.state.entries
21+
const prev = get('entries', p.getState())
2022
const map = await entries.get()
2123

22-
if (map && !equal(old, map)) {
24+
if (map && !equal(prev, map)) {
2325
await writeImports(map)
24-
p.setState('entries', map)
26+
p.setState('entries', mapToArray(map))
2527
}
2628
}
2729

0 commit comments

Comments
 (0)