@darcas/rollup-plugin-license-json
v2.0.1
Published
A Vite plugin to generate a JSON file with detailed information about third-party licenses.
Maintainers
Readme
Rollup License JSON
Rollup License JSON is a Vite-compatible Rollup plugin that scans your project's dependencies and generates a JSON file containing detailed information about their licenses. It is a native implementation (since 2.0.0).
Installation
To use this plugin, install it via npm:
npm install @darcas/rollup-plugin-license-jsonOr, if you're using yarn:
yarn add @darcas/rollup-plugin-license-jsonUsage
In your vite.config.mts just add:
import LicensePluginJson from '@darcas/rollup-plugin-license-json';
import { fileURLToPath } from 'node:url';
import { defineConfig } from 'vite';
export default defineConfig({
//...
plugins: [
//...
LicensePluginJson(fileURLToPath(new URL('./dist/licenses.json', import.meta.url)), {
debug: true,
thirdParty: {
includePrivate: true,
multipleVersions: true,
}
}),
//...
],
//...
});Parameters
LicensePluginJson accepts 2 parameters:
file(string, required): The path where the JSON file will be generated.options(object, optional): Plugin options.
Available options:
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| cwd | string | process.cwd() | Directory used as the project root for "self" detection and package discovery. |
| debug | boolean | false | Log debug information about scanned modules. |
| thirdParty.allow | string \| function \| object | — | SPDX expression string (e.g. 'MIT', '(MIT OR Apache-2.0)'), a validator function, or { test, failOnUnlicensed?, failOnViolation? }. Validation is skipped if omitted. |
| thirdParty.includePrivate | boolean | false | Include packages marked private. |
| thirdParty.includeSelf | boolean | false | Include the project itself. |
| thirdParty.multipleVersions | boolean | false | Keep multiple installed versions of the same package (instead of deduplicating by name). |
You can't override the output path (always
file).
Output
The plugin generates a JSON file with the following structure for each dependency:
type LicensePluginJsonRecord = {
readonly author: {
readonly name: string
readonly email: string | null
readonly url: string | null
} | null
readonly description: string
readonly homepage: string | null
readonly license: string
readonly licenseText: string
readonly name: string
readonly version: string
}Key Notes
licenseText: If the license text is empty or unavailable, it will be omitted.- Formatting: HTML tags are removed, and newlines are replaced with
<br>tags to preserve readability.
Example
Assuming your project contains the following dependencies:
axioslodash
The generated JSON file might look like this:
[
{
"name": "axios",
"version": "0.21.4",
"author": {
"name": "Matt Zabriskie",
"email": "[email protected]"
},
"description": "Promise based HTTP client for the browser and node.js",
"homepage": "https://axios-http.com/",
"license": "MIT",
"licenseText": "Permission is hereby granted, free of charge, to any person..."
},
{
"name": "lodash",
"version": "4.17.21",
"author": {
"name": "John-David Dalton",
"email": "[email protected]"
},
"description": "A modern JavaScript utility library delivering modularity, performance, and extras.",
"homepage": "https://lodash.com/",
"license": "MIT",
"licenseText": "Permission is hereby granted, free of charge, to any person..."
}
]How to use
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import type { LicensePluginJsonRecord } from '@darcas/rollup-plugin-license-json';
const licenses = ref<LicensePluginJsonRecord[]>([]);
onMounted(async () => {
const response = await fetch('/licenses.json', {
headers: {
Accept: 'application/json',
},
});
if (response.ok) {
const records = (await response.json()) as LicensePluginJsonRecord[];
licenses.value = [...records].sort((a, b) => a.name.localeCompare(b.name));
}
});
</script>Compatibility
LicensePluginJson is a native Rollup plugin implemented via the renderChunk and generateBundle hooks. Because Vite uses Rollup under the hood, it works in both Vite and Rollup projects. It has no runtime dependency.
Contributing
If you'd like to contribute to the project, feel free to fork it and create a pull request. Please ensure that your changes are well-tested and properly documented.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Made with ❤️ by Dario Casertano (DarCas).
