forked from medusajs/medusa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.ts
More file actions
659 lines (588 loc) · 17.5 KB
/
Copy pathcompiler.ts
File metadata and controls
659 lines (588 loc) · 17.5 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import type { AdminOptions, ConfigModule, Logger } from "@medusajs/types"
import { FileSystem, getConfigFile, getResolvedPlugins } from "@medusajs/utils"
import chokidar from "chokidar"
import { access, constants, copyFile, mkdir, rm } from "fs/promises"
import path from "path"
import type tsStatic from "typescript"
/**
* The compiler exposes the opinionated APIs for compiling Medusa
* applications and plugins. You can perform the following
* actions.
*
* - loadTSConfigFile: Load and parse the TypeScript config file. All errors
* will be reported using the logger.
*
* - buildAppBackend: Compile the Medusa application backend source code to the
* ".medusa/server" directory. The admin source and integration-tests are
* skipped.
*
* - buildAppFrontend: Compile the admin extensions using the "@medusjs/admin-bundler"
* package. Admin can be compiled for self hosting (aka adminOnly), or can be compiled
* to be bundled with the backend output.
*/
export class Compiler {
#logger: Logger
#projectRoot: string
#tsConfigPath: string
#pluginsDistFolder: string
#backendIgnoreFiles: string[]
#adminOnlyDistFolder: string
#tsCompiler?: typeof tsStatic
constructor(projectRoot: string, logger: Logger) {
this.#projectRoot = projectRoot
this.#logger = logger
this.#tsConfigPath = path.join(this.#projectRoot, "tsconfig.json")
this.#adminOnlyDistFolder = path.join(this.#projectRoot, ".medusa/admin")
this.#pluginsDistFolder = path.join(this.#projectRoot, ".medusa/server")
this.#backendIgnoreFiles = [
"integration-tests",
"test",
"unit-tests",
"src/admin",
]
}
/**
* Util to track duration using hrtime
*/
#trackDuration() {
const startTime = process.hrtime()
return {
getSeconds() {
const duration = process.hrtime(startTime)
return (duration[0] + duration[1] / 1e9).toFixed(2)
},
}
}
/**
* Returns the dist folder from the tsconfig.outDir property
* or uses the ".medusa/server" folder
*/
#computeDist(tsConfig: { options: { outDir?: string } }): string {
const distFolder = tsConfig.options.outDir ?? ".medusa/server"
return path.isAbsolute(distFolder)
? distFolder
: path.join(this.#projectRoot, distFolder)
}
/**
* Imports and stores a reference to the TypeScript compiler.
* We dynamically import "typescript", since its is a dev
* only dependency
*/
async #loadTSCompiler() {
if (!this.#tsCompiler) {
this.#tsCompiler = await import("typescript")
}
return this.#tsCompiler
}
/**
* Copies the file to the destination without throwing any
* errors if the source file is missing
*/
async #copy(source: string, destination: string) {
let sourceExists = false
try {
await access(source, constants.F_OK)
sourceExists = true
} catch (error) {
if (error.code !== "ENOENT") {
throw error
}
}
if (sourceExists) {
await copyFile(path.join(source), path.join(destination))
}
}
/**
* Copies package manager files from the project root
* to the specified dist folder
*/
async #copyPkgManagerFiles(dist: string) {
/**
* Copying package manager files
*/
await this.#copy(
path.join(this.#projectRoot, "package.json"),
path.join(dist, "package.json")
)
await this.#copy(
path.join(this.#projectRoot, "yarn.lock"),
path.join(dist, "yarn.lock")
)
await this.#copy(
path.join(this.#projectRoot, "pnpm.lock"),
path.join(dist, "pnpm.lock")
)
await this.#copy(
path.join(this.#projectRoot, "package-lock.json"),
path.join(dist, "package-lock.json")
)
}
/**
* Removes the directory and its children recursively and
* ignores any errors
*/
async #clean(path: string) {
await rm(path, { recursive: true }).catch(() => {})
}
/**
* Returns a boolean indicating if a file extension belongs
* to a JavaScript or TypeScript file
*/
#isScriptFile(filePath: string) {
if (filePath.endsWith(".ts") && !filePath.endsWith(".d.ts")) {
return true
}
return filePath.endsWith(".js")
}
/**
* Loads the medusa config file and prints the error to
* the console (in case of any errors). Otherwise, the
* file path and the parsed config is returned
*/
async #loadMedusaConfig() {
const { configModule, configFilePath, error } =
await getConfigFile<ConfigModule>(this.#projectRoot, "medusa-config")
if (error) {
this.#logger.error(`Failed to load medusa-config.(js|ts) file`)
this.#logger.error(error)
return
}
return { configFilePath, configModule }
}
/**
* Prints typescript diagnostic messages
*/
#printDiagnostics(ts: typeof tsStatic, diagnostics: tsStatic.Diagnostic[]) {
if (diagnostics.length) {
console.error(
ts.formatDiagnosticsWithColorAndContext(
diagnostics,
ts.createCompilerHost({})
)
)
}
}
/**
* Given a tsconfig file, this method will write the compiled
* output to the specified destination
*/
async #emitBuildOutput(
tsConfig: tsStatic.ParsedCommandLine,
chunksToIgnore: string[],
dist: string
): Promise<{
emitResult: tsStatic.EmitResult
diagnostics: tsStatic.Diagnostic[]
}> {
const ts = await this.#loadTSCompiler()
const filesToCompile = tsConfig.fileNames.filter((fileName) => {
const relativeFileName = path.relative(this.#projectRoot, fileName)
return !chunksToIgnore.some((chunk) =>
relativeFileName.includes(`${chunk}`)
)
})
/**
* Create emit program to compile and emit output
*/
const program = ts.createProgram(filesToCompile, {
...tsConfig.options,
...{
outDir: dist,
inlineSourceMap: !tsConfig.options.sourceMap,
},
})
const emitResult = program.emit()
const diagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics)
/**
* Log errors (if any)
*/
this.#printDiagnostics(ts, diagnostics)
return { emitResult, diagnostics }
}
/**
* Loads and parses the TypeScript config file. In case of an error, the errors
* will be logged using the logger and undefined it returned
*/
async loadTSConfigFile(): Promise<tsStatic.ParsedCommandLine | undefined> {
const ts = await this.#loadTSCompiler()
let tsConfigErrors: tsStatic.Diagnostic[] = []
const tsConfig = ts.getParsedCommandLineOfConfigFile(
this.#tsConfigPath,
{
inlineSourceMap: true,
excludes: [],
},
{
...ts.sys,
useCaseSensitiveFileNames: true,
getCurrentDirectory: () => this.#projectRoot,
onUnRecoverableConfigFileDiagnostic: (error) =>
(tsConfigErrors = [error]),
}
)
/**
* Push errors from the tsConfig parsed output to the
* tsConfigErrors array.
*/
if (tsConfig?.errors.length) {
tsConfigErrors.push(...tsConfig.errors)
}
/**
* Display all config errors using the diagnostics reporter
*/
this.#printDiagnostics(ts, tsConfigErrors)
/**
* Return undefined when there are errors in parsing the config
* file
*/
if (tsConfigErrors.length) {
return
}
return tsConfig
}
/**
* Builds the application backend source code using
* TypeScript's official compiler. Also performs
* type-checking
*/
async buildAppBackend(
tsConfig: tsStatic.ParsedCommandLine
): Promise<boolean> {
const tracker = this.#trackDuration()
const dist = this.#computeDist(tsConfig)
this.#logger.info("Compiling backend source...")
/**
* Step 1: Cleanup existing build output
*/
this.#logger.info(
`Removing existing "${path.relative(this.#projectRoot, dist)}" folder`
)
await this.#clean(dist)
/**
* Create first the target directory now that everything is clean
*/
await mkdir(dist, { recursive: true })
/**
* Step 2: Compile TypeScript source code
*/
const { emitResult, diagnostics } = await this.#emitBuildOutput(
tsConfig,
this.#backendIgnoreFiles,
dist
)
/**
* Exit early if no output is written to the disk
*/
if (emitResult.emitSkipped) {
this.#logger.warn("Backend build completed without emitting any output")
return false
}
/**
* Step 3: Copy package manager files to the output folder
*/
await this.#copyPkgManagerFiles(dist)
/**
* Notify about the state of build
*/
if (diagnostics.length) {
this.#logger.warn(
`Backend build completed with errors (${tracker.getSeconds()}s)`
)
return false
}
this.#logger.info(
`Backend build completed successfully (${tracker.getSeconds()}s)`
)
return true
}
/**
* Builds the frontend source code of a Medusa application
* using the "@medusajs/admin-bundler" package.
*/
async buildAppFrontend(
adminOnly: boolean,
tsConfig: tsStatic.ParsedCommandLine,
adminBundler: {
build: (
options: AdminOptions & {
sources: string[]
plugins: string[]
outDir: string
}
) => Promise<void>
}
): Promise<boolean> {
const tracker = this.#trackDuration()
/**
* Step 1: Load the medusa config file to read
* admin options
*/
const configFile = await this.#loadMedusaConfig()
if (!configFile) {
return false
}
/**
* Return early when admin is disabled and we are trying to
* create a bundled build for the admin.
*/
if (configFile.configModule.admin.disable && !adminOnly) {
this.#logger.info(
"Skipping admin build, since its disabled inside the medusa-config file"
)
return true
}
/**
* Warn when we are creating an admin only build, but forgot to disable
* the admin inside the config file
*/
if (!configFile.configModule.admin.disable && adminOnly) {
this.#logger.warn(
`You are building using the flag --admin-only but the admin is enabled in your medusa-config, If you intend to host the dashboard separately you should disable the admin in your medusa config`
)
}
const plugins = await getResolvedPlugins(
this.#projectRoot,
configFile.configModule,
true
)
const adminSources = plugins
.map((plugin) =>
plugin.admin?.type === "local" ? plugin.admin.resolve : undefined
)
.filter(Boolean) as string[]
const adminPlugins = plugins
.map((plugin) =>
plugin.admin?.type === "package" ? plugin.admin.resolve : undefined
)
.filter(Boolean) as string[]
try {
this.#logger.info("Compiling frontend source...")
await adminBundler.build({
disable: false,
sources: adminSources,
plugins: adminPlugins,
...configFile.configModule.admin,
outDir: adminOnly
? this.#adminOnlyDistFolder
: path.join(this.#computeDist(tsConfig), "./public/admin"),
})
this.#logger.info(
`Frontend build completed successfully (${tracker.getSeconds()}s)`
)
return true
} catch (error) {
this.#logger.error("Unable to compile frontend source")
this.#logger.error(error)
return false
}
}
/**
* Compiles the plugin source code to JavaScript using the
* TypeScript's official compiler
*/
async buildPluginBackend(tsConfig: tsStatic.ParsedCommandLine) {
const tracker = this.#trackDuration()
const dist = this.#pluginsDistFolder
this.#logger.info("Compiling plugin source...")
/**
* Step 1: Cleanup existing build output
*/
this.#logger.info(
`Removing existing "${path.relative(this.#projectRoot, dist)}" folder`
)
await this.#clean(dist)
/**
* Step 2: Compile TypeScript source code
*/
const { emitResult, diagnostics } = await this.#emitBuildOutput(
tsConfig,
this.#backendIgnoreFiles,
dist
)
/**
* Exit early if no output is written to the disk
*/
if (emitResult.emitSkipped) {
this.#logger.warn("Plugin build completed without emitting any output")
return false
}
/**
* Notify about the state of build
*/
if (diagnostics.length) {
this.#logger.warn(
`Plugin build completed with errors (${tracker.getSeconds()}s)`
)
return false
}
this.#logger.info(
`Plugin build completed successfully (${tracker.getSeconds()}s)`
)
return true
}
/**
* Compiles the backend source code of a plugin project in watch
* mode. Type-checking is disabled to keep compilation fast.
*
* The "onFileChange" argument can be used to get notified when
* a file has changed.
*/
async developPluginBackend(
transformer: (filePath: string) => Promise<string>,
onFileChange?: (
filePath: string,
action: "add" | "change" | "unlink"
) => void
) {
const fs = new FileSystem(this.#pluginsDistFolder)
await fs.createJson("medusa-plugin-options.json", {
srcDir: path.join(this.#projectRoot, "src"),
})
const watcher = chokidar.watch(["."], {
ignoreInitial: true,
cwd: this.#projectRoot,
ignored: [
/(^|[\\/\\])\../,
"node_modules",
"dist",
"static",
"private",
".medusa",
...this.#backendIgnoreFiles,
],
})
watcher.on("add", async (file) => {
if (!this.#isScriptFile(file)) {
return
}
const relativePath = path.relative(this.#projectRoot, file)
const outputPath = relativePath.replace(/\.ts$/, ".js")
this.#logger.info(`${relativePath} updated: Republishing changes`)
await fs.create(outputPath, await transformer(file))
onFileChange?.(file, "add")
})
watcher.on("change", async (file) => {
if (!this.#isScriptFile(file)) {
return
}
const relativePath = path.relative(this.#projectRoot, file)
const outputPath = relativePath.replace(/\.ts$/, ".js")
this.#logger.info(`${relativePath} updated: Republishing changes`)
await fs.create(outputPath, await transformer(file))
onFileChange?.(file, "change")
})
watcher.on("unlink", async (file) => {
if (!this.#isScriptFile(file)) {
return
}
const relativePath = path.relative(this.#projectRoot, file)
const outputPath = relativePath.replace(/\.ts$/, ".js")
this.#logger.info(`${relativePath} removed: Republishing changes`)
await fs.remove(outputPath)
onFileChange?.(file, "unlink")
})
watcher.on("ready", () => {
this.#logger.info("watching for file changes")
})
}
async #hasPluginAdminExtensions() {
try {
await access(path.join(this.#projectRoot, "src/admin"), constants.F_OK)
return true
} catch (error) {
if (error.code !== "ENOENT") {
throw error
}
return false
}
}
async buildPluginAdminExtensions(bundler: {
plugin: (options: { root: string; outDir: string }) => Promise<void>
}) {
if (!(await this.#hasPluginAdminExtensions())) {
this.#logger.info(
"Skipping plugin admin extensions build, since src/admin does not exist"
)
return true
}
const tracker = this.#trackDuration()
this.#logger.info("Compiling plugin admin extensions...")
try {
await bundler.plugin({
root: this.#projectRoot,
outDir: this.#pluginsDistFolder,
})
this.#logger.info(
`Plugin admin extensions build completed successfully (${tracker.getSeconds()}s)`
)
return true
} catch (error) {
this.#logger.error(`Plugin admin extensions build failed`, error)
return false
}
}
async developPluginAdminExtensions(
bundler: {
plugin: (options: { root: string; outDir: string }) => Promise<void>
},
onFileChange?: (
filePath: string,
action: "add" | "change" | "unlink"
) => void
) {
let isBuilding = false
let hasQueuedBuild = false
let latestQueuedFile: string | undefined
let latestQueuedAction: "add" | "change" | "unlink" | undefined
const rebuild = async (
file: string,
action: "add" | "change" | "unlink"
) => {
if (isBuilding) {
hasQueuedBuild = true
latestQueuedFile = file
latestQueuedAction = action
return
}
let currentFile = file
let currentAction = action
do {
hasQueuedBuild = false
latestQueuedFile = undefined
latestQueuedAction = undefined
isBuilding = true
this.#logger.info(
`${currentFile} updated: Rebuilding admin extensions`
)
const buildSucceeded = await this.buildPluginAdminExtensions(bundler)
isBuilding = false
if (buildSucceeded) {
onFileChange?.(currentFile, currentAction)
}
if (hasQueuedBuild && latestQueuedFile && latestQueuedAction) {
currentFile = latestQueuedFile
currentAction = latestQueuedAction
}
} while (hasQueuedBuild)
}
const watcher = chokidar.watch(["src/admin"], {
ignoreInitial: true,
cwd: this.#projectRoot,
ignored: [/node_modules/, /(^|[\\/\\])\../, ".medusa"],
})
watcher.on("add", (file) => {
void rebuild(file, "add")
})
watcher.on("change", (file) => {
void rebuild(file, "change")
})
watcher.on("unlink", (file) => {
void rebuild(file, "unlink")
})
watcher.on("ready", () => {
this.#logger.info("watching for plugin admin extension file changes")
})
}
}