forked from HenrikJoreteg/hjs-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (161 loc) · 5.46 KB
/
index.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
var fs = require('fs')
var path = require('path')
var rimraf = require('rimraf')
var webpack = require('webpack')
var defaults = require('lodash.defaults')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var getBaseConfig = require('./lib/base-config')
var getPackage = require('./lib/get-package')
var installedStyleLoaders = require('./lib/installed-style-loaders')
var isInstalled = require('./lib/is-installed')
// figure out if we're running `webpack` or `webpack-dev-server`
// we'll use this as the default for `isDev`
var isDev = (process.argv[1] || '').indexOf('webpack-dev-server') !== -1
module.exports = function (opts) {
checkRequired(opts)
var outputFolder = path.resolve(opts.out)
// add in our defaults
var spec = defaults(opts, {
entry: path.resolve(opts.in),
output: defaults(opts.output || {}, {
path: outputFolder + '/',
filename: null,
cssFilename: null,
hash: false,
publicPath: '/'
}),
configFile: null,
isDev: isDev,
package: null,
replace: null,
port: 3000,
https: false,
hostname: 'localhost',
html: true,
urlLoaderLimit: 10000,
clearBeforeBuild: false,
serveCustomHtmlInDev: true,
devServer: defaults(opts.devServer || {}, {
info: false,
historyApiFallback: true,
// For some reason simply setting this doesn't seem to be enough
// which is why we also do the manual entry above and the
// manual adding of the hot module replacment plugin below
hot: true,
contentBase: outputFolder
})
})
spec.package = getPackage(spec.package)
if (!spec.output.filename) {
spec.output.filename = spec.isDev ? 'app.js' : buildFilename(spec.package, spec.output.hash, 'js')
}
if (!spec.output.cssFilename) {
spec.output.cssFilename = spec.isDev ? 'app.css' : buildFilename(spec.package, spec.output.hash, 'css')
}
var config = getBaseConfig(spec)
// re-attach original spec items so they can be accessed from dev-server script
config.spec = spec
// check for any module replacements
if (spec.replace) {
for (var item in spec.replace) {
// allow for simple strings
if (typeof item === 'string') {
var regex = new RegExp('^' + item + '$')
}
var newResource = spec.replace[item]
if (typeof newResource === 'string') {
newResource = path.resolve(newResource)
}
config.plugins.push(new webpack.NormalModuleReplacementPlugin(regex, newResource))
}
}
// check for any module definitions
if (spec.define) {
config.plugins.push(new webpack.DefinePlugin(spec.define))
}
// dev specific stuff
if (spec.isDev) {
// debugging option
// https://webpack.github.io/docs/configuration.html#devtool
// https://github.com/HenrikJoreteg/hjs-webpack/issues/63
// Supports original code (before transforms) with pretty good initial
// build speed and good rebuild speed
config.devtool = 'cheap-module-eval-source-map'
// add dev server and hotloading clientside code
config.entry.unshift(
'webpack-dev-server/client?' + (spec.https ? 'https://' : 'http://') + spec.hostname + ':' + spec.port,
'webpack/hot/only-dev-server'
)
config.devServer = spec.devServer
config.devServer.port = spec.port
config.devServer.host = spec.hostname
config.devServer.https = spec.https
// add dev plugins
config.plugins = config.plugins.concat([
new webpack.HotModuleReplacementPlugin()
])
// add react-hot as module loader if it is installed
if (isInstalled('react-hot-loader') && config.spec.devServer.hot) {
config.module.loaders[0].loaders.unshift('react-hot')
}
// Add optional loaders
installedStyleLoaders.forEach(function (item) {
config.module.loaders.push(item.dev)
})
} else {
// clear out output folder if so configured
if (spec.clearBeforeBuild) {
// allow passing a glob (limit to within folder though)
if (typeof spec.clearBeforeBuild === 'string') {
// create the output folder if it doesn't exist
// just for convenience
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder)
}
rimraf.sync(outputFolder + '/' + spec.clearBeforeBuild)
} else {
rimraf.sync(outputFolder)
fs.mkdirSync(outputFolder)
}
}
// minify in production
config.plugins.push(
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(true),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
output: {
comments: false
},
sourceMap: false
}),
new ExtractTextPlugin(config.output.cssFilename, {
allChunks: true
}),
new webpack.DefinePlugin({
'process.env': {NODE_ENV: JSON.stringify('production')}
})
)
// Add optional loaders
installedStyleLoaders.forEach(function (item) {
config.module.loaders.push(item.production)
})
}
return config
}
function buildFilename (pack, hash, ext) {
return [
pack.name,
// extract-text-plugin uses [contenthash] and webpack uses [hash]
hash ? (ext === 'css' ? '[contenthash]' : '[hash]') : pack.version,
ext || 'js'
].join('.')
}
function checkRequired (opts) {
var props = ['out', 'in']
if (!opts || !props.every(function (prop) { return opts.hasOwnProperty(prop) })) {
throw new Error('Must pass in options object with `in` and `out` properties')
}
}