Skip to content

Commit

Permalink
feat: filter files using Vite's recommended mode #1
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-layne committed Jul 22, 2023
1 parent 820ee17 commit aeaafd1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
53 changes: 44 additions & 9 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
export const DEFAULT_EXTENSIONS = [
'.jpg',
'.jpeg',
'.png',
'.apng',
'.gif',
'.bmp',
'.svg',
'.webp',
// ** 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` 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
'apng',
'png',
'jpe?g',
'jfif',
'pjpeg',
'pjp',
'gif',
'svg',
'ico',
'webp',
'avif',

// media
'mp4',
'webm',
'ogg',
'mp3',
'wav',
'flac',
'aac',
'opus',

// fonts
'woff2?',
'eot',
'ttf',
'otf',

// other
'webmanifest',
'pdf',
'txt',
]

export const DEFAULT_ASSETS_RE = new RegExp(
`\\.(${KNOWN_ASSET_TYPES.join('|')})(\\?.*)?$`,
)
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'node:path'
import type { Plugin } from 'vite'
import { type Plugin, createFilter } from 'vite'
import { interpolateName } from 'loader-utils'
import { checkFormats, getAssetContent } from './utils'
import { DEFAULT_EXTENSIONS } from './constants'
import { DEFAULT_ASSETS_RE } from './constants'

type LoaderContext = Parameters<typeof interpolateName>[0]

Expand All @@ -14,27 +14,30 @@ type FuncOutputPath = (
) => string

export interface Options {
include?: string | RegExp | (string | RegExp)[]
exclude?: string | RegExp | (string | RegExp)[]
name?: string | FuncName
limit?: number
extensions?: string[]
outputPath?: string | FuncOutputPath
regExp?: RegExp
publicUrl?: string
}

export default function VitePluginLibAssets(options: Options = {}): Plugin {
const {
include = DEFAULT_ASSETS_RE,
exclude,
name = '[contenthash].[ext]',
limit,
outputPath,
regExp,
extensions = DEFAULT_EXTENSIONS,
publicUrl = '',
} = options
let isLibBuild = false
let assetsDir: string
let outDir: string

const filter = createFilter(include, exclude)
const assetsPathMap = new Map<string, string>()

return {
Expand Down Expand Up @@ -67,9 +70,7 @@ export default function VitePluginLibAssets(options: Options = {}): Plugin {
: path.dirname(importer)
// Full path of the imported file
const id = path.resolve(importerDir, source)
const [pureId, resourceQuery] = id.split('?')
const ext = path.extname(pureId)
if (ext && extensions.includes(ext)) {
if (filter(id)) {
const content = getAssetContent(id)

if (!content)
Expand All @@ -78,6 +79,7 @@ export default function VitePluginLibAssets(options: Options = {}): Plugin {
if (limit && content.byteLength < limit)
return null

const [pureId, resourceQuery] = id.split('?')
const context = {
resourcePath: pureId,
resourceQuery,
Expand Down

0 comments on commit aeaafd1

Please sign in to comment.