1 | import fs from 'fs-extra'
|
2 | import { build as esbuild } from 'esbuild'
|
3 |
|
4 | import { outputLambdas } from './outputLambdas'
|
5 | import { getFiles, isStatic, isDynamic } from './getFiles'
|
6 | import { renderStaticEntries } from './renderStaticEntries'
|
7 | import { timer } from './timer'
|
8 | import * as logger from './log'
|
9 | import { Presta } from './types'
|
10 |
|
11 | function getRoutesManifestSafely(manifestFilepath: string) {
|
12 | try {
|
13 | return require(manifestFilepath)
|
14 | } catch (e) {
|
15 | return {}
|
16 | }
|
17 | }
|
18 |
|
19 | export async function build(config: Presta) {
|
20 | const totalTime = timer()
|
21 | const files = getFiles(config)
|
22 | const staticIds = files.filter(isStatic)
|
23 | const dynamicIds = files.filter(isDynamic)
|
24 |
|
25 | logger.debug({
|
26 | label: 'build',
|
27 | message: 'starting build',
|
28 | })
|
29 |
|
30 | if (!staticIds.length && !dynamicIds.length) {
|
31 | logger.warn({
|
32 | label: 'files',
|
33 | message: 'no files were found, nothing to build',
|
34 | })
|
35 | } else {
|
36 | let staticTime = ''
|
37 | let staticFileAmount = 0
|
38 | let dynamicTime = ''
|
39 | let copyTime = ''
|
40 |
|
41 | const tasks = await Promise.allSettled([
|
42 | (async () => {
|
43 | if (staticIds.length) {
|
44 | const time = timer()
|
45 |
|
46 | const { allGeneratedFiles } = await renderStaticEntries(staticIds, config)
|
47 |
|
48 | staticTime = time()
|
49 | staticFileAmount = allGeneratedFiles.length
|
50 | }
|
51 | })(),
|
52 | (async () => {
|
53 | if (dynamicIds.length) {
|
54 | const time = timer()
|
55 |
|
56 | outputLambdas(dynamicIds, config)
|
57 |
|
58 | await esbuild({
|
59 | entryPoints: Object.values(require(config.functionsManifest)),
|
60 | outdir: config.functionsOutputDir,
|
61 | bundle: true,
|
62 | platform: 'node',
|
63 | target: ['node12'],
|
64 | minify: true,
|
65 | allowOverwrite: true,
|
66 | define: { PRESTA_SERVERLESS_RUNTIME: 'true' },
|
67 | })
|
68 |
|
69 | dynamicTime = time()
|
70 | }
|
71 | })(),
|
72 | (async () => {
|
73 | if (fs.existsSync(config.assets)) {
|
74 | const time = timer()
|
75 |
|
76 | fs.copySync(config.assets, config.staticOutputDir)
|
77 |
|
78 | copyTime = time()
|
79 | }
|
80 | })(),
|
81 | ])
|
82 |
|
83 |
|
84 | if (tasks.find((task) => task.status === 'rejected')) {
|
85 | logger.debug({
|
86 | label: 'build',
|
87 | message: 'build partially failed',
|
88 | })
|
89 |
|
90 |
|
91 | tasks.forEach((task) => {
|
92 | if (task.status === 'rejected') {
|
93 | logger.error({
|
94 | label: 'error',
|
95 | error: task.reason,
|
96 | })
|
97 | }
|
98 | })
|
99 |
|
100 | process.exit(1)
|
101 | return
|
102 | }
|
103 |
|
104 | if (staticTime || dynamicTime) {
|
105 | logger.newline()
|
106 | }
|
107 |
|
108 | if (staticTime) {
|
109 | logger.info({
|
110 | label: 'static',
|
111 | message: `rendered ${staticFileAmount} file(s)`,
|
112 | duration: staticTime,
|
113 | })
|
114 | }
|
115 |
|
116 | if (dynamicTime) {
|
117 | logger.info({
|
118 | label: 'lambda',
|
119 | message: `compiled ${dynamicIds.length} function(s)`,
|
120 | duration: dynamicTime,
|
121 | })
|
122 | }
|
123 |
|
124 | if (copyTime) {
|
125 | logger.info({
|
126 | label: 'assets',
|
127 | message: `copied in ${copyTime}`,
|
128 | })
|
129 | }
|
130 |
|
131 | config.hooks.emitPostBuild({
|
132 | output: config.output,
|
133 | staticOutput: config.staticOutputDir,
|
134 | functionsOutput: config.functionsOutputDir,
|
135 | functionsManifest: getRoutesManifestSafely(config.functionsManifest),
|
136 | })
|
137 |
|
138 | if (staticTime || dynamicTime) {
|
139 | logger.newline()
|
140 | logger.info({
|
141 | label: 'complete',
|
142 | message: `in ${totalTime()}`,
|
143 | })
|
144 | logger.newline()
|
145 | }
|
146 | }
|
147 | }
|
148 |
|
\ | No newline at end of file |