-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
142 lines (116 loc) · 3.63 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
const debug = require('debug')('screeps-webpack-plugin')
const path = require('path')
const fs = require('fs')
const ScreepsModules = require('screeps-modules')
// Events.
const COLLECT_MODULES = 'screeps-webpack-plugin-collect-modules'
const CONFIG_CLIENT = 'screeps-webpack-plugin-configure-client'
const BEFORE_COMMIT = 'screeps-webpack-plugin-before-commit'
const AFTER_COMMIT = 'screeps-webpack-plugin-after-commit'
class ScreepsWebpackPluginError extends Error {
constructor (msg) {
super(msg)
this.name = 'ScreepsWebpackPluginError'
}
}
class ScreepsWebpackPlugin {
constructor (options = {}) {
this.options = options
}
apply (compiler) {
compiler.plugin('compilation', (compilation) => {
if (compiler.options.target !== 'node') {
const err = new ScreepsWebpackPluginError(`Can only support Node.js {target: 'node'}`)
return compilation.errors.push(err)
}
this.registerHandlers(compilation)
})
compiler.plugin('after-emit', (compilation, cb) => {
Promise.resolve()
.then(() => {
return new Promise((resolve, reject) => {
const initial = {
modules: {},
plugin: this,
compilation
}
compilation.applyPluginsAsyncWaterfall(COLLECT_MODULES, initial, (err, {modules}) => {
if (err) {
debug('Error while collecting modules', err.stack)
return reject(err)
} else {
resolve(modules)
}
})
})
})
.then((modules) => {
const client = compilation.applyPluginsWaterfall(CONFIG_CLIENT, null, this)
const {branch} = this.options
compilation.applyPlugins(BEFORE_COMMIT, branch, modules)
return client.commit(branch, modules)
.then((body) => {
compilation.applyPlugins(AFTER_COMMIT, body)
})
.catch((body) => {
throw new Error(body)
})
})
.then(cb)
.catch((err) => {
compilation.errors.push(new ScreepsWebpackPluginError(err.stack))
cb()
})
})
}
registerHandlers (compilation) {
compilation.plugin(COLLECT_MODULES, this.collectModules)
compilation.plugin(CONFIG_CLIENT, this.configureClient)
}
collectModules ({modules: initial, plugin, compilation}, cb) {
const chunks = compilation.getStats().toJson().chunks
const outputPath = compilation.options.output.path
const files = []
for (const chunk of chunks) {
for (const file of chunk.files) {
files.push(path.resolve(outputPath, file))
}
}
const outputFileSystem = (
compilation.compiler.outputFileSystem.readFile
? compilation.compiler.outputFileSystem
: fs
)
const promises = []
for (const file of files) {
promises.push(new Promise((resolve, reject) => {
outputFileSystem.readFile(file, 'utf-8', (err, data) => {
if (err) {
return reject(err)
}
const moduleName = path.basename(file, '.js')
resolve({[moduleName]: data})
})
}))
}
Promise.all(promises)
.then((files) => {
const modules = files.reduce((modules, file) => {
Object.assign(modules, file)
return modules
}, initial || {})
cb(null, {modules, plugin, compilation})
})
.catch(cb)
}
configureClient (initial, plugin) {
return new ScreepsModules(plugin.options)
}
}
Object.assign(ScreepsWebpackPlugin, {
COLLECT_MODULES,
CONFIG_CLIENT,
BEFORE_COMMIT,
AFTER_COMMIT
})
module.exports = ScreepsWebpackPlugin