Skip to content

Commit 8051832

Browse files
author
yunkui.zhou
committed
提取公共配置
1 parent fc0fdf6 commit 8051832

File tree

6 files changed

+57
-87
lines changed

6 files changed

+57
-87
lines changed

server/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ if (process.env.NODE_ENV === 'local') {
3131
}))
3232
}
3333

34-
//组件页面
34+
//以路径名称作为页面区分
35+
3536
app.use('/components', extractMapping, (req, res, next) => {
37+
//可以根据路径,针对某一个页面进行服务端渲染
3638
const content = renderToString(<StaticRouter
3739
location={req.url}
3840
context={{}}

server/middleware/extractMapping.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const extractMapping = (req, res, next) => {
2121
}
2222
}
2323
global.staticAssetsMapping = mapping
24-
console.log(JSON.stringify(global.staticAssetsMapping))
2524
next()
2625
}
2726

webpack.config.common.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,46 @@
22
* Created by ink on 2018/4/4.
33
*/
44
const path = require('path')
5-
let contentPath, publicPath, entry
6-
entry = {
5+
const ManifestPlugin = require('webpack-manifest-plugin')
6+
//这里可以路径前一个名称作为页面区分
7+
const entry = {
78
'components/index': ['./client/pages/example/index.js']
89
}
9-
if (process.env.NODE_ENV === 'local') {
10-
contentPath = path.resolve(__dirname, 'public')
11-
publicPath = '/'
12-
Object.keys(entry).forEach((item, index) => {
13-
entry[item].unshift('webpack-hot-middleware/client?path=/__webpack_hmr&timeout=2000&reload=true')
10+
const rules = [{
11+
enforce: 'pre',
12+
test: '/\.jsx?$/',
13+
exclude: /node_modules/,
14+
use: ['eslint-loader']
15+
}, {
16+
test: /\.jsx?$/,
17+
use: ['babel-loader']
18+
}, {
19+
test: /\.(png|jpg|gif)$/,
20+
use: ['url-loader']
21+
}]
22+
const plugins = [
23+
new ManifestPlugin({
24+
fileName: 'mapping.json'
1425
})
15-
} else {
16-
contentPath = path.resolve(__dirname, 'dist/public/mapping')
17-
publicPath = '/mapping/'
18-
}
26+
]
1927
const config = {
2028
entry,
2129
target: 'web',
2230
output: {
23-
path: contentPath,
24-
publicPath,
2531
filename: '[name].[hash].js',
2632
chunkFilename: '[name].[hash].js',
2733
libraryTarget: 'umd'
2834
},
35+
module: {
36+
rules
37+
},
2938
resolve: {
3039
modules: [
3140
'node_modules',
3241
path.resolve(__dirname, 'client'),
3342
path.resolve(__dirname, 'server')
3443
]
3544
},
45+
plugins,
3646
}
3747
module.exports = config

webpack.config.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
const devConfig = require("./webpack.config.local")
2-
const proConfig = require("./webpack.config.pro")
31
const NODE_ENV = process.env.NODE_ENV
42
let config
5-
if (NODE_ENV === 'development') {
6-
config = devConfig
3+
if (NODE_ENV === 'local') {
4+
config = require('./webpack.config.local')
75
} else if (NODE_ENV === 'production') {
8-
config = proConfig
6+
config = require('./webpack.config.pro')
97
} else {
10-
config = devConfig
8+
config = require('./webpack.config.local')
119
}
12-
module.exports = config
10+
module.exports = config

webpack.config.local.js

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,32 @@
44
const path = require('path')
55
const webpack = require('webpack')
66
const contentPath = path.resolve(__dirname, 'public')
7+
const commonConfig = require('./webpack.config.common')
78
const publicPath = '/'
8-
const ManifestPlugin = require('webpack-manifest-plugin')
9+
const copyEntry = Object.assign({}, commonConfig.entry)
10+
const entry = {}
11+
Object.keys(copyEntry).map(item => {
12+
let temp = [...copyEntry[item]]
13+
temp.unshift('webpack-hot-middleware/client?path=/__webpack_hmr&timeout=2000&reload=true')
14+
entry[item] = temp
15+
})
16+
console.log('读取devConfig')
917
const config = {
1018
mode: 'development',
1119
devtool: 'eval-source-map',
12-
entry: {
13-
'components/index': ['webpack-hot-middleware/client?path=/__webpack_hmr&timeout=2000&reload=true', './client/pages/example/index.js']
14-
},
15-
target: 'web',
16-
output: {
20+
entry,
21+
target: commonConfig.target,
22+
output: Object.assign({}, commonConfig.output, {
1723
path: contentPath,
1824
publicPath,
19-
filename: '[name].[hash].js',
20-
chunkFilename: '[name].[hash].js',
21-
libraryTarget: 'umd'
22-
},
25+
}),
2326
module: {
2427
rules: [{
25-
enforce: 'pre',
26-
test: '/\.jsx?$/',
27-
exclude: /node_modules/,
28-
use: ['eslint-loader']
29-
}, {
30-
test: /\.jsx?$/,
31-
use: ['babel-loader']
32-
}, {
3328
test: /\.less$/,
3429
use: ['style-loader', 'css-loader', 'less-loader']
35-
}, {
36-
test: /\.(png|jpg|gif)$/,
37-
use: ['url-loader']
38-
}]
39-
},
40-
resolve: {
41-
modules: [
42-
'node_modules',
43-
path.resolve(__dirname, 'client'),
44-
path.resolve(__dirname, 'server')
45-
]
30+
}, ...commonConfig.module.rules]
4631
},
32+
resolve: commonConfig.resolve,
4733
watchOptions: {
4834
aggregateTimeout: 400,
4935
poll: 1000,
@@ -64,9 +50,7 @@ const config = {
6450
new webpack.DefinePlugin({
6551
'process.env.NODE_ENV': JSON.stringify('development')
6652
}),
67-
new ManifestPlugin({
68-
fileName: 'mapping.json'
69-
}),
53+
...commonConfig.plugins
7054
]
7155
}
7256
module.exports = config

webpack.config.pro.js

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,37 @@
44
const path = require('path')
55
const webpack = require('webpack')
66
const contentPath = path.resolve(__dirname, 'dist/public/mapping')
7-
const ManifestPlugin = require('webpack-manifest-plugin')
87
const ExtractCssChunks = require('extract-text-webpack-plugin')
98
const publicPath = '/mapping/'
9+
const commonConfig = require('./webpack.config.common')
10+
console.log('读取production')
11+
console.log(`入口数组:${JSON.stringify(commonConfig.entry)}`)
1012
const config = {
1113
mode: 'production',
1214
devtool: 'source-map',
13-
entry: {
14-
'components/index': ['./client/index.js']
15-
},
16-
target: 'web',
17-
output: {
15+
entry: commonConfig.entry,
16+
output: Object.assign({}, commonConfig.output, {
1817
path: contentPath,
1918
publicPath,
20-
filename: '[name].[hash].js',
21-
chunkFilename: '[name].[hash].js',
22-
libraryTarget: 'umd'
23-
},
19+
}),
2420
module: {
2521
rules: [{
26-
enforce: 'pre',
27-
test: '/\.jsx?$/',
28-
exclude: /node_modules/,
29-
use: ['eslint-loader']
30-
}, {
31-
test: /\.jsx?$/,
32-
use: ['babel-loader']
33-
}, {
3422
test: /\.less$/,
3523
use: ExtractCssChunks.extract({
3624
fallback: 'style-loader',
3725
use: ['css-loader', 'less-loader']
3826
})
39-
}, {
40-
test: /\.(png|jpg|gif)$/,
41-
use: ['url-loader']
42-
}]
43-
},
44-
resolve: {
45-
modules: [
46-
'node_modules',
47-
path.resolve(__dirname, 'client'),
48-
path.resolve(__dirname, 'server')
49-
]
27+
}, ...commonConfig.module.rules]
5028
},
29+
resolve: commonConfig.resolve,
5130
plugins: [
5231
new webpack.DefinePlugin({
5332
'process.env.NODE_ENV': JSON.stringify('production')
5433
}),
55-
new ManifestPlugin({
56-
fileName: 'mapping.json'
57-
}),
5834
new ExtractCssChunks({
5935
filename: '[name].[id].css'
60-
})
36+
}),
37+
...commonConfig.plugins
6138
]
6239
}
6340
module.exports = config

0 commit comments

Comments
 (0)