-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcompile.js
159 lines (143 loc) · 4.83 KB
/
compile.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
const path = require('path')
const babel = require('@babel/core')
const { reporter, prettyPrint } = require('@dhis2/cli-helpers-engine')
const chokidar = require('chokidar')
const fs = require('fs-extra')
const makeBabelConfig = require('../../../config/makeBabelConfig.js')
const { isApp } = require('../parseConfig')
const {
verifyEntrypoints,
createAppEntrypointWrapper,
createPluginEntrypointWrapper,
} = require('./entrypoints.js')
const {
extensionPattern,
normalizeExtension,
} = require('./extensionHelpers.js')
const watchFiles = ({ inputDir, outputDir, processFileCallback, watch }) => {
const processFile = async (source) => {
const relative = path.relative(inputDir, source)
const destination = path.join(outputDir, relative)
reporter.debug(`File ${relative} changed or added...`)
await fs.ensureDir(path.dirname(destination))
await processFileCallback(source, destination)
}
const removeFile = async (file) => {
const relative = path.relative(inputDir, file)
const outFile = path.join(outputDir, relative)
reporter.debug(`File ${relative} removed... removing: `, outFile)
fs.remove(outFile)
}
return new Promise((resolve, reject) => {
const watcher = chokidar.watch(inputDir, { persistent: true })
watcher
.on('ready', async () => {
if (watch) {
reporter.debug('watching...')
} else {
await watcher.close()
}
resolve()
})
.on('add', processFile)
.on('change', processFile)
.on('unlink', removeFile)
.on('error', (error) => {
reporter.debugErr('Chokidar error:', error)
reject('Chokidar error!')
})
process.on('SIGINT', async () => {
reporter.debug('Caught interrupt signal')
await watcher.close()
})
})
}
const compile = async ({
config,
paths,
moduleType = 'es',
mode = 'development',
watch = false,
}) => {
const isAppType = isApp(config.type)
verifyEntrypoints({ config, paths })
if (isAppType) {
if (config.entryPoints.app) {
await createAppEntrypointWrapper({
entrypoint: config.entryPoints.app,
paths,
})
}
if (config.entryPoints.plugin) {
await createPluginEntrypointWrapper({
entrypoint: config.entryPoints.plugin,
paths,
})
}
}
const outDir = isAppType
? paths.shellApp
: path.join(paths.buildOutput, moduleType)
fs.removeSync(outDir)
fs.ensureDirSync(outDir)
if (isAppType) {
fs.removeSync(paths.shellPublic)
fs.copySync(paths.shellSourcePublic, paths.shellPublic)
}
const babelConfig = makeBabelConfig({ moduleType, mode })
const copyFile = async (source, destination) => {
reporter.debug(
`Copying ${prettyPrint.relativePath(
source
)} to ${prettyPrint.relativePath(destination)}`
)
await fs.copy(source, destination)
}
const compileFile = async (source, destination) => {
if (source.match(extensionPattern)) {
try {
const result = await babel.transformFileAsync(
source,
babelConfig
)
// Always write .js files
const jsDestination = normalizeExtension(destination)
reporter.debug(
`Compiled ${prettyPrint.relativePath(
source
)} with Babel, saving to ${prettyPrint.relativePath(
jsDestination
)}`
)
await fs.writeFile(jsDestination, result.code)
} catch (err) {
reporter.dumpErr(err)
reporter.error(
`Failed to compile ${prettyPrint.relativePath(
source
)}. Fix the problem and save the file to automatically reload.`
)
}
} else {
await copyFile(source, destination)
}
}
return Promise.all([
watchFiles({
inputDir: paths.src,
outputDir: outDir,
// todo: handle lib compilations with Vite
// https://dhis2.atlassian.net/browse/LIBS-722
processFileCallback: isAppType ? copyFile : compileFile,
watch,
}),
isAppType &&
watchFiles({
inputDir: paths.public,
outputDir: paths.shellPublic,
processFileCallback: copyFile,
watch,
}),
])
}
module.exports = compile