Skip to content
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

feat(asset): inline svg in dev if within limit #18581

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ export interface BuildEnvironmentOptions {
assetsDir?: string
/**
* Static asset files smaller than this number (in bytes) will be inlined as
* base64 strings. Default limit is `4096` (4 KiB). Set to `0` to disable.
* base64 strings. If a callback is passed, a boolean can be returned to opt-in
* or opt-out of inlining. If nothing is returned the default logic applies.
*
* Default limit is `4096` (4 KiB). Set to `0` to disable.
* @default 4096
*/
assetsInlineLimit?:
Expand Down
54 changes: 36 additions & 18 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const jsSourceMapRE = /\.[cm]?js\.map$/

const noInlineRE = /[?&]no-inline\b/
const inlineRE = /[?&]inline\b/
const svgExtRE = /\.svg(?:$|\?)/

const assetCache = new WeakMap<Environment, Map<string, string>>()

Expand Down Expand Up @@ -180,11 +181,11 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
let url = await fileToUrl(this, id)

// Inherit HMR timestamp if this asset was invalidated
const environment = this.environment
const mod =
environment.mode === 'dev' && environment.moduleGraph.getModuleById(id)
if (mod && mod.lastHMRTimestamp > 0) {
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
if (!url.startsWith('data:') && this.environment.mode === 'dev') {
const mod = this.environment.moduleGraph.getModuleById(id)
if (mod && mod.lastHMRTimestamp > 0) {
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
}
}

return {
Expand Down Expand Up @@ -266,16 +267,27 @@ export async function fileToDevUrl(
skipBase = false,
): Promise<string> {
const config = environment.getTopLevelConfig()
const publicFile = checkPublicFile(id, config)

// If has inline query, unconditionally inline the asset
if (inlineRE.test(id)) {
const file = checkPublicFile(id, config) || cleanUrl(id)
const file = publicFile || cleanUrl(id)
const content = await fsp.readFile(file)
return assetToDataURL(environment, file, content)
}

// If is svg and it's inlined in build, also inline it in dev to match
// the behaviour in build due to quote handling differences.
if (svgExtRE.test(id)) {
const file = publicFile || cleanUrl(id)
const content = await fsp.readFile(file)
if (shouldInline(environment, file, id, content, undefined, undefined)) {
return assetToDataURL(environment, file, content)
}
}

let rtn: string
if (checkPublicFile(id, config)) {
if (publicFile) {
// in public dir during dev, keep the url as-is
rtn = id
} else if (id.startsWith(withTrailingSlash(config.root))) {
Expand Down Expand Up @@ -369,7 +381,9 @@ async function fileToBuiltUrl(
const content = await fsp.readFile(file)

let url: string
if (shouldInline(pluginContext, file, id, content, forceInline)) {
if (
shouldInline(environment, file, id, content, pluginContext, forceInline)
) {
url = assetToDataURL(environment, file, content)
} else {
// emit as asset
Expand Down Expand Up @@ -413,31 +427,35 @@ export async function urlToBuiltUrl(
)
}

const shouldInline = (
pluginContext: PluginContext,
function shouldInline(
environment: Environment,
file: string,
id: string,
content: Buffer,
/** Should be passed only in build */
buildPluginContext: PluginContext | undefined,
forceInline: boolean | undefined,
): boolean => {
const environment = pluginContext.environment
const { assetsInlineLimit } = environment.config.build
): boolean {
if (noInlineRE.test(id)) return false
if (inlineRE.test(id)) return true
if (environment.config.build.lib) return true
if (pluginContext.getModuleInfo(id)?.isEntry) return false
// Do build only checks if passed the plugin context during build
if (buildPluginContext) {
if (environment.config.build.lib) return true
if (buildPluginContext.getModuleInfo(id)?.isEntry) return false
}
if (forceInline !== undefined) return forceInline
if (file.endsWith('.html')) return false
// Don't inline SVG with fragments, as they are meant to be reused
if (file.endsWith('.svg') && id.includes('#')) return false
let limit: number
const { assetsInlineLimit } = environment.config.build
if (typeof assetsInlineLimit === 'function') {
const userShouldInline = assetsInlineLimit(file, content)
if (userShouldInline != null) return userShouldInline
limit = DEFAULT_ASSETS_INLINE_LIMIT
} else {
limit = Number(assetsInlineLimit)
}
if (file.endsWith('.html')) return false
// Don't inline SVG with fragments, as they are meant to be reused
if (file.endsWith('.svg') && id.includes('#')) return false
return content.length < limit && !isGitLfsPlaceholder(content)
}

Expand Down
14 changes: 4 additions & 10 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,11 @@ describe('css url() references', () => {
})

test('url() with svg', async () => {
expect(await getBg('.css-url-svg')).toMatch(
isBuild ? /data:image\/svg\+xml,.+/ : '/foo/bar/nested/fragment-bg.svg',
)
expect(await getBg('.css-url-svg')).toMatch(/data:image\/svg\+xml,.+/)
})

test('image-set() with svg', async () => {
expect(await getBg('.css-image-set-svg')).toMatch(
isBuild ? /data:image\/svg\+xml,.+/ : '/foo/bar/nested/fragment-bg.svg',
)
expect(await getBg('.css-image-set-svg')).toMatch(/data:image\/svg\+xml,.+/)
})
})

Expand Down Expand Up @@ -376,10 +372,8 @@ describe('svg fragments', () => {
test('from js import', async () => {
const img = await page.$('.svg-frag-import')
expect(await img.getAttribute('src')).toMatch(
isBuild
? // Assert trimmed (data URI starts with < and ends with >)
/^data:image\/svg\+xml,%3c.*%3e#icon-heart-view$/
: /svg#icon-heart-view$/,
// Assert trimmed (data URI starts with < and ends with >)
/^data:image\/svg\+xml,%3c.*%3e#icon-heart-view$/,
)
})

Expand Down
22 changes: 20 additions & 2 deletions playground/hmr-ssr/__tests__/hmr-ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,17 +884,35 @@ if (!isBuild) {
)
})

test('assets HMR', async () => {
test('not inlined assets HMR', async () => {
await setupModuleRunner('/hmr.ts')
const el = () => hmr('#logo-no-inline')
await untilConsoleLogAfter(
() =>
editFile('logo-no-inline.svg', (code) =>
code.replace('height="30px"', 'height="40px"'),
),
/Logo-no-inline updated/,
)
await vi.waitUntil(() => el().includes('logo-no-inline.svg?t='))
})

test('inlined assets HMR', async () => {
await setupModuleRunner('/hmr.ts')
const el = () => hmr('#logo')
const initialLogoUrl = el()
expect(initialLogoUrl).toMatch(/^data:image\/svg\+xml/)
await untilConsoleLogAfter(
() =>
editFile('logo.svg', (code) =>
code.replace('height="30px"', 'height="40px"'),
),
/Logo updated/,
)
await vi.waitUntil(() => el().includes('logo.svg?t='))
// Should be updated with new data url
const updatedLogoUrl = el()
expect(updatedLogoUrl).toMatch(/^data:image\/svg\+xml/)
expect(updatedLogoUrl).not.toEqual(initialLogoUrl)
})
} else {
test('this file only includes test for serve', () => {
Expand Down
15 changes: 11 additions & 4 deletions playground/hmr-ssr/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import './intermediate-file-delete'
import './circular'
import './queries'
import logo from './logo.svg'
import logoNoInline from './logo-no-inline.svg'
import { msg as softInvalidationMsg } from './soft-invalidation'

export const foo = 1
Expand All @@ -16,7 +17,8 @@ text('.dep', depFoo)
text('.nested', nestedFoo)
text('.virtual', virtual)
text('.soft-invalidation', softInvalidationMsg)
setLogo(logo)
setImgSrc('#logo', logo)
setImgSrc('#logo-no-inline', logoNoInline)

globalThis.__HMR__['virtual:increment'] = () => {
if (import.meta.hot) {
Expand All @@ -41,10 +43,15 @@ if (import.meta.hot) {
}

import.meta.hot.accept('./logo.svg', (newUrl) => {
setLogo(newUrl.default)
setImgSrc('#logo', newUrl.default)
log('Logo updated', newUrl.default)
})

import.meta.hot.accept('./logo-no-inline.svg', (newUrl) => {
setImgSrc('#logo-no-inline', newUrl.default)
log('Logo-no-inline updated', newUrl.default)
})

import.meta.hot.accept('./hmrDep', ({ foo, nestedFoo }) => {
handleDep('single dep', foo, nestedFoo)
})
Expand Down Expand Up @@ -98,8 +105,8 @@ function text(el, text) {
hmr(el, text)
}

function setLogo(src) {
hmr('#logo', src)
function setImgSrc(el, src) {
hmr(el, src)
}

function removeCb({ msg }) {
Expand Down
3 changes: 3 additions & 0 deletions playground/hmr-ssr/logo-no-inline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions playground/hmr-ssr/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ export default defineConfig({
experimental: {
hmrPartialAccept: true,
},
build: {
assetsInlineLimit(filePath) {
if (filePath.endsWith('logo-no-inline.svg')) {
return false
}
},
},
plugins: [
{
name: 'mock-custom',
Expand Down
15 changes: 14 additions & 1 deletion playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,20 @@ if (!isBuild) {
)
})

test('assets HMR', async () => {
test('not inlined assets HMR', async () => {
await page.goto(viteTestUrl)
const el = await page.$('#logo-no-inline')
await untilBrowserLogAfter(
() =>
editFile('logo-no-inline.svg', (code) =>
code.replace('height="30px"', 'height="40px"'),
),
/Logo-no-inline updated/,
)
await untilUpdated(() => el.evaluate((it) => `${it.clientHeight}`), '40')
})

test('inlined assets HMR', async () => {
await page.goto(viteTestUrl)
const el = await page.$('#logo')
await untilBrowserLogAfter(
Expand Down
15 changes: 11 additions & 4 deletions playground/hmr/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import './optional-chaining/parent'
import './intermediate-file-delete'
import './circular'
import logo from './logo.svg'
import logoNoInline from './logo-no-inline.svg'
import { msg as softInvalidationMsg } from './soft-invalidation'

export const foo = 1
Expand All @@ -14,7 +15,8 @@ text('.dep', depFoo)
text('.nested', nestedFoo)
text('.virtual', virtual)
text('.soft-invalidation', softInvalidationMsg)
setLogo(logo)
setImgSrc('#logo', logo)
setImgSrc('#logo-no-inline', logoNoInline)

const btn = document.querySelector('.virtual-update') as HTMLButtonElement
btn.onclick = () => {
Expand All @@ -40,10 +42,15 @@ if (import.meta.hot) {
}

import.meta.hot.accept('./logo.svg', (newUrl) => {
setLogo(newUrl.default)
setImgSrc('#logo', newUrl.default)
console.log('Logo updated', newUrl.default)
})

import.meta.hot.accept('./logo-no-inline.svg', (newUrl) => {
setImgSrc('#logo-no-inline', newUrl.default)
console.log('Logo-no-inline updated', newUrl.default)
})

import.meta.hot.accept('./hmrDep', ({ foo, nestedFoo }) => {
handleDep('single dep', foo, nestedFoo)
})
Expand Down Expand Up @@ -131,8 +138,8 @@ function text(el, text) {
document.querySelector(el).textContent = text
}

function setLogo(src) {
;(document.querySelector('#logo') as HTMLImageElement).src = src
function setImgSrc(el, src) {
;(document.querySelector(el) as HTMLImageElement).src = src
}

function removeCb({ msg }) {
Expand Down
3 changes: 2 additions & 1 deletion playground/hmr/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
<div class="optional-chaining"></div>
<button class="intermediate-file-delete-increment">1</button>
<div class="intermediate-file-delete-display"></div>
<image id="logo"></image>
<img id="logo" />
<img id="logo-no-inline" />
<div class="circular"></div>
3 changes: 3 additions & 0 deletions playground/hmr/logo-no-inline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions playground/hmr/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ export default defineConfig({
experimental: {
hmrPartialAccept: true,
},
build: {
assetsInlineLimit(filePath) {
if (filePath.endsWith('logo-no-inline.svg')) {
return false
}
},
},
plugins: [
{
name: 'mock-custom',
Expand Down