rolldown-license-plugin 1.1.0 → 1.3.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 +7 -1
- package/dist/index.d.ts +14 -7
- package/dist/index.js +36 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.org/package/rolldown-license-plugin) [](https://www.npmjs.org/package/rolldown-license-plugin)
|
|
4
4
|
|
|
5
|
-
Rolldown plugin to extract dependency licenses from bundled `node_modules
|
|
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,26 @@ 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
|
+
/** Word-wrap plain text to a specified column width */
|
|
5747
|
+
declare function wrap(text: string, width: number): string;
|
|
5748
|
+
/** Rolldown plugin that extracts license information from bundled dependencies */
|
|
5743
5749
|
declare const licensePlugin: ({
|
|
5744
5750
|
onDone,
|
|
5745
|
-
match
|
|
5751
|
+
match,
|
|
5752
|
+
wrapText
|
|
5746
5753
|
}: RolldownLicensePluginOpts) => Plugin;
|
|
5747
5754
|
//#endregion
|
|
5748
|
-
export { LicenseInfo, RolldownLicensePluginOpts, licensePlugin };
|
|
5755
|
+
export { LicenseInfo, RolldownLicensePluginOpts, licensePlugin, wrap };
|
package/dist/index.js
CHANGED
|
@@ -4,13 +4,43 @@ 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
|
+
/** Word-wrap plain text to a specified column width */
|
|
8
|
+
function wrap(text, width) {
|
|
9
|
+
const lines = [];
|
|
10
|
+
for (const rawLine of text.replace(/\r/g, "").split("\n")) {
|
|
11
|
+
const trimmed = rawLine.replace(/\t/g, (_, offset) => " ".repeat(8 - offset % 8)).trim();
|
|
12
|
+
if (trimmed.length <= width) {
|
|
13
|
+
lines.push(trimmed);
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
let pos = 0;
|
|
17
|
+
while (pos < trimmed.length) {
|
|
18
|
+
if (pos + width >= trimmed.length) {
|
|
19
|
+
lines.push(trimmed.slice(pos).trim());
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
let breakAt = trimmed.lastIndexOf(" ", pos + width);
|
|
23
|
+
if (breakAt <= pos) {
|
|
24
|
+
breakAt = trimmed.indexOf(" ", pos + width);
|
|
25
|
+
if (breakAt === -1) {
|
|
26
|
+
lines.push(trimmed.slice(pos).trim());
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
lines.push(trimmed.slice(pos, breakAt).trimEnd());
|
|
31
|
+
pos = breakAt + 1;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return lines.join("\n");
|
|
35
|
+
}
|
|
7
36
|
function parseLicense(pkgJson) {
|
|
8
37
|
if (typeof pkgJson.license === "string") return pkgJson.license;
|
|
9
38
|
if (pkgJson.license?.type) return pkgJson.license.type;
|
|
10
39
|
if (Array.isArray(pkgJson.licenses)) return pkgJson.licenses.map((entry) => typeof entry === "string" ? entry : entry?.type ?? "").filter(Boolean).join(" OR ");
|
|
11
40
|
return "";
|
|
12
41
|
}
|
|
13
|
-
|
|
42
|
+
/** Rolldown plugin that extracts license information from bundled dependencies */
|
|
43
|
+
const licensePlugin = ({ onDone, match = defaultMatch, wrapText }) => ({
|
|
14
44
|
name: "rolldown-license-plugin",
|
|
15
45
|
async generateBundle(_opts, bundle) {
|
|
16
46
|
const pkgJsonCache = /* @__PURE__ */ new Map();
|
|
@@ -63,7 +93,10 @@ const licensePlugin = ({ onDone, match = defaultMatch }) => ({
|
|
|
63
93
|
let licenseText = "";
|
|
64
94
|
try {
|
|
65
95
|
const licenseFile = (await readdir(dir)).find((entry) => match.test(entry));
|
|
66
|
-
if (licenseFile)
|
|
96
|
+
if (licenseFile) {
|
|
97
|
+
licenseText = await readFile(join(dir, licenseFile), "utf8");
|
|
98
|
+
if (wrapText) licenseText = wrap(licenseText, wrapText).trim();
|
|
99
|
+
}
|
|
67
100
|
} catch {}
|
|
68
101
|
return {
|
|
69
102
|
name: pkgJson.name,
|
|
@@ -78,4 +111,4 @@ const licensePlugin = ({ onDone, match = defaultMatch }) => ({
|
|
|
78
111
|
});
|
|
79
112
|
|
|
80
113
|
//#endregion
|
|
81
|
-
export { licensePlugin };
|
|
114
|
+
export { licensePlugin, wrap };
|
package/package.json
CHANGED