Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(vite): align asset inline threshold with webpack
  • Loading branch information
Jarvis636431 authored and Single-Dancer committed Oct 23, 2026
commit 29bb839f616f0b0a7471b4dfc51161fd7dd9889c
2 changes: 1 addition & 1 deletion packages/taro-vite-runner/src/common/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default function (viteCompilerContext: ViteH5CompilerContext | ViteMiniCo
const isEsModule = isBoolean(options.esModule) ? options.esModule : true

let url: string
if (limit === true || (!isBoolean(limit) && source.length < limit)) {
if (limit === true || (!isBoolean(limit) && source.length <= limit)) {
const mimeType = mrmime.lookup(id) ?? 'application/octet-stream'
url = `data:${mimeType};base64,${source.toString('base64')}`
} else {
Expand Down
24 changes: 22 additions & 2 deletions packages/taro-vite-runner/tests/common/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('asset inline limits', () => {
[undefined, false],
[0, false],
[10, false],
[32, false],
[32, true],
[33, true]
] as const)('limit=%s respects the byte threshold (inline=%s)', async (limit, inline) => {
const { code, emitFile } = await loadAsset(extension, { [option]: { limit } })
Expand All @@ -78,6 +78,26 @@ describe('asset inline limits', () => {
})
}
})

test.each([
[true, true],
[false, false],
[undefined, false],
[0, true]
] as const)('limit=%s handles empty assets (inline=%s)', async (limit, inline) => {
const { code, emitFile } = await loadAsset(extension, { [option]: { limit } }, 0)
if (inline) {
expect(code).toMatch(/^export default "data:[^;]+;base64,"$/)
expect(emitFile).not.toHaveBeenCalled()
} else {
expect(code).toBe('export default "__VITE_ASSET__test-asset__"')
expect(emitFile).toHaveBeenCalledWith({
type: 'asset',
fileName: `asset.${extension}`,
source: new Uint8Array(0)
})
}
})
})

describe.each([
Expand All @@ -97,7 +117,7 @@ describe('asset inline limits', () => {

expect(context.taroConfig[option]?.limit).toBe(defaultLimit)

for (const [size, inline] of [[defaultLimit - 1, true], [defaultLimit, false], [defaultLimit + 1, false]] as const) {
for (const [size, inline] of [[defaultLimit - 1, true], [defaultLimit, true], [defaultLimit + 1, false]] as const) {
const { code, emitFile } = await loadAsset(extension, context.taroConfig, size)
if (inline) {
expect(code).toMatch(/^export default "data:/)
Expand Down
Loading