From d5964629c944fd0bec9cfbec0a151f529a4573b1 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 11 Nov 2021 11:36:47 +0800 Subject: [PATCH] fix(h5): respect config.build.assetsDir (#2982) --- packages/uni-h5-vite/src/index.ts | 4 +- packages/uni-h5-vite/src/plugin/config.ts | 79 ++++++++++++++++ packages/uni-h5-vite/src/plugin/index.ts | 95 +++++--------------- packages/uni-h5-vite/src/plugin/uni.ts | 24 +++++ packages/vite-plugin-uni/src/config/build.ts | 35 -------- 5 files changed, 128 insertions(+), 109 deletions(-) create mode 100644 packages/uni-h5-vite/src/plugin/config.ts create mode 100644 packages/uni-h5-vite/src/plugin/uni.ts diff --git a/packages/uni-h5-vite/src/index.ts b/packages/uni-h5-vite/src/index.ts index 9fc63d44f5..a6a315bb07 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 0000000000..e16ebd386d --- /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 a166a62a07..1faa0031a5 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 0000000000..3de0f427f2 --- /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 8a8bed1729..e01f510c49 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' - }, - }, - }, } } -- GitLab