Skip to content

Commit

Permalink
fix: a lot of bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Dec 27, 2018
1 parent 19d1426 commit c10a8bd
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 1,917 deletions.
13 changes: 3 additions & 10 deletions core/docz-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@
"@babel/polyfill": "7.2.5",
"@babel/runtime": "^7.2.0",
"@mdx-js/loader": "^0.16.6",
"@mdx-js/mdx": "^0.16.6",
"@mdx-js/mdxast": "^0.16.6",
"@sindresorhus/slugify": "^0.6.0",
"@svgr/webpack": "^4.1.0",
"art-template": "^4.13.2",
"babel-loader": "^8.0.2",
"babel-plugin-export-metadata": "^0.13.4",
"babel-preset-docz": "^0.13.5",
"babel-preset-react-app": "^7.0.0",
"babylon": "^6.18.0",
"cache-loader": "^2.0.0",
"chalk": "^2.4.1",
"chokidar": "^2.0.4",
"common-tags": "^1.8.0",
Expand All @@ -52,15 +48,12 @@
"friendly-errors-webpack-plugin": "^1.7.0",
"fs-extra": "^7.0.1",
"get-pkg-repo": "^4.1.0",
"happypack": "^5.0.0",
"html-minifier": "^3.5.21",
"humanize-string": "^1.0.2",
"koa-range": "^0.3.0",
"load-cfg": "^0.13.3",
"lodash": "^4.17.11",
"mini-html-webpack-plugin": "^0.2.3",
"p-reduce": "^1.0.0",
"poi": "^12.2.7",
"progress-estimator": "^0.2.2",
"react-dev-utils": "^7.0.0",
"react-docgen": "^2.21.0",
Expand All @@ -78,6 +71,7 @@
"signale": "^1.3.0",
"source-map-loader": "^0.2.4",
"terser-webpack-plugin": "^1.2.0",
"thread-loader": "^2.1.1",
"titleize": "^1.0.1",
"url-loader": "^1.1.2",
"webpack": "^4.28.2",
Expand All @@ -89,6 +83,5 @@
"webpackbar": "^3.1.4",
"ws": "^6.1.2",
"yargs": "^12.0.5"
},
"devDependencies": {}
}
}
5 changes: 1 addition & 4 deletions core/docz-core/src/config/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const getBabelConfig = async (
const localBabelRc = load('babel', { presets: [], plugins: [] }, false, true)
const presets = [
[
require.resolve('poi/babel'),
require.resolve('babel-preset-react-app'),
{
flow: !args.typescript,
typescript: args.typescript,
Expand All @@ -38,10 +38,7 @@ export const getBabelConfig = async (
? null
: getCacheIdentifier(isProd ? 'production' : isDev && 'development', [
'docz',
'docz-theme-default',
'docz-utils',
'docz-core',
'babel-preset-docz',
]),
compact: isProd,
plugins: [require.resolve('babel-plugin-export-metadata')].concat(
Expand Down
15 changes: 3 additions & 12 deletions core/docz-core/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
declare module '@mdx-js/mdx'
declare module '@mdx-js/mdxast'
declare module '@sindresorhus/slugify'
declare module 'art-template'
declare module 'babel-traverse'
declare module 'babylon'
declare module 'common-tags'
declare module 'detect-port'
declare module 'env-dot-prop'
declare module 'find-up'
declare module 'friendly-errors-webpack-plugin'
declare module 'get-pkg-repo'
declare module 'happypack'
declare module 'hast-util-to-string'
declare module 'html-minifer'
declare module 'humanize-string'
declare module 'mini-html-webpack-plugin'
declare module 'p-reduce'
declare module 'progress-estimator'
declare module 'react-docgen'
declare module 'react-docgen-external-proptypes-handler'
declare module 'react-docgen-imported-proptype-handler'
declare module 'react-dev-utils/errorOverlayMiddleware'
declare module 'react-dev-utils/evalSourceMapMiddleware'
declare module 'react-dev-utils/FileSizeReporter'
Expand All @@ -28,15 +20,14 @@ declare module 'react-dev-utils/getCacheIdentifier'
declare module 'react-dev-utils/ignoredFiles'
declare module 'react-dev-utils/printBuildError'
declare module 'react-dev-utils/WebpackDevServerUtils'
declare module 'rehype-autolink-headings'
declare module 'react-docgen-external-proptypes-handler'
declare module 'react-docgen-imported-proptype-handler'
declare module 'react-docgen'
declare module 'rehype-slug'
declare module 'remark-frontmatter'
declare module 'remark-parse/lib/block-elements.json'
declare module 'signale'
declare module 'strip-indent'
declare module 'terser-webpack-plugin'
declare module 'titleize'
declare module 'unist-util-remove'
declare module 'webpack-bundle-analyzer'
declare module 'webpack-manifest-plugin'
declare module 'webpackbar'
7 changes: 3 additions & 4 deletions core/docz-core/src/webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,13 @@ export const createConfig = (args: Args, env: Env) => async (
* loaders
*/

config.when(args.sourcemaps, cfg => loaders.sourceMaps(cfg, args))
loaders.js(config, args)
loaders.mdx(config, args)
config.when(args.sourcemaps, cfg => loaders.sourceMaps(cfg))
loaders.js(config, args, babelrc)
loaders.mdx(config, args, babelrc)
loaders.images(config)
loaders.svg(config)
loaders.media(config)
loaders.fonts(config)
loaders.setupHappypack(config, args, babelrc)

/**
* plugins
Expand Down
65 changes: 22 additions & 43 deletions core/docz-core/src/webpack/loaders.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
import * as path from 'path'
import HappyPack from 'happypack'

import Config from 'webpack-chain'
import { Config as Args } from '../commands/args'

import * as paths from '../config/paths'
import * as mdxConfig from '../config/mdx'
import { Config as Args } from '../commands/args'
import { BabelRC } from '../config/babel'

const outsideNodeModules = (filepath: string) => !/node_modules/.test(filepath)
const excludeNodeModules = (filepath: string) => /node_modules/.test(filepath)

export const sourceMaps = (config: Config, args: Args) => {
const srcPath = path.resolve(paths.root, args.src)

export const sourceMaps = (config: Config) => {
config.module
.rule('sourcemaps')
.test(/\.(js|mjs|jsx|ts|tsx|md|mdx)$/)
.include.add(srcPath)
.add(paths.root)
.add(paths.docz)
.end()
.exclude.add(excludeNodeModules)
.include.add(outsideNodeModules)
.end()
.use('sourcemaps')
.loader(require.resolve('source-map-loader'))
.end()
.enforce('pre')
}

export const js = (config: Config, args: Args) => {
export const js = (config: Config, args: Args, babelrc: BabelRC) => {
const srcPath = path.resolve(paths.root, args.src)

config.module
.rule('js')
.test(/\.(js|mjs|jsx|ts|tsx)$/)
.include.add(srcPath)
.add(paths.root)
.add(paths.docz)
.end()
.exclude.add(excludeNodeModules)
.end()
.use('happypack-jsx')
.loader('happypack/loader?id=jsx')
.use('thread-loader')
.loader(require.resolve('thread-loader'))
.end()
.use('babel-loader')
.loader(require.resolve('babel-loader'))
.options(babelrc)
.end()
}

export const mdx = (config: Config, args: Args) => {
export const mdx = (config: Config, args: Args, babelrc: BabelRC) => {
const { mdPlugins, hastPlugins } = args
const srcPath = path.resolve(paths.root, args.src)

config.module
.rule('mdx')
.test(/\.(md|markdown|mdx)$/)
.include.add(srcPath)
.add(paths.root)
.add(paths.docz)
.end()
.exclude.add(excludeNodeModules)
.end()
.use('happypack-jsx')
.loader('happypack/loader?id=jsx')
.use('thread-loader')
.loader(require.resolve('thread-loader'))
.end()
.use('babel-loader')
.loader(require.resolve('babel-loader'))
.options(babelrc)
.end()
.use('mdx-loader')
.loader(require.resolve('@mdx-js/loader'))
Expand All @@ -66,31 +70,6 @@ export const mdx = (config: Config, args: Args) => {
})
}

export const setupHappypack = (config: Config, args: Args, babelrc: any) => {
const happyThreadPool = HappyPack.ThreadPool({ size: 6 })
const loaders = [
!args.debug && {
loader: require.resolve('cache-loader'),
options: {
cacheDirectory: paths.cache,
},
},
{
loader: require.resolve('babel-loader'),
options: babelrc,
},
]

config.plugin('happypack-jsx').use(HappyPack, [
{
id: 'jsx',
verbose: args.debug,
threadPool: happyThreadPool,
loaders: loaders.filter(Boolean),
},
])
}

const INLINE_LIMIT = 10000

export const images = (config: Config) => {
Expand Down
4 changes: 0 additions & 4 deletions core/docz/src/components/DataServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ export class DataServer extends Component<DataServerProps> {
if (this.socket) this.setupWebsockets(this.socket)
}

public componentWillUnmount(): void {
if (this.socket) this.socket.close()
}

public render(): ReactNode {
return this.props.children
}
Expand Down
76 changes: 0 additions & 76 deletions core/docz/src/components/ErrorBoundary.tsx

This file was deleted.

1 change: 1 addition & 0 deletions core/docz/src/state.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// tslint:disable-next-line
import { create } from './utils/createState'

export interface Heading {
Expand Down
17 changes: 7 additions & 10 deletions core/docz/src/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { HashRouter, BrowserRouter } from 'react-router-dom'
import { StaticRouterProps } from 'react-router'

import { state, ThemeConfig, TransformFn } from './state'
import { ErrorBoundary } from './components/ErrorBoundary'
import { DataServer } from './components/DataServer'
import { ScrollToTop } from './utils/ScrollToTop'

Expand Down Expand Up @@ -48,15 +47,13 @@ export function theme(
)

return (
<ErrorBoundary>
<state.Provider initial={{ ...db, themeConfig, transform }}>
<DataServer websocketUrl={DOCZ_WEBSOCKET_URL}>
<Router basename={DOCZ_BASE_URL}>
<ScrollToTop>{wrapped}</ScrollToTop>
</Router>
</DataServer>
</state.Provider>
</ErrorBoundary>
<state.Provider initial={{ ...db, themeConfig, transform }}>
<DataServer websocketUrl={DOCZ_WEBSOCKET_URL}>
<Router basename={DOCZ_BASE_URL}>
<ScrollToTop>{wrapped}</ScrollToTop>
</Router>
</DataServer>
</state.Provider>
)
}

Expand Down
4 changes: 2 additions & 2 deletions core/docz/src/utils/createState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export interface State<T> {
Provider: React.ComponentType<ProviderProps<T>>
}

const subject = makeSubject()
export function create<T = any>(initial: T = {} as T): State<T> {
const subject = makeSubject()
const { Provider, Consumer }: any = createContext<T>(initial)
Consumer.displayName = 'StateConsumer'

Expand All @@ -29,7 +29,7 @@ export function create<T = any>(initial: T = {} as T): State<T> {
public static displayName = 'StateProvider'
public state: T = this.props.initial || initial
public componentDidMount(): void {
observe((v: T) => this.setState(v))(subject)
observe((fn: T) => this.setState(fn))(subject)
}
public componentWillUnmount(): void {
subject(2)
Expand Down
Loading

0 comments on commit c10a8bd

Please sign in to comment.