提交 efea3e80 编写于 作者: fxy060608's avatar fxy060608

wip(mp): assets inline

上级 fbd8c553
......@@ -45,5 +45,7 @@ export const configResolved: Plugin['configResolved'] = (config) => {
return cssCode
},
})
// 强制不inline
config.build.assetsInlineLimit = 0
injectAssetPlugin(config)
}
import path from 'path'
import { parse as parseUrl } from 'url'
import mime from 'mime/lite'
import fs, { promises as fsp } from 'fs'
import { Plugin } from '../plugin'
import { ResolvedConfig } from '../config'
......@@ -137,9 +138,10 @@ export function checkPublicFile(
export function fileToUrl(
id: string,
config: ResolvedConfig,
ctx: PluginContext
ctx: PluginContext,
canInline: boolean = false
): string {
return fileToBuiltUrl(id, config, ctx)
return fileToBuiltUrl(id, config, ctx, false, canInline)
}
export function getAssetFilename(
......@@ -156,7 +158,8 @@ function fileToBuiltUrl(
id: string,
config: ResolvedConfig,
pluginContext: PluginContext,
skipPublicCheck = false
skipPublicCheck = false,
canInline = false
): string {
if (!skipPublicCheck && checkPublicFile(id, config)) {
return config.base + id.slice(1)
......@@ -172,31 +175,36 @@ function fileToBuiltUrl(
let url: string
const map = assetHashToFilenameMap.get(config)!
const contentHash = getAssetHash(content)
const { search, hash } = parseUrl(id)
const postfix = (search || '') + (hash || '')
const fileName = normalizePath(
path.posix.relative(process.env.UNI_INPUT_DIR, file)
)
if (!map.has(contentHash)) {
map.set(contentHash, fileName)
}
if (canInline && content.length < Number(config.build.assetsInlineLimit)) {
// base64 inlined as a string
url = `data:${mime.getType(file)};base64,${content.toString('base64')}`
} else {
const map = assetHashToFilenameMap.get(config)!
const contentHash = getAssetHash(content)
const { search, hash } = parseUrl(id)
const postfix = (search || '') + (hash || '')
const fileName = normalizePath(
path.posix.relative(process.env.UNI_INPUT_DIR, file)
)
if (!map.has(contentHash)) {
map.set(contentHash, fileName)
}
if (!fileName.includes('/static/')) {
const emittedSet = emittedHashMap.get(config)!
if (!emittedSet.has(contentHash)) {
pluginContext.emitFile({
name: fileName,
fileName,
type: 'asset',
source: content,
})
emittedSet.add(contentHash)
if (!fileName.includes('/static/')) {
const emittedSet = emittedHashMap.get(config)!
if (!emittedSet.has(contentHash)) {
pluginContext.emitFile({
name: fileName,
fileName,
type: 'asset',
source: content,
})
emittedSet.add(contentHash)
}
}
}
url = `__VITE_ASSET__${contentHash}__${postfix ? `$_${postfix}__` : ``}`
url = `__VITE_ASSET__${contentHash}__${postfix ? `$_${postfix}__` : ``}`
}
cache.set(id, url)
return url
}
......
......@@ -138,7 +138,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
}
const resolved = await resolveUrl(url, importer)
if (resolved) {
return fileToUrl(resolved, config, this)
return fileToUrl(resolved, config, this, true)
}
return url
}
......
......@@ -33,7 +33,7 @@ export function buildOptions(): UserConfig['build'] {
// sourcemap: 'inline', // TODO
target: ['chrome53'],
emptyOutDir: false, // 不清空输出目录,否则会影响自定义的一些文件输出,比如wxml
assetsInlineLimit: 0, // TODO
assetsInlineLimit: 40 * 1024, // 40kb
lib: {
entry: resolveMainPathOnce(inputDir),
formats: ['cjs'],
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册