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

fix: mime missing extensions #8568

Merged
merged 2 commits into from
Jun 13, 2022
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/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export const CLIENT_DIR = path.dirname(CLIENT_ENTRY)

// ** READ THIS ** before editing `KNOWN_ASSET_TYPES`.
// If you add an asset to `KNOWN_ASSET_TYPES`, make sure to also add it
// to the TypeScript declaration file `packages/vite/client.d.ts`.
// to the TypeScript declaration file `packages/vite/client.d.ts` and
// add a mime type to the `registerCustomMime` in
// `packages/vite/src/node/plugin/assets.ts` if mime type cannot be
// looked up by mrmime.
export const KNOWN_ASSET_TYPES = [
// images
'png',
Expand Down
20 changes: 16 additions & 4 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ const assetHashToFilenameMap = new WeakMap<
// save hashes of the files that has been emitted in build watch
const emittedHashMap = new WeakMap<ResolvedConfig, Set<string>>()

// add own dictionary entry by directly assigning mrmime
export function registerCustomMime(): void {
// https://github.com/lukeed/mrmime/issues/3
mrmime.mimes['ico'] = 'image/x-icon'
// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Containers#flac
mrmime.mimes['flac'] = 'audio/flac'
// mrmime and mime-db is not released yet: https://github.com/jshttp/mime-db/commit/c9242a9b7d4bb25d7a0c9244adec74aeef08d8a1
mrmime.mimes['aac'] = 'audio/aac'
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
mrmime.mimes['eot'] = 'application/vnd.ms-fontobject'
}

/**
* Also supports loading plain strings with import text from './foo.txt?raw'
*/
Expand All @@ -31,9 +43,8 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
assetHashToFilenameMap.set(config, new Map())
const relativeBase = isRelativeBase(config.base)

// add own dictionary entry by directly assigning mrmine
// https://github.com/lukeed/mrmime/issues/3
mrmime.mimes['ico'] = 'image/x-icon'
registerCustomMime()

return {
name: 'vite:asset',

Expand Down Expand Up @@ -338,8 +349,9 @@ async function fileToBuiltUrl(
(!file.endsWith('.svg') &&
content.length < Number(config.build.assetsInlineLimit))
) {
const mimeType = mrmime.lookup(file) ?? 'application/octet-stream'
// base64 inlined as a string
url = `data:${mrmime.lookup(file)};base64,${content.toString('base64')}`
url = `data:${mimeType};base64,${content.toString('base64')}`
} else {
// emit as asset
// rollup supports `import.meta.ROLLUP_FILE_URL_*`, but it generates code
Expand Down
6 changes: 6 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ describe('svg fragments', () => {
})
})

test('Unknown extension assets import', async () => {
expect(await page.textContent('.unknown-ext')).toMatch(
isBuild ? 'data:application/octet-stream;' : '/nested/foo.unknown'
)
})

test('?raw import', async () => {
expect(await page.textContent('.raw')).toMatch('SVG')
})
Expand Down
6 changes: 6 additions & 0 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ <h2>SVG Fragments via JS Import</h2>
<img class="svg-frag-import" alt="" />
</div>

<h2>Unknown extension assets import</h2>
<code class="unknown-ext"></code>

<h2>?raw import</h2>
<code class="raw"></code>

Expand Down Expand Up @@ -318,6 +321,9 @@ <h3>style in svg</h3>
text('.svg-frag-import-path', svgFrag)
document.querySelector('.svg-frag-import').src = svgFrag + '#icon-heart-view'

import unknownExtUrl from './nested/foo.unknown'
text('.unknown-ext', unknownExtUrl)

import rawSvg from './nested/fragment.svg?raw'
text('.raw', rawSvg)

Expand Down
1 change: 1 addition & 0 deletions playground/assets/nested/foo.unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom file
1 change: 1 addition & 0 deletions playground/assets/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
'@': path.resolve(__dirname, 'nested')
}
},
assetsInclude: ['**/*.unknown'],
build: {
outDir: 'dist/foo',
assetsInlineLimit: 8192, // 8kb
Expand Down