Skip to content

Commit

Permalink
feat: zero-config integration with vercel analytics (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 25, 2021
1 parent 95ab537 commit 5001a4a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
"minimist": ">=1.2.5"
},
"dependencies": {
"@nuxtjs/web-vitals": "^0.1.4",
"@vercel/build-utils": "2.9.0",
"@vercel/node-bridge": "1.3.2",
"consola": "2.15.3",
"execa": "^5.0.0",
"fs-extra": "9.1.0",
"jiti": "^1.3.0",
"rc9": "^1.2.0",
"replace-in-file": "^6.2.0",
"resolve-from": "^5.0.0",
"semver": "7.3.4"
Expand Down
16 changes: 14 additions & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import consola from 'consola'
import fs from 'fs-extra'
import resolveFrom from 'resolve-from'
import { gte, gt } from 'semver'
import { update as updaterc } from 'rc9'

import { endStep, exec, getNuxtConfig, getNuxtConfigName, globAndPrefix, MutablePackageJson, prepareNodeModules, preparePkgForProd, readJSON, startStep, validateEntrypoint } from './utils'
import { prepareTypescriptEnvironment, compileTypescriptBuildFiles, JsonOptions } from './typescript'
Expand Down Expand Up @@ -99,6 +100,15 @@ export async function build (opts: BuildOptions & { config: NuxtBuilderConfig })
const yarnCachePath = path.join(cachePath, 'yarn')
await fs.mkdirp(yarnCachePath)

// Detect vercel analytics
if (process.env.VERCEL_ANALYTICS_ID) {
consola.log('Vercel Analytics Detected. Adding @nuxtjs/web-vitals to .nuxtrc')
updaterc(
{ 'buildModules[]': require.resolve('@nuxtjs/web-vitals') },
{ dir: entrypointPath, name: '.nuxtrc' }
)
}

// ----------------- Install devDependencies -----------------
startStep('Install devDependencies')

Expand Down Expand Up @@ -150,15 +160,17 @@ export async function build (opts: BuildOptions & { config: NuxtBuilderConfig })
'build',
'--standalone',
'--no-lock', // #19
`--config-file "${nuxtConfigName}"`
`--config-file "${nuxtConfigName}"`,
entrypointPath
], spawnOpts)

if (config.generateStaticRoutes) {
await exec('nuxt', [
'generate',
'--no-build',
'--no-lock', // #19
`--config-file "${nuxtConfigName}"`
`--config-file "${nuxtConfigName}"`,
entrypointPath
], spawnOpts)
}

Expand Down
11 changes: 10 additions & 1 deletion test/build.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const path = require('path')
const fs = require('fs')
const rc9 = require('rc9')
const runBuildLambda = require('./utils/run-build-lambda')

const FOUR_MINUTES = 240000

it('Should build the standard example', async () => {
const { buildResult } = await runBuildLambda(
process.env.VERCEL_ANALYTICS_ID = 'test'
const { buildResult, workPath } = await runBuildLambda(
path.join(__dirname, 'fixture')
)
delete process.env.VERCEL_ANALYTICS_ID

const { output, routes } = buildResult

// Lambda
expect(output.index).toBeDefined()
expect(routes).toBeDefined()
Expand All @@ -22,6 +27,10 @@ it('Should build the standard example', async () => {
for (const file of buildFiles) {
expect(output[file]).toBeDefined()
}

const nuxtrc = rc9.read({ name: '.nuxtrc', dir: path.resolve(workPath, 'www') })
expect(nuxtrc.buildModules).toMatchObject([/@nuxtjs\/web-vitals/])
expect(fs.existsSync(path.resolve(workPath, 'www/.nuxt/vitals.client.js')))
}, FOUR_MINUTES)

it('Should build a Typescript example', async () => {
Expand Down
8 changes: 1 addition & 7 deletions test/fixture/www/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
const { resolve } = require('path')

module.exports = {
rootDir: resolve(__dirname, '../..'),
buildDir: resolve(__dirname, '.nuxt'),
srcDir: __dirname
}
export default {}
3 changes: 2 additions & 1 deletion test/utils/run-build-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ async function runBuildLambda (inputPath) {
return {
analyzeResult,
buildResult,
cacheResult
cacheResult,
workPath
}
}

Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,14 @@
eslint-plugin-unicorn "^23.0.0"
eslint-plugin-vue "^7.1.0"

"@nuxtjs/web-vitals@^0.1.4":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@nuxtjs/web-vitals/-/web-vitals-0.1.4.tgz#f7ecd908c7c6e42d3b9f2270267992ce6d7caeab"
integrity sha512-//63OcBoOzXyv2G5EA32Z/gL3naQf6SIarin0coz+/wBimO2U38jRmAdHaao8bCMVasojoyg95sSdzu7Per4DQ==
dependencies:
defu "^3.2.2"
web-vitals "^1.1.0"

"@nuxtjs/youch@^4.2.3":
version "4.2.3"
resolved "https://registry.yarnpkg.com/@nuxtjs/youch/-/youch-4.2.3.tgz#36f8b22df5a0efaa81373109851e1d857aca6bed"
Expand Down Expand Up @@ -12118,6 +12126,11 @@ wcwidth@^1.0.1:
dependencies:
defaults "^1.0.3"

web-vitals@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/web-vitals/-/web-vitals-1.1.0.tgz#7f410d9a1f7a1cd5d952806b45776204b47dc274"
integrity sha512-1cx54eRxY/+M0KNKdNpNnuXAXG+vJEvwScV4DiV9rOYDguHoeDIzm09ghBohOPtkqPO5OtPC14FWkNva3SDisg==

webidl-conversions@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-5.0.0.tgz#ae59c8a00b121543a2acc65c0434f57b0fc11aff"
Expand Down

0 comments on commit 5001a4a

Please sign in to comment.