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) {
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 })
consola.info('Compiled', prettyPath(dstPath))
}

View File

@ -1,13 +1,14 @@
import { resolve } from 'path'
import defu from 'defu'
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 {
node: false
target: 'worker' | 'node' | string
target: string
entry: string
outDir: string
slsDir: string
outName: string
logStartup: boolean
@ -17,7 +18,7 @@ export interface SLSOptions {
staticDir: string
targetDir: string
rootDir: string
templates: { src: string, dst: string }[]
templates: { src: string, dst: UnresolvedPath }[]
static: string[]
renderer: string
nuxt: 2 | 3
@ -34,7 +35,9 @@ export interface SLSOptions {
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 {
const defaults: SLSConfig = {
@ -49,7 +52,8 @@ export function getoptions (nuxtOptions: NuxtOptions): SLSOptions {
static: [],
nuxt: 2,
logStartup: true,
inlineChunks: true
inlineChunks: true,
targetDir: null
}
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.publicDir = resolve(options.rootDir, options.publicDir || 'dist')
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
}

View File

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

View File

@ -1,6 +1,7 @@
import { relative, dirname, resolve } from 'path'
import { readFile, writeFile, mkdirp } from 'fs-extra'
import jiti from 'jiti'
import { SLSOptions, UnresolvedPath } from './config'
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 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 RUNTIME_DIR = resolve(LIB_DIR, 'runtime')