MCPcopy
hub / github.com/vitejs/vite / generateBundle

Function generateBundle

packages/vite/src/node/plugins/license.ts:48–112  ·  view source on GitHub ↗
(_, bundle)

Source from the content-addressed store, hash-verified

46 name: 'vite:license',
47
48 generateBundle(_, bundle) {
49 const licenseOption = this.environment.config.build.license
50 if (licenseOption === false) return
51
52 const packageCache: PackageCache = new Map()
53 // Track license via a key to its license entry.
54 // A key consists of "name@version" of a package.
55 const licenses: Record<string, LicenseEntry> = {}
56
57 for (const file in bundle) {
58 const chunk = bundle[file]
59 if (chunk.type === 'asset') continue
60
61 for (const moduleId of chunk.moduleIds) {
62 if (moduleId.startsWith('\0') || !isInNodeModules(moduleId)) continue
63
64 // Find the dependency package.json
65 const pkgData = findNearestMainPackageData(
66 path.dirname(moduleId),
67 packageCache,
68 )
69 if (!pkgData) continue
70
71 // Grab the package.json keys and check if already exists in the licenses
72 const { name, version = '0.0.0', license } = pkgData.data
73 const key = `${name}@${version}`
74 if (licenses[key]) continue
75
76 // If not, create a new license entry
77 const entry: LicenseEntry = { name, version }
78 if (license) {
79 entry.identifier = license.trim()
80 }
81 const licenseFile = findLicenseFile(pkgData.dir)
82 if (licenseFile) {
83 entry.text = fs.readFileSync(licenseFile, 'utf-8').trim()
84 }
85 licenses[key] = entry
86 }
87 }
88
89 const licenseEntries = Object.values(sortObjectKeys(licenses))
90 const licenseOutputFileName =
91 typeof licenseOption === 'object'
92 ? licenseOption.fileName
93 : licenseConfigDefaults.fileName
94
95 // Emit as a JSON file
96 if (licenseOutputFileName.endsWith('.json')) {
97 this.emitFile({
98 fileName: licenseOutputFileName,
99 type: 'asset',
100 source: JSON.stringify(licenseEntries, null, 2),
101 })
102 return
103 }
104
105 // Emit a license file as markdown

Callers

nothing calls this directly

Calls 6

isInNodeModulesFunction · 0.90
sortObjectKeysFunction · 0.90
findLicenseFileFunction · 0.85
licenseEntryToMarkdownFunction · 0.85
emitFileMethod · 0.80

Tested by

no test coverage detected