mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat: improve types (#6)
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
parent
e0505568aa
commit
dfdd466270
@ -7,8 +7,8 @@ import gzipSize from 'gzip-size'
|
|||||||
import chalk from 'chalk'
|
import chalk from 'chalk'
|
||||||
import { emptyDir } from 'fs-extra'
|
import { emptyDir } from 'fs-extra'
|
||||||
import { getRollupConfig } from './rollup/config'
|
import { getRollupConfig } from './rollup/config'
|
||||||
import { getTargetConfig } from './config'
|
|
||||||
import { hl, prettyPath, renderTemplate, compileTemplateToJS } from './utils'
|
import { hl, prettyPath, renderTemplate, compileTemplateToJS } from './utils'
|
||||||
|
import { getTargetConfig, SLSConfig } from './config'
|
||||||
|
|
||||||
export async function build (baseConfig, target) {
|
export async function build (baseConfig, target) {
|
||||||
console.log('\n')
|
console.log('\n')
|
||||||
@ -43,7 +43,7 @@ export async function build (baseConfig, target) {
|
|||||||
await hooks.callHook('done', config)
|
await hooks.callHook('done', config)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function compileHTMLTemplate (baseConfig) {
|
export async function compileHTMLTemplate (baseConfig: SLSConfig) {
|
||||||
const htmlTemplateFile = resolve(baseConfig.buildDir, `views/${{ 2: 'app', 3: 'document' }[baseConfig.nuxt]}.template.html`)
|
const htmlTemplateFile = resolve(baseConfig.buildDir, `views/${{ 2: 'app', 3: 'document' }[baseConfig.nuxt]}.template.html`)
|
||||||
const htmlTemplateFileJS = htmlTemplateFile.replace(/.html$/, '.js').replace('app.', 'document.')
|
const htmlTemplateFileJS = htmlTemplateFile.replace(/.html$/, '.js').replace('app.', 'document.')
|
||||||
await compileTemplateToJS(htmlTemplateFile, htmlTemplateFileJS)
|
await compileTemplateToJS(htmlTemplateFile, htmlTemplateFileJS)
|
||||||
|
@ -1,8 +1,29 @@
|
|||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
import defu from 'defu'
|
import defu from 'defu'
|
||||||
|
import { NuxtOptions } from '@nuxt/types'
|
||||||
import { tryImport, LIB_DIR } from './utils'
|
import { tryImport, LIB_DIR } from './utils'
|
||||||
|
|
||||||
export function getBaseConfig (options) {
|
export interface SLSConfig {
|
||||||
|
node: false
|
||||||
|
entry: string
|
||||||
|
outDir: string
|
||||||
|
slsDir: string
|
||||||
|
outName: string
|
||||||
|
logStartup: boolean
|
||||||
|
buildDir: string
|
||||||
|
publicDir: string
|
||||||
|
staticDir: string
|
||||||
|
rootDir: string
|
||||||
|
targets: ((SLSConfig & { target: string }) | string)[]
|
||||||
|
target: string
|
||||||
|
templates: string[]
|
||||||
|
renderer: string
|
||||||
|
nuxt: 2 | 3
|
||||||
|
analyze: boolean
|
||||||
|
minify: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getBaseConfig (options: NuxtOptions): SLSConfig {
|
||||||
const baseConfig = {
|
const baseConfig = {
|
||||||
rootDir: options.rootDir,
|
rootDir: options.rootDir,
|
||||||
buildDir: options.buildDir,
|
buildDir: options.buildDir,
|
||||||
@ -33,7 +54,7 @@ export function getBaseConfig (options) {
|
|||||||
return baseConfig
|
return baseConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTargetConfig (baseConfig, target) {
|
export function getTargetConfig (baseConfig: SLSConfig, target: SLSConfig) {
|
||||||
const _targetDefaults = tryImport(LIB_DIR, `./targets/${target.target}`) ||
|
const _targetDefaults = tryImport(LIB_DIR, `./targets/${target.target}`) ||
|
||||||
tryImport(baseConfig.rootDir, target.target)
|
tryImport(baseConfig.rootDir, target.target)
|
||||||
if (!_targetDefaults) {
|
if (!_targetDefaults) {
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
import type { Module } from '@nuxt/types'
|
||||||
import { build, compileHTMLTemplate } from './build'
|
import { build, compileHTMLTemplate } from './build'
|
||||||
import { getBaseConfig } from './config'
|
import { getBaseConfig } from './config'
|
||||||
|
|
||||||
export default function () {
|
export default <Module> function slsModule () {
|
||||||
const { nuxt } = this
|
const { nuxt } = this
|
||||||
|
|
||||||
if (nuxt.options.dev) {
|
if (nuxt.options.dev) {
|
||||||
|
@ -8,12 +8,14 @@ import alias from '@rollup/plugin-alias'
|
|||||||
import json from '@rollup/plugin-json'
|
import json from '@rollup/plugin-json'
|
||||||
import replace from '@rollup/plugin-replace'
|
import replace from '@rollup/plugin-replace'
|
||||||
import analyze from 'rollup-plugin-analyzer'
|
import analyze from 'rollup-plugin-analyzer'
|
||||||
|
|
||||||
|
import { SLSConfig } from '../config'
|
||||||
import { RUNTIME_DIR } from '../utils'
|
import { RUNTIME_DIR } from '../utils'
|
||||||
import dynamicRequire from './dynamic-require'
|
import dynamicRequire from './dynamic-require'
|
||||||
|
|
||||||
export type RollupConfig = InputOptions & { output: OutputOptions }
|
export type RollupConfig = InputOptions & { output: OutputOptions }
|
||||||
|
|
||||||
export const getRollupConfig = (config) => {
|
export const getRollupConfig = (config: SLSConfig) => {
|
||||||
const mocks = [
|
const mocks = [
|
||||||
// @nuxt/devalue
|
// @nuxt/devalue
|
||||||
'consola',
|
'consola',
|
||||||
|
@ -6,7 +6,13 @@ const PLUGIN_NAME = 'dynamic-require'
|
|||||||
const HELPER_DYNAMIC = `\0${PLUGIN_NAME}.js`
|
const HELPER_DYNAMIC = `\0${PLUGIN_NAME}.js`
|
||||||
const DYNAMIC_REQUIRE_RE = /require\("\.\/" ?\+/g
|
const DYNAMIC_REQUIRE_RE = /require\("\.\/" ?\+/g
|
||||||
|
|
||||||
const TMPL_INLINE = ({ imports }) =>
|
interface Import {
|
||||||
|
name: string
|
||||||
|
id: string
|
||||||
|
import: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const TMPL_INLINE = ({ imports }: { imports: Import[]}) =>
|
||||||
`${imports.map(i => `import ${i.name} from '${i.import}'`).join('\n')}
|
`${imports.map(i => `import ${i.name} from '${i.import}'`).join('\n')}
|
||||||
const dynamicChunks = {
|
const dynamicChunks = {
|
||||||
${imports.map(i => ` ['${i.id}']: ${i.name}`).join(',\n')}
|
${imports.map(i => ` ['${i.id}']: ${i.name}`).join(',\n')}
|
||||||
@ -30,13 +36,13 @@ interface Options {
|
|||||||
export default function dynamicRequire ({ dir, globbyOptions, outDir, chunksDir }: Options) {
|
export default function dynamicRequire ({ dir, globbyOptions, outDir, chunksDir }: Options) {
|
||||||
return {
|
return {
|
||||||
name: PLUGIN_NAME,
|
name: PLUGIN_NAME,
|
||||||
transform (code, _id) {
|
transform (code: string, _id: string) {
|
||||||
return code.replace(DYNAMIC_REQUIRE_RE, `require('${HELPER_DYNAMIC}')(`)
|
return code.replace(DYNAMIC_REQUIRE_RE, `require('${HELPER_DYNAMIC}')(`)
|
||||||
},
|
},
|
||||||
resolveId (id) {
|
resolveId (id: string) {
|
||||||
return id === HELPER_DYNAMIC ? id : null
|
return id === HELPER_DYNAMIC ? id : null
|
||||||
},
|
},
|
||||||
async load (id) {
|
async load (id: string) {
|
||||||
if (id === HELPER_DYNAMIC) {
|
if (id === HELPER_DYNAMIC) {
|
||||||
const files = await globby('**/*.js', { cwd: dir, absolute: false, ...globbyOptions })
|
const files = await globby('**/*.js', { cwd: dir, absolute: false, ...globbyOptions })
|
||||||
|
|
||||||
|
@ -4,19 +4,19 @@ import jiti from 'jiti'
|
|||||||
|
|
||||||
const pwd = process.cwd()
|
const pwd = process.cwd()
|
||||||
|
|
||||||
export const hl = str => '`' + str + '`'
|
export const hl = (str: string) => '`' + str + '`'
|
||||||
|
|
||||||
export function prettyPath (p, highlight = true) {
|
export function prettyPath (p: string, highlight = true) {
|
||||||
p = relative(pwd, p)
|
p = relative(pwd, p)
|
||||||
return highlight ? hl(p) : p
|
return highlight ? hl(p) : p
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadTemplate (src) {
|
export async function loadTemplate (src: string) {
|
||||||
const contents = await readFile(src, 'utf-8')
|
const contents = await readFile(src, 'utf-8')
|
||||||
return params => contents.replace(/{{ (\w+) }}/g, `${params.$1}`)
|
return (params: Record<string, string>) => contents.replace(/{{ (\w+) }}/g, `${params.$1}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function renderTemplate (src, dst: string, params: any) {
|
export async function renderTemplate (src: string, dst: string, params: any) {
|
||||||
const tmpl = await loadTemplate(src)
|
const tmpl = await loadTemplate(src)
|
||||||
const rendered = tmpl(params)
|
const rendered = tmpl(params)
|
||||||
await mkdirp(dirname(dst))
|
await mkdirp(dirname(dst))
|
||||||
@ -31,8 +31,8 @@ export async function compileTemplateToJS (src: string, dst: string) {
|
|||||||
await writeFile(dst, compiled)
|
await writeFile(dst, compiled)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const jitiImport = (dir, path) => jiti(dir)(path)
|
export const jitiImport = (dir: string, path: string) => jiti(dir)(path)
|
||||||
export const tryImport = (dir, path) => { try { return jitiImport(dir, path) } catch (_err) { } }
|
export const tryImport = (dir: string, path: string) => { try { return jitiImport(dir, path) } catch (_err) { } }
|
||||||
|
|
||||||
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')
|
||||||
|
Loading…
Reference in New Issue
Block a user