diff --git a/packages/uni-app-vite/package.json b/packages/uni-app-vite/package.json index 0bf318d2d512b2acc718b1a5d09148c1f1bbd750..ea5d7b9c270e19894919e20707c8cf59dd2dd50b 100644 --- a/packages/uni-app-vite/package.json +++ b/packages/uni-app-vite/package.json @@ -33,6 +33,7 @@ "devDependencies": { "@types/debug": "^4.1.7", "@types/fs-extra": "^9.0.13", - "@vue/compiler-core": "3.2.26" + "@vue/compiler-core": "3.2.26", + "esbuild": "^0.13.12" } } diff --git a/packages/uni-app-vite/src/nvue/index.ts b/packages/uni-app-vite/src/nvue/index.ts index d1e1b597d507992071b0b4a3b8fb060c4851ad88..5542141fa0e25f2d8f41a3032c26ed5b8ed8fd66 100644 --- a/packages/uni-app-vite/src/nvue/index.ts +++ b/packages/uni-app-vite/src/nvue/index.ts @@ -7,6 +7,7 @@ import { import { uniEasycomPlugin } from '../plugins/easycom' import { uniManifestJsonPlugin } from '../plugins/manifestJson' +import { uniStatsPlugin } from '../plugins/stats' import { uniAppNVuePlugin } from './plugin' import { uniEsbuildPlugin } from './plugins/esbuild' import { uniMainJsPlugin } from './plugins/mainJs' @@ -20,6 +21,7 @@ export function initNVuePlugins() { ...(process.env.UNI_RENDERER === 'native' ? [uniManifestJsonPlugin()] : []), uniPagesJsonPlugin(), uniViteInjectPlugin('uni:app-inject', initAppProvide()), + uniStatsPlugin(), uniAppNVuePlugin(), uniEsbuildPlugin(), ] diff --git a/packages/uni-app-vite/src/nvue/plugins/esbuild.ts b/packages/uni-app-vite/src/nvue/plugins/esbuild.ts index 5f3522ebf1e6279b7e349f58d6393239ceb0d06b..a55c865c9deb68eec72ba89e018f662cd8986582 100644 --- a/packages/uni-app-vite/src/nvue/plugins/esbuild.ts +++ b/packages/uni-app-vite/src/nvue/plugins/esbuild.ts @@ -1,11 +1,25 @@ -import { esbuild } from '@dcloudio/uni-cli-shared' +import path from 'path' +import fs from 'fs-extra' +import { transformWithEsbuild } from '@dcloudio/uni-cli-shared' +import type { BuildOptions, PluginBuild } from 'esbuild' import type { Plugin } from 'vite' import { nvueOutDir } from '../../utils' export function uniEsbuildPlugin(): Plugin { + let buildOptions: BuildOptions + const outputDir = process.env.UNI_OUTPUT_DIR return { name: 'uni:app-nvue-esbuild', enforce: 'post', + configResolved(config) { + buildOptions = { + format: 'iife', + minify: config.build.minify ? true : false, + bundle: true, + write: false, + plugins: [esbuildGlobalPlugin({ vue: 'Vue' })], + } + }, async writeBundle(_, bundle) { const entryPoints: string[] = [] Object.keys(bundle).forEach((name) => { @@ -18,12 +32,48 @@ export function uniEsbuildPlugin(): Plugin { entryPoints.push(name) } }) - await esbuild({ - absWorkingDir: nvueOutDir(), - entryPoints, - bundle: true, - external: ['vue'], - outdir: process.env.UNI_OUTPUT_DIR, + await Promise.all( + entryPoints.map((filename) => { + return buildNVuePage(filename, buildOptions).then((code) => { + return fs.outputFile(path.resolve(outputDir, filename), code) + }) + }) + ) + }, + } +} + +function buildNVuePage(filename: string, options: BuildOptions) { + return transformWithEsbuild( + `import NVuePageComponent from './${filename}' +Vue.createApp(NVuePageComponent).mount('#root')`, + path.join(nvueOutDir(), 'main.js'), + options + ).then((res) => { + if (res.outputFiles) { + return res.outputFiles[0].text + } + return '' + }) +} + +function esbuildGlobalPlugin(options: Record) { + const keys = Object.keys(options) + return { + name: 'global', + setup(build: PluginBuild) { + keys.forEach((key) => { + const namespace = key + '-ns' + build.onResolve({ filter: new RegExp('^' + key + '$') }, ({ path }) => { + return { + path, + namespace, + } + }) + build.onLoad({ filter: /.*/, namespace }, () => ({ + contents: `module.exports = ${options[key]}`, + loader: 'js', + })) }) }, } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f530351bccf3c390e4b322669812a0497af11c9a..eb1e05dbbc36a31a0a8e8f4e6a9700cc11d05c24 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -207,6 +207,7 @@ importers: '@vue/compiler-core': 3.2.26 '@vue/compiler-sfc': 3.2.26 debug: ^4.3.2 + esbuild: ^0.13.12 fs-extra: ^10.0.0 rollup: ^2.59.0 dependencies: @@ -224,6 +225,7 @@ importers: '@types/debug': 4.1.7 '@types/fs-extra': 9.0.13 '@vue/compiler-core': 3.2.26 + esbuild: 0.13.15 packages/uni-app-vue: specifiers: