diff --git a/packages/uni-h5-vite/src/index.ts b/packages/uni-h5-vite/src/index.ts index 9fc63d44f515ca0d39e70ab24658798047a9e83d..a6a315bb07efa7cdece6201094ab5ce5716e38ef 100644 --- a/packages/uni-h5-vite/src/index.ts +++ b/packages/uni-h5-vite/src/index.ts @@ -2,7 +2,7 @@ import { uniCssScopedPlugin, UNI_EASYCOM_EXCLUDE, } from '@dcloudio/uni-cli-shared' -import { UniH5Plugin } from './plugin' +import { uniH5PLugin } from './plugin' import { uniCssPlugin } from './plugins/css' import { uniEasycomPlugin } from './plugins/easycom' import { uniInjectPlugin } from './plugins/inject' @@ -26,5 +26,5 @@ export default [ uniSSRPlugin(), uniSetupPlugin(), uniRenderjsPlugin(), - UniH5Plugin, + uniH5PLugin(), ] diff --git a/packages/uni-h5-vite/src/plugin/config.ts b/packages/uni-h5-vite/src/plugin/config.ts new file mode 100644 index 0000000000000000000000000000000000000000..e16ebd386d0cc3a218784e72284eb55f518dbd1b --- /dev/null +++ b/packages/uni-h5-vite/src/plugin/config.ts @@ -0,0 +1,79 @@ +import fs from 'fs' +import path from 'path' +import { normalizePath, Plugin, ResolvedConfig } from 'vite' +import { isInHBuilderX, resolveMainPathOnce } from '@dcloudio/uni-cli-shared' +import { createDefine, isSsr } from '../utils' +import { esbuildPrePlugin } from './esbuild/esbuildPrePlugin' +import { external } from './configureServer/ssr' +export function createConfig(options: { + resolvedConfig: ResolvedConfig | null +}): Plugin['config'] { + return function config(config, env) { + if (isInHBuilderX()) { + if ( + !fs.existsSync(path.resolve(process.env.UNI_INPUT_DIR, 'index.html')) + ) { + console.error(`请确认您的项目模板是否支持vue3:根目录缺少 index.html`) + process.exit() + } + } + return { + optimizeDeps: { + entries: resolveMainPathOnce(process.env.UNI_INPUT_DIR), + exclude: external, + esbuildOptions: { + plugins: [esbuildPrePlugin()], + }, + }, + define: createDefine(env.command, config), + server: { + host: true, + fs: { + strict: false, + }, + }, + ssr: { + external, + }, + build: { + rollupOptions: { + // resolveSSRExternal 会判定package.json,hbx 工程可能没有,通过 rollup 来配置 + external: isSsr(env.command, config) ? external : [], + onwarn(warning, warn) { + if (warning.code === 'UNUSED_EXTERNAL_IMPORT') { + const { message } = warning + // ignore + if ( + message.includes('"vue"') || + message.includes('"resolveComponent"') || + message.includes('"@dcloudio/uni-h5"') + ) { + return + } + } + warn(warning) + }, + output: { + chunkFileNames(chunkInfo) { + const { assetsDir } = options.resolvedConfig!.build + if (chunkInfo.facadeModuleId) { + const dirname = path.relative( + process.env.UNI_INPUT_DIR, + path.dirname(chunkInfo.facadeModuleId) + ) + if (dirname) { + return path.posix.join( + assetsDir, + normalizePath(dirname).replace(/\//g, '-') + + '-[name].[hash].js' + ) + } + } + return path.posix.join(assetsDir, '[name].[hash].js') + }, + }, + }, + }, + } + } +} diff --git a/packages/uni-h5-vite/src/plugin/index.ts b/packages/uni-h5-vite/src/plugin/index.ts index a166a62a07297d52ff9c305c7cf5f948ecb6f179..1faa0031a557a1d1eafb03aa13fa48c437ee6fd7 100644 --- a/packages/uni-h5-vite/src/plugin/index.ts +++ b/packages/uni-h5-vite/src/plugin/index.ts @@ -1,78 +1,29 @@ -import fs from 'fs' -import path from 'path' -import { isH5CustomElement, isH5NativeTag } from '@dcloudio/uni-shared' -import { - isInHBuilderX, - resolveMainPathOnce, - transformMatchMedia, - transformTapToClick, - UniVitePlugin, -} from '@dcloudio/uni-cli-shared' +import { ResolvedConfig } from 'vite' +import { UniVitePlugin } from '@dcloudio/uni-cli-shared' import { createHandleHotUpdate } from './handleHotUpdate' import { createTransformIndexHtml } from './transformIndexHtml' -import { transformPageHead } from './transforms/transformPageHead' -import { createDefine } from '../utils/features' -import { isSsr } from '../utils' -import { esbuildPrePlugin } from './esbuild/esbuildPrePlugin' -import { external } from './configureServer/ssr' import { createConfigureServer } from './configureServer' +import { createUni } from './uni' -export const UniH5Plugin: UniVitePlugin = { - name: 'vite:uni-h5', - uni: { - copyOptions: { - assets: ['hybrid/html'], - }, - compilerOptions: { - isNativeTag: isH5NativeTag, - isCustomElement: isH5CustomElement, - nodeTransforms: [ - transformTapToClick, - transformMatchMedia, - transformPageHead, - ], +import { createConfig } from './config' + +export function uniH5PLugin(): UniVitePlugin { + const configOptions: { + resolvedConfig: ResolvedConfig | null + } = { + resolvedConfig: null, + } + return { + name: 'vite:uni-h5', + uni: createUni(), + config: createConfig(configOptions), + configResolved(config) { + configOptions.resolvedConfig = config + // TODO 禁止 optimizeDeps + ;(config as any).cacheDir = '' }, - }, - config(config, env) { - if (isInHBuilderX()) { - if ( - !fs.existsSync(path.resolve(process.env.UNI_INPUT_DIR, 'index.html')) - ) { - console.error(`请确认您的项目模板是否支持vue3:根目录缺少 index.html`) - process.exit() - } - } - return { - optimizeDeps: { - entries: resolveMainPathOnce(process.env.UNI_INPUT_DIR), - exclude: external, - esbuildOptions: { - plugins: [esbuildPrePlugin()], - }, - }, - define: createDefine(env.command, config), - server: { - host: true, - fs: { - strict: false, - }, - }, - ssr: { - external, - }, - build: { - rollupOptions: { - // resolveSSRExternal 会判定package.json,hbx 工程可能没有,通过 rollup 来配置 - external: isSsr(env.command, config) ? external : [], - }, - }, - } - }, - configResolved(config) { - // TODO 禁止 optimizeDeps - ;(config as any).cacheDir = '' - }, - configureServer: createConfigureServer(), - handleHotUpdate: createHandleHotUpdate(), - transformIndexHtml: createTransformIndexHtml(), + configureServer: createConfigureServer(), + handleHotUpdate: createHandleHotUpdate(), + transformIndexHtml: createTransformIndexHtml(), + } } diff --git a/packages/uni-h5-vite/src/plugin/uni.ts b/packages/uni-h5-vite/src/plugin/uni.ts new file mode 100644 index 0000000000000000000000000000000000000000..3de0f427f23e4efdad2506727360eacd07b6ca6e --- /dev/null +++ b/packages/uni-h5-vite/src/plugin/uni.ts @@ -0,0 +1,24 @@ +import { + transformMatchMedia, + transformPageHead, + transformTapToClick, + UniVitePlugin, +} from '@dcloudio/uni-cli-shared' +import { isH5NativeTag, isH5CustomElement } from '@dcloudio/uni-shared' + +export function createUni(): UniVitePlugin['uni'] { + return { + copyOptions: { + assets: ['hybrid/html'], + }, + compilerOptions: { + isNativeTag: isH5NativeTag, + isCustomElement: isH5CustomElement, + nodeTransforms: [ + transformTapToClick, + transformMatchMedia, + transformPageHead, + ], + }, + } +} diff --git a/packages/vite-plugin-uni/src/config/build.ts b/packages/vite-plugin-uni/src/config/build.ts index 8a8bed1729732a9a302a8430a7faeaf2575be129..e01f510c499fd1475d62261038e74df852267911 100644 --- a/packages/vite-plugin-uni/src/config/build.ts +++ b/packages/vite-plugin-uni/src/config/build.ts @@ -1,8 +1,6 @@ -import path from 'path' import { UserConfig } from 'vite' import { initEasycomsOnce, - normalizePath, resolveComponentsLibPath, } from '@dcloudio/uni-cli-shared' import { VitePluginUniResolvedOptions } from '..' @@ -16,38 +14,5 @@ export function createBuild( }) return { chunkSizeWarningLimit: 100000000, - rollupOptions: { - onwarn(warning, warn) { - if (warning.code === 'UNUSED_EXTERNAL_IMPORT') { - const { message } = warning - // ignore - if ( - message.includes('"vue"') || - message.includes('"resolveComponent"') || - message.includes('"@dcloudio/uni-h5"') - ) { - return - } - } - warn(warning) - }, - output: { - chunkFileNames(chunkInfo) { - if (chunkInfo.facadeModuleId) { - const dirname = path.relative( - options.inputDir, - path.dirname(chunkInfo.facadeModuleId) - ) - if (dirname) { - return `${options.assetsDir}/${normalizePath(dirname).replace( - /\//g, - '-' - )}-[name].[hash].js` - } - } - return '[name].[hash].js' - }, - }, - }, } }