Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable1] fix(REUSELicensesPlugin): Emit files in generateBundle hook to have real filenames #284

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 46 additions & 41 deletions lib/plugins/REUSELicensesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,55 +189,60 @@ export function REUSELicensesPlugin(options: REUSELicensesPluginOptions = {}): P
return {
name: 'reuse-licenses',

async renderChunk(_, chunk, config) {
async generateBundle(outputOptions, bundle) {
onError = this.error

const modules = new Set<string>()
for (const [moduleName, module] of Object.entries(chunk.modules)) {
if (module.renderedLength > 0) {
modules.add(moduleName)
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'asset') {
continue
}
}

const packages = new Set((
await Promise.all(
[...modules.values()]
.map(sanitizeName)
.map(findPackage),
)).filter(Boolean),
)

const sortedPackages = [...packages].sort((a, b) => a.name.localeCompare(b.name) || a.version.localeCompare(b.version, undefined, { numeric: true }))
const authors = new Set(sortedPackages.map(({ author }) => author))
const licenses = new Set<string>()

let source = 'This file is generated from multiple sources. Included packages:\n'
for (const pkg of sortedPackages) {
const license = verifyLicense(pkg.license, pkg.name, pkg.version)
licenses.add(license)
source += `- ${pkg.name}\n\t- version: ${pkg.version}\n\t- license: ${license}\n`
}
source = [...licenses.values()].sort().map((license) => `SPDX-License-Identifier: ${license}`).join('\n')
+ '\n'
+ [...authors.values()].sort().map((author) => `SPDX-FileCopyrightText: ${author}`).join('\n')
+ '\n\n'
+ source

this.emitFile({
name: `${chunk.name}.license`,
fileName: `${chunk.fileName}.license`,
type: 'asset',
source,
})

// Also emit the sourcemap license file (as it includes the sources we need the same licenses)
if (config.sourcemap && config.sourcemap !== 'inline' && options.includeSourceMaps) {
const modules = new Set<string>()
for (const [moduleName, module] of Object.entries(chunk.modules)) {
if (module.renderedLength > 0) {
modules.add(moduleName)
}
}

const packages = new Set((
await Promise.all(
[...modules.values()]
.map(sanitizeName)
.map(findPackage),
)).filter(Boolean),
)

const sortedPackages = [...packages].sort((a, b) => a.name.localeCompare(b.name) || a.version.localeCompare(b.version, undefined, { numeric: true }))
const authors = new Set(sortedPackages.map(({ author }) => author))
const licenses = new Set<string>()

let source = 'This file is generated from multiple sources. Included packages:\n'
for (const pkg of sortedPackages) {
const license = verifyLicense(pkg.license, pkg.name, pkg.version)
licenses.add(license)
source += `- ${pkg.name}\n\t- version: ${pkg.version}\n\t- license: ${license}\n`
}
source = [...licenses.values()].sort().map((license) => `SPDX-License-Identifier: ${license}`).join('\n')
+ '\n'
+ [...authors.values()].sort().map((author) => `SPDX-FileCopyrightText: ${author}`).join('\n')
+ '\n\n'
+ source

this.emitFile({
name: `${chunk.name}.map.license`,
fileName: `${chunk.fileName}.map.license`,
name: `${chunk.name}.license`,
fileName: `${chunk.fileName}.license`,
type: 'asset',
source,
})

if (outputOptions.sourcemap && outputOptions.sourcemap !== 'inline' && options.includeSourceMaps) {
this.emitFile({
type: 'asset',
name: `${chunk.name}.map.license`,
fileName: `${chunk.fileName}.map.license`,
source,
})
}
}
},
}
Expand Down
Loading