Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.

Commit ce45247

Browse files
authored
Convert project to typescript (#125)
1 parent 651334a commit ce45247

16 files changed

+274
-105
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
# Tests
3131
- run:
3232
name: Tests
33-
command: yarn jest
33+
command: yarn tsc && yarn jest
3434

3535
# Coverage
3636
- run:

.eslintrc.yml

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1-
extends: "@nuxtjs"
1+
extends:
2+
- "plugin:@typescript-eslint/recommended"
3+
- "@nuxtjs"
24
rules:
35
no-console: 1
6+
"@typescript-eslint/no-var-requires": 0
7+
plugins:
8+
- "@typescript-eslint"
9+
parserOptions:
10+
parser: "@typescript-eslint/parser"
11+
tsconfigRootDir: "./src"
12+
ecmaFeatures:
13+
jsx: false
14+
useJSXTextNode: false
15+
ecmaVersion: 11
16+
sourceType: "module"

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ coverage
99
dist
1010
.cache
1111
.tmp
12+
lib
13+
!lib/.yarnclean

examples/.eslintrc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rules:
2+
"@typescript-eslint/explicit-function-return-type": 0

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'esm';

lib/config.js

-5
This file was deleted.

package.json

+13-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
"lib"
99
],
1010
"scripts": {
11-
"lint": "eslint --ext .vue,.js lib examples test",
12-
"test": "jest test",
11+
"lint": "eslint --ext .vue,.js,.ts src examples test",
12+
"test": "tsc && jest test",
1313
"update-fixture": "cd test/fixture && yarn && rm -rf node_modules",
1414
"update-example": "cd test/example && yarn && rm -rf node_modules",
15+
"prepublish": "tsc",
1516
"release": "standard-version && git push --follow-tags && npm publish"
1617
},
1718
"dependencies": {
@@ -26,6 +27,14 @@
2627
"devDependencies": {
2728
"@now/build-utils": "^0.10.0",
2829
"@nuxtjs/eslint-config": "^1.1.2",
30+
"@types/execa": "^0.9.0",
31+
"@types/fs-extra": "^8.0.0",
32+
"@types/glob": "^7.1.1",
33+
"@types/node": "^12.7.4",
34+
"@types/resolve-from": "^5.0.1",
35+
"@types/semver": "^6.0.2",
36+
"@typescript-eslint/eslint-plugin": "^2.2.0",
37+
"@typescript-eslint/parser": "^2.2.0",
2938
"codecov": "^3.5.0",
3039
"eslint": "^6.3.0",
3140
"eslint-config-standard": "^14.1.0",
@@ -36,6 +45,7 @@
3645
"eslint-plugin-standard": "^4.0.1",
3746
"eslint-plugin-vue": "^5.2.3",
3847
"jest": "^24.9.0",
39-
"standard-version": "^7.0.0"
48+
"standard-version": "^7.0.0",
49+
"typescript": "^3.6.2"
4050
}
4151
}

lib/build.js renamed to src/build.ts

+30-23
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
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'
77

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'
99

10-
const { exec, validateEntrypoint, globAndPrefix, glob, preparePkgForProd, startStep, endStep } = require('./utils')
10+
import { exec, validateEntrypoint, globAndPrefix, preparePkgForProd, startStep, endStep } from './utils'
1111

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> {
1319
// ----------------- Prepare build -----------------
1420
startStep('Prepare build')
1521

@@ -30,7 +36,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
3036
consola.log('Working directory:', process.cwd())
3137

3238
// Read package.json
33-
let pkg
39+
let pkg: PackageJson
3440
try {
3541
pkg = await fs.readJson('package.json')
3642
} catch (e) {
@@ -40,13 +46,16 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
4046
// Node version
4147
const nodeVersion = await getNodeVersion(rootDir)
4248
const spawnOpts = getSpawnOptions(meta, nodeVersion)
49+
if (!spawnOpts.env) {
50+
spawnOpts.env = {}
51+
}
4352

4453
const usesTypescript = pkg.dependencies && (Object.keys(pkg.dependencies).includes('@nuxt/typescript-build') || Object.keys(pkg.dependencies).includes('@nuxt/typescript'))
4554
if (usesTypescript) {
46-
spawnOpts.env.NODE_PRESERVE_SYMLINKS = 1
55+
spawnOpts.env.NODE_PRESERVE_SYMLINKS = '1'
4756
}
4857

49-
if (usesTypescript && (await fs.exists('tsconfig.json'))) {
58+
if (usesTypescript && (fs.existsSync('tsconfig.json'))) {
5059
let tsConfig
5160
try {
5261
tsConfig = await fs.readJson('tsconfig.json')
@@ -58,7 +67,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
5867
}
5968

6069
// Detect npm (prefer yarn)
61-
const isYarn = !await fs.exists('package-lock.json')
70+
const isYarn = !fs.existsSync('package-lock.json')
6271
consola.log('Using', isYarn ? 'yarn' : 'npm')
6372

6473
// Write .npmrc
@@ -68,7 +77,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
6877
}
6978

7079
// Write .yarnclean
71-
if (isYarn && !await fs.exists('.yarnclean')) {
80+
if (isYarn && !fs.existsSync('.yarnclean')) {
7281
await fs.copyFile(path.join(__dirname, '.yarnclean'), '.yarnclean')
7382
}
7483

@@ -84,7 +93,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
8493

8594
// Prepare node_modules
8695
await fs.mkdirp('node_modules_dev')
87-
if (await fs.exists('node_modules')) {
96+
if (fs.existsSync('node_modules')) {
8897
await fs.unlink('node_modules')
8998
}
9099
await fs.symlink('node_modules_dev', 'node_modules')
@@ -123,7 +132,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
123132
// Execute nuxt build
124133
spawnOpts.env.NODE_ENV = 'production'
125134

126-
if (await fs.exists(buildDir)) {
135+
if (fs.existsSync(buildDir)) {
127136
consola.warn(buildDir, 'exists! Please ensure to ignore it with `.nowignore`')
128137
}
129138

@@ -139,7 +148,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
139148

140149
// Use node_modules_prod
141150
await fs.mkdirp('node_modules_prod')
142-
if (await fs.exists('node_modules')) {
151+
if (fs.existsSync('node_modules')) {
143152
await fs.unlink('node_modules')
144153
}
145154
await fs.symlink('node_modules_prod', 'node_modules')
@@ -165,10 +174,10 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
165174

166175
// Validate nuxt version
167176
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')) {
169178
throw new Error(`nuxt >= 2.4.0 is required, detected version ${nuxtPkg.version}`)
170179
}
171-
if (semver.gt(nuxtPkg.version, '3.0.0')) {
180+
if (gt(nuxtPkg.version, '3.0.0')) {
172181
consola.warn('WARNING: nuxt >= 3.0.0 is not tested against this builder!')
173182
}
174183

@@ -196,7 +205,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
196205
const nodeModules = await globAndPrefix('**', nodeModulesDir, 'node_modules')
197206

198207
// Lambdas
199-
const lambdas = {}
208+
const lambdas: Record<string, Lambda> = {}
200209

201210
const launcherPath = path.join(__dirname, 'launcher.js')
202211
const launcherSrc = (await fs.readFile(launcherPath, 'utf8'))
@@ -213,7 +222,7 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
213222

214223
// Extra files to be included in lambda
215224
const serverFiles = [
216-
...(config.serverFiles || []),
225+
...(Array.isArray(config.serverFiles) ? config.serverFiles : []),
217226
'package.json'
218227
]
219228

@@ -249,5 +258,3 @@ async function build ({ files, entrypoint, workPath, config = {}, meta = {} }) {
249258
]
250259
}
251260
}
252-
253-
module.exports = build

src/config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Config } from '@now/build-utils'
2+
3+
const config: Config = {
4+
maxLambdaSize: '50mb'
5+
}
6+
7+
export default config

lib/index.js renamed to src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const build = require('./build')
2-
const config = require('./config')
3-
const prepareCache = require('./prepare-cache')
1+
import { build } from './build'
2+
import config from './config'
3+
import prepareCache from './prepare-cache'
44

55
// Docs: https://zeit.co/docs/v2/deployments/builders/developer-guide/
66
module.exports = {

lib/launcher.js renamed to src/launcher.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { IncomingMessage, ServerResponse } from 'http'
2+
import esmCompiler from 'esm'
3+
14
const startTime = process.hrtime()
25

36
// Load Config
4-
const esm = require('esm')(module, {
7+
const esm = esmCompiler(module, {
58
cjs: {
69
dedefault: true
710
}
@@ -23,17 +26,17 @@ const readyPromise = nuxt.ready().then(() => {
2326
const hrTimeMs = ((hrTime[0] * 1e9) + hrTime[1]) / 1e6
2427
// eslint-disable-next-line no-console
2528
console.log(`λ Cold start took: ${hrTimeMs}ms`)
26-
}).catch((error) => {
29+
}).catch((error: string | Error) => {
2730
// eslint-disable-next-line no-console
2831
console.error('λ Error while initializing nuxt:', error)
2932
process.exit(1)
3033
})
3134

32-
// Create brdige and start listening
35+
// Create bridge and start listening
3336
const { Server } = require('http') // eslint-disable-line import/order
3437
const { Bridge } = require('./now__bridge.js')
3538

36-
const server = new Server(async (req, res) => {
39+
const server = new Server(async (req: IncomingMessage, res: ServerResponse): Promise<void> => {
3740
if (!isReady) {
3841
await readyPromise
3942
}
@@ -43,4 +46,4 @@ const bridge = new Bridge(server)
4346

4447
bridge.listen()
4548

46-
exports.launcher = bridge.launcher
49+
export const launcher = bridge.launcher

lib/prepare-cache.js renamed to src/prepare-cache.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
const path = require('path')
2-
const fs = require('fs-extra')
3-
const consola = require('consola')
4-
const { glob, startStep, endStep } = require('./utils')
1+
import path from 'path'
2+
import { PrepareCacheOptions, glob, FileRef } from '@now/build-utils'
53

6-
async function prepareCache ({ prepareCachePath, workPath, entrypoint }) {
4+
import fs from 'fs-extra'
5+
import consola from 'consola'
6+
import { startStep, endStep } from './utils'
7+
8+
// Amend below line once https://github.com/zeit/now/issues/2992 is resolved
9+
async function prepareCache ({ prepareCachePath, workPath, entrypoint }: PrepareCacheOptions & { prepareCachePath: string }): Promise<Record<string, FileRef>> {
710
const entryDir = path.dirname(entrypoint)
811
const rootDir = path.join(workPath, entryDir)
912
const cacheDir = path.join(prepareCachePath, entryDir)
@@ -16,11 +19,11 @@ async function prepareCache ({ prepareCachePath, workPath, entrypoint }) {
1619
endStep()
1720

1821
startStep('Collect cache')
19-
const cache = {}
22+
const cache: Record<string, FileRef> = {}
2023
for (const dir of ['.now_cache', 'node_modules_dev', 'node_modules_prod']) {
2124
const src = path.join(rootDir, dir)
2225
const dst = path.join(cacheDir, dir)
23-
if (!await fs.exists(src)) {
26+
if (!fs.existsSync(src)) {
2427
consola.warn(src, 'not exists. skipping!')
2528
continue
2629
}
@@ -34,4 +37,4 @@ async function prepareCache ({ prepareCachePath, workPath, entrypoint }) {
3437
return cache
3538
}
3639

37-
module.exports = prepareCache
40+
export default prepareCache

0 commit comments

Comments
 (0)