Home / Function/ licensePlugin() — vite Function Reference

licensePlugin() — vite Function Reference

Architecture documentation for the licensePlugin() function in license.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  481d1103_be94_cde4_36c2_68b3faf11385["licensePlugin()"]
  82f800f1_6da8_8cca_d4a5_6dbc08ca3747["license.ts"]
  481d1103_be94_cde4_36c2_68b3faf11385 -->|defined in| 82f800f1_6da8_8cca_d4a5_6dbc08ca3747
  8c4db194_5dfd_4391_cc9a_833655009196["resolveBuildPlugins()"]
  8c4db194_5dfd_4391_cc9a_833655009196 -->|calls| 481d1103_be94_cde4_36c2_68b3faf11385
  50ac7e51_9f94_e985_bfec_ae95273b23b0["isInNodeModules()"]
  481d1103_be94_cde4_36c2_68b3faf11385 -->|calls| 50ac7e51_9f94_e985_bfec_ae95273b23b0
  d0858125_c73b_0a0a_6dee_ba3fba7e64aa["findNearestMainPackageData()"]
  481d1103_be94_cde4_36c2_68b3faf11385 -->|calls| d0858125_c73b_0a0a_6dee_ba3fba7e64aa
  8b9bb404_07c6_e43e_c1c2_b74b7e73b0b7["findLicenseFile()"]
  481d1103_be94_cde4_36c2_68b3faf11385 -->|calls| 8b9bb404_07c6_e43e_c1c2_b74b7e73b0b7
  b8325ee3_313b_4bc2_ecfd_3e3b5441a5f3["sortObjectKeys()"]
  481d1103_be94_cde4_36c2_68b3faf11385 -->|calls| b8325ee3_313b_4bc2_ecfd_3e3b5441a5f3
  042b8bcd_ab30_7f68_d37b_fe2aac735fd9["licenseEntryToMarkdown()"]
  481d1103_be94_cde4_36c2_68b3faf11385 -->|calls| 042b8bcd_ab30_7f68_d37b_fe2aac735fd9
  style 481d1103_be94_cde4_36c2_68b3faf11385 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/license.ts lines 44–114

export function licensePlugin(): Plugin {
  return {
    name: 'vite:license',

    async generateBundle(_, bundle) {
      const licenseOption = this.environment.config.build.license
      if (licenseOption === false) return

      const packageCache: PackageCache = new Map()
      // Track license via a key to its license entry.
      // A key consists of "name@version" of a package.
      const licenses: Record<string, LicenseEntry> = {}

      for (const file in bundle) {
        const chunk = bundle[file]
        if (chunk.type === 'asset') continue

        for (const moduleId of chunk.moduleIds) {
          if (moduleId.startsWith('\0') || !isInNodeModules(moduleId)) continue

          // Find the dependency package.json
          const pkgData = findNearestMainPackageData(
            path.dirname(moduleId),
            packageCache,
          )
          if (!pkgData) continue

          // Grab the package.json keys and check if already exists in the licenses
          const { name, version = '0.0.0', license } = pkgData.data
          const key = `${name}@${version}`
          if (licenses[key]) continue

          // If not, create a new license entry
          const entry: LicenseEntry = { name, version }
          if (license) {
            entry.identifier = license.trim()
          }
          const licenseFile = findLicenseFile(pkgData.dir)
          if (licenseFile) {
            entry.text = fs.readFileSync(licenseFile, 'utf-8').trim()
          }
          licenses[key] = entry
        }
      }

      const licenseEntries = Object.values(sortObjectKeys(licenses))
      const licenseOutputFileName =
        typeof licenseOption === 'object'
          ? licenseOption.fileName
          : licenseConfigDefaults.fileName

      // Emit as a JSON file
      if (licenseOutputFileName.endsWith('.json')) {
        this.emitFile({
          fileName: licenseOutputFileName,
          type: 'asset',
          source: JSON.stringify(licenseEntries, null, 2),
        })
        return
      }

      // Emit a license file as markdown
      const markdown = licenseEntryToMarkdown(licenseEntries)
      this.emitFile({
        fileName: licenseOutputFileName,
        type: 'asset',
        source: markdown,
      })
    },
  }
}

Domain

Subdomains

Frequently Asked Questions

What does licensePlugin() do?
licensePlugin() is a function in the vite codebase, defined in packages/vite/src/node/plugins/license.ts.
Where is licensePlugin() defined?
licensePlugin() is defined in packages/vite/src/node/plugins/license.ts at line 44.
What does licensePlugin() call?
licensePlugin() calls 5 function(s): findLicenseFile, findNearestMainPackageData, isInNodeModules, licenseEntryToMarkdown, sortObjectKeys.
What calls licensePlugin()?
licensePlugin() is called by 1 function(s): resolveBuildPlugins.

Analyze Your Own Codebase

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

Try Supermodel Free