rolldown-license-plugin 2.2.1 → 2.2.3
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 +4 -4
- package/dist/index.js +37 -46
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -12,8 +12,8 @@ import {licensePlugin} from "rolldown-license-plugin";
|
|
|
12
12
|
export default {
|
|
13
13
|
plugins: [
|
|
14
14
|
licensePlugin({
|
|
15
|
-
done(
|
|
16
|
-
console.info(
|
|
15
|
+
done(deps) {
|
|
16
|
+
console.info(deps);
|
|
17
17
|
// => [{name, version, license, licenseText}]
|
|
18
18
|
},
|
|
19
19
|
}),
|
|
@@ -77,9 +77,9 @@ type LicenseInfo = {
|
|
|
77
77
|
};
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
-
### `wrap(
|
|
80
|
+
### `wrap(text, width)`
|
|
81
81
|
|
|
82
|
-
Function to word-wrap text to a certain column width.
|
|
82
|
+
Function to word-wrap `text` to a certain column `width`. Returns the wrapped string.
|
|
83
83
|
|
|
84
84
|
## License
|
|
85
85
|
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { readFile, readdir } from "node:fs/promises";
|
|
2
|
-
import {
|
|
3
|
-
import { dirname, join } from "node:path";
|
|
2
|
+
import { join, sep } from "node:path";
|
|
4
3
|
|
|
5
4
|
//#region index.ts
|
|
6
5
|
const defaultMatch = /^((UN)?LICEN(S|C)E|COPYING).*$/i;
|
|
@@ -39,57 +38,49 @@ function parseLicense(pkgJson) {
|
|
|
39
38
|
if (Array.isArray(pkgJson.licenses)) return pkgJson.licenses.map((entry) => typeof entry === "string" ? entry : entry?.type ?? "").filter(Boolean).join(" OR ");
|
|
40
39
|
return "";
|
|
41
40
|
}
|
|
41
|
+
const nmSep = "/node_modules/";
|
|
42
|
+
/** Resolve the package root directory from a file path inside node_modules */
|
|
43
|
+
function findPkgRoot(fsPath) {
|
|
44
|
+
const p = sep !== "/" ? fsPath.replaceAll(sep, "/") : fsPath;
|
|
45
|
+
const nmIdx = p.lastIndexOf(nmSep);
|
|
46
|
+
if (nmIdx === -1) return null;
|
|
47
|
+
const base = nmIdx + 14;
|
|
48
|
+
const rest = p.slice(base);
|
|
49
|
+
if (rest.startsWith("@")) {
|
|
50
|
+
const firstSlash = rest.indexOf("/");
|
|
51
|
+
if (firstSlash === -1) return null;
|
|
52
|
+
const secondSlash = rest.indexOf("/", firstSlash + 1);
|
|
53
|
+
return p.slice(0, base) + rest.slice(0, secondSlash === -1 ? rest.length : secondSlash);
|
|
54
|
+
}
|
|
55
|
+
const firstSlash = rest.indexOf("/");
|
|
56
|
+
return p.slice(0, base) + rest.slice(0, firstSlash === -1 ? rest.length : firstSlash);
|
|
57
|
+
}
|
|
42
58
|
/** Rolldown plugin that extracts license information from bundled dependencies */
|
|
43
59
|
const licensePlugin = ({ done, match = defaultMatch, wrapText, allow, failOnViolation = false, failOnUnlicensed = false }) => ({
|
|
44
60
|
name: "rolldown-license-plugin",
|
|
45
61
|
async generateBundle(_opts, bundle) {
|
|
46
|
-
const
|
|
47
|
-
const pkgDirs = /* @__PURE__ */ new Map();
|
|
62
|
+
const roots = /* @__PURE__ */ new Set();
|
|
48
63
|
for (const chunk of Object.values(bundle)) {
|
|
49
64
|
if (chunk.type !== "chunk") continue;
|
|
50
65
|
for (const moduleId of Object.keys(chunk.modules)) {
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const cached = pkgJsonCache.get(dir);
|
|
55
|
-
if (cached !== void 0) {
|
|
56
|
-
if (cached?.name) {
|
|
57
|
-
const key = `${cached.name}@${cached.version ?? ""}`;
|
|
58
|
-
if (!pkgDirs.has(key)) pkgDirs.set(key, {
|
|
59
|
-
dir,
|
|
60
|
-
pkgJson: cached
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
let pkgJson = null;
|
|
66
|
-
const walked = [];
|
|
67
|
-
while (dir !== dirname(dir) && dir.includes("node_modules")) {
|
|
68
|
-
const cachedInner = pkgJsonCache.get(dir);
|
|
69
|
-
if (cachedInner !== void 0) {
|
|
70
|
-
pkgJson = cachedInner;
|
|
71
|
-
break;
|
|
72
|
-
}
|
|
73
|
-
walked.push(dir);
|
|
74
|
-
try {
|
|
75
|
-
pkgJson = JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
|
|
76
|
-
} catch {
|
|
77
|
-
pkgJson = null;
|
|
78
|
-
}
|
|
79
|
-
if (pkgJson?.name) break;
|
|
80
|
-
pkgJson = null;
|
|
81
|
-
dir = dirname(dir);
|
|
82
|
-
}
|
|
83
|
-
for (const walkedDir of walked) pkgJsonCache.set(walkedDir, pkgJson);
|
|
84
|
-
if (!pkgJson?.name) continue;
|
|
85
|
-
const key = `${pkgJson.name}@${pkgJson.version ?? ""}`;
|
|
86
|
-
if (!pkgDirs.has(key)) pkgDirs.set(key, {
|
|
87
|
-
dir,
|
|
88
|
-
pkgJson
|
|
89
|
-
});
|
|
66
|
+
const qIdx = moduleId.indexOf("?");
|
|
67
|
+
const root = findPkgRoot(qIdx === -1 ? moduleId : moduleId.slice(0, qIdx));
|
|
68
|
+
if (root) roots.add(root);
|
|
90
69
|
}
|
|
91
70
|
}
|
|
92
|
-
const
|
|
71
|
+
const seen = /* @__PURE__ */ new Set();
|
|
72
|
+
const licenses = [];
|
|
73
|
+
await Promise.all(Array.from(roots, async (dir) => {
|
|
74
|
+
let pkgJson;
|
|
75
|
+
try {
|
|
76
|
+
pkgJson = JSON.parse(await readFile(join(dir, "package.json"), "utf8"));
|
|
77
|
+
} catch {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (!pkgJson.name) return;
|
|
81
|
+
const key = `${pkgJson.name}@${pkgJson.version ?? ""}`;
|
|
82
|
+
if (seen.has(key)) return;
|
|
83
|
+
seen.add(key);
|
|
93
84
|
let licenseText = "";
|
|
94
85
|
try {
|
|
95
86
|
const licenseFile = (await readdir(dir)).find((entry) => match.test(entry));
|
|
@@ -98,12 +89,12 @@ const licensePlugin = ({ done, match = defaultMatch, wrapText, allow, failOnViol
|
|
|
98
89
|
if (wrapText) licenseText = wrap(licenseText, wrapText).trim();
|
|
99
90
|
}
|
|
100
91
|
} catch {}
|
|
101
|
-
|
|
92
|
+
licenses.push({
|
|
102
93
|
name: pkgJson.name,
|
|
103
94
|
version: pkgJson.version ?? "",
|
|
104
95
|
license: parseLicense(pkgJson),
|
|
105
96
|
licenseText
|
|
106
|
-
};
|
|
97
|
+
});
|
|
107
98
|
}));
|
|
108
99
|
licenses.sort((a, b) => a.name.localeCompare(b.name));
|
|
109
100
|
if (allow) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rolldown-license-plugin",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"description": "Rolldown plugin to extract dependency licenses",
|
|
5
5
|
"author": "silverwind <me@silverwind.io>",
|
|
6
6
|
"repository": "silverwind/rolldown-license-plugin",
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"tsdown-config-silverwind": "2.0.5",
|
|
30
30
|
"typescript": "5.9.3",
|
|
31
31
|
"typescript-config-silverwind": "17.0.0",
|
|
32
|
-
"updates": "17.
|
|
32
|
+
"updates": "17.14.0",
|
|
33
33
|
"updates-config-silverwind": "2.0.1",
|
|
34
|
-
"versions": "14.2.
|
|
34
|
+
"versions": "14.2.7",
|
|
35
35
|
"vitest": "4.1.4",
|
|
36
36
|
"vitest-config-silverwind": "11.1.4"
|
|
37
37
|
}
|