@pob/rollup 8.1.3 → 8.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.
Files changed (2) hide show
  1. package/lib/index.js +55 -0
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import fs from "node:fs";
2
+ import { createRequire } from "node:module";
2
3
  import path from "node:path";
3
4
 
4
5
  export const nodeFormatToExt = (format, pkgType) => {
@@ -31,3 +32,57 @@ export const resolveEntry = (cwd, entryName) => {
31
32
 
32
33
  return entryPath;
33
34
  };
35
+
36
+ const sourceExtensionRegExp = /\.[cm]?tsx?$/;
37
+
38
+ const readClosestPackageJson = (filePath) => {
39
+ let directory = path.dirname(filePath);
40
+ while (directory !== path.dirname(directory)) {
41
+ const pkgPath = path.join(directory, "package.json");
42
+ if (fs.existsSync(pkgPath)) {
43
+ return JSON.parse(fs.readFileSync(pkgPath));
44
+ }
45
+ directory = path.dirname(directory);
46
+ }
47
+ return {};
48
+ };
49
+
50
+ /**
51
+ * Workspace dependencies whose entry is a typescript source, like the
52
+ * "untranspiled-library" apps of pob. They must be bundled: node does not strip
53
+ * types under node_modules, so the build could not run outside the monorepo.
54
+ */
55
+ export const createSourceWorkspaceDependencies = (cwd, pkg) => {
56
+ const require = createRequire(path.join(cwd, "package.json"));
57
+ const resolve = (source) => {
58
+ try {
59
+ return fs.realpathSync(require.resolve(source));
60
+ } catch {
61
+ return undefined;
62
+ }
63
+ };
64
+
65
+ const dependencies = Object.entries(pkg.dependencies || {})
66
+ .filter(([, version]) => version.startsWith("workspace:"))
67
+ .flatMap(([name]) => {
68
+ const entryPath = resolve(name);
69
+ if (!entryPath || !sourceExtensionRegExp.test(entryPath)) return [];
70
+ return [{ name, pkg: readClosestPackageJson(entryPath) }];
71
+ });
72
+
73
+ const isSourceWorkspaceDependency = (id) =>
74
+ dependencies.some(({ name }) => id === name || id.startsWith(`${name}/`));
75
+
76
+ return {
77
+ isSourceWorkspaceDependency,
78
+ // their own dependencies stay external
79
+ pkgs: dependencies.map((dependency) => dependency.pkg),
80
+ plugin: {
81
+ name: "@pob/rollup/source-workspace-dependencies",
82
+ resolveId(source) {
83
+ if (!isSourceWorkspaceDependency(source)) return null;
84
+ return resolve(source) ?? null;
85
+ },
86
+ },
87
+ };
88
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pob/rollup",
3
- "version": "8.1.3",
3
+ "version": "8.3.0",
4
4
  "description": "rollup configuration for pob",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/christophehurpeau/pob",
@@ -43,6 +43,6 @@
43
43
  "scripts": {
44
44
  "lint": "pnpm run lint:oxlint && pnpm run lint:eslint",
45
45
  "lint:eslint": "pnpm --dir '../..' exec eslint --quiet '@pob/rollup'",
46
- "lint:oxlint": "pnpm --dir '../..' exec oxlint '@pob/rollup'"
46
+ "lint:oxlint": "pnpm --dir '../..' exec oxlint --quiet '@pob/rollup'"
47
47
  }
48
48
  }