Skip to content

Commit

Permalink
feat(docz-core): use react-dev-utils create compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 24, 2019
1 parent 74fc451 commit 4d062de
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 44 deletions.
1 change: 0 additions & 1 deletion core/docz-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"fast-glob": "^2.2.6",
"file-loader": "^3.0.1",
"find-up": "^3.0.0",
"friendly-errors-webpack-plugin": "^1.7.0",
"fs-extra": "^7.0.1",
"get-pkg-repo": "^4.1.0",
"html-minifier": "^4.0.0",
Expand Down
5 changes: 1 addition & 4 deletions core/docz-core/src/bundler/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ export const createConfig = (args: Args, env: Env) => async (hooks: Hooks) => {

config.when(debug, cfg => plugins.analyzer(cfg))
config.when(!isProd, cfg => plugins.watchNodeModulesPlugin(cfg))
config.when(!debug && !isProd, cfg => {
plugins.webpackBar(cfg, args)
plugins.friendlyErrors(cfg, args)
})
config.when(!debug && !isProd, cfg => plugins.webpackBar(cfg, args))

/**
* typescript setup
Expand Down
16 changes: 0 additions & 16 deletions core/docz-core/src/bundler/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Config from 'webpack-chain'
import { IgnorePlugin, HotModuleReplacementPlugin } from 'webpack'
import webpackBarPlugin from 'webpackbar'
import friendlyErrorsPlugin from 'friendly-errors-webpack-plugin'
import { minify } from 'html-minifier'
import miniHtmlWebpack from 'mini-html-webpack-plugin'
import manifestPlugin from 'webpack-manifest-plugin'
Expand Down Expand Up @@ -37,21 +36,6 @@ export const analyzer = (config: Config) => {
])
}

export const friendlyErrors = (config: Config, args: Args) => {
const { host, port, clearConsole } = args
const isLocalhost = host === '127.0.0.1' || host === '0.0.0.0'
const hostname = isLocalhost ? 'localhost' : host

config.plugin('friendly-errors').use(friendlyErrorsPlugin, [
{
clearConsole,
compilationSuccessInfo: {
messages: [`Your application is running at http://${hostname}:${port}`],
},
},
])
}

export const injections = (config: Config, args: Args, env: Env) => {
const { stringify } = JSON
const base = paths.servedPath(args.base)
Expand Down
44 changes: 30 additions & 14 deletions core/docz-core/src/bundler/server.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
import chalk from 'chalk'
import * as fs from 'fs'
import logger from 'signale'
import webpack from 'webpack'
import WebpackDevServer from 'webpack-dev-server'
import { Configuration as Config } from 'webpack'
import { createCompiler } from 'react-dev-utils/WebpackDevServerUtils'
import { prepareUrls } from 'react-dev-utils/WebpackDevServerUtils'

import { devServerConfig } from './devserver'
import { Config as Args } from '../config/argv'
import { ServerHooks as Hooks } from '../lib/Bundler'

const createCompiler = (config: Config) =>
new Promise<any>(resolve => {
try {
resolve(require('webpack')(config))
} catch (err) {
logger.fatal(chalk.red('Failed to compile.'))
logger.error(err)
process.exit(1)
}
})
import * as paths from '../config/paths'
const useYarn = fs.existsSync(paths.appYarnLock)

export const server = (args: Args) => async (config: Config, hooks: Hooks) => ({
start: async () => {
const serverConfig: any = devServerConfig(hooks, args)
const compiler = await createCompiler(config)
const devServer = new WebpackDevServer(compiler, serverConfig)
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http'
const appName = require(paths.packageJson).name
const useTypescript = args.typescript
const urls = prepareUrls(protocol, args.host, args.port)

const devSocket = {
warnings: (warnings: any) =>
devServer.sockWrite(devServer.sockets, 'warnings', warnings),
errors: (errors: any) =>
devServer.sockWrite(devServer.sockets, 'errors', errors),
}

const compiler = createCompiler({
appName,
config,
devSocket,
urls,
useYarn,
useTypescript,
webpack,
})

const devServer = new WebpackDevServer(compiler, serverConfig) as any

return devServer.listen(args.port, args.host, err => {
return devServer.listen(args.port, args.host, (err: any) => {
if (err) return logger.fatal(err)
hooks.onServerListening<WebpackDevServer>(devServer)
})
Expand Down
1 change: 1 addition & 0 deletions core/docz-core/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ declare module 'react-dev-utils/getCacheIdentifier'
declare module 'react-dev-utils/ignoredFiles'
declare module 'react-dev-utils/ModuleNotFoundPlugin'
declare module 'react-dev-utils/WatchMissingNodeModulesPlugin'
declare module 'react-dev-utils/WebpackDevServerUtils'
declare module 'react-docgen-external-proptypes-handler'
declare module 'react-docgen'
declare module 'rehype-slug'
Expand Down
9 changes: 0 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7364,15 +7364,6 @@ fresh@0.5.2:
resolved "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7"
integrity sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=

friendly-errors-webpack-plugin@^1.7.0:
version "1.7.0"
resolved "https://registry.npmjs.org/friendly-errors-webpack-plugin/-/friendly-errors-webpack-plugin-1.7.0.tgz#efc86cbb816224565861a1be7a9d84d0aafea136"
integrity sha512-K27M3VK30wVoOarP651zDmb93R9zF28usW4ocaK3mfQeIEI5BPht/EzZs5E8QLLwbLRJQMwscAjDxYPb1FuNiw==
dependencies:
chalk "^1.1.3"
error-stack-parser "^2.0.0"
string-width "^2.0.0"

from2@^2.1.0, from2@^2.1.1:
version "2.3.0"
resolved "https://registry.npmjs.org/from2/-/from2-2.3.0.tgz#8bfb5502bde4a4d36cfdeea007fcca21d7e382af"
Expand Down

0 comments on commit 4d062de

Please sign in to comment.