-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: build support for data uri import
- Loading branch information
Showing
6 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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,16 @@ | ||
import { isBuild, findAssetFile } from '../../testUtils' | ||
|
||
test('plain', async () => { | ||
expect(await page.textContent('.plain')).toBe('hi') | ||
}) | ||
|
||
test('base64', async () => { | ||
expect(await page.textContent('.base64')).toBe('hi') | ||
}) | ||
|
||
if (isBuild) { | ||
test('should compile away the import for build', async () => { | ||
const file = findAssetFile('index') | ||
expect(file).not.toMatch('import') | ||
}) | ||
} |
This file contains 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,10 @@ | ||
<div class="plain"></div> | ||
<div class="base64"></div> | ||
|
||
<script type="module"> | ||
import msg from "data:text/javascript, export default 'hi'" | ||
document.querySelector('.plain').textContent = msg | ||
|
||
import base64Msg from 'data:text/javascript;base64, ZXhwb3J0IGRlZmF1bHQgJ2hpJw==' | ||
document.querySelector('.base64').textContent = base64Msg | ||
</script> |
This file contains 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,10 @@ | ||
{ | ||
"name": "test-data-uri", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"debug": "node --inspect-brk ../../vite/bin/vite" | ||
} | ||
} |
This file contains 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,56 @@ | ||
// This is based on @rollup/plugin-data-uri | ||
// MIT Licensed https://github.com/rollup/plugins/blob/master/LICENSE | ||
// ref https://github.com/vitejs/vite/issues/1428#issuecomment-757033808 | ||
import { Plugin } from '../plugin' | ||
import { URL } from 'url' | ||
|
||
const dataUriRE = /^([^/]+\/[^;,]+)(;base64)?,([\s\S]*)$/ | ||
|
||
const dataUriPrefix = `/@data-uri/` | ||
|
||
export function dataURIPlugin(): Plugin { | ||
const resolved: { | ||
[key: string]: string | ||
} = {} | ||
|
||
return { | ||
name: 'vite:data-uri', | ||
resolveId(id) { | ||
if (!dataUriRE.test(id)) { | ||
return null | ||
} | ||
|
||
const uri = new URL(id) | ||
if (uri.protocol !== 'data:') { | ||
return null | ||
} | ||
|
||
const match = uri.pathname.match(dataUriRE) | ||
if (!match) { | ||
return null | ||
} | ||
|
||
const [, mime, format, data] = match | ||
if (mime !== 'text/javascript') { | ||
throw new Error( | ||
`data URI with non-JavaScript mime type is not supported.` | ||
) | ||
} | ||
|
||
// decode data | ||
const base64 = format && /base64/i.test(format.substring(1)) | ||
const content = base64 | ||
? Buffer.from(data, 'base64').toString('utf-8') | ||
: data | ||
resolved[id] = content | ||
return dataUriPrefix + id | ||
}, | ||
|
||
load(id) { | ||
if (id.startsWith(dataUriPrefix)) { | ||
id = id.slice(dataUriPrefix.length) | ||
return resolved[id] || null | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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