feat: update vercel and improve internals

This commit is contained in:
Pooya Parsa 2020-11-10 19:19:24 +01:00
parent 1df092d08e
commit c7b88defa4
6 changed files with 58 additions and 17 deletions

View File

@ -7,7 +7,7 @@ import gzipSize from 'gzip-size'
import chalk from 'chalk'
import { emptyDir, readFile } from 'fs-extra'
import { getRollupConfig } from './rollup/config'
import { hl, prettyPath, serializeTemplate, writeFileP } from './utils'
import { hl, prettyPath, serializeTemplate, writeFile } from './utils'
import { SLSOptions } from './config'
export async function build (options: SLSOptions) {
@ -24,8 +24,7 @@ export async function build (options: SLSOptions) {
htmlTemplate.contents = await readFile(htmlTemplate.src, 'utf-8')
htmlTemplate.compiled = serializeTemplate(htmlTemplate.contents)
await hooks.callHook('template:document', htmlTemplate)
await writeFileP(htmlTemplate.dst, htmlTemplate.compiled)
consola.info('Generated', prettyPath(htmlTemplate.dst))
await writeFile(htmlTemplate.dst, htmlTemplate.compiled)
emptyDir(options.slsDir)

View File

@ -92,6 +92,7 @@ export function getoptions (nuxt: SLSNuxt): SLSOptions {
options.slsDir = resolve(options.rootDir, options.slsDir || '.sls')
options.targetDir = options.targetDir ? resolvePath(options, options.targetDir) : resolve(options.slsDir, target)
options.publicDir = resolvePath(options, options.publicDir)
return options
}

View File

@ -26,6 +26,8 @@ export default <Module> function slsModule () {
]))
}
nuxt.options.generate.dir = options.publicDir
if (options.nuxtHooks) {
nuxt.addHooks(options.nuxtHooks)
}

View File

@ -1,5 +1,5 @@
import Module from 'module'
import { basename, extname, resolve } from 'path'
import { basename, dirname, extname, resolve } from 'path'
import { InputOptions, OutputOptions } from 'rollup'
import { terser } from 'rollup-plugin-terser'
import commonjs from '@rollup/plugin-commonjs'
@ -50,10 +50,12 @@ export const getRollupConfig = (config: SLSOptions) => {
external.push(...Module.builtinModules)
}
const outFile = resolve(config.targetDir, config.outName)
const options: RollupConfig = {
input: resolvePath(config, config.entry),
output: {
file: resolve(config.targetDir, config.outName),
file: outFile,
format: 'cjs',
intro: '',
outro: '',
@ -84,8 +86,8 @@ export const getRollupConfig = (config: SLSOptions) => {
// Dynamic Require Support
options.plugins.push(dynamicRequire({
dir: resolve(config.buildDir, 'dist/server'),
outDir: (config.node === false || config.inlineChunks) ? undefined : config.targetDir,
chunksDir: '_' + basename(config.outName, extname(config.outName)),
outDir: (config.node === false || config.inlineChunks) ? undefined : dirname(outFile),
chunksDir: '_' + basename(outFile, extname(outFile)),
globbyOptions: {
ignore: [
'server.js'

View File

@ -1,12 +1,47 @@
import { extendTarget } from '../utils'
import { resolve } from 'path'
import { extendTarget, writeFile } from '../utils'
import { SLSTarget } from '../config'
import { node } from './node'
export const vercel: SLSTarget = extendTarget(node, {
targetDir: '{{ rootDir }}/.vercel_build_output/functions/node/api/_nuxt',
outName: 'index.js',
targetDir: '{{ rootDir }}/.vercel_build_output',
outName: 'functions/_nuxt/index.js',
publicDir: '{{ targetDir }}/static',
inlineChunks: false,
generateIgnore: [
'vercel.json'
]
],
hooks: {
async done ({ targetDir }) {
await wrtieRoutes({ targetDir })
}
}
})
async function wrtieRoutes ({ targetDir }) {
const routes = [
{
src: '/sw.js',
headers: {
'cache-control': 'public, max-age=0, must-revalidate'
},
continue: true
},
{
src: '/_nuxt/(.*)',
headers: {
'cache-control': 'public,max-age=31536000,immutable'
},
continue: true
},
{
handle: 'filesystem'
},
{
src: '(.*)',
dest: '/.vercel/functions/_nuxt'
}
]
await writeFile(resolve(targetDir, 'routes.json'), JSON.stringify(routes, null, 2))
}

View File

@ -1,8 +1,9 @@
import { relative, dirname, resolve } from 'path'
import { writeFile, mkdirp } from 'fs-extra'
import fse from 'fs-extra'
import jiti from 'jiti'
import defu from 'defu'
import Hookable from 'hookable'
import consola from 'consola'
import { SLSOptions, UnresolvedPath, SLSTarget, SLSTargetFn, SLSConfig } from './config'
export function hl (str: string) {
@ -23,11 +24,6 @@ export function serializeTemplate (contents: string) {
return `export default (params) => \`${contents.replace(/{{ (\w+) }}/g, '${params.$1}')}\``
}
export async function writeFileP (path: string, contents: string) {
await mkdirp(dirname(path))
await writeFile(path, contents)
}
export function jitiImport (dir: string, path: string) {
return jiti(dir)(path)
}
@ -38,6 +34,12 @@ export function tryImport (dir: string, path: string) {
} catch (_err) { }
}
export async function writeFile (file, contents) {
await fse.mkdirp(dirname(file))
await fse.writeFile(file, contents, 'utf-8')
consola.info('Generated ', prettyPath(file))
}
export function resolvePath (options: SLSOptions, path: UnresolvedPath, resolveBase: string = '') {
if (typeof path === 'function') {
path = path(options)