feat: generate public (dist/)

This commit is contained in:
Pooya Parsa 2020-11-04 20:01:53 +01:00
parent 425941d1b4
commit 0245bd65de
3 changed files with 24 additions and 9 deletions

View File

@ -5,7 +5,7 @@ import Hookable from 'hookable'
import prettyBytes from 'pretty-bytes'
import gzipSize from 'gzip-size'
import chalk from 'chalk'
import { copy, emptyDir, existsSync } from 'fs-extra'
import { copy, emptyDir, existsSync, mkdirp } from 'fs-extra'
import { getRollupConfig } from './rollup/config'
import { getTargetConfig } from './config'
import { hl, prettyPath, renderTemplate, compileTemplateToJS } from './utils'
@ -39,13 +39,6 @@ export async function build (baseConfig, target) {
consola.info('Compiled', prettyPath(dstPath))
}
if (config.copyAssets) {
const publicDir = typeof config.copyAssets === 'string' ? config.copyAssets : 'public'
const dst = resolve(config.outDir, publicDir, '_nuxt')
await copy(resolve(config.buildDir, 'dist/client'), dst)
consola.info('Copied public assets to', prettyPath(dst))
}
await hooks.callHook('done', config)
}
@ -63,3 +56,18 @@ export function ensureDist (baseConfig) {
consola.success('Using existing nuxt build from', prettyPath(baseConfig.buildDir))
}
}
export async function generatePublic (baseConfig) {
await emptyDir(baseConfig.publicDir)
await mkdirp(baseConfig.publicDir)
await copy(
baseConfig.staticDir,
baseConfig.publicDir
)
await copy(
resolve(baseConfig.buildDir, 'dist/client'),
resolve(baseConfig.publicDir, '_nuxt')
)
}

View File

@ -1,5 +1,5 @@
import { resolve } from 'path'
import { build, compileHTMLTemplate, ensureDist } from './build'
import { build, compileHTMLTemplate, ensureDist, generatePublic } from './build'
import { getBaseConfig } from './config'
export async function runCLI () {
@ -14,6 +14,9 @@ export async function runCLI () {
// Compile html template
await compileHTMLTemplate(baseConfig)
// Generate public dir
await generatePublic(baseConfig)
// Bundle for each target
for (const target of baseConfig.targets) {
if (baseConfig.target && target.target !== baseConfig.target) {

View File

@ -6,6 +6,8 @@ export function getBaseConfig (rootDir) {
let baseConfig = {
rootDir,
buildDir: '',
publicDir: '',
staticDir: '',
targets: [],
templates: [],
nuxt: 2,
@ -24,6 +26,8 @@ export function getBaseConfig (rootDir) {
}
baseConfig.buildDir = resolve(baseConfig.rootDir, baseConfig.buildDir || '.nuxt')
baseConfig.publicDir = resolve(baseConfig.rootDir, baseConfig.publicDir || 'dist')
baseConfig.staticDir = resolve(baseConfig.rootDir, baseConfig.staticDir || 'static')
baseConfig.targets = baseConfig.targets.map(t => typeof t === 'string' ? { target: t } : t)
if (baseConfig.target && !baseConfig.targets.find(t => t.target === baseConfig.target)) {