-
Notifications
You must be signed in to change notification settings - Fork 127
/
index.js
179 lines (159 loc) · 5.4 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
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 UglifyJsPlugin = require('uglifyjs-webpack-plugin')
var containsPath = require('contains-path')
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')
var isInstalled = require('./lib/is-installed')
// default to `true` if running `hjs-dev-server`
var isDev = (process.argv[1] || '').indexOf('hjs-dev-server') !== -1
module.exports = function (opts) {
checkRequired(opts)
var outputFolder = path.resolve(opts.out)
var cwd = process.cwd()
// 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: {},
uglify: opts.uglify
})
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 = spec.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
})
// Enable Webpack HMR unless explictly disabled
if (config.devServer.hot) {
installedHotLoaders.load(config)
}
// Add optional loaders
installedStyleLoaders.forEach(function (item) {
config.module.rules.push(item.dev)
})
// Add visualizer plugin
if (isInstalled('webpack-visualizer-plugin')) {
config.plugins.push(
new (require('webpack-visualizer-plugin'))()
)
}
} else {
// clear out output folder if so configured
if (spec.clearBeforeBuild) {
// Throw error if trying to clear output directory but it contains the cwd
// See https://github.com/HenrikJoreteg/hjs-webpack/issues/186
if (containsPath(cwd, outputFolder)) {
throw new Error('Cannot clear out directory since it contains the current working directory.\nTried to clear ' + outputFolder + ' from ' + cwd)
}
// 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 UglifyJsPlugin(spec.uglify),
new ExtractTextPlugin({
filename: spec.output.cssFilename,
allChunks: true
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production')
})
)
// Add optional loaders
installedStyleLoaders.forEach(function (item) {
config.module.rules.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')
}
}