feat: make browser target working again

This commit is contained in:
Pooya Parsa 2020-11-05 22:23:24 +01:00
parent 4185ec896f
commit 9d02552c3c
4 changed files with 23 additions and 9 deletions

View File

@ -33,7 +33,11 @@ export async function build (options: SLSOptions) {
) )
for (const tmpl of options.templates) { for (const tmpl of options.templates) {
const dstPath = resolve(options.targetDir, tmpl.dst) let dst = tmpl.dst
if (typeof dst === 'function') {
dst = dst(options)
}
const dstPath = resolve(options.targetDir, dst)
await renderTemplate(tmpl.src, dstPath, { options }) await renderTemplate(tmpl.src, dstPath, { options })
consola.info('Compiled', prettyPath(dstPath)) consola.info('Compiled', prettyPath(dstPath))
} }

View File

@ -1,13 +1,14 @@
import { resolve } from 'path' import { resolve } from 'path'
import defu from 'defu' import defu from 'defu'
import { NuxtOptions } from '@nuxt/types' import { NuxtOptions } from '@nuxt/types'
import { tryImport, LIB_DIR } from './utils' import { tryImport, LIB_DIR, resolvePath } from './utils'
export type UnresolvedPath = string | ((SLSOptions) => string)
export interface SLSOptions { export interface SLSOptions {
node: false node: false
target: 'worker' | 'node' | string target: string
entry: string entry: string
outDir: string
slsDir: string slsDir: string
outName: string outName: string
logStartup: boolean logStartup: boolean
@ -17,7 +18,7 @@ export interface SLSOptions {
staticDir: string staticDir: string
targetDir: string targetDir: string
rootDir: string rootDir: string
templates: { src: string, dst: string }[] templates: { src: string, dst: UnresolvedPath }[]
static: string[] static: string[]
renderer: string renderer: string
nuxt: 2 | 3 nuxt: 2 | 3
@ -34,7 +35,9 @@ export interface SLSOptions {
hooks: { [key: string]: any } // TODO: export from hookable hooks: { [key: string]: any } // TODO: export from hookable
} }
export interface SLSConfig extends Partial<SLSOptions> {} export interface SLSConfig extends Omit<Partial<SLSOptions>, 'targetDir'> {
targetDir: UnresolvedPath
}
export function getoptions (nuxtOptions: NuxtOptions): SLSOptions { export function getoptions (nuxtOptions: NuxtOptions): SLSOptions {
const defaults: SLSConfig = { const defaults: SLSConfig = {
@ -49,7 +52,8 @@ export function getoptions (nuxtOptions: NuxtOptions): SLSOptions {
static: [], static: [],
nuxt: 2, nuxt: 2,
logStartup: true, logStartup: true,
inlineChunks: true inlineChunks: true,
targetDir: null
} }
let target = process.env.NUXT_SLS_TARGET || nuxtOptions.serverless.target || 'node' let target = process.env.NUXT_SLS_TARGET || nuxtOptions.serverless.target || 'node'
@ -67,7 +71,7 @@ export function getoptions (nuxtOptions: NuxtOptions): SLSOptions {
options.buildDir = resolve(options.rootDir, options.buildDir || '.nuxt') options.buildDir = resolve(options.rootDir, options.buildDir || '.nuxt')
options.publicDir = resolve(options.rootDir, options.publicDir || 'dist') options.publicDir = resolve(options.rootDir, options.publicDir || 'dist')
options.slsDir = resolve(options.rootDir, options.slsDir || '.sls') options.slsDir = resolve(options.rootDir, options.slsDir || '.sls')
options.targetDir = resolve(options.slsDir, target) options.targetDir = options.targetDir ? resolvePath(options, options.targetDir) : resolve(options.slsDir, target)
return options return options
} }

View File

@ -10,7 +10,7 @@ import replace from '@rollup/plugin-replace'
import analyze from 'rollup-plugin-analyzer' import analyze from 'rollup-plugin-analyzer'
import { SLSOptions } from '../config' import { SLSOptions } from '../config'
import { RUNTIME_DIR } from '../utils' import { RUNTIME_DIR, resolvePath } from '../utils'
import dynamicRequire from './dynamic-require' import dynamicRequire from './dynamic-require'
export type RollupConfig = InputOptions & { output: OutputOptions } export type RollupConfig = InputOptions & { output: OutputOptions }
@ -75,6 +75,7 @@ export const getRollupConfig = (config: SLSOptions) => {
'typeof window': '"undefined"', 'typeof window': '"undefined"',
'process.env.NUXT_STATIC_BASE': JSON.stringify(config.staticAssets.base), 'process.env.NUXT_STATIC_BASE': JSON.stringify(config.staticAssets.base),
'process.env.NUXT_STATIC_VERSION': JSON.stringify(config.staticAssets.version), 'process.env.NUXT_STATIC_VERSION': JSON.stringify(config.staticAssets.version),
// @ts-ignore
'process.env.NUXT_FULL_STATIC': config.fullStatic 'process.env.NUXT_FULL_STATIC': config.fullStatic
} }
})) }))

View File

@ -1,6 +1,7 @@
import { relative, dirname, resolve } from 'path' import { relative, dirname, resolve } from 'path'
import { readFile, writeFile, mkdirp } from 'fs-extra' import { readFile, writeFile, mkdirp } from 'fs-extra'
import jiti from 'jiti' import jiti from 'jiti'
import { SLSOptions, UnresolvedPath } from './config'
const pwd = process.cwd() const pwd = process.cwd()
@ -34,5 +35,9 @@ export async function compileTemplateToJS (src: string, dst: string) {
export const jitiImport = (dir: string, path: string) => jiti(dir)(path) export const jitiImport = (dir: string, path: string) => jiti(dir)(path)
export const tryImport = (dir: string, path: string) => { try { return jitiImport(dir, path) } catch (_err) { } } export const tryImport = (dir: string, path: string) => { try { return jitiImport(dir, path) } catch (_err) { } }
export function resolvePath (options: SLSOptions, path: UnresolvedPath, resolveBase: string = '') {
return resolve(resolveBase, typeof path === 'string' ? path : path(options))
}
export const LIB_DIR = resolve(__dirname, '../lib') export const LIB_DIR = resolve(__dirname, '../lib')
export const RUNTIME_DIR = resolve(LIB_DIR, 'runtime') export const RUNTIME_DIR = resolve(LIB_DIR, 'runtime')