This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
createServer.js
147 lines (124 loc) · 4.23 KB
/
createServer.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
#!/usr/bin/env node
'use strict'
const path = require('path')
const createCoverageSaver = require('./createCoverageSaver')
function createCompiler (inConfig) {
const debug = require('debug')('test-bed:compiler')
const webpack = require('webpack')
const config = Object.assign({ }, inConfig)
const OccurrenceOrderPlugin = webpack.optimize.OccurrenceOrderPlugin
const NoErrorsPlugin = webpack.NoErrorsPlugin
const plugins = (config.plugins || [ ]).slice()
ensurePlugin(OccurrenceOrderPlugin)
ensurePlugin(NoErrorsPlugin)
if (typeof config.entry !== 'string') {
throw new Error('config.entry should be a string.')
}
config.entry = [
'!!' + require.resolve('./src/test-bed'),
config.entry
]
if (config.output) {
console.warn('[test-bed]: Replacing config.output with test-bed’s configuration.')
}
config.output = {
path: path.resolve(process.cwd(), 'build/test-assets'),
publicPath: '/test-assets/',
filename: 'test.bundle.js',
pathinfo: true
}
config.devtool = config.devtool || 'eval'
config.plugins = plugins
function ensurePlugin (Plugin) {
if (plugins.some(plugin => plugin instanceof Plugin)) {
debug('%s already exists in webpack configuration, skipping.', Plugin.name)
} else {
debug('Adding %s to configuration.', Plugin.name)
plugins.push(new Plugin())
}
}
return webpack(config)
}
module.exports = function createServer (config) {
const testBedConfig = config.testBed
delete config.testBed
const webpackMiddlewareConfig = config.webpackMiddleware
delete config.webpackMiddleware
const debug = require('debug')('test-bed:server')
const debugSocket = require('debug')('test-bed:socket')
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const compiler = createCompiler(config)
const coverageSaver = createCoverageSaver()
let lastHash
// Allow the test-bed app to be configured!
if (testBedConfig && typeof testBedConfig.configureExpressApp === 'function') {
testBedConfig.configureExpressApp(app, express)
}
app.use(express.static(path.resolve(__dirname, 'static')))
app.use(require('webpack-dev-middleware')(compiler, Object.assign({
quiet: true,
publicPath: '/test-assets/',
stats: { colors: true }
}, webpackMiddlewareConfig || { })))
io.on('connection', function (socket) {
debugSocket('Client connected')
function saveCoverage (report) {
coverageSaver.receiveReport(report)
}
function onDisconnected () {
socket.removeListener('coverage', saveCoverage)
socket.removeListener('disconnect', onDisconnected)
debugSocket('Client disconnected')
}
socket.on('coverage', saveCoverage)
socket.on('disconnect', onDisconnected)
})
compiler.plugin('done', function (stats) {
const compilation = stats.compilation
const json = stats.toJson()
debug('Hash: %s', json.hash)
if (lastHash === json.hash) {
return
}
lastHash = json.hash
const builtModules = findBuiltModules()
const affectedModuleIds = calculateAffectedModuleIds(builtModules)
const errors = json.errors || [ ]
debug('Built modules: %o', builtModules.map(builtModule => builtModule.id))
debug('Affected modules: %o', affectedModuleIds)
io.emit('compiled', {
affectedModuleIds,
errors
})
function calculateAffectedModuleIds (modules) {
const visited = { }
const affectedModuleIds = [ ]
for (let moduleToTraverse of modules) {
traverse(moduleToTraverse)
}
return affectedModuleIds
function traverse (moduleToTraverse) {
const id = moduleToTraverse.id
if (visited[id]) return
visited[id] = true
affectedModuleIds.push(id)
const parents = (moduleToTraverse.reasons
.filter(reason => reason.dependency && reason.module)
.map(reason => reason.module)
)
for (let parent of parents) traverse(parent)
}
}
function findBuiltModules () {
const built = [ ]
for (let chunk of compilation.chunks) {
built.push.apply(built, chunk.modules.filter(module => module.built))
}
return built
}
})
return server
}