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

fix: ignore non-current platform static (#3132)

上级 e7b75950
......@@ -19,6 +19,7 @@ export * from './filter'
export * from './esbuild'
export * from './resolve'
export * from './scripts'
export * from './platform'
export { M } from './messages'
......
const BUILT_IN_PLATFORMS = [
'app',
'app-plus',
'h5',
'mp-360',
'mp-alipay',
'mp-baidu',
'mp-jd',
'mp-kuaishou',
'mp-lark',
'mp-qq',
'mp-toutiao',
'mp-weixin',
'quickapp-webview',
'quickapp-webview-huawei',
'quickapp-webview-union',
]
const platforms = [...BUILT_IN_PLATFORMS]
export function registerPlatform(platform: string) {
if (!platforms.includes(platform)) {
platforms.push(platform)
}
}
export function getPlatforms() {
return platforms
}
import type { WatchOptions } from 'chokidar'
import type { Plugin, ResolvedConfig } from 'vite'
import { FileWatcher, FileWatcherOptions } from '../../watcher'
import { M } from '../../messages'
export type UniViteCopyPluginTarget = Omit<FileWatcherOptions, 'verbose'>
export type UniViteCopyPluginTarget = Omit<FileWatcherOptions, 'verbose'> & {
watchOptions?: WatchOptions
}
export interface UniViteCopyPluginOptions {
targets: UniViteCopyPluginTarget[]
verbose: boolean
......@@ -28,7 +32,7 @@ export function uniViteCopyPlugin({
inited = true
return new Promise((resolve) => {
Promise.all(
targets.map((target) => {
targets.map(({ watchOptions, ...target }) => {
return new Promise((resolve) => {
new FileWatcher({
verbose,
......@@ -36,6 +40,7 @@ export function uniViteCopyPlugin({
}).watch(
{
cwd: process.env.UNI_INPUT_DIR,
...watchOptions,
},
(watcher) => {
if (process.env.NODE_ENV !== 'development') {
......
......@@ -68,14 +68,14 @@ export class FileWatcher {
if (content) {
return fs
.outputFile(to, content)
.catch((e) => {
.catch(() => {
// this.info('copy', e)
})
.then(() => this.onChange && this.onChange())
}
return fs
.copy(this.from(from), to, { overwrite: true })
.catch((e) => {
.catch(() => {
// this.info('copy', e)
})
.then(() => this.onChange && this.onChange())
......@@ -85,7 +85,7 @@ export class FileWatcher {
this.info('remove', from + '=>' + to)
return fs
.remove(to)
.catch((e) => {
.catch(() => {
// this.info('remove', e)
})
.then(() => this.onChange && this.onChange())
......
import fs from 'fs'
import path from 'path'
import debug from 'debug'
import type { Plugin } from 'vite'
......@@ -8,6 +9,7 @@ import {
UniViteCopyPluginTarget,
parseSubpackagesRootOnce,
normalizePath,
getPlatforms,
} from '@dcloudio/uni-cli-shared'
import { VitePluginUniResolvedOptions } from '..'
......@@ -31,10 +33,29 @@ export function uniCopyPlugin({
copyOptions!.assets.forEach((asset) => {
assets.push(asset)
})
const platform = process.env.UNI_PLATFORM
// 非当前平台 static 目录
const platformStaticDirs = getPlatforms()
.filter((p) => {
if (platform === 'app') {
return p !== 'app' && p !== 'app-plus'
}
return p !== platform
})
.map((p) => '/' + PUBLIC_DIR + '/' + p)
const targets: UniViteCopyPluginTarget[] = [
{
src: assets,
dest: outputDir,
watchOptions: {
ignored(path: string) {
const normalizedPath = normalizePath(path)
if (platformStaticDirs.find((dir) => normalizedPath.includes(dir))) {
return fs.statSync(normalizedPath).isDirectory
}
},
},
},
]
targets.push(...copyOptions!.targets)
......
import path from 'path'
import type { Plugin } from 'vite'
import { extend, isArray, isString, isFunction } from '@vue/shared'
import type {
import {
CopyOptions,
registerPlatform,
UniViteCopyPluginTarget,
UniVitePlugin,
} from '@dcloudio/uni-cli-shared'
......@@ -116,6 +117,8 @@ function resolvePlugins(cliRoot: string, platform: UniApp.PLATFORM) {
}
const { apply } = config
if (isArray(apply)) {
// 注册所有平台
apply.forEach((p) => registerPlatform(p))
if (!apply.includes(platform)) {
return
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册