-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
191 lines (156 loc) · 5.75 KB
/
build.ts
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
import { Extractor, ExtractorConfig, ExtractorLogLevel, ExtractorMessageCategory } from '@microsoft/api-extractor'
import '@plugjs/eslint'
import { assert, banner, context, exec, Files, find, log, merge, plugjs, resolve, rmrf, using } from '@plugjs/plug'
import { logLevels } from '@plugjs/plug/logging'
import * as vite from 'vite'
import type { ReportLevel } from '@plugjs/plug/logging'
import type { RollupError } from 'rollup'
export default plugjs({
async vite(): Promise<Files> {
banner('Building with Vite')
const errors = new WeakSet<Error | RollupError>()
const warnings = new Set<string>()
const builder = await vite.createBuilder({
configFile: resolve('vite.config.ts'),
clearScreen: false,
customLogger: {
hasWarned: false,
info(msg: string, opts: vite.LogErrorOptions = {}) {
if (opts.error) errors.add(opts.error)
log.notice(msg)
},
warn(msg: string, opts: vite.LogErrorOptions = {}) {
if (opts.error) errors.add(opts.error)
this.hasWarned = true
log.warn(msg)
},
warnOnce(msg: string, opts: vite.LogErrorOptions = {}) {
if (opts.error) errors.add(opts.error)
if (warnings.has(msg)) return
warnings.add(msg)
this.hasWarned = true
log.warn(msg)
},
error(msg: string, opts: vite.LogErrorOptions = {}) {
if (opts.error) errors.add(opts.error)
this.hasWarned = true
log.warn(msg)
},
hasErrorLogged(error: Error) {
return errors.has(error)
},
clearScreen() {}, // do nothing!
},
})
const environment = builder.environments['client']
assert(environment, 'Environment "client" not found')
const result = await builder.build(environment)
const entries = Array.isArray(result) ? result : [ result ]
const files = Files.builder(resolve(builder.config.build.outDir))
for (const entry of entries) {
if ('output' in entry) {
entry.output.forEach((output) => {
files.add(output.fileName)
})
}
}
return files.build()
},
async tsc(): Promise<void> {
banner('Checking and generating TypeScript types')
await rmrf('./build/lib')
await exec('vue-tsc', '--build', '--force')
},
async lint(): Promise<void> {
banner('Linting source code')
await merge([
find('**/*.([cm])?ts', '**/*.([cm])?js', '**/*.vue', { directory: 'lib' }),
find('**/*.([cm])?ts', '**/*.([cm])?js', '**/*.vue', { directory: 'src' }),
]).eslint()
},
async copy(): Promise<Files> {
banner('Copying assets')
return using('index.scss', 'globals.d.ts', { directory: 'lib' }).copy('dist')
},
async dts(): Promise<void> {
await this.tsc()
banner('Bundling declaration files')
const config = ExtractorConfig.prepare({
packageJsonFullPath: resolve('./package.json'),
configObjectFullPath: resolve('./api-extractor.json'),
configObject: {
projectFolder: resolve('.'),
mainEntryPointFilePath: resolve('./build/lib/index.d.ts'),
compiler: {
tsconfigFilePath: resolve('./tsconfig.app.json'),
},
apiReport: { enabled: false },
docModel: { enabled: false },
tsdocMetadata: { enabled: false },
dtsRollup: {
enabled: true,
untrimmedFilePath: '',
publicTrimmedFilePath: resolve('./dist/index.d.ts'),
},
messages: {
compilerMessageReporting: {
default: { logLevel: ExtractorLogLevel.Warning },
},
extractorMessageReporting: {
'default': { logLevel: ExtractorLogLevel.Warning },
'ae-forgotten-export': { logLevel: ExtractorLogLevel.None },
'ae-missing-release-tag': { logLevel: ExtractorLogLevel.None },
},
tsdocMessageReporting: {
'default': { logLevel: ExtractorLogLevel.Warning },
},
},
},
})
const report = context().log.report('DTS Bundler Report')
Extractor.invoke(config, {
localBuild: false,
showDiagnostics: false,
showVerboseMessages: true,
typescriptCompilerFolder: resolve('node_modules/typescript'),
messageCallback(message) {
message.handled = true
if (message.category === ExtractorMessageCategory.Console) {
switch (message.logLevel) {
case ExtractorLogLevel.Error: log.error(message.text); break
case ExtractorLogLevel.Warning: log.warn(message.text); break
case ExtractorLogLevel.Verbose: log.notice(message.text); break
case ExtractorLogLevel.Info: log.notice(message.text); break
case ExtractorLogLevel.None: log.info(message.text); break
}
} else {
let level: ReportLevel | null = null
switch (message.logLevel) {
case ExtractorLogLevel.Error: level = logLevels.ERROR; break
case ExtractorLogLevel.Warning: level = logLevels.WARN; break
case ExtractorLogLevel.Verbose: level = logLevels.NOTICE; break
case ExtractorLogLevel.Info: level = logLevels.NOTICE; break
}
if (! level) return
report.add({
message: message.text,
tags: message.messageId,
file: message.sourceFilePath ? resolve(message.sourceFilePath) : undefined,
line: message.sourceFileLine,
column: message.sourceFileColumn,
level,
})
}
},
})
report.done()
},
async default(): Promise<void> {
await this.tsc()
await this.lint()
await this.vite()
await this.copy()
await this.dts() // after "vite" (it wipes the "dist" folder)
banner('Build complete')
},
})