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

Commit 964832a

Browse files
committed
feat(docz-bundler-webpack): improve webpack config
1 parent 1ceac6c commit 964832a

File tree

10 files changed

+365
-133
lines changed

10 files changed

+365
-133
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages/docz-bundler-webpack/src/config.ts

packages/docz-bundler-webpack/package.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,40 @@
88
"typings": "./dist/index.d.ts",
99
"license": "MIT",
1010
"scripts": {
11-
"clean": "trash dist",
12-
"compile": "tsc -p tsconfig.json",
13-
"dev": "libundler watch --ts --external all",
14-
"build": "libundler build --ts --external all --compress --sourcemap",
15-
"fix": "run-s fix:*",
11+
"dev": "libundler watch --ts -e all",
12+
"build": "libundler build --ts -e all -c -sm",
1613
"fix:prettier": "prettier \"src/**/*.{ts,tsx}\" --write",
17-
"fix:tslint": "tslint --fix --project ."
14+
"fix:tslint": "yarn run tslint --fix",
15+
"tslint": "tslint --project ."
1816
},
1917
"dependencies": {
2018
"@babel/core": "^7.0.0-beta.44",
2119
"@babel/runtime": "^7.0.0-beta.44",
22-
"babel-polyfill": "^7.0.0-beta.3",
2320
"babel-loader": "^8.0.0-beta.1",
21+
"babel-polyfill": "^7.0.0-beta.3",
2422
"babel-preset-react-app": "^4.0.0-next.b2fd8db8",
23+
"express": "^4.16.3",
2524
"deepmerge": "^2.1.0",
25+
"docz-core": "^0.0.1",
26+
"file-loader": "^1.1.11",
2627
"html-webpack-plugin": "^3.2.0",
2728
"load-cfg": "^0.0.1",
28-
"docz-core": "^0.0.1",
2929
"react-dev-utils": "^5.0.1",
3030
"react-hot-loader": "^4.0.1",
3131
"thread-loader": "^1.1.5",
32+
"url-loader": "^1.0.1",
3233
"webpack": "^4.5.0",
34+
"webpack-chain": "^4.6.0",
3335
"webpack-dev-middleware": "^3.1.2",
34-
"webpack-dev-server": "^3.1.3"
36+
"webpack-dev-server": "^3.1.3",
37+
"webpackbar": "^2.6.1"
3538
},
3639
"devDependencies": {
3740
"@types/deepmerge": "^2.1.0",
3841
"@types/html-webpack-plugin": "^2.30.3",
3942
"@types/node": "9.6.4",
4043
"@types/webpack": "^4.1.3",
44+
"@types/webpack-chain": "^4.0.2",
4145
"@types/webpack-dev-server": "^2.9.4"
4246
}
4347
}

packages/docz-bundler-webpack/src/config.dev.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import * as path from 'path'
2+
import * as webpack from 'webpack'
3+
import { Configuration } from 'webpack'
4+
import { ConfigArgs } from 'docz-core'
5+
import { load } from 'load-cfg'
6+
import Webpackbar from 'webpackbar'
7+
import HtmlWebpackPlugin from 'html-webpack-plugin'
8+
import Config from 'webpack-chain'
9+
import merge from 'deepmerge'
10+
11+
const INLINE_LIMIT = 10000
12+
13+
export const createConfig = (args: ConfigArgs) => (): Configuration => {
14+
const { paths, env, debug } = args
15+
16+
const isProd = env === 'production'
17+
const config = new Config()
18+
19+
/**
20+
* general
21+
*/
22+
config.context(paths.root)
23+
config.set('mode', isProd && !debug ? 'production' : 'development')
24+
25+
config.when(debug, cfg => cfg.devtool('source-map'))
26+
config.when(!isProd, cfg => cfg.devtool('cheap-module-eval-source-map'))
27+
28+
config.node.merge({
29+
child_process: 'empty',
30+
dgram: 'empty',
31+
fs: 'empty',
32+
net: 'empty',
33+
tls: 'empty',
34+
})
35+
36+
/**
37+
* output
38+
*/
39+
const outputProd = (output: Config.Output) =>
40+
output
41+
.filename('static/js/[name].[chunkhash:8].js')
42+
.sourceMapFilename('static/js/[name].[chunkhash:8].js.map')
43+
.publicPath(paths.servedPath)
44+
45+
const outputDev = (output: Config.Output) =>
46+
output
47+
.filename('static/js/[name].js')
48+
.sourceMapFilename('static/js/[name].js.map')
49+
.publicPath('/')
50+
51+
config.output
52+
.pathinfo(true)
53+
.path(paths.dist)
54+
.when(isProd, outputProd, outputDev )
55+
.crossOriginLoading('anonymous')
56+
57+
/**
58+
* entries
59+
*/
60+
61+
const addHotClientEntry = (entry: Config.EntryPoint) =>
62+
entry.add(require.resolve('react-dev-utils/webpackHotDevClient'))
63+
64+
config
65+
.entry('app')
66+
.when(!isProd, addHotClientEntry)
67+
.add(require.resolve('babel-polyfill'))
68+
.add(paths.indexJs)
69+
70+
/**
71+
* resolve
72+
*/
73+
74+
config.resolve
75+
.set('symlinks', true)
76+
.alias
77+
.set('@babel/runtime', path.dirname(
78+
require.resolve('@babel/runtime/package.json')
79+
))
80+
81+
config.resolve
82+
.extensions
83+
.merge(['.web.js', '.mjs', '.js', '.json', '.web.jsx', '.jsx'])
84+
.end()
85+
.modules
86+
.add('node_modules')
87+
.add(paths.root)
88+
89+
config.resolveLoader
90+
.set('symlinks', true)
91+
.modules
92+
.add('node_modules')
93+
.add(paths.root)
94+
95+
/**
96+
* loaders
97+
*/
98+
99+
const babelrc = merge(load('babel', null), {
100+
babelrc: false,
101+
cacheDirectory: true,
102+
highlightCode: true,
103+
plugins: [require.resolve('react-hot-loader/babel')],
104+
presets: [require.resolve('babel-preset-react-app')],
105+
})
106+
107+
config.module
108+
.rule('js')
109+
.test(/\.js?x$/)
110+
.include
111+
.add(paths.root)
112+
.add(paths.docz)
113+
.end()
114+
.exclude
115+
.add(/node_modules/)
116+
.end()
117+
.use('thread-loader')
118+
.loader(require.resolve('thread-loader'))
119+
.end()
120+
.use('babel-loader')
121+
.loader(require.resolve('babel-loader'))
122+
.options(babelrc)
123+
124+
config.module
125+
.rule('images')
126+
.test(/\.(png|jpe?g|gif)(\?.*)?$/)
127+
.use('url-loader')
128+
.loader(require.resolve('url-loader'))
129+
.options({
130+
limit: INLINE_LIMIT,
131+
name: `static/img/[name].[hash:8].[ext]`,
132+
})
133+
134+
config.module
135+
.rule('svg')
136+
.test(/\.(svg)(\?.*)?$/)
137+
.use('file-loader')
138+
.loader(require.resolve('file-loader'))
139+
.options({
140+
name: `static/img/[name].[hash:8].[ext]`,
141+
})
142+
143+
config.module
144+
.rule('media')
145+
.test(/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/)
146+
.use('url-loader')
147+
.loader(require.resolve('url-loader'))
148+
.options({
149+
limit: INLINE_LIMIT,
150+
name: `static/media/[name].[hash:8].[ext]`,
151+
})
152+
153+
config.module
154+
.rule('fonts')
155+
.test(/\.(woff2?|eot|ttf|otf)(\?.*)?$/i)
156+
.use('url-loader')
157+
.loader(require.resolve('url-loader'))
158+
.options({
159+
limit: INLINE_LIMIT,
160+
name: `static/fonts/[name].[hash:8].[ext]`,
161+
})
162+
163+
/**
164+
* plugins
165+
*/
166+
167+
config.when(!isProd, cfg =>
168+
cfg
169+
.plugin('hot-module-replacement-plugin')
170+
.use(webpack.HotModuleReplacementPlugin)
171+
)
172+
173+
config
174+
.plugin('named-modules-plugin')
175+
.use(webpack.NamedModulesPlugin)
176+
.end()
177+
.plugin('no-emit-errors')
178+
.use(webpack.NoEmitOnErrorsPlugin)
179+
.end()
180+
.plugin('html-webpack-plugin')
181+
.use(HtmlWebpackPlugin, [{
182+
inject: true,
183+
template: paths.indexHtml,
184+
}])
185+
186+
config.when(!debug, cfg =>
187+
cfg
188+
.plugin('webpackbar')
189+
.use(Webpackbar, [{
190+
color: '#41b883',
191+
compiledIn: false,
192+
name: 'Client',
193+
}])
194+
)
195+
196+
return config.toConfig()
197+
}

packages/docz-bundler-webpack/src/config-devserver.ts renamed to packages/docz-bundler-webpack/src/devserver.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,30 @@ import { ConfigArgs } from 'docz-core'
22
import { Application } from 'express'
33
import errorOverlayMiddleware from 'react-dev-utils/errorOverlayMiddleware'
44

5-
export const PROTOCOL = process.env.HTTPS === 'true' ? 'https' : 'http'
6-
export const HOST = process.env.HOST || '0.0.0.0'
7-
8-
export const devServerConfig = ({ paths }: ConfigArgs) => ({
9-
compress: true,
5+
export const devServerConfig = ({ paths, host, protocol }: ConfigArgs) => ({
6+
host,
7+
before(app: Application): void {
8+
app.use(errorOverlayMiddleware())
9+
},
1010
clientLogLevel: 'none',
11+
compress: true,
1112
contentBase: paths.docz,
12-
watchContentBase: true,
13+
historyApiFallback: {
14+
disableDotRule: true,
15+
},
1316
hot: true,
14-
quiet: true,
17+
https: protocol === 'https',
1518
noInfo: true,
16-
publicPath: '/',
17-
https: PROTOCOL === 'https',
18-
host: HOST,
1919
overlay: false,
20-
watchOptions: {
21-
ignored: /node_modules/,
22-
},
20+
publicPath: '/',
21+
quiet: true,
2322
stats: {
24-
colors: true,
25-
chunks: false,
2623
chunkModules: false,
24+
chunks: false,
25+
colors: true,
2726
},
28-
historyApiFallback: {
29-
disableDotRule: true,
30-
},
31-
before(app: Application) {
32-
app.use(errorOverlayMiddleware())
27+
watchContentBase: true,
28+
watchOptions: {
29+
ignored: /node_modules/,
3330
},
3431
})

0 commit comments

Comments
 (0)