rolldown-license-plugin 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![](https://img.shields.io/npm/v/rolldown-license-plugin.svg?style=flat)](https://www.npmjs.org/package/rolldown-license-plugin) [![](https://img.shields.io/npm/dm/rolldown-license-plugin.svg)](https://www.npmjs.org/package/rolldown-license-plugin)
4
4
 
5
- Rolldown plugin to extract dependency licenses from bundled `node_modules`. Zero dependencies, and will always stay that way.
5
+ Rolldown plugin to extract dependency licenses from bundled `node_modules` with zero dependencies.
6
6
 
7
7
  ## Installation
8
8
 
@@ -46,6 +46,12 @@ Default: `/^((UN)?LICEN(S|C)E|COPYING).*$/i`
46
46
 
47
47
  Regex to match license filenames in package directories.
48
48
 
49
+ #### `opts.wrapText`
50
+
51
+ Type: `number`
52
+
53
+ When set, word-wrap `licenseText` to this column width.
54
+
49
55
  ### `LicenseInfo`
50
56
 
51
57
  ```typescript
package/dist/index.d.ts CHANGED
@@ -5730,19 +5730,24 @@ interface InputOptions {
5730
5730
  //#region src/types/rolldown-options.d.ts
5731
5731
  //#endregion
5732
5732
  //#region index.d.ts
5733
+ /** License information for a single bundled dependency */
5733
5734
  type LicenseInfo = {
5734
- name: string;
5735
- version: string;
5736
- license: string;
5735
+ /** Package name from package.json */name: string; /** Package version from package.json */
5736
+ version: string; /** SPDX license identifier from package.json */
5737
+ license: string; /** Contents of the LICENSE/COPYING file, or empty string if not found */
5737
5738
  licenseText: string;
5738
5739
  };
5740
+ /** Options for {@link licensePlugin} */
5739
5741
  type RolldownLicensePluginOpts = {
5740
- onDone: (licenses: LicenseInfo[]) => void;
5741
- match?: RegExp;
5742
+ /** Called during `generateBundle` with the collected license data, sorted by name */onDone: (licenses: LicenseInfo[]) => void; /** Regex to match license filenames. Default: `/^((UN)?LICEN(S|C)E|COPYING).*$/i` */
5743
+ match?: RegExp; /** When set, word-wrap `licenseText` to this column width */
5744
+ wrapText?: number;
5742
5745
  };
5746
+ /** Rolldown plugin that extracts license information from bundled dependencies */
5743
5747
  declare const licensePlugin: ({
5744
5748
  onDone,
5745
- match
5749
+ match,
5750
+ wrapText
5746
5751
  }: RolldownLicensePluginOpts) => Plugin;
5747
5752
  //#endregion
5748
5753
  export { LicenseInfo, RolldownLicensePluginOpts, licensePlugin };
package/dist/index.js CHANGED
@@ -4,13 +4,42 @@ import { dirname, join } from "node:path";
4
4
 
5
5
  //#region index.ts
6
6
  const defaultMatch = /^((UN)?LICEN(S|C)E|COPYING).*$/i;
7
+ function wrap(text, width) {
8
+ const lines = [];
9
+ for (const rawLine of text.replace(/\r/g, "").split("\n")) {
10
+ const trimmed = rawLine.replace(/\t/g, (_, offset) => " ".repeat(8 - offset % 8)).trim();
11
+ if (trimmed.length <= width) {
12
+ lines.push(trimmed);
13
+ continue;
14
+ }
15
+ let pos = 0;
16
+ while (pos < trimmed.length) {
17
+ if (pos + width >= trimmed.length) {
18
+ lines.push(trimmed.slice(pos).trim());
19
+ break;
20
+ }
21
+ let breakAt = trimmed.lastIndexOf(" ", pos + width);
22
+ if (breakAt <= pos) {
23
+ breakAt = trimmed.indexOf(" ", pos + width);
24
+ if (breakAt === -1) {
25
+ lines.push(trimmed.slice(pos).trim());
26
+ break;
27
+ }
28
+ }
29
+ lines.push(trimmed.slice(pos, breakAt).trimEnd());
30
+ pos = breakAt + 1;
31
+ }
32
+ }
33
+ return lines.join("\n");
34
+ }
7
35
  function parseLicense(pkgJson) {
8
36
  if (typeof pkgJson.license === "string") return pkgJson.license;
9
37
  if (pkgJson.license?.type) return pkgJson.license.type;
10
38
  if (Array.isArray(pkgJson.licenses)) return pkgJson.licenses.map((entry) => typeof entry === "string" ? entry : entry?.type ?? "").filter(Boolean).join(" OR ");
11
39
  return "";
12
40
  }
13
- const licensePlugin = ({ onDone, match = defaultMatch }) => ({
41
+ /** Rolldown plugin that extracts license information from bundled dependencies */
42
+ const licensePlugin = ({ onDone, match = defaultMatch, wrapText }) => ({
14
43
  name: "rolldown-license-plugin",
15
44
  async generateBundle(_opts, bundle) {
16
45
  const pkgJsonCache = /* @__PURE__ */ new Map();
@@ -63,7 +92,10 @@ const licensePlugin = ({ onDone, match = defaultMatch }) => ({
63
92
  let licenseText = "";
64
93
  try {
65
94
  const licenseFile = (await readdir(dir)).find((entry) => match.test(entry));
66
- if (licenseFile) licenseText = await readFile(join(dir, licenseFile), "utf8");
95
+ if (licenseFile) {
96
+ licenseText = await readFile(join(dir, licenseFile), "utf8");
97
+ if (wrapText) licenseText = wrap(licenseText, wrapText).trim();
98
+ }
67
99
  } catch {}
68
100
  return {
69
101
  name: pkgJson.name,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-license-plugin",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Rolldown plugin to extract dependency licenses",
5
5
  "author": "silverwind <me@silverwind.io>",
6
6
  "repository": "silverwind/rolldown-license-plugin",