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: {
|
67 | 'process.env.PRESTA_SERVERLESS_RUNTIME': 'true',
|
68 | },
|
69 | })
|
70 |
|
71 | dynamicTime = time()
|
72 | }
|
73 | })(),
|
74 | (async () => {
|
75 | if (fs.existsSync(config.assets)) {
|
76 | const time = timer()
|
77 |
|
78 | fs.copySync(config.assets, config.staticOutputDir)
|
79 |
|
80 | copyTime = time()
|
81 | }
|
82 | })(),
|
83 | ])
|
84 |
|
85 |
|
86 | if (tasks.find((task) => task.status === 'rejected')) {
|
87 | logger.debug({
|
88 | label: 'build',
|
89 | message: 'build partially failed',
|
90 | })
|
91 |
|
92 |
|
93 | tasks.forEach((task) => {
|
94 | if (task.status === 'rejected') {
|
95 | logger.error({
|
96 | label: 'error',
|
97 | error: task.reason,
|
98 | })
|
99 | }
|
100 | })
|
101 |
|
102 | process.exit(1)
|
103 | return
|
104 | }
|
105 |
|
106 | if (staticTime || dynamicTime) {
|
107 | logger.newline()
|
108 | }
|
109 |
|
110 | if (staticTime) {
|
111 | logger.info({
|
112 | label: 'static',
|
113 | message: `rendered ${staticFileAmount} file(s)`,
|
114 | duration: staticTime,
|
115 | })
|
116 | }
|
117 |
|
118 | if (dynamicTime) {
|
119 | logger.info({
|
120 | label: 'lambda',
|
121 | message: `compiled ${dynamicIds.length} function(s)`,
|
122 | duration: dynamicTime,
|
123 | })
|
124 | }
|
125 |
|
126 | if (copyTime) {
|
127 | logger.info({
|
128 | label: 'assets',
|
129 | message: `copied in ${copyTime}`,
|
130 | })
|
131 | }
|
132 |
|
133 | config.hooks.emitPostBuild({
|
134 | output: config.output,
|
135 | staticOutput: config.staticOutputDir,
|
136 | functionsOutput: config.functionsOutputDir,
|
137 | functionsManifest: getRoutesManifestSafely(config.functionsManifest),
|
138 | })
|
139 |
|
140 | if (staticTime || dynamicTime) {
|
141 | logger.newline()
|
142 | logger.info({
|
143 | label: 'complete',
|
144 | message: `in ${totalTime()}`,
|
145 | })
|
146 | logger.newline()
|
147 | }
|
148 | }
|
149 | }
|
150 |
|
\ | No newline at end of file |