Home / Function/ assetPlugin() — vite Function Reference

assetPlugin() — vite Function Reference

Architecture documentation for the assetPlugin() function in asset.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  2b04bf20_6455_3250_24f8_b60c71116be3["assetPlugin()"]
  e71b94ef_3010_e358_13d8_f3b3acb0a268["asset.ts"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|defined in| e71b94ef_3010_e358_13d8_f3b3acb0a268
  b1b40b5b_3e43_2197_dea0_d36389d312a1["resolvePlugins()"]
  b1b40b5b_3e43_2197_dea0_d36389d312a1 -->|calls| 2b04bf20_6455_3250_24f8_b60c71116be3
  e995b1e8_2ea5_80dd_58cf_ce18dc7062fb["registerCustomMime()"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| e995b1e8_2ea5_80dd_58cf_ce18dc7062fb
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  be351481_35b7_f392_a229_ac14e1fa7efb["checkPublicFile()"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| be351481_35b7_f392_a229_ac14e1fa7efb
  3e98c2d6_886c_9673_6385_bb73ddab86cc["removeUrlQuery()"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| 3e98c2d6_886c_9673_6385_bb73ddab86cc
  13b5d2b4_0a9d_90ef_9bfa_129b74a9d64e["fileToUrl()"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| 13b5d2b4_0a9d_90ef_9bfa_129b74a9d64e
  1948f092_e5a5_076b_2f59_79ef22dec191["injectQuery()"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| 1948f092_e5a5_076b_2f59_79ef22dec191
  41d0f7a0_ed36_9f0f_d0d6_f403e4f50763["encodeURIPath()"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| 41d0f7a0_ed36_9f0f_d0d6_f403e4f50763
  edf78a01_8215_eb46_3896_3db717c46747["renderAssetUrlInJS()"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| edf78a01_8215_eb46_3896_3db717c46747
  51afdf58_3045_64b1_cf5b_929b1091e877["get()"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| 51afdf58_3045_64b1_cf5b_929b1091e877
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath()"]
  2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| a4adb1a7_cf54_091f_eb63_8217e684a8e1
  style 2b04bf20_6455_3250_24f8_b60c71116be3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/asset.ts lines 152–317

export function assetPlugin(config: ResolvedConfig): Plugin {
  registerCustomMime()

  return {
    name: 'vite:asset',

    perEnvironmentStartEndDuringDev: true,

    buildStart() {
      assetCache.set(this.environment, new Map())
      cssEntriesMap.set(this.environment, new Map())
    },

    resolveId: {
      filter: {
        id: [
          urlRE,
          DEFAULT_ASSETS_RE,
          ...makeIdFiltersToMatchWithQuery(config.rawAssetsInclude).map((v) =>
            typeof v === 'string' ? picomatch.makeRe(v, { dot: true }) : v,
          ),
        ],
      },
      handler(id) {
        if (!config.assetsInclude(cleanUrl(id)) && !urlRE.test(id)) {
          return
        }
        // imports to absolute urls pointing to files in /public
        // will fail to resolve in the main resolver. handle them here.
        const publicFile = checkPublicFile(id, config)
        if (publicFile) {
          return id
        }
      },
    },

    load: {
      filter: {
        id: {
          include: [
            rawRE,
            urlRE,
            DEFAULT_ASSETS_RE,
            ...makeIdFiltersToMatchWithQuery(config.rawAssetsInclude),
          ],
          // Rollup convention, this id should be handled by the
          // plugin that marked it with \0
          exclude: /^\0/,
        },
      },
      async handler(id) {
        // raw requests, read from disk
        if (rawRE.test(id)) {
          const file = checkPublicFile(id, config) || cleanUrl(id)
          this.addWatchFile(file)
          // raw query, read file and return as string
          return {
            code: `export default ${JSON.stringify(
              await fsp.readFile(file, 'utf-8'),
            )}`,
            moduleType: 'js', // NOTE: needs to be set to avoid double `export default` in `?raw&.txt`s
          }
        }

        if (!urlRE.test(id) && !config.assetsInclude(cleanUrl(id))) {
          return
        }

        id = removeUrlQuery(id)
        let url = await fileToUrl(this, id)

        // Inherit HMR timestamp if this asset was invalidated
        if (!url.startsWith('data:') && this.environment.mode === 'dev') {
          const mod = this.environment.moduleGraph.getModuleById(id)
          if (mod && mod.lastHMRTimestamp > 0) {
            url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
          }
        }

        return {
          code: `export default ${JSON.stringify(encodeURIPath(url))}`,

Domain

Subdomains

Called By

Frequently Asked Questions

What does assetPlugin() do?
assetPlugin() is a function in the vite codebase, defined in packages/vite/src/node/plugins/asset.ts.
Where is assetPlugin() defined?
assetPlugin() is defined in packages/vite/src/node/plugins/asset.ts at line 152.
What does assetPlugin() call?
assetPlugin() calls 10 function(s): checkPublicFile, cleanUrl, encodeURIPath, fileToUrl, get, injectQuery, normalizePath, registerCustomMime, and 2 more.
What calls assetPlugin()?
assetPlugin() is called by 1 function(s): resolvePlugins.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free