提交 a3156716 编写于 作者: fxy060608's avatar fxy060608

feat(app): copy hybrid/html

上级 199f7e66
......@@ -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 中同样支持
*/
......
......@@ -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,
......
......@@ -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,
})
......
......@@ -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<CopyOptions>
}
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
}
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<string, string> = Object.create(null)
const compilerOptions: CompilerOptions = {
const compilerOptions: Required<UniVitePlugin>['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,
}
......
......@@ -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<Required<UniVitePlugin>['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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册