forked from HenrikJoreteg/hjs-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
172 lines (154 loc) · 4.97 KB
/
Copy pathindex.js
File metadata and controls
172 lines (154 loc) · 4.97 KB
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
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 installedHotLoaders = require('./lib/installed-hot-loaders')
// 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('hjs-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,
https: false,
port: 3000,
hostname: 'localhost',
html: true,
urlLoaderLimit: 10000,
clearBeforeBuild: false,
serveCustomHtmlInDev: true,
devServer: {}
})
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)
// 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'
// Create our dev server config for use in bin/hjs-dev-server
config.devServer = defaults(spec.devServer, {
// For webpack-dev-middleware
noInfo: true,
quiet: false,
lazy: false,
publicPath: spec.output.publicPath,
// Our own options for hjs-dev-server
historyApiFallback: true,
hot: true,
contentBase: outputFolder,
port: spec.port,
https: spec.https,
hostname: spec.hostname || spec.host
})
// Add react-hot module loader if it is installed
if (config.devServer.hot) {
// configure babel loader
installedHotLoaders.load(config)
}
// 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')
}
}