Skip to content

Commit 9c7e43d

Browse files
authoredAug 24, 2022
fix: Skip inlining Git LFS placeholders (fix #9714) (#9795)
1 parent b2c0ee0 commit 9c7e43d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed
 

‎docs/config/build-options.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ Specify the directory to nest generated assets under (relative to `build.outDir`
5353

5454
Imported or referenced assets that are smaller than this threshold will be inlined as base64 URLs to avoid extra http requests. Set to `0` to disable inlining altogether.
5555

56+
Git LFS placeholders are automatically excluded from inlining because they do not contain the content of the file they represent.
57+
5658
::: tip Note
57-
If you specify `build.lib`, `build.assetsInlineLimit` will be ignored and assets will always be inlined, regardless of file size.
59+
If you specify `build.lib`, `build.assetsInlineLimit` will be ignored and assets will always be inlined, regardless of file size or being a Git LFS placeholder.
5860
:::
5961

6062
## build.cssCodeSplit

‎docs/guide/assets.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ The behavior is similar to webpack's `file-loader`. The difference is that the i
2626

2727
- Assets smaller in bytes than the [`assetsInlineLimit` option](/config/build-options.md#build-assetsinlinelimit) will be inlined as base64 data URLs.
2828

29+
- Git LFS placeholders are automatically excluded from inlining because they do not contain the content of the file they represent. To get inlining, make sure to download the file contents via Git LFS before building.
30+
2931
### Explicit URL Imports
3032

3133
Assets that are not included in the internal list or in `assetsInclude`, can be explicitly imported as a URL using the `?url` suffix. This is useful, for example, to import [Houdini Paint Worklets](https://houdini.how/usage).

‎packages/vite/src/node/plugins/asset.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'node:path'
22
import { parse as parseUrl } from 'node:url'
33
import fs, { promises as fsp } from 'node:fs'
4+
import { Buffer } from 'node:buffer'
45
import * as mrmime from 'mrmime'
56
import type {
67
NormalizedOutputOptions,
@@ -10,6 +11,7 @@ import type {
1011
RenderedChunk
1112
} from 'rollup'
1213
import MagicString from 'magic-string'
14+
import colors from 'picocolors'
1315
import { toOutputFilePathInString } from '../build'
1416
import type { Plugin } from '../plugin'
1517
import type { ResolvedConfig } from '../config'
@@ -398,6 +400,13 @@ export function publicFileToBuiltUrl(
398400
return `__VITE_PUBLIC_ASSET__${hash}__`
399401
}
400402

403+
const GIT_LFS_PREFIX = Buffer.from('version https://git-lfs.github.com')
404+
function isGitLfsPlaceholder(content: Buffer): boolean {
405+
if (content.length < GIT_LFS_PREFIX.length) return false
406+
// Check whether the content begins with the characteristic string of Git LFS placeholders
407+
return GIT_LFS_PREFIX.compare(content, 0, GIT_LFS_PREFIX.length) === 0
408+
}
409+
401410
/**
402411
* Register an asset to be emitted as part of the bundle (if necessary)
403412
* and returns the resolved public URL
@@ -426,8 +435,15 @@ async function fileToBuiltUrl(
426435
config.build.lib ||
427436
(!file.endsWith('.svg') &&
428437
!file.endsWith('.html') &&
429-
content.length < Number(config.build.assetsInlineLimit))
438+
content.length < Number(config.build.assetsInlineLimit) &&
439+
!isGitLfsPlaceholder(content))
430440
) {
441+
if (config.build.lib && isGitLfsPlaceholder(content)) {
442+
config.logger.warn(
443+
colors.yellow(`Inlined file ${id} was not downloaded via Git LFS`)
444+
)
445+
}
446+
431447
const mimeType = mrmime.lookup(file) ?? 'application/octet-stream'
432448
// base64 inlined as a string
433449
url = `data:${mimeType};base64,${content.toString('base64')}`

0 commit comments

Comments
 (0)
Please sign in to comment.