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

fix(h5): respect config.build.assetsDir (#2982)

上级 b2e1e8d5
......@@ -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(),
]
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')
},
},
},
},
}
}
}
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(),
}
}
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,
],
},
}
}
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'
},
},
},
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册