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

Commit 4157e05

Browse files
committed
fix(docz-core): use webpack-dev-server instead of webpack-serve
1 parent 6ecd2ae commit 4157e05

File tree

27 files changed

+724
-955
lines changed

27 files changed

+724
-955
lines changed

examples/typescript/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
"outDir": "dist",
55
"rootDir": "src",
66
"declaration": true,
7-
"types": ["node"],
8-
"typeRoots": ["node_modules/@types"]
7+
"typeRoots": ["../../node_modules/@types", "node_modules/@types"]
98
},
109
"include": ["src/**/*"],
1110
"exclude": ["node_modules/**"]

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@
3636
"@commitlint/config-conventional": "^7.1.2",
3737
"@commitlint/config-lerna-scopes": "^7.2.1",
3838
"@types/chokidar": "^1.7.5",
39-
"@types/connect-history-api-fallback": "^1.3.1",
4039
"@types/del": "^3.0.1",
4140
"@types/find-up": "^2.1.1",
4241
"@types/fs-extra": "^5.0.4",
4342
"@types/html-webpack-plugin": "^3.2.0",
44-
"@types/koa": "^2.0.47",
4543
"@types/lodash": "^4.14.119",
4644
"@types/lodash.flatten": "^4.4.4",
4745
"@types/lodash.flattendepth": "^4.7.4",
@@ -56,6 +54,7 @@
5654
"@types/resolve": "^0.0.8",
5755
"@types/webpack": "^4.4.21",
5856
"@types/webpack-chain": "^5.0.0",
57+
"@types/webpack-dev-server": "^3.1.1",
5958
"@types/ws": "^6.0.1",
6059
"@types/yargs": "^12.0.1",
6160
"all-contributors-cli": "^5.4.1",

packages/babel-preset-docz/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
"outDir": "dist",
55
"rootDir": "src",
66
"declaration": true,
7-
"types": ["node"],
8-
"typeRoots": ["node_modules/@types", "src/types"]
7+
"typeRoots": ["../../node_modules/@types", "node_modules/@types", "src/types"]
98
},
109
"include": ["src/**/*"],
1110
"exclude": ["node_modules/**"]

packages/docz-core/librc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ const copy = (files, dest) => ({
1111
})
1212

1313
module.exports = {
14+
plugins: [copy('templates/*.{js,html,json}', 'dist/templates')],
1415
external: Object.keys(pkg.dependencies).concat([
1516
'crypto',
17+
'react-dev-utils/errorOverlayMiddleware',
18+
'react-dev-utils/evalSourceMapMiddleware',
1619
'react-dev-utils/FileSizeReporter',
1720
'react-dev-utils/formatWebpackMessages',
21+
'react-dev-utils/ignoredFiles',
1822
'react-dev-utils/printBuildError',
23+
'react-dev-utils/WebpackDevServerUtils',
1924
'react-dom/server',
2025
]),
21-
plugins: [copy('templates/*.{js,html,json}', 'dist/templates')],
2226
}

packages/docz-core/package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"chalk": "^2.4.1",
3939
"chokidar": "^2.0.4",
4040
"common-tags": "^1.8.0",
41-
"connect-history-api-fallback": "^1.5.0",
4241
"cpy": "^7.0.1",
4342
"deepmerge": "^3.0.0",
4443
"detect-port": "^1.3.0",
@@ -55,11 +54,7 @@
5554
"happypack": "^5.0.0",
5655
"html-minifier": "^3.5.21",
5756
"humanize-string": "^1.0.2",
58-
"koa": "^2.6.2",
59-
"koa-connect": "^2.0.1",
60-
"koa-mount": "^4.0.0",
6157
"koa-range": "^0.3.0",
62-
"koa-static": "^5.0.0",
6358
"load-cfg": "^0.12.16",
6459
"lodash.get": "^4.4.2",
6560
"mini-html-webpack-plugin": "^0.2.3",
@@ -81,10 +76,9 @@
8176
"webpack": "^4.27.1",
8277
"webpack-bundle-analyzer": "^3.0.3",
8378
"webpack-chain": "^5.0.1",
79+
"webpack-dev-server": "^3.1.10",
8480
"webpack-hot-client": "^4.1.1",
8581
"webpack-manifest-plugin": "^2.0.4",
86-
"webpack-serve": "^2.0.3",
87-
"webpack-serve-overlay": "^0.3.0",
8882
"webpackbar": "^3.1.4",
8983
"ws": "^6.1.2",
9084
"yargs": "^12.0.5"

packages/docz-core/src/Bundler.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
import * as path from 'path'
22
import logger from 'signale'
3+
import WebpackDevServer from 'webpack-dev-server'
34

45
import { Plugin } from './Plugin'
56

67
import { Config as Args, Env } from './commands/args'
78
import { getBabelConfig, BabelRC } from './utils/babel-config'
89
import * as paths from './config/paths'
910

10-
export interface Server {
11-
app: any
12-
on: (event: string, cb: (server: any) => void) => void
13-
close: () => void
14-
}
15-
1611
export interface ServerHooks {
12+
onPreCreateApp<A>(app: A): void
1713
onCreateApp<A>(app: A): void
1814
OnServerListening<S>(server: S): void
1915
}
2016

2117
export interface BundlerServer {
22-
start(): Promise<Server>
18+
start(): Promise<WebpackDevServer>
2319
}
2420

2521
export type ConfigFn<C> = (babelrc: BabelRC) => Promise<C>
2622
export type BuildFn<C> = (config: C, dist: string, publicDir: string) => void
27-
28-
export type ServerFnReturn = BundlerServer | Promise<BundlerServer>
29-
export type ServerFn<C> = (config: C, hooks: ServerHooks) => ServerFnReturn
23+
export type ServerFn<C> = (
24+
config: C,
25+
hooks: ServerHooks
26+
) => BundlerServer | Promise<BundlerServer>
3027

3128
export interface BundlerConstructor<Config> {
3229
args: Args
@@ -54,16 +51,23 @@ export class Bundler<C = ConfigObj> {
5451
this.builder = build
5552
}
5653

57-
public async getConfig(env: Env): Promise<C> {
54+
public async mountConfig(env: Env): Promise<C> {
55+
const { plugins } = this.args
56+
const isDev = env !== 'production'
57+
const reduce = Plugin.reduceFromPlugins<C>(plugins)
5858
const babelConfig = await getBabelConfig(this.args, env)
59-
const config = this.mountConfig(await this.config(babelConfig), env)
59+
const userConfig = await this.config(babelConfig)
60+
const config = reduce('modifyBundlerConfig', userConfig, isDev, this.args)
6061

61-
return this.args.modifyBundlerConfig(config, !this.isProd(env), this.args)
62+
return this.args.modifyBundlerConfig(config, isDev, this.args)
6263
}
6364

64-
public async createServer(config: C): Promise<BundlerServer> {
65+
public async createApp(config: C): Promise<BundlerServer> {
6566
const run = Plugin.runPluginsMethod(this.args.plugins)
6667
const hooks = {
68+
onPreCreateApp<A>(app: A): void {
69+
run('onPreCreateApp', app)
70+
},
6771
onCreateApp<A>(app: A): void {
6872
run('onCreateApp', app)
6973
},
@@ -90,15 +94,4 @@ export class Bundler<C = ConfigObj> {
9094

9195
await this.builder(config, dist, publicDir)
9296
}
93-
94-
private mountConfig(config: C, env: Env): any {
95-
const { plugins } = this.args
96-
const reduce = Plugin.reduceFromPlugins<C>(plugins)
97-
98-
return reduce('modifyBundlerConfig', config, !this.isProd(env), this.args)
99-
}
100-
101-
private isProd(env: Env): boolean {
102-
return env === 'production'
103-
}
10497
}

packages/docz-core/src/Plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type ModifyBundlerConfig<C = any> = (
1313
) => C
1414
export type ModifyBabelRC = (babelrc: BabelRC, args: Config) => BabelRC
1515
export type ModifyFiles = (files: string[], args: Config) => string[]
16+
export type onPreCreateApp = <A>(app: A) => void
1617
export type onCreateApp = <A>(app: A) => void
1718
export type OnServerListening = <S>(server: S) => void
1819
export type OnPreBuild = (args: Config) => void
@@ -25,6 +26,7 @@ export interface PluginFactory {
2526
modifyBundlerConfig?: ModifyBundlerConfig
2627
modifyBabelRc?: ModifyBabelRC
2728
modifyFiles?: ModifyFiles
29+
onPreCreateApp?: onPreCreateApp
2830
onCreateApp?: onCreateApp
2931
onServerListening?: OnServerListening
3032
onPreBuild?: OnPreBuild
@@ -86,6 +88,7 @@ export class Plugin<C = any> implements PluginFactory {
8688
public readonly modifyBundlerConfig?: ModifyBundlerConfig<C>
8789
public readonly modifyBabelRc?: ModifyBabelRC
8890
public readonly modifyFiles?: ModifyFiles
91+
public readonly onPreCreateApp?: onPreCreateApp
8992
public readonly onCreateApp?: onCreateApp
9093
public readonly onServerListening?: OnServerListening
9194
public readonly onPreBuild?: OnPreBuild
@@ -98,6 +101,7 @@ export class Plugin<C = any> implements PluginFactory {
98101
this.modifyBundlerConfig = p.modifyBundlerConfig
99102
this.modifyBabelRc = p.modifyBabelRc
100103
this.modifyFiles = p.modifyFiles
104+
this.onPreCreateApp = p.onPreCreateApp
101105
this.onCreateApp = p.onCreateApp
102106
this.onServerListening = p.onServerListening
103107
this.onPreBuild = p.onPreBuild

packages/docz-core/src/commands/args.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ export interface Argv {
6464
port: number
6565
websocketPort: number
6666
websocketHost: string
67-
hotPort: number
68-
hotHost: string
6967
native: boolean
7068
codeSandbox: boolean
7169
/* template args */
@@ -179,14 +177,6 @@ export const args = (env: Env) => (yargs: any) => {
179177
type: 'number',
180178
default: getEnv('docz.port', 3000),
181179
})
182-
yargs.positional('hotHost', {
183-
type: 'string',
184-
default: getEnv('docz.hot.host', '127.0.0.1'),
185-
})
186-
yargs.positional('hotPort', {
187-
type: 'number',
188-
default: getEnv('docz.hot.port', 60757),
189-
})
190180
yargs.positional('websocketHost', {
191181
type: 'string',
192182
default: getEnv('docz.websocket.host', '127.0.0.1'),

packages/docz-core/src/commands/build.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const build = async (args: Config) => {
1515
const entries = new Entries(config)
1616

1717
const bundler = webpack(config, env)
18+
const bundlerConfig = await bundler.mountConfig(env)
1819
const run = Plugin.runPluginsMethod(config.plugins)
1920
const dataServer = new DataServer()
2021

@@ -25,7 +26,7 @@ export const build = async (args: Config) => {
2526
await dataServer.init()
2627

2728
await run('onPreBuild', config)
28-
await bundler.build(await bundler.getConfig(env))
29+
await bundler.build(bundlerConfig)
2930
await run('onPostBuild', config)
3031
await dataServer.close()
3132
} catch (err) {

packages/docz-core/src/commands/dev.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,13 @@ export const dev = async (args: Config) => {
1515
const env = envDotProp.get('node.env')
1616
const config = await loadConfig(args)
1717
const port = await detectPort(config.port)
18-
const hotPort = await detectPort(config.hotPort)
1918
const websocketPort = await detectPort(config.websocketPort)
20-
21-
envDotProp.set(
22-
'webpack.server.overlay.ws.url',
23-
`ws://${config.hotHost}:${hotPort}`
24-
)
25-
26-
const newConfig = { ...config, websocketPort, hotPort, port }
19+
const newConfig = { ...config, websocketPort, port }
2720
const bundler = webpack(newConfig, env)
2821
const entries = new Entries(config)
2922

30-
const bundlerConfig = await bundler.getConfig(env)
31-
const server = await bundler.createServer(bundlerConfig)
23+
const bundlerConfig = await bundler.mountConfig(env)
24+
const app = await bundler.createApp(bundlerConfig)
3225

3326
try {
3427
await Entries.writeApp(newConfig, true)
@@ -37,12 +30,8 @@ export const dev = async (args: Config) => {
3730
process.exit(1)
3831
}
3932

40-
const instance = await server.start()
41-
const dataServer = new DataServer(
42-
instance.app.server,
43-
websocketPort,
44-
config.websocketHost
45-
)
33+
const server = await app.start()
34+
const dataServer = new DataServer(server, websocketPort, config.websocketHost)
4635

4736
dataServer.register([
4837
states.config(newConfig),
@@ -60,7 +49,7 @@ export const dev = async (args: Config) => {
6049
const signals: any = ['SIGINT', 'SIGTERM']
6150
for (const sig of signals) {
6251
process.on(sig, async () => {
63-
instance.close()
52+
server.close()
6453
process.exit()
6554
})
6655
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ export const getClientEnvironment = (publicUrl: string) => {
7777
// only be used as an escape hatch. Normally you would put images into the `src`
7878
// and `import` them in code to get their
7979
PUBLIC_URL: publicUrl,
80-
WEBPACK_SERVE_OVERLAY_WS_URL: envDotProp.get(
81-
'webpack.server.overlay.ws.url'
82-
),
8380
}
8481
)
8582
const stringified = {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface Paths {
3030
appPublic: string
3131
appNodeModules: string
3232
appPackageJson: string
33+
appYarnLock: string
3334
ownNodeModules: string
3435

3536
getDist: (dest: string) => string
@@ -53,6 +54,7 @@ export const cache = path.resolve(docz, 'cache/')
5354
export const appPublic = path.resolve(docz, 'public/')
5455
export const appNodeModules = resolveApp('node_modules')
5556
export const appPackageJson = resolveApp('package.json')
57+
export const appYarnLock = resolveOwn('yarn.lock')
5658
export const ownNodeModules = resolveOwn('node_modules')
5759

5860
export const getDist = (dest: string) => path.join(root, dest)

packages/docz-core/src/types.d.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
declare module '@mdx-js/mdxast'
21
declare module '@mdx-js/mdx'
2+
declare module '@mdx-js/mdxast'
33
declare module '@sindresorhus/slugify'
4-
declare module 'babylon'
4+
declare module 'art-template'
55
declare module 'babel-traverse'
6-
declare module 'env-dot-prop'
6+
declare module 'babylon'
77
declare module 'chokidar'
8+
declare module 'common-tags'
9+
declare module 'detect-port'
10+
declare module 'env-dot-prop'
811
declare module 'find-up'
9-
declare module 'humanize-string'
12+
declare module 'friendly-errors-webpack-plugin'
1013
declare module 'get-pkg-repo'
11-
declare module 'titleize'
12-
declare module 'strip-indent'
13-
declare module 'unist-util-remove'
14+
declare module 'happypack'
1415
declare module 'hast-util-to-string'
1516
declare module 'html-minifer'
16-
declare module 'mini-html-webpack-plugin'
17-
declare module 'happypack'
18-
declare module 'common-tags'
19-
declare module 'remark-frontmatter'
20-
declare module 'remark-parse/lib/block-elements.json'
21-
declare module 'rehype-slug'
22-
declare module 'rehype-autolink-headings'
23-
declare module 'art-template'
24-
declare module 'friendly-errors-webpack-plugin'
25-
declare module 'webpack-manifest-plugin'
26-
declare module 'terser-webpack-plugin'
17+
declare module 'humanize-string'
2718
declare module 'lodash.get'
28-
declare module 'signale'
29-
declare module 'webpackbar'
30-
declare module 'webpack-serve'
31-
declare module 'koa-mount'
32-
declare module 'koa-range'
33-
declare module 'koa-connect'
34-
declare module 'koa-static'
35-
declare module 'detect-port'
19+
declare module 'mini-html-webpack-plugin'
20+
declare module 'p-reduce'
21+
declare module 'react-dev-utils/errorOverlayMiddleware'
22+
declare module 'react-dev-utils/evalSourceMapMiddleware'
3623
declare module 'react-dev-utils/FileSizeReporter'
3724
declare module 'react-dev-utils/formatWebpackMessages'
25+
declare module 'react-dev-utils/ignoredFiles'
3826
declare module 'react-dev-utils/printBuildError'
27+
declare module 'react-dev-utils/WebpackDevServerUtils'
28+
declare module 'rehype-autolink-headings'
29+
declare module 'rehype-slug'
30+
declare module 'remark-frontmatter'
31+
declare module 'remark-parse/lib/block-elements.json'
32+
declare module 'signale'
33+
declare module 'strip-indent'
34+
declare module 'terser-webpack-plugin'
35+
declare module 'titleize'
36+
declare module 'unist-util-remove'
3937
declare module 'webpack-bundle-analyzer'
40-
declare module 'p-reduce'
38+
declare module 'webpack-manifest-plugin'
39+
declare module 'webpackbar'

0 commit comments

Comments
 (0)