forked from diegohaz/arc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
184 lines (170 loc) · 4.57 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const path = require('path')
const fs = require('fs')
const HappyPack = require('happypack')
const WebpackMd5Hash = require('webpack-md5-hash')
const nodeExternals = require('webpack-node-externals')
const { spawn } = require('child_process')
const fkill = require('fkill')
const devServer = require('@webpack-blocks/dev-server2')
const {
addPlugins, createConfig, entryPoint, env, setOutput, sourceMaps, defineConstants, webpack,
setDevTool, group,
} = require('@webpack-blocks/webpack2')
const host = process.env.HOST || 'localhost'
const port = (+process.env.PORT + 1) || 3001
const publicPath = `/${process.env.PUBLIC_PATH || ''}/`.replace('//', '/')
const devDomain = `http://${host}:${port}/`
const serverEntryPath = path.join(__dirname, 'src/server.js')
const clientEntryPath = path.join(__dirname, 'src/client.js')
const outputPath = path.join(__dirname, 'dist/public')
const assetsPath = path.join(__dirname, 'dist/assets.json')
const isVendor = ({ userRequest }) => (
userRequest &&
userRequest.indexOf('node_modules') >= 0 &&
userRequest.match(/\.js$/)
)
let watching
const runServerConfig = (serverConfig, prod) => () => ({
plugins: [
function run() {
this.plugin('done', stats => {
console.log(this.options)
const { output } = this.options
const { client } = stats.toJson({ modules: false }).assetsByChunkName
const assets = {
js: [].concat(client)
.filter(path => !/map$/.test(path))
.map(path => output.publicPath + path),
}
fs.writeFileSync(assetsPath, JSON.stringify(assets))
if (!watching) {
const serverCompiler = webpack(serverConfig)
serverCompiler['watch'](null, () => {
prod ? process.exit() : console.log('watching')
})
watching = true
}
})
},
],
})
let serverPid
const startServer = () => () => ({
plugins: [
function start() {
this.plugin('done', () => {
const promise = serverPid ? fkill(serverPid) : Promise.resolve()
promise.then(() => {
const server = spawn('node', ['.'])
serverPid = server.pid
server.stdout.on('data', data => console.log(`stdout: ${data}`))
server.stderr.on('data', data => console.log(`stderr: ${data}`))
console.log(serverPid)
})
})
},
],
})
const baseConfig = (name) => group([
setOutput({
path: outputPath,
}),
defineConstants({
'process.env.NODE_ENV': process.env.NODE_ENV,
'process.env.PUBLIC_PATH': publicPath,
}),
addPlugins([
new HappyPack({
loaders: ['babel-loader'],
cacheContext: {
env: process.env.NODE_ENV,
},
}),
]),
() => ({
name,
resolve: {
modules: ['src', 'node_modules'],
},
module: {
rules: [
{ test: /\.jsx?$/, loader: 'happypack/loader', exclude: /node_modules/ },
{ test: /\.(png|jpe?g|svg)$/, loader: 'url-loader?&limit=8000' },
{ test: /\.(woff2?|ttf|eot)$/, loader: 'url-loader?&limit=8000' },
],
},
}),
env('development', [
setOutput({
publicPath: devDomain,
}),
]),
env('production', [
setOutput({ publicPath }),
]),
])
const serverConfig = createConfig([
baseConfig('server'),
setDevTool('sourcemap'),
entryPoint({
server: serverEntryPath,
}),
setOutput({
filename: '../[name].js',
libraryTarget: 'commonjs2',
}),
addPlugins([
new webpack.BannerPlugin({
banner: 'require("source-map-support").install();',
raw: true,
entryOnly: false,
}),
new webpack.BannerPlugin({
banner: 'global.assets = require("./assets.json");',
raw: true,
}),
]),
startServer(),
() => ({
target: 'node',
externals: [nodeExternals()],
}),
])
const clientConfig = createConfig([
baseConfig('client'),
entryPoint({
client: clientEntryPath,
}),
setOutput({
filename: '[name].[hash].js',
}),
env('development', [
sourceMaps(),
devServer({
contentBase: 'public',
stats: 'errors-only',
publicPath: devDomain,
host,
port,
}),
addPlugins([
new webpack.NamedModulesPlugin(),
]),
runServerConfig(serverConfig),
]),
env('production', [
setOutput({
filename: '[name].[chunkhash].js',
}),
addPlugins([
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: isVendor,
}),
new WebpackMd5Hash(),
new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }),
]),
runServerConfig(serverConfig, true),
]),
])
module.exports = clientConfig