rolldown-license-plugin 2.2.1 → 2.2.2

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.
Files changed (3) hide show
  1. package/README.md +4 -4
  2. package/dist/index.js +36 -46
  3. 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(licenses) {
16
- console.info(licenses);
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(length)`
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 { readFileSync } from "node:fs";
3
- import { dirname, join } from "node:path";
2
+ import { join } from "node:path";
4
3
 
5
4
  //#region index.ts
6
5
  const defaultMatch = /^((UN)?LICEN(S|C)E|COPYING).*$/i;
@@ -39,57 +38,48 @@ 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 nmIdx = fsPath.lastIndexOf(nmSep);
45
+ if (nmIdx === -1) return null;
46
+ const base = nmIdx + 14;
47
+ const rest = fsPath.slice(base);
48
+ if (rest.startsWith("@")) {
49
+ const firstSlash = rest.indexOf("/");
50
+ if (firstSlash === -1) return null;
51
+ const secondSlash = rest.indexOf("/", firstSlash + 1);
52
+ return fsPath.slice(0, base) + rest.slice(0, secondSlash === -1 ? rest.length : secondSlash);
53
+ }
54
+ const firstSlash = rest.indexOf("/");
55
+ return fsPath.slice(0, base) + rest.slice(0, firstSlash === -1 ? rest.length : firstSlash);
56
+ }
42
57
  /** Rolldown plugin that extracts license information from bundled dependencies */
43
58
  const licensePlugin = ({ done, match = defaultMatch, wrapText, allow, failOnViolation = false, failOnUnlicensed = false }) => ({
44
59
  name: "rolldown-license-plugin",
45
60
  async generateBundle(_opts, bundle) {
46
- const pkgJsonCache = /* @__PURE__ */ new Map();
47
- const pkgDirs = /* @__PURE__ */ new Map();
61
+ const roots = /* @__PURE__ */ new Set();
48
62
  for (const chunk of Object.values(bundle)) {
49
63
  if (chunk.type !== "chunk") continue;
50
64
  for (const moduleId of Object.keys(chunk.modules)) {
51
- const fsPath = moduleId.split("?")[0];
52
- if (!fsPath.includes("node_modules")) continue;
53
- let dir = dirname(fsPath);
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
- });
65
+ const qIdx = moduleId.indexOf("?");
66
+ const root = findPkgRoot(qIdx === -1 ? moduleId : moduleId.slice(0, qIdx));
67
+ if (root) roots.add(root);
90
68
  }
91
69
  }
92
- const licenses = await Promise.all(Array.from(pkgDirs.values(), async ({ dir, pkgJson }) => {
70
+ const seen = /* @__PURE__ */ new Set();
71
+ const licenses = [];
72
+ await Promise.all(Array.from(roots, async (dir) => {
73
+ let pkgJson;
74
+ try {
75
+ pkgJson = JSON.parse(await readFile(join(dir, "package.json"), "utf8"));
76
+ } catch {
77
+ return;
78
+ }
79
+ if (!pkgJson.name) return;
80
+ const key = `${pkgJson.name}@${pkgJson.version ?? ""}`;
81
+ if (seen.has(key)) return;
82
+ seen.add(key);
93
83
  let licenseText = "";
94
84
  try {
95
85
  const licenseFile = (await readdir(dir)).find((entry) => match.test(entry));
@@ -98,12 +88,12 @@ const licensePlugin = ({ done, match = defaultMatch, wrapText, allow, failOnViol
98
88
  if (wrapText) licenseText = wrap(licenseText, wrapText).trim();
99
89
  }
100
90
  } catch {}
101
- return {
91
+ licenses.push({
102
92
  name: pkgJson.name,
103
93
  version: pkgJson.version ?? "",
104
94
  license: parseLicense(pkgJson),
105
95
  licenseText
106
- };
96
+ });
107
97
  }));
108
98
  licenses.sort((a, b) => a.name.localeCompare(b.name));
109
99
  if (allow) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-license-plugin",
3
- "version": "2.2.1",
3
+ "version": "2.2.2",
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.13.5",
32
+ "updates": "17.14.0",
33
33
  "updates-config-silverwind": "2.0.1",
34
- "versions": "14.2.6",
34
+ "versions": "14.2.7",
35
35
  "vitest": "4.1.4",
36
36
  "vitest-config-silverwind": "11.1.4"
37
37
  }