-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: resolve css relative urls + base64 inline
- Loading branch information
Showing
3 changed files
with
84 additions
and
20 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
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 |
---|---|---|
@@ -1,38 +1,69 @@ | ||
import path from 'path' | ||
import { promises as fs } from 'fs' | ||
import { Plugin } from 'rollup' | ||
import { Plugin, OutputBundle } from 'rollup' | ||
import { isStaticAsset } from './utils' | ||
import hash_sum from 'hash-sum' | ||
import slash from 'slash' | ||
import mime from 'mime-types' | ||
|
||
const debug = require('debug')('vite:build:asset') | ||
|
||
// TODO make this configurable | ||
const inlineThreshold = 10000 | ||
|
||
export const getAssetPublicPath = async (id: string, assetsDir: string) => { | ||
const ext = path.extname(id) | ||
const baseName = path.basename(id, ext) | ||
const resolvedFileName = `${baseName}.${hash_sum(id)}${ext}` | ||
|
||
let url = slash(path.join('/', assetsDir, resolvedFileName)) | ||
const content = await fs.readFile(id) | ||
if (!id.endsWith(`.svg`)) { | ||
if (content.length < inlineThreshold) { | ||
url = `data:${mime.lookup(id)};base64,${content.toString('base64')}` | ||
} | ||
} | ||
|
||
return { | ||
content, | ||
fileName: resolvedFileName, | ||
url | ||
} | ||
} | ||
|
||
export const registerAssets = ( | ||
assets: Map<string, string>, | ||
bundle: OutputBundle | ||
) => { | ||
for (const [fileName, source] of assets) { | ||
bundle[fileName] = { | ||
isAsset: true, | ||
type: 'asset', | ||
fileName, | ||
source | ||
} | ||
} | ||
} | ||
|
||
export const createBuildAssetPlugin = (assetsDir: string): Plugin => { | ||
const assets = new Map() | ||
|
||
return { | ||
name: 'vite:asset', | ||
load(id) { | ||
async load(id) { | ||
if (isStaticAsset(id)) { | ||
const ext = path.extname(id) | ||
const baseName = path.basename(id, ext) | ||
const resolvedName = `${baseName}.${hash_sum(id)}${ext}` | ||
assets.set(id, resolvedName) | ||
const publicPath = slash(path.join('/', assetsDir, resolvedName)) | ||
debug(`${id} -> ${publicPath}`) | ||
return `export default ${JSON.stringify(publicPath)}` | ||
const { fileName, content, url } = await getAssetPublicPath( | ||
id, | ||
assetsDir | ||
) | ||
assets.set(fileName, content) | ||
debug(`${id} -> ${url}`) | ||
return `export default ${JSON.stringify(url)}` | ||
} | ||
}, | ||
|
||
async generateBundle(_options, bundle) { | ||
for (const [from, fileName] of assets) { | ||
bundle[fileName] = { | ||
isAsset: true, | ||
type: 'asset', | ||
fileName, | ||
source: await fs.readFile(from) | ||
} | ||
} | ||
generateBundle(_options, bundle) { | ||
registerAssets(assets, bundle) | ||
} | ||
} | ||
} |
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