From a3156716e68f4976b75a10c38f2ae6b86ca775da Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 4 Aug 2021 20:26:03 +0800 Subject: [PATCH] feat(app): copy hybrid/html --- packages/uni-cli-shared/src/vite/index.ts | 2 +- packages/vite-plugin-uni/src/config/index.ts | 20 +------- .../src/configResolved/plugins/copy.ts | 46 +++++++++++++------ packages/vite-plugin-uni/src/index.ts | 28 ++++++++--- packages/vite-plugin-uni/src/utils/plugin.ts | 29 ++++++++++-- packages/vite-plugin-uni/src/vue/options.ts | 11 ++--- 6 files changed, 85 insertions(+), 51 deletions(-) diff --git a/packages/uni-cli-shared/src/vite/index.ts b/packages/uni-cli-shared/src/vite/index.ts index 35d6ff215e..f94afd60da 100644 --- a/packages/uni-cli-shared/src/vite/index.ts +++ b/packages/uni-cli-shared/src/vite/index.ts @@ -2,7 +2,7 @@ import type { Plugin } from 'vite' import type { ParserOptions } from '@vue/compiler-core' import type { CompilerOptions } from '@vue/compiler-sfc' import type { Target } from 'rollup-plugin-copy' -interface CopyOptions { +export interface CopyOptions { /** * 静态资源,配置的目录,在 uni_modules 中同样支持 */ diff --git a/packages/vite-plugin-uni/src/config/index.ts b/packages/vite-plugin-uni/src/config/index.ts index eca66e1661..89f3cc348b 100644 --- a/packages/vite-plugin-uni/src/config/index.ts +++ b/packages/vite-plugin-uni/src/config/index.ts @@ -14,23 +14,9 @@ import { createBuild } from './build' import { createOptimizeDeps } from './optimizeDeps' import { createDefine } from './define' -import { - initPluginViteLegacyOptions, - initPluginVueJsxOptions, - initPluginVueOptions, -} from '../vue' - -// function normalizeRoot(config: UserConfig) { -// return normalizePath(config.root ? path.resolve(config.root) : process.cwd()) -// } - -// function normalizeInputDir(config: UserConfig) { -// return process.env.UNI_INPUT_DIR || path.resolve(normalizeRoot(config), 'src') -// } - export function createConfig( options: VitePluginUniResolvedOptions, - uniPlugins: Plugin[] + _uniPlugins: Plugin[] ): Plugin['config'] { return (config, env) => { options.command = env.command @@ -44,10 +30,6 @@ export function createConfig( base = '/' } options.base = base! - // TODO 似乎没必要 - options.vueOptions = initPluginVueOptions(options, uniPlugins) - options.vueJsxOptions = initPluginVueJsxOptions(options) - options.viteLegacyOptions = initPluginViteLegacyOptions(options) return { base, diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/copy.ts b/packages/vite-plugin-uni/src/configResolved/plugins/copy.ts index 00f5e4c55d..3bdb1b172d 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/copy.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/copy.ts @@ -7,27 +7,45 @@ import { uniViteCopyPlugin, } from '@dcloudio/uni-cli-shared' import { VitePluginUniResolvedOptions } from '../..' +import type { Target } from 'rollup-plugin-copy' export function uniCopyPlugin({ inputDir, outputDir, + copyOptions, }: VitePluginUniResolvedOptions): Plugin { - return uniViteCopyPlugin({ - targets: [ - { - src: normalizePath(path.resolve(inputDir, PUBLIC_DIR)), - dest: outputDir, + const targets: Target[] = [ + { + src: normalizePath(path.resolve(inputDir, PUBLIC_DIR)), + dest: outputDir, + }, + { + src: normalizePath(path.resolve(inputDir, 'uni_modules/*/' + PUBLIC_DIR)), + dest: outputDir, + rename: (_name, _extension, fullPath) => { + return path.relative(inputDir, fullPath) + }, + }, + ] + copyOptions!.assets.forEach((asset) => { + targets.push({ + src: normalizePath(path.resolve(inputDir, asset)), + dest: outputDir, + rename: (_name, _extension, fullPath) => { + return path.relative(inputDir, fullPath) }, - { - src: normalizePath( - path.resolve(inputDir, 'uni_modules/*/' + PUBLIC_DIR) - ), - dest: outputDir, - rename: (_name, _extension, fullPath) => { - return path.relative(inputDir, fullPath) - }, + }) + targets.push({ + src: normalizePath(path.resolve(inputDir, 'uni_modules/*/' + asset)), + dest: outputDir, + rename: (_name, _extension, fullPath) => { + return path.relative(inputDir, fullPath) }, - ], + }) + }) + targets.push(...copyOptions!.targets) + return uniViteCopyPlugin({ + targets, hook: 'writeBundle', verbose: process.env.DEBUG ? true : false, }) diff --git a/packages/vite-plugin-uni/src/index.ts b/packages/vite-plugin-uni/src/index.ts index 6cde3d2ebe..a1dde8658c 100644 --- a/packages/vite-plugin-uni/src/index.ts +++ b/packages/vite-plugin-uni/src/index.ts @@ -7,13 +7,21 @@ import vuePlugin from '@vitejs/plugin-vue' import type VueJsxPlugin from '@vitejs/plugin-vue-jsx' import type ViteLegacyPlugin from '@vitejs/plugin-legacy' -import { initModuleAlias, initPreContext } from '@dcloudio/uni-cli-shared' +import { + CopyOptions, + initModuleAlias, + initPreContext, +} from '@dcloudio/uni-cli-shared' import { createConfig } from './config' import { createConfigResolved } from './configResolved' import { createConfigureServer } from './configureServer' -import { initExtraPlugins } from './utils' -import { initPluginVueOptions } from './vue' +import { initExtraPlugins, initPluginUniOptions } from './utils' +import { + initPluginViteLegacyOptions, + initPluginVueJsxOptions, + initPluginVueOptions, +} from './vue' // import { createResolveId } from './resolveId' const debugUni = debug('vite:uni:plugin') @@ -39,6 +47,7 @@ export interface VitePluginUniResolvedOptions extends VitePluginUniOptions { outputDir: string assetsDir: string devServer?: ViteDevServer + copyOptions?: Required } export { runDev, runBuild } from './cli/action' @@ -76,13 +85,13 @@ export default function uniPlugin( if (createViteLegacyPlugin && options.viteLegacyOptions !== false) { plugins.push( ...(createViteLegacyPlugin( - options.viteLegacyOptions + initPluginViteLegacyOptions(options) ) as unknown as Plugin[]) ) } if (createVueJsxPlugin && options.vueJsxOptions !== false) { - plugins.push(createVueJsxPlugin(options.vueJsxOptions)) + plugins.push(createVueJsxPlugin(initPluginVueJsxOptions(options))) } const uniPlugins = initExtraPlugins( @@ -90,6 +99,11 @@ export default function uniPlugin( (process.env.UNI_PLATFORM as UniApp.PLATFORM) || 'h5' ) debugUni(uniPlugins) + + const uniPluginOptions = initPluginUniOptions(uniPlugins) + + options.copyOptions = uniPluginOptions.copyOptions + plugins.push({ name: 'vite:uni', config: createConfig(options, uniPlugins), @@ -99,7 +113,9 @@ export default function uniPlugin( }) plugins.push(...uniPlugins) - plugins.unshift(vuePlugin(initPluginVueOptions(options, uniPlugins))) + plugins.unshift( + vuePlugin(initPluginVueOptions(options, uniPlugins, uniPluginOptions)) + ) return plugins } diff --git a/packages/vite-plugin-uni/src/utils/plugin.ts b/packages/vite-plugin-uni/src/utils/plugin.ts index 60d17f1f8d..481224245b 100644 --- a/packages/vite-plugin-uni/src/utils/plugin.ts +++ b/packages/vite-plugin-uni/src/utils/plugin.ts @@ -1,9 +1,9 @@ import path from 'path' -import { Plugin } from 'vite' -import { extend, isArray, isString } from '@vue/shared' +import type { Plugin } from 'vite' +import { extend, isArray, isString, isFunction } from '@vue/shared' import { isCustomElement, isNativeTag } from '@dcloudio/uni-shared' -import { UniVitePlugin } from '@dcloudio/uni-cli-shared' -import { CompilerOptions } from '@vue/compiler-sfc' +import type { CopyOptions, UniVitePlugin } from '@dcloudio/uni-cli-shared' +import type { Target } from 'rollup-plugin-copy' interface PluginConfig { id: string @@ -16,13 +16,16 @@ interface PluginConfig { } export function initPluginUniOptions(UniVitePlugins: UniVitePlugin[]) { + const assets: string[] = [] + const targets: Target[] = [] const transformEvent: Record = Object.create(null) - const compilerOptions: CompilerOptions = { + const compilerOptions: Required['uni']['compilerOptions'] = { isNativeTag, isCustomElement, } UniVitePlugins.forEach((plugin) => { const { + copyOptions: pluginCopyOptions, compilerOptions: pluginCompilerOptions, transformEvent: pluginTransformEvent, } = plugin.uni || {} @@ -41,8 +44,24 @@ export function initPluginUniOptions(UniVitePlugins: UniVitePlugin[]) { if (pluginTransformEvent) { extend(transformEvent, pluginTransformEvent) } + if (pluginCopyOptions) { + let copyOptions = pluginCopyOptions as CopyOptions + if (isFunction(pluginCopyOptions)) { + copyOptions = pluginCopyOptions() + } + if (copyOptions.assets) { + assets.push(...copyOptions.assets) + } + if (copyOptions.targets) { + targets.push(...copyOptions.targets) + } + } }) return { + copyOptions: { + assets, + targets, + }, transformEvent, compilerOptions, } diff --git a/packages/vite-plugin-uni/src/vue/options.ts b/packages/vite-plugin-uni/src/vue/options.ts index f18bebb4a6..0adebd1293 100644 --- a/packages/vite-plugin-uni/src/vue/options.ts +++ b/packages/vite-plugin-uni/src/vue/options.ts @@ -10,7 +10,6 @@ import { import { VitePluginUniResolvedOptions } from '..' import { transformMatchMedia } from './transforms/transformMatchMedia' import { createTransformEvent } from './transforms/transformEvent' -import { initPluginUniOptions } from '../utils/plugin' // import { transformContext } from './transforms/transformContext' function createUniVueTransformAssetUrls( @@ -38,7 +37,8 @@ function createUniVueTransformAssetUrls( export function initPluginVueOptions( options: VitePluginUniResolvedOptions, - UniVitePlugins: UniVitePlugin[] + UniVitePlugins: UniVitePlugin[], + uniPluginOptions: Required['uni']> ) { const vueOptions = options.vueOptions || (options.vueOptions = {}) if (!vueOptions.include) { @@ -55,13 +55,12 @@ export function initPluginVueOptions( options.base ) - const { - compilerOptions: { isNativeTag, isCustomElement, directiveTransforms }, - } = initPluginUniOptions(UniVitePlugins) - const compilerOptions = templateOptions.compilerOptions || (templateOptions.compilerOptions = {}) + const { + compilerOptions: { isNativeTag, isCustomElement, directiveTransforms }, + } = uniPluginOptions compilerOptions.isNativeTag = isNativeTag compilerOptions.isCustomElement = isCustomElement if (directiveTransforms) { -- GitLab