-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
Β·225 lines (204 loc) Β· 5.95 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const fs = require('fs')
const path = require('path')
const exit = require('exit')
const c = require('ansi-colors')
const webpack = require('webpack')
const ExtractCSS = require('mini-css-extract-plugin')
const onExit = require('exit-hook')
const gzip = require('gzip-size')
const { log, resolve, join } = require('./util.js')
const userBabelConfig = fs.existsSync(resolve('.babelrc'))
const reloadScript = `
(function (global) {
try {
const socketio = document.createElement('script')
socketio.src = 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.slim.js'
socketio.onload = function init () {
var disconnected = false
var socket = io('https://localhost:3001', {
reconnectionAttempts: 3
})
socket.on('connect', () => console.log('@thecouch/spaghetti connected'))
socket.on('refresh', () => {
global.location.reload()
})
socket.on('disconnect', () => {
disconnected = true
})
socket.on('reconnect_failed', e => {
if (disconnected) return
console.error("@thecouch/spaghetti - connection to the update server failed")
})
}
document.head.appendChild(socketio)
} catch (e) {}
})(this);
`
module.exports = (config = {}) => {
let server
let socket
if (config.reload) {
server = require('http').createServer((req, res) => {
res.writeHead(200, {
'Content-Type': 'text/plain'
})
res.write('@slater/cli successfully connected')
res.end()
}).listen(3001)
socket = require('socket.io')(server, {
serveClient: false
})
}
const output = Object.assign({
path: config.outDir,
filename: config.filename + '.js'
}, config.output || {})
const compiler = webpack({
mode: config.watch ? 'development' : 'production',
target: config.target || 'web',
node: config.node || {},
externals: config.externals || [],
performance: { hints: false },
devtool: config.map || 'cheap-module-source-map',
entry: resolve(config.in),
output,
module: {
rules: [
Object.assign(
{
test: /\.js$/,
exclude: /node_modules/,
loader: require.resolve('babel-loader')
},
userBabelConfig ? {} : {
options: {
babelrc: false,
plugins: [
require.resolve('babel-plugin-lodash'),
require.resolve('@babel/plugin-syntax-object-rest-spread'),
require.resolve('@babel/plugin-proposal-class-properties'),
require.resolve('fast-async')
],
presets: [
require.resolve('@babel/preset-env'),
'@babel/preset-react'
]
}
}
),
{
test: /\.(sa|sc|c)ss$/,
exclude: /node_modules/,
use: [
ExtractCSS.loader,
require.resolve('css-loader'),
config.sass ? {
loader: require.resolve('sass-loader'),
options: {
implementation: require('sass')
}
} : {
loader: require.resolve('postcss-loader'),
options: {
plugins: [
require('postcss-import'),
require('postcss-nested'),
require('postcss-cssnext')({
warnForDuplicates: false
}),
require('postcss-discard-comments'),
!config.watch && require('cssnano')
].filter(Boolean)
}
}
]
}
].filter(Boolean)
},
resolve: {
alias: config.alias || {}
},
plugins: [
new webpack.BannerPlugin({
banner: config.reload ? (
config.banner ? (
reloadScript + config.banner
) : reloadScript
) : config.banner || '',
raw: true,
entryOnly: true,
exclude: /\.(sa|sc|c)ss$/
}),
// new webpack.optimize.OccurrenceOrderPlugin(),
// new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
new ExtractCSS({
filename: '[name].css',
sourceMap: false
})
].filter(Boolean).concat(config.plugins || [])
})
function emit(fns, data) {
fns && fns.map(f => f(data))
}
function methods (bundle) {
let fns = {}
emit(fns.start)
bundle((err, stats) => {
if (err || stats.hasErrors()) {
return emit(fns.error, err || stats.compilation.errors)
}
const assets = Object.keys(stats.compilation.assets).reduce((_, filename) => {
const asset = stats.compilation.assets[filename]
const size = parseFloat((asset.size() / 1024).toFixed(2))
const s = {
filename,
size: {
raw: size
}
}
if (!/\.map$/.test(filename) && !config.watch) {
const code = fs.readFileSync(
path.resolve(output.path, filename)
).toString('utf8')
s.size.gzip = parseFloat((gzip.sync(code) / 1024).toFixed(2), 10)
}
return _.concat(s)
}, []).sort((a, b) => {
if (a.filename < b.filename) return -1
if (a.filename > b.filename) return 1
return 0
})
emit(fns.end, {
stats,
assets,
duration: stats.endTime - stats.startTime
})
socket && socket.emit('refresh')
})
return {
start (cb) {
fns.start = (fns.start || []).concat(cb)
return this
},
end (cb) {
fns.end = (fns.end || []).concat(cb)
return this
},
error (cb) {
fns.error = (fns.error || []).concat(cb)
return this
}
}
}
onExit(() => {
server && server.close()
})
return {
build () {
return methods(done => compiler.run(done))
},
watch () {
return methods(done => compiler.watch({}, done))
}
}
}