-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeCompile.ts
More file actions
183 lines (156 loc) · 5.3 KB
/
codeCompile.ts
File metadata and controls
183 lines (156 loc) · 5.3 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
173
174
175
176
177
178
179
180
181
182
183
import * as webpack from 'webpack'
import { config } from '../../utilities/config'
import { basename, extname, join } from 'path'
import { lstat, readdirSync } from 'fs'
import { ExecuteStep } from '../core/executeStep'
import { executor } from '../core/executor'
import { projectConfig } from '../../project/config'
export class CodeCompile extends ExecuteStep {
public async method(context) {
const path = context.serviceRoot
let isDir = await this.isDirectory(path)
let files = [path]
if (isDir) {
files = await this.getJsFiles(path)
}
context.files = files
await executor(context, bundle)
return context
}
private getJsFiles(folder) {
let files = readdirSync(folder);
let filter = /\.js|\.ts$/
return files.filter(name => filter.test(name)).map(file => join(folder, file))
}
private isDirectory(path) {
return new Promise((resolve, reject) => {
lstat(path, (err, stats) => {
if (err) return reject(err);
return resolve(stats.isDirectory())
});
})
}
}
export const codeCompile = new CodeCompile('CodeCompile')
export const bundle = ExecuteStep.register('WebpackBundle', (context) => {
return new Promise(async (resolve, reject) => {
const webpackConfig = await executor(context, bundleConfig)
let buildHash = ''
const done = (err?, res?) => {
const isWatch = context.watchCallback && webpackConfig.watch
if (err) {
if (isWatch) {
// watch mode, wait to fix
return
}
return reject(err)
}
if (isWatch) {
context.watchCallback(context)
} else {
return resolve(res)
}
}
try {
webpack(webpackConfig, function (err, stats) {
if (err) return done(err)
if (buildHash === stats.hash) return
if (buildHash && context.watchCallback) context.clearRequireCache = true
buildHash = stats.hash
let jsonStats = stats.toJson();
if (jsonStats.errors.length > 0) {
console.log('WEBPACK ERROR')
jsonStats.errors.forEach(msg => console.log(msg))
return done(jsonStats.errors);
}
// if (jsonStats.warnings.length > 0) {
// console.log('WEBPACK WARNINGS')
// console.log(jsonStats.warnings)
// }
context.originalFiles = context.files
context.files = []
Object.keys(jsonStats.entrypoints).forEach((entryKey) => {
let entry = jsonStats.entrypoints[entryKey]
let assets = entry.assets.map((file) => join(webpackConfig.output.path, file))
context.files.push(...assets)
})
done()
});
} catch (e) {
return done(e)
}
})
})
export const bundleConfig = ExecuteStep.register('WebpackBundleConfig', async (context) => {
let entry = {}
context.files.forEach((file) => {
let name = basename(file)
const ext = extname(name)
const nameKey = name.substring(0, name.length - ext.length)
entry[nameKey] = file
})
const externals = {}
let compile = {}
if (projectConfig.compile) {
compile = await executor({ context, projectConfig }, bundleCompileConfig)
}
let watch = {}
if (context.watchCallback && projectConfig.watch) {
watch = await executor({ context, projectConfig }, watchConfig)
}
const webpackConfig = {
...config.webpack,
...compile,
...watch,
...projectConfig.webpack,
entry
}
webpackConfig.externals = { ...webpackConfig.externals, ...externals }
if (!context.watchCallback && webpackConfig.watch) {
delete webpackConfig.watch
}
return webpackConfig
})
export const bundleCompileConfig = ExecuteStep.register('WebpackCompileConfig', (context) => {
switch (context.projectConfig.compile) {
case "ts-loader":
return {
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
}
}
case "babel-loader":
return {
module: {
rules: [
{
test: /\.jsx?$/,
use: 'babel-loader',
exclude: /node_modules/
}
]
}
}
default:
return {}
}
})
export const watchConfig = ExecuteStep.register('WebpackWatchConfig', (context) => {
return {
watch: true,
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /dist/
}
}
})