1
- const path = require ( 'path' )
2
- const resolveFrom = require ( 'resolve-from' )
3
- const fs = require ( 'fs-extra' )
4
- const semver = require ( 'semver' )
5
- const consola = require ( 'consola' )
6
- const esm = require ( 'esm' )
1
+ import path from 'path'
2
+ import resolveFrom from 'resolve-from'
3
+ import fs from 'fs-extra'
4
+ import { gte , gt } from 'semver'
5
+ import consola from 'consola'
6
+ import esm from 'esm'
7
7
8
- const { createLambda, download, FileFsRef, FileBlob, getNodeVersion, getSpawnOptions } = require ( '@now/build-utils' )
8
+ import { createLambda , download , FileFsRef , FileBlob , glob , getNodeVersion , getSpawnOptions , BuildOptions , Route , Lambda , File , PackageJson } from '@now/build-utils'
9
9
10
- const { exec, validateEntrypoint, globAndPrefix, glob , preparePkgForProd, startStep, endStep } = require ( './utils' )
10
+ import { exec , validateEntrypoint , globAndPrefix , preparePkgForProd , startStep , endStep } from './utils'
11
11
12
- async function build ( { files, entrypoint, workPath, config = { } , meta = { } } ) {
12
+ interface BuilderOutput {
13
+ watch ?: string [ ] ;
14
+ output : Record < string , Lambda | File | FileFsRef > ;
15
+ routes : Route [ ] ;
16
+ }
17
+
18
+ export async function build ( { files, entrypoint, workPath, config = { } , meta = { } } : BuildOptions ) : Promise < BuilderOutput > {
13
19
// ----------------- Prepare build -----------------
14
20
startStep ( 'Prepare build' )
15
21
@@ -30,7 +36,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
30
36
consola . log ( 'Working directory:' , process . cwd ( ) )
31
37
32
38
// Read package.json
33
- let pkg
39
+ let pkg : PackageJson
34
40
try {
35
41
pkg = await fs . readJson ( 'package.json' )
36
42
} catch ( e ) {
@@ -40,13 +46,16 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
40
46
// Node version
41
47
const nodeVersion = await getNodeVersion ( rootDir )
42
48
const spawnOpts = getSpawnOptions ( meta , nodeVersion )
49
+ if ( ! spawnOpts . env ) {
50
+ spawnOpts . env = { }
51
+ }
43
52
44
53
const usesTypescript = pkg . dependencies && ( Object . keys ( pkg . dependencies ) . includes ( '@nuxt/typescript-build' ) || Object . keys ( pkg . dependencies ) . includes ( '@nuxt/typescript' ) )
45
54
if ( usesTypescript ) {
46
- spawnOpts . env . NODE_PRESERVE_SYMLINKS = 1
55
+ spawnOpts . env . NODE_PRESERVE_SYMLINKS = '1'
47
56
}
48
57
49
- if ( usesTypescript && ( await fs . exists ( 'tsconfig.json' ) ) ) {
58
+ if ( usesTypescript && ( fs . existsSync ( 'tsconfig.json' ) ) ) {
50
59
let tsConfig
51
60
try {
52
61
tsConfig = await fs . readJson ( 'tsconfig.json' )
@@ -58,7 +67,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
58
67
}
59
68
60
69
// Detect npm (prefer yarn)
61
- const isYarn = ! await fs . exists ( 'package-lock.json' )
70
+ const isYarn = ! fs . existsSync ( 'package-lock.json' )
62
71
consola . log ( 'Using' , isYarn ? 'yarn' : 'npm' )
63
72
64
73
// Write .npmrc
@@ -68,7 +77,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
68
77
}
69
78
70
79
// Write .yarnclean
71
- if ( isYarn && ! await fs . exists ( '.yarnclean' ) ) {
80
+ if ( isYarn && ! fs . existsSync ( '.yarnclean' ) ) {
72
81
await fs . copyFile ( path . join ( __dirname , '.yarnclean' ) , '.yarnclean' )
73
82
}
74
83
@@ -84,7 +93,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
84
93
85
94
// Prepare node_modules
86
95
await fs . mkdirp ( 'node_modules_dev' )
87
- if ( await fs . exists ( 'node_modules' ) ) {
96
+ if ( fs . existsSync ( 'node_modules' ) ) {
88
97
await fs . unlink ( 'node_modules' )
89
98
}
90
99
await fs . symlink ( 'node_modules_dev' , 'node_modules' )
@@ -123,7 +132,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
123
132
// Execute nuxt build
124
133
spawnOpts . env . NODE_ENV = 'production'
125
134
126
- if ( await fs . exists ( buildDir ) ) {
135
+ if ( fs . existsSync ( buildDir ) ) {
127
136
consola . warn ( buildDir , 'exists! Please ensure to ignore it with `.nowignore`' )
128
137
}
129
138
@@ -139,7 +148,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
139
148
140
149
// Use node_modules_prod
141
150
await fs . mkdirp ( 'node_modules_prod' )
142
- if ( await fs . exists ( 'node_modules' ) ) {
151
+ if ( fs . existsSync ( 'node_modules' ) ) {
143
152
await fs . unlink ( 'node_modules' )
144
153
}
145
154
await fs . symlink ( 'node_modules_prod' , 'node_modules' )
@@ -165,10 +174,10 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
165
174
166
175
// Validate nuxt version
167
176
const nuxtPkg = require ( resolveFrom ( rootDir , `@nuxt/core${ nuxtDep . suffix } /package.json` ) )
168
- if ( ! semver . gte ( nuxtPkg . version , '2.4.0' ) ) {
177
+ if ( ! gte ( nuxtPkg . version , '2.4.0' ) ) {
169
178
throw new Error ( `nuxt >= 2.4.0 is required, detected version ${ nuxtPkg . version } ` )
170
179
}
171
- if ( semver . gt ( nuxtPkg . version , '3.0.0' ) ) {
180
+ if ( gt ( nuxtPkg . version , '3.0.0' ) ) {
172
181
consola . warn ( 'WARNING: nuxt >= 3.0.0 is not tested against this builder!' )
173
182
}
174
183
@@ -196,7 +205,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
196
205
const nodeModules = await globAndPrefix ( '**' , nodeModulesDir , 'node_modules' )
197
206
198
207
// Lambdas
199
- const lambdas = { }
208
+ const lambdas : Record < string , Lambda > = { }
200
209
201
210
const launcherPath = path . join ( __dirname , 'launcher.js' )
202
211
const launcherSrc = ( await fs . readFile ( launcherPath , 'utf8' ) )
@@ -213,7 +222,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
213
222
214
223
// Extra files to be included in lambda
215
224
const serverFiles = [
216
- ...( config . serverFiles || [ ] ) ,
225
+ ...( Array . isArray ( config . serverFiles ) ? config . serverFiles : [ ] ) ,
217
226
'package.json'
218
227
]
219
228
@@ -249,5 +258,3 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
249
258
]
250
259
}
251
260
}
252
-
253
- module . exports = build
0 commit comments