diff --git a/docs/content/en/1.getting-started/3.build.md b/docs/content/en/1.getting-started/3.build.md new file mode 100644 index 0000000..b49b836 --- /dev/null +++ b/docs/content/en/1.getting-started/3.build.md @@ -0,0 +1,17 @@ +# Build + +From v0.2.0, we shipped experimental build support with Vite. It's disabled by default and you can enable it by setting `vite.build: true` in config. + +```js +// nuxt.config +export default { + buildModules: [ + 'nuxt-vite' + ], + vite: { + build: true + } +} +``` + +Then run `nuxt build` with the power of Vite! diff --git a/package.json b/package.json index 4af081f..064e44a 100644 --- a/package.json +++ b/package.json @@ -12,13 +12,17 @@ "scripts": { "build": "siroc build && mkdist --src src/runtime --dist dist/runtime", "prepublishOnly": "yarn build", - "dev": "nuxt dev test/fixture", + "dev": "yarn fixture:dev", + "fixture:dev": "nuxt dev test/fixture", + "fixture:build": "nuxt build test/fixture", + "fixture:start": "nuxt start test/fixture", "lint": "eslint --ext .ts .", "release": "yarn test && standard-version && git push --follow-tags && npm publish", "test": "yarn lint && yarn jest" }, "dependencies": { "@nuxt/http": "^0.6.4", + "@vitejs/plugin-legacy": "^1.5.1", "chokidar": "^3.5.2", "consola": "^2.15.3", "fs-extra": "^10.0.0", @@ -39,6 +43,7 @@ "@nuxt/types": "^2.15.8", "@nuxtjs/composition-api": "^0.26.0", "@nuxtjs/eslint-config-typescript": "^6.0.1", + "@types/fs-extra": "^9.0.12", "@types/jest": "^27.0.0", "eslint": "^7.32.0", "jest": "^27.0.6", diff --git a/src/client.ts b/src/client.ts index 9d9e4b5..cb4f138 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,6 +1,7 @@ import { resolve } from 'path' import * as vite from 'vite' import { createVuePlugin } from 'vite-plugin-vue2' +import PluginLegacy from '@vitejs/plugin-legacy' import { jsxPlugin } from './plugins/jsx' import { replace } from './plugins/replace' import { ViteBuildContext, ViteOptions } from './types' @@ -17,6 +18,7 @@ export async function buildClient (ctx: ViteBuildContext) { define: { 'process.server': false, 'process.client': true, + 'process.static': false, global: 'window', 'module.hot': false }, @@ -29,12 +31,15 @@ export async function buildClient (ctx: ViteBuildContext) { assetsDir: '.', rollupOptions: { input: resolve(ctx.nuxt.options.buildDir, 'client.js') - } + }, + manifest: true, + ssrManifest: true }, plugins: [ replace({ 'process.env': 'import.meta.env' }), jsxPlugin(), - createVuePlugin(ctx.config.vue) + createVuePlugin(ctx.config.vue), + PluginLegacy() ], server: { middlewareMode: true @@ -43,6 +48,13 @@ export async function buildClient (ctx: ViteBuildContext) { await ctx.nuxt.callHook('vite:extendConfig', clientConfig, { isClient: true, isServer: false }) + // Production build + if (!ctx.nuxt.options.dev) { + await vite.build(clientConfig) + return + } + + // Create development server const viteServer = await vite.createServer(clientConfig) await ctx.nuxt.callHook('vite:serverCreated', viteServer) diff --git a/src/index.ts b/src/index.ts index a4e31f0..70e7467 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,8 +7,10 @@ import type { ViteOptions } from './types' function nuxtVite () { const { nuxt } = this + const viteOptions = nuxt.options.vite || {} - if (!nuxt.options.dev) { + // Only enable for development or production if `build: true` is set + if (!nuxt.options.dev && !viteOptions.build) { return } @@ -92,6 +94,7 @@ declare module '@nuxt/types/config/index' { */ vite?: ViteOptions & { ssr: false | ViteOptions['ssr'], + build: boolean | ViteOptions['build'], experimentWarning: boolean } } diff --git a/src/server.ts b/src/server.ts index 6322e56..73bbbd9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,8 +1,9 @@ import { resolve } from 'path' +import { createHash } from 'crypto' import * as vite from 'vite' import { createVuePlugin } from 'vite-plugin-vue2' import { watch } from 'chokidar' -import { exists, readFile, mkdirp, writeFile } from 'fs-extra' +import { existsSync, readFile, mkdirp, writeFile, readJSON, remove } from 'fs-extra' import debounce from 'p-debounce' import consola from 'consola' import { ViteBuildContext, ViteOptions } from './types' @@ -16,9 +17,7 @@ const DEFAULT_APP_TEMPLATE = ` {{ HEAD }}
-