mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
feat: rewrite as nuxt module
This commit is contained in:
parent
0245bd65de
commit
486c881b2d
@ -5,12 +5,13 @@ import Hookable from 'hookable'
|
||||
import prettyBytes from 'pretty-bytes'
|
||||
import gzipSize from 'gzip-size'
|
||||
import chalk from 'chalk'
|
||||
import { copy, emptyDir, existsSync, mkdirp } from 'fs-extra'
|
||||
import { emptyDir } from 'fs-extra'
|
||||
import { getRollupConfig } from './rollup/config'
|
||||
import { getTargetConfig } from './config'
|
||||
import { hl, prettyPath, renderTemplate, compileTemplateToJS } from './utils'
|
||||
|
||||
export async function build (baseConfig, target) {
|
||||
console.log('\n')
|
||||
consola.info(`Generating bundle for ${hl(target.target)}`)
|
||||
|
||||
const config: any = getTargetConfig(baseConfig, target)
|
||||
@ -20,7 +21,7 @@ export async function build (baseConfig, target) {
|
||||
|
||||
await hooks.callHook('config', config)
|
||||
|
||||
emptyDir(config.outDir)
|
||||
emptyDir(config.targetDir)
|
||||
|
||||
config.rollupConfig = getRollupConfig(config)
|
||||
await hooks.callHook('rollup:before', config)
|
||||
@ -34,7 +35,7 @@ export async function build (baseConfig, target) {
|
||||
)
|
||||
|
||||
for (const tmpl of config.templates) {
|
||||
const dstPath = resolve(config.outDir, tmpl.dst)
|
||||
const dstPath = resolve(config.targetDir, tmpl.dst)
|
||||
await renderTemplate(tmpl.src, dstPath, { config })
|
||||
consola.info('Compiled', prettyPath(dstPath))
|
||||
}
|
||||
@ -48,26 +49,3 @@ export async function compileHTMLTemplate (baseConfig) {
|
||||
await compileTemplateToJS(htmlTemplateFile, htmlTemplateFileJS)
|
||||
consola.info('Generated', prettyPath(htmlTemplateFileJS))
|
||||
}
|
||||
|
||||
export function ensureDist (baseConfig) {
|
||||
if (!existsSync(resolve(baseConfig.buildDir, 'dist/server'))) {
|
||||
return consola.error('Please use `nuxt build` first to build project!')
|
||||
} else {
|
||||
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')
|
||||
)
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
import { resolve } from 'path'
|
||||
import { build, compileHTMLTemplate, ensureDist, generatePublic } from './build'
|
||||
import { getBaseConfig } from './config'
|
||||
|
||||
export async function runCLI () {
|
||||
const rootDir = resolve(process.cwd(), process.argv[2] || '.')
|
||||
|
||||
// Config
|
||||
const baseConfig = getBaseConfig(rootDir)
|
||||
|
||||
// Ensure dist exists
|
||||
await ensureDist(baseConfig)
|
||||
|
||||
// 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) {
|
||||
continue
|
||||
}
|
||||
await build(baseConfig, target)
|
||||
}
|
||||
}
|
@ -2,32 +2,25 @@ import { resolve } from 'path'
|
||||
import defu from 'defu'
|
||||
import { tryImport, LIB_DIR } from './utils'
|
||||
|
||||
export function getBaseConfig (rootDir) {
|
||||
let baseConfig = {
|
||||
rootDir,
|
||||
buildDir: '',
|
||||
publicDir: '',
|
||||
staticDir: '',
|
||||
export function getBaseConfig (options) {
|
||||
const baseConfig = {
|
||||
rootDir: options.rootDir,
|
||||
buildDir: options.buildDir,
|
||||
publicDir: options.generate.dir,
|
||||
slsDir: null,
|
||||
targets: [],
|
||||
templates: [],
|
||||
nuxt: 2,
|
||||
target: process.argv[3] && process.argv[3][0] !== '-' ? process.argv[3] : null,
|
||||
minify: process.argv.includes('--minify') ? true : null,
|
||||
analyze: process.argv.includes('--analyze') ? true : null,
|
||||
logStartup: true
|
||||
}
|
||||
|
||||
const nuxtConfig = tryImport(rootDir, './nuxt.config')
|
||||
if (!nuxtConfig) {
|
||||
throw new Error('`nuxt.config` file not found in: ' + rootDir)
|
||||
}
|
||||
if (nuxtConfig.serverless) {
|
||||
baseConfig = defu(nuxtConfig.serverless, baseConfig)
|
||||
target: null,
|
||||
minify: null,
|
||||
analyze: null,
|
||||
logStartup: true,
|
||||
...options.serverless
|
||||
}
|
||||
|
||||
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.slsDir = resolve(baseConfig.rootDir, baseConfig.slsDir || '.sls')
|
||||
|
||||
baseConfig.targets = baseConfig.targets.map(t => typeof t === 'string' ? { target: t } : t)
|
||||
if (baseConfig.target && !baseConfig.targets.find(t => t.target === baseConfig.target)) {
|
||||
@ -55,7 +48,7 @@ export function getTargetConfig (baseConfig, target) {
|
||||
_targetDefaults,
|
||||
// Generic defaults
|
||||
{
|
||||
outDir: resolve(baseConfig.buildDir, `sls/${target.target}`),
|
||||
targetDir: resolve(baseConfig.slsDir, target.target),
|
||||
outName: 'index.js'
|
||||
}
|
||||
)
|
||||
|
@ -1 +1,38 @@
|
||||
export { runCLI } from './cli'
|
||||
import { build, compileHTMLTemplate } from './build'
|
||||
import { getBaseConfig } from './config'
|
||||
|
||||
export default function () {
|
||||
const { nuxt } = this
|
||||
|
||||
if (nuxt.options.dev) {
|
||||
return
|
||||
}
|
||||
|
||||
// Config
|
||||
const baseConfig = getBaseConfig(nuxt.options)
|
||||
|
||||
if (baseConfig.minify !== false) {
|
||||
nuxt.options.build._minifyServer = true
|
||||
}
|
||||
|
||||
nuxt.options.build.standalone = true
|
||||
|
||||
nuxt.hook('generate:cache:ignore', (ignore) => {
|
||||
ignore.push(baseConfig.slsDir)
|
||||
})
|
||||
|
||||
nuxt.hook('generate:done', () => buildSLS(baseConfig))
|
||||
}
|
||||
|
||||
async function buildSLS (baseConfig) {
|
||||
// Compile html template
|
||||
await compileHTMLTemplate(baseConfig)
|
||||
|
||||
// Bundle for each target
|
||||
for (const target of baseConfig.targets) {
|
||||
if (baseConfig.target && target.target !== baseConfig.target) {
|
||||
continue
|
||||
}
|
||||
await build(baseConfig, target)
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
|
||||
require('../dist').runCLI().catch((error) => {
|
||||
const consola = require('consola')
|
||||
consola.error(error)
|
||||
process.exit(1)
|
||||
})
|
@ -50,7 +50,7 @@ export const getRollupConfig = (config) => {
|
||||
const options: RollupConfig = {
|
||||
input: config.entry,
|
||||
output: {
|
||||
file: path.resolve(config.outDir, config.outName),
|
||||
file: path.resolve(config.targetDir, config.outName),
|
||||
format: 'cjs',
|
||||
intro: '',
|
||||
outro: '',
|
||||
@ -77,7 +77,7 @@ export const getRollupConfig = (config) => {
|
||||
// Dynamic Require Support
|
||||
options.plugins.push(dynamicRequire({
|
||||
dir: path.resolve(config.buildDir, 'dist/server'),
|
||||
outDir: config.node === false ? undefined : config.outDir,
|
||||
outDir: (config.node === false || config.inlineChunks) ? undefined : config.targetDir,
|
||||
globbyOptions: {
|
||||
ignore: [
|
||||
'server.js'
|
||||
@ -123,7 +123,7 @@ export const getRollupConfig = (config) => {
|
||||
options.plugins.push(analyze())
|
||||
}
|
||||
|
||||
if (config.minify) {
|
||||
if (config.minify !== false) {
|
||||
options.plugins.push(terser())
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ import { copyFile, mkdirp } from 'fs-extra'
|
||||
|
||||
const PLUGIN_NAME = 'dynamic-require'
|
||||
const HELPER_DYNAMIC = `\0${PLUGIN_NAME}.js`
|
||||
const DYNAMIC_REQUIRE_RE = /require\("\.\/" \+/g
|
||||
const DYNAMIC_REQUIRE_RE = /require\("\.\/" ?\+/g
|
||||
|
||||
const TMPL_INLINE = ({ imports }) =>
|
||||
`${imports.map(i => `import ${i.name} from '${i.import}'`).join('\n')}
|
||||
|
Loading…
Reference in New Issue
Block a user