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

feat(app): copy hybrid/html

上级 199f7e66
...@@ -2,7 +2,7 @@ import type { Plugin } from 'vite' ...@@ -2,7 +2,7 @@ import type { Plugin } from 'vite'
import type { ParserOptions } from '@vue/compiler-core' import type { ParserOptions } from '@vue/compiler-core'
import type { CompilerOptions } from '@vue/compiler-sfc' import type { CompilerOptions } from '@vue/compiler-sfc'
import type { Target } from 'rollup-plugin-copy' import type { Target } from 'rollup-plugin-copy'
interface CopyOptions { export interface CopyOptions {
/** /**
* 静态资源,配置的目录,在 uni_modules 中同样支持 * 静态资源,配置的目录,在 uni_modules 中同样支持
*/ */
......
...@@ -14,23 +14,9 @@ import { createBuild } from './build' ...@@ -14,23 +14,9 @@ import { createBuild } from './build'
import { createOptimizeDeps } from './optimizeDeps' import { createOptimizeDeps } from './optimizeDeps'
import { createDefine } from './define' 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( export function createConfig(
options: VitePluginUniResolvedOptions, options: VitePluginUniResolvedOptions,
uniPlugins: Plugin[] _uniPlugins: Plugin[]
): Plugin['config'] { ): Plugin['config'] {
return (config, env) => { return (config, env) => {
options.command = env.command options.command = env.command
...@@ -44,10 +30,6 @@ export function createConfig( ...@@ -44,10 +30,6 @@ export function createConfig(
base = '/' base = '/'
} }
options.base = base! options.base = base!
// TODO 似乎没必要
options.vueOptions = initPluginVueOptions(options, uniPlugins)
options.vueJsxOptions = initPluginVueJsxOptions(options)
options.viteLegacyOptions = initPluginViteLegacyOptions(options)
return { return {
base, base,
......
...@@ -7,27 +7,45 @@ import { ...@@ -7,27 +7,45 @@ import {
uniViteCopyPlugin, uniViteCopyPlugin,
} from '@dcloudio/uni-cli-shared' } from '@dcloudio/uni-cli-shared'
import { VitePluginUniResolvedOptions } from '../..' import { VitePluginUniResolvedOptions } from '../..'
import type { Target } from 'rollup-plugin-copy'
export function uniCopyPlugin({ export function uniCopyPlugin({
inputDir, inputDir,
outputDir, outputDir,
copyOptions,
}: VitePluginUniResolvedOptions): Plugin { }: VitePluginUniResolvedOptions): Plugin {
return uniViteCopyPlugin({ const targets: Target[] = [
targets: [ {
{ src: normalizePath(path.resolve(inputDir, PUBLIC_DIR)),
src: normalizePath(path.resolve(inputDir, PUBLIC_DIR)), dest: outputDir,
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( targets.push({
path.resolve(inputDir, 'uni_modules/*/' + PUBLIC_DIR) src: normalizePath(path.resolve(inputDir, 'uni_modules/*/' + asset)),
), dest: outputDir,
dest: outputDir, rename: (_name, _extension, fullPath) => {
rename: (_name, _extension, fullPath) => { return path.relative(inputDir, fullPath)
return path.relative(inputDir, fullPath)
},
}, },
], })
})
targets.push(...copyOptions!.targets)
return uniViteCopyPlugin({
targets,
hook: 'writeBundle', hook: 'writeBundle',
verbose: process.env.DEBUG ? true : false, verbose: process.env.DEBUG ? true : false,
}) })
......
...@@ -7,13 +7,21 @@ import vuePlugin from '@vitejs/plugin-vue' ...@@ -7,13 +7,21 @@ import vuePlugin from '@vitejs/plugin-vue'
import type VueJsxPlugin from '@vitejs/plugin-vue-jsx' import type VueJsxPlugin from '@vitejs/plugin-vue-jsx'
import type ViteLegacyPlugin from '@vitejs/plugin-legacy' 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 { createConfig } from './config'
import { createConfigResolved } from './configResolved' import { createConfigResolved } from './configResolved'
import { createConfigureServer } from './configureServer' import { createConfigureServer } from './configureServer'
import { initExtraPlugins } from './utils' import { initExtraPlugins, initPluginUniOptions } from './utils'
import { initPluginVueOptions } from './vue' import {
initPluginViteLegacyOptions,
initPluginVueJsxOptions,
initPluginVueOptions,
} from './vue'
// import { createResolveId } from './resolveId' // import { createResolveId } from './resolveId'
const debugUni = debug('vite:uni:plugin') const debugUni = debug('vite:uni:plugin')
...@@ -39,6 +47,7 @@ export interface VitePluginUniResolvedOptions extends VitePluginUniOptions { ...@@ -39,6 +47,7 @@ export interface VitePluginUniResolvedOptions extends VitePluginUniOptions {
outputDir: string outputDir: string
assetsDir: string assetsDir: string
devServer?: ViteDevServer devServer?: ViteDevServer
copyOptions?: Required<CopyOptions>
} }
export { runDev, runBuild } from './cli/action' export { runDev, runBuild } from './cli/action'
...@@ -76,13 +85,13 @@ export default function uniPlugin( ...@@ -76,13 +85,13 @@ export default function uniPlugin(
if (createViteLegacyPlugin && options.viteLegacyOptions !== false) { if (createViteLegacyPlugin && options.viteLegacyOptions !== false) {
plugins.push( plugins.push(
...(createViteLegacyPlugin( ...(createViteLegacyPlugin(
options.viteLegacyOptions initPluginViteLegacyOptions(options)
) as unknown as Plugin[]) ) as unknown as Plugin[])
) )
} }
if (createVueJsxPlugin && options.vueJsxOptions !== false) { if (createVueJsxPlugin && options.vueJsxOptions !== false) {
plugins.push(createVueJsxPlugin(options.vueJsxOptions)) plugins.push(createVueJsxPlugin(initPluginVueJsxOptions(options)))
} }
const uniPlugins = initExtraPlugins( const uniPlugins = initExtraPlugins(
...@@ -90,6 +99,11 @@ export default function uniPlugin( ...@@ -90,6 +99,11 @@ export default function uniPlugin(
(process.env.UNI_PLATFORM as UniApp.PLATFORM) || 'h5' (process.env.UNI_PLATFORM as UniApp.PLATFORM) || 'h5'
) )
debugUni(uniPlugins) debugUni(uniPlugins)
const uniPluginOptions = initPluginUniOptions(uniPlugins)
options.copyOptions = uniPluginOptions.copyOptions
plugins.push({ plugins.push({
name: 'vite:uni', name: 'vite:uni',
config: createConfig(options, uniPlugins), config: createConfig(options, uniPlugins),
...@@ -99,7 +113,9 @@ export default function uniPlugin( ...@@ -99,7 +113,9 @@ export default function uniPlugin(
}) })
plugins.push(...uniPlugins) plugins.push(...uniPlugins)
plugins.unshift(vuePlugin(initPluginVueOptions(options, uniPlugins))) plugins.unshift(
vuePlugin(initPluginVueOptions(options, uniPlugins, uniPluginOptions))
)
return plugins return plugins
} }
import path from 'path' import path from 'path'
import { Plugin } from 'vite' import type { Plugin } from 'vite'
import { extend, isArray, isString } from '@vue/shared' import { extend, isArray, isString, isFunction } from '@vue/shared'
import { isCustomElement, isNativeTag } from '@dcloudio/uni-shared' import { isCustomElement, isNativeTag } from '@dcloudio/uni-shared'
import { UniVitePlugin } from '@dcloudio/uni-cli-shared' import type { CopyOptions, UniVitePlugin } from '@dcloudio/uni-cli-shared'
import { CompilerOptions } from '@vue/compiler-sfc' import type { Target } from 'rollup-plugin-copy'
interface PluginConfig { interface PluginConfig {
id: string id: string
...@@ -16,13 +16,16 @@ interface PluginConfig { ...@@ -16,13 +16,16 @@ interface PluginConfig {
} }
export function initPluginUniOptions(UniVitePlugins: UniVitePlugin[]) { export function initPluginUniOptions(UniVitePlugins: UniVitePlugin[]) {
const assets: string[] = []
const targets: Target[] = []
const transformEvent: Record<string, string> = Object.create(null) const transformEvent: Record<string, string> = Object.create(null)
const compilerOptions: CompilerOptions = { const compilerOptions: Required<UniVitePlugin>['uni']['compilerOptions'] = {
isNativeTag, isNativeTag,
isCustomElement, isCustomElement,
} }
UniVitePlugins.forEach((plugin) => { UniVitePlugins.forEach((plugin) => {
const { const {
copyOptions: pluginCopyOptions,
compilerOptions: pluginCompilerOptions, compilerOptions: pluginCompilerOptions,
transformEvent: pluginTransformEvent, transformEvent: pluginTransformEvent,
} = plugin.uni || {} } = plugin.uni || {}
...@@ -41,8 +44,24 @@ export function initPluginUniOptions(UniVitePlugins: UniVitePlugin[]) { ...@@ -41,8 +44,24 @@ export function initPluginUniOptions(UniVitePlugins: UniVitePlugin[]) {
if (pluginTransformEvent) { if (pluginTransformEvent) {
extend(transformEvent, 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 { return {
copyOptions: {
assets,
targets,
},
transformEvent, transformEvent,
compilerOptions, compilerOptions,
} }
......
...@@ -10,7 +10,6 @@ import { ...@@ -10,7 +10,6 @@ import {
import { VitePluginUniResolvedOptions } from '..' import { VitePluginUniResolvedOptions } from '..'
import { transformMatchMedia } from './transforms/transformMatchMedia' import { transformMatchMedia } from './transforms/transformMatchMedia'
import { createTransformEvent } from './transforms/transformEvent' import { createTransformEvent } from './transforms/transformEvent'
import { initPluginUniOptions } from '../utils/plugin'
// import { transformContext } from './transforms/transformContext' // import { transformContext } from './transforms/transformContext'
function createUniVueTransformAssetUrls( function createUniVueTransformAssetUrls(
...@@ -38,7 +37,8 @@ function createUniVueTransformAssetUrls( ...@@ -38,7 +37,8 @@ function createUniVueTransformAssetUrls(
export function initPluginVueOptions( export function initPluginVueOptions(
options: VitePluginUniResolvedOptions, options: VitePluginUniResolvedOptions,
UniVitePlugins: UniVitePlugin[] UniVitePlugins: UniVitePlugin[],
uniPluginOptions: Required<Required<UniVitePlugin>['uni']>
) { ) {
const vueOptions = options.vueOptions || (options.vueOptions = {}) const vueOptions = options.vueOptions || (options.vueOptions = {})
if (!vueOptions.include) { if (!vueOptions.include) {
...@@ -55,13 +55,12 @@ export function initPluginVueOptions( ...@@ -55,13 +55,12 @@ export function initPluginVueOptions(
options.base options.base
) )
const {
compilerOptions: { isNativeTag, isCustomElement, directiveTransforms },
} = initPluginUniOptions(UniVitePlugins)
const compilerOptions = const compilerOptions =
templateOptions.compilerOptions || (templateOptions.compilerOptions = {}) templateOptions.compilerOptions || (templateOptions.compilerOptions = {})
const {
compilerOptions: { isNativeTag, isCustomElement, directiveTransforms },
} = uniPluginOptions
compilerOptions.isNativeTag = isNativeTag compilerOptions.isNativeTag = isNativeTag
compilerOptions.isCustomElement = isCustomElement compilerOptions.isCustomElement = isCustomElement
if (directiveTransforms) { if (directiveTransforms) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册