Repository navigation
fix(vite): 修复资源内联的数字阈值判断 #19559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Single-Dancer
merged 4 commits into
NervJS:main
from
Jarvis636431:fix/vite-asset-inline-limit
Oct 23, 2026
+149
−1
Merged
fix(vite): 修复资源内联的数字阈值判断 #19559
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
52374c7
fix(vite): 修复资源内联的数字阈值判断
Jarvis636431 ee3cb2e
test(vite): 统一资源测试路径以兼容 Windows
Jarvis636431 e06c6db
test(vite): cover asset limits after compiler defaults merge
Jarvis636431 29bb839
fix(vite): align asset inline threshold with webpack
Jarvis636431 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fix(vite): 修复资源内联的数字阈值判断
- Loading branch information
commit 52374c773e5b864948ac8ede457698589544c076
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import path from 'node:path' | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' | ||
|
|
||
| import assetsPlugin from '../../src/common/vite-plugin-assets' | ||
|
|
||
| import type { ViteH5CompilerContext } from '@tarojs/taro/types/compile/viteCompilerContext' | ||
| import type { PluginContext } from 'rollup' | ||
| import type { ResolvedConfig } from 'vite' | ||
|
|
||
| // The runner is compiled to CommonJS; mirror its default-import interop in Vitest's ESM loader. | ||
| vi.mock('mrmime', async () => ({ default: await vi.importActual('mrmime') })) | ||
|
|
||
| const assetTypes = [ | ||
| ['png', 'imageUrlLoaderOption'], | ||
| ['woff', 'fontUrlLoaderOption'], | ||
| ['mp4', 'mediaUrlLoaderOption'] | ||
| ] as const | ||
|
|
||
| describe('asset inline limits', () => { | ||
| let root: string | ||
|
|
||
| beforeEach(() => { | ||
| root = mkdtempSync(path.join(tmpdir(), 'taro-asset-limit-')) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| rmSync(root, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| async function loadAsset (extension: string, option: string, limit: number | boolean | undefined, esModule = true) { | ||
| const id = path.join(root, `asset.${extension}`) | ||
| writeFileSync(id, Buffer.alloc(32, 1)) | ||
| const plugin = assetsPlugin({ | ||
| sourceDir: root, | ||
| taroConfig: { [option]: { limit, esModule } } | ||
| } as unknown as ViteH5CompilerContext) as unknown as { | ||
| configResolved(config: ResolvedConfig): void | ||
| buildStart(): void | ||
| load(this: PluginContext, id: string): Promise<string> | ||
| } | ||
| plugin.configResolved({ assetsInclude: () => true } as unknown as ResolvedConfig) | ||
| plugin.buildStart() | ||
| const emitFile = vi.fn(() => 'test-asset') | ||
| const code = await plugin.load.call({ emitFile } as unknown as PluginContext, id) | ||
| return { code, emitFile } | ||
| } | ||
|
|
||
| describe.each(assetTypes)('%s assets', (extension, option) => { | ||
| test.each([ | ||
| [true, true], | ||
| [false, false], | ||
| [undefined, false], | ||
| [0, false], | ||
| [10, false], | ||
| [32, false], | ||
| [33, true] | ||
| ] as const)('limit=%s respects the byte threshold (inline=%s)', async (limit, inline) => { | ||
| const { code, emitFile } = await loadAsset(extension, option, limit) | ||
| if (inline) { | ||
| expect(code).toMatch(/^export default "data:/) | ||
| expect(code).toContain(Buffer.alloc(32, 1).toString('base64')) | ||
| expect(emitFile).not.toHaveBeenCalled() | ||
| } else { | ||
| expect(code).toBe('export default "__VITE_ASSET__test-asset__"') | ||
| expect(emitFile).toHaveBeenCalledWith({ | ||
| type: 'asset', | ||
| fileName: `asset.${extension}`, | ||
| source: Uint8Array.from(Buffer.alloc(32, 1)) | ||
| }) | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| test.each([true, false])('preserves CommonJS output for inline=%s', async inline => { | ||
| const { code } = await loadAsset('png', 'imageUrlLoaderOption', inline ? 33 : 10, false) | ||
| expect(code.startsWith('module.exports = "')).toBe(true) | ||
| expect(code.includes('data:')).toBe(inline) | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: NervJS/taro
Length of output: 18673
🏁 Script executed:
Repository: NervJS/taro
Length of output: 16288
修正 Windows 上的资源文件名。
Windows CI 包含
pnpm test。测试中的path.join(root, ...)会生成反斜杠路径,但插件只移除sourceDir + '/'。因此,emitFile会收到绝对路径,asset.png等断言会失败。请使用跨平台的相对路径计算,并保留此断言。建议修复
🤖 Prompt for AI Agents