mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 17:35:57 +00:00
fix(bridge): can't enable vite mode (#1505)
This commit is contained in:
parent
e8ea92ce14
commit
7ede9b14d9
@ -51,7 +51,7 @@ export default defineNuxtModule({
|
|||||||
await setupAutoImports()
|
await setupAutoImports()
|
||||||
}
|
}
|
||||||
if (opts.vite) {
|
if (opts.vite) {
|
||||||
const viteModule = await import('./vite/module')
|
const viteModule = await import('./vite/module').then(r => r.default || r)
|
||||||
await installModule(nuxt, viteModule)
|
await installModule(nuxt, viteModule)
|
||||||
}
|
}
|
||||||
if (opts.postcss8) {
|
if (opts.postcss8) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { resolve } from 'pathe'
|
import { resolve } from 'pathe'
|
||||||
import { readJSON, remove, existsSync, readFile, writeFile, mkdirp } from 'fs-extra'
|
import fse from 'fs-extra'
|
||||||
import { ViteBuildContext } from './types'
|
import { ViteBuildContext } from './types'
|
||||||
import { uniq, isJS, isCSS, hash } from './utils'
|
import { uniq, isJS, isCSS, hash } from './utils'
|
||||||
|
|
||||||
@ -17,11 +17,11 @@ const DEFAULT_APP_TEMPLATE = `
|
|||||||
|
|
||||||
export async function prepareManifests (ctx: ViteBuildContext) {
|
export async function prepareManifests (ctx: ViteBuildContext) {
|
||||||
const rDist = (...args: string[]): string => resolve(ctx.nuxt.options.buildDir, 'dist', ...args)
|
const rDist = (...args: string[]): string => resolve(ctx.nuxt.options.buildDir, 'dist', ...args)
|
||||||
await mkdirp(rDist('server'))
|
await fse.mkdirp(rDist('server'))
|
||||||
|
|
||||||
const customAppTemplateFile = resolve(ctx.nuxt.options.srcDir, 'app.html')
|
const customAppTemplateFile = resolve(ctx.nuxt.options.srcDir, 'app.html')
|
||||||
const APP_TEMPLATE = existsSync(customAppTemplateFile)
|
const APP_TEMPLATE = fse.existsSync(customAppTemplateFile)
|
||||||
? (await readFile(customAppTemplateFile, 'utf-8'))
|
? (await fse.readFile(customAppTemplateFile, 'utf-8'))
|
||||||
: DEFAULT_APP_TEMPLATE
|
: DEFAULT_APP_TEMPLATE
|
||||||
|
|
||||||
const DEV_TEMPLATE = APP_TEMPLATE
|
const DEV_TEMPLATE = APP_TEMPLATE
|
||||||
@ -32,8 +32,8 @@ export async function prepareManifests (ctx: ViteBuildContext) {
|
|||||||
const SPA_TEMPLATE = ctx.nuxt.options.dev ? DEV_TEMPLATE : APP_TEMPLATE
|
const SPA_TEMPLATE = ctx.nuxt.options.dev ? DEV_TEMPLATE : APP_TEMPLATE
|
||||||
const SSR_TEMPLATE = ctx.nuxt.options.dev ? DEV_TEMPLATE : APP_TEMPLATE
|
const SSR_TEMPLATE = ctx.nuxt.options.dev ? DEV_TEMPLATE : APP_TEMPLATE
|
||||||
|
|
||||||
await writeFile(rDist('server/index.ssr.html'), SSR_TEMPLATE)
|
await fse.writeFile(rDist('server/index.ssr.html'), SSR_TEMPLATE)
|
||||||
await writeFile(rDist('server/index.spa.html'), SPA_TEMPLATE)
|
await fse.writeFile(rDist('server/index.spa.html'), SPA_TEMPLATE)
|
||||||
|
|
||||||
if (ctx.nuxt.options.dev) {
|
if (ctx.nuxt.options.dev) {
|
||||||
await stubManifest(ctx)
|
await stubManifest(ctx)
|
||||||
@ -47,7 +47,7 @@ export async function generateBuildManifest (ctx: ViteBuildContext) {
|
|||||||
const rDist = (...args: string[]): string => resolve(ctx.nuxt.options.buildDir, 'dist', ...args)
|
const rDist = (...args: string[]): string => resolve(ctx.nuxt.options.buildDir, 'dist', ...args)
|
||||||
|
|
||||||
const publicPath = ctx.nuxt.options.app.assetsPath // Default: /nuxt/
|
const publicPath = ctx.nuxt.options.app.assetsPath // Default: /nuxt/
|
||||||
const viteClientManifest = await readJSON(rDist('client/manifest.json'))
|
const viteClientManifest = await fse.readJSON(rDist('client/manifest.json'))
|
||||||
const clientEntries = Object.entries(viteClientManifest)
|
const clientEntries = Object.entries(viteClientManifest)
|
||||||
|
|
||||||
const asyncEntries = uniq(clientEntries.filter((id: any) => id[1].isDynamicEntry).flatMap(getModuleIds)).filter(Boolean)
|
const asyncEntries = uniq(clientEntries.filter((id: any) => id[1].isDynamicEntry).flatMap(getModuleIds)).filter(Boolean)
|
||||||
@ -93,14 +93,14 @@ export async function generateBuildManifest (ctx: ViteBuildContext) {
|
|||||||
maps: {}
|
maps: {}
|
||||||
}
|
}
|
||||||
|
|
||||||
await writeFile(rDist('client', clientEntryName), clientEntryCode, 'utf-8')
|
await fse.writeFile(rDist('client', clientEntryName), clientEntryCode, 'utf-8')
|
||||||
|
|
||||||
await writeClientManifest(clientManifest, ctx.nuxt.options.buildDir)
|
await writeClientManifest(clientManifest, ctx.nuxt.options.buildDir)
|
||||||
await writeServerManifest(serverManifest, ctx.nuxt.options.buildDir)
|
await writeServerManifest(serverManifest, ctx.nuxt.options.buildDir)
|
||||||
|
|
||||||
// Remove SSR manifest from public client dir
|
// Remove SSR manifest from public client dir
|
||||||
await remove(rDist('client/manifest.json'))
|
await fse.remove(rDist('client/manifest.json'))
|
||||||
await remove(rDist('client/ssr-manifest.json'))
|
await fse.remove(rDist('client/ssr-manifest.json'))
|
||||||
}
|
}
|
||||||
|
|
||||||
// stub manifest on dev
|
// stub manifest on dev
|
||||||
@ -132,7 +132,7 @@ export async function stubManifest (ctx: ViteBuildContext) {
|
|||||||
export async function generateDevSSRManifest (ctx: ViteBuildContext) {
|
export async function generateDevSSRManifest (ctx: ViteBuildContext) {
|
||||||
const rDist = (...args: string[]): string => resolve(ctx.nuxt.options.buildDir, 'dist', ...args)
|
const rDist = (...args: string[]): string => resolve(ctx.nuxt.options.buildDir, 'dist', ...args)
|
||||||
|
|
||||||
const ssrManifest = await readJSON(rDist('server/ssr-manifest.json'))
|
const ssrManifest = await fse.readJSON(rDist('server/ssr-manifest.json'))
|
||||||
const css = Object.keys(ssrManifest).filter(isCSS)
|
const css = Object.keys(ssrManifest).filter(isCSS)
|
||||||
|
|
||||||
const entires = [
|
const entires = [
|
||||||
@ -155,14 +155,14 @@ export async function generateDevSSRManifest (ctx: ViteBuildContext) {
|
|||||||
|
|
||||||
async function writeServerManifest (serverManifest: any, buildDir: string) {
|
async function writeServerManifest (serverManifest: any, buildDir: string) {
|
||||||
const serverManifestJSON = JSON.stringify(serverManifest, null, 2)
|
const serverManifestJSON = JSON.stringify(serverManifest, null, 2)
|
||||||
await writeFile(resolve(buildDir, 'dist/server/server.manifest.json'), serverManifestJSON, 'utf-8')
|
await fse.writeFile(resolve(buildDir, 'dist/server/server.manifest.json'), serverManifestJSON, 'utf-8')
|
||||||
await writeFile(resolve(buildDir, 'dist/server/server.manifest.mjs'), `export default ${serverManifestJSON}`, 'utf-8')
|
await fse.writeFile(resolve(buildDir, 'dist/server/server.manifest.mjs'), `export default ${serverManifestJSON}`, 'utf-8')
|
||||||
}
|
}
|
||||||
|
|
||||||
async function writeClientManifest (clientManifest: any, buildDir: string) {
|
async function writeClientManifest (clientManifest: any, buildDir: string) {
|
||||||
const clientManifestJSON = JSON.stringify(clientManifest, null, 2)
|
const clientManifestJSON = JSON.stringify(clientManifest, null, 2)
|
||||||
await writeFile(resolve(buildDir, 'dist/server/client.manifest.json'), clientManifestJSON, 'utf-8')
|
await fse.writeFile(resolve(buildDir, 'dist/server/client.manifest.json'), clientManifestJSON, 'utf-8')
|
||||||
await writeFile(resolve(buildDir, 'dist/server/client.manifest.mjs'), `export default ${clientManifestJSON}`, 'utf-8')
|
await fse.writeFile(resolve(buildDir, 'dist/server/client.manifest.mjs'), `export default ${clientManifestJSON}`, 'utf-8')
|
||||||
}
|
}
|
||||||
|
|
||||||
function getModuleIds ([, value]: [string, any]) {
|
function getModuleIds ([, value]: [string, any]) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import type { Plugin } from 'vite'
|
import type { Plugin } from 'vite'
|
||||||
import { readFile } from 'fs-extra'
|
import fse from 'fs-extra'
|
||||||
|
|
||||||
// const PREFIX = 'defaultexport:'
|
// const PREFIX = 'defaultexport:'
|
||||||
const PREFIX = 'defaultexport:'
|
const PREFIX = 'defaultexport:'
|
||||||
@ -25,7 +25,7 @@ export function defaultExportPlugin () {
|
|||||||
|
|
||||||
async load (id) {
|
async load (id) {
|
||||||
if (hasPrefix(id)) {
|
if (hasPrefix(id)) {
|
||||||
let code = await readFile(removePrefix(id), 'utf8')
|
let code = await fse.readFile(removePrefix(id), 'utf8')
|
||||||
if (!hasDefaultExport(code)) {
|
if (!hasDefaultExport(code)) {
|
||||||
code = addDefaultExport(code)
|
code = addDefaultExport(code)
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import { resolve } from 'pathe'
|
|||||||
import * as vite from 'vite'
|
import * as vite from 'vite'
|
||||||
import { createVuePlugin } from 'vite-plugin-vue2'
|
import { createVuePlugin } from 'vite-plugin-vue2'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import { writeFile } from 'fs-extra'
|
import fse from 'fs-extra'
|
||||||
import pDebounce from 'p-debounce'
|
import pDebounce from 'p-debounce'
|
||||||
import { bundleRequest } from '../../../vite/src/dev-bundler'
|
import { bundleRequest } from '../../../vite/src/dev-bundler'
|
||||||
import { ViteBuildContext, ViteOptions } from './types'
|
import { ViteBuildContext, ViteOptions } from './types'
|
||||||
@ -95,14 +95,14 @@ export async function buildServer (ctx: ViteBuildContext) {
|
|||||||
await viteServer.pluginContainer.buildStart({})
|
await viteServer.pluginContainer.buildStart({})
|
||||||
|
|
||||||
// Generate manifest files
|
// Generate manifest files
|
||||||
await writeFile(resolve(ctx.nuxt.options.buildDir, 'dist/server/ssr-manifest.json'), JSON.stringify({}, null, 2), 'utf-8')
|
await fse.writeFile(resolve(ctx.nuxt.options.buildDir, 'dist/server/ssr-manifest.json'), JSON.stringify({}, null, 2), 'utf-8')
|
||||||
await generateDevSSRManifest(ctx)
|
await generateDevSSRManifest(ctx)
|
||||||
|
|
||||||
// Build and watch
|
// Build and watch
|
||||||
const _doBuild = async () => {
|
const _doBuild = async () => {
|
||||||
const start = Date.now()
|
const start = Date.now()
|
||||||
const { code } = await bundleRequest({ viteServer }, '/.nuxt/server.js')
|
const { code } = await bundleRequest({ viteServer }, '/.nuxt/server.js')
|
||||||
await writeFile(resolve(ctx.nuxt.options.buildDir, 'dist/server/server.mjs'), code, 'utf-8')
|
await fse.writeFile(resolve(ctx.nuxt.options.buildDir, 'dist/server/server.mjs'), code, 'utf-8')
|
||||||
const time = (Date.now() - start)
|
const time = (Date.now() - start)
|
||||||
consola.info(`Server built in ${time}ms`)
|
consola.info(`Server built in ${time}ms`)
|
||||||
await onBuild()
|
await onBuild()
|
||||||
|
Loading…
Reference in New Issue
Block a user