@quilted/rollup 0.2.4 → 0.2.5

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.
@@ -0,0 +1,150 @@
1
+ import * as path from 'path';
2
+ import {readFileSync} from 'fs';
3
+ import {fileURLToPath} from 'url';
4
+
5
+ import {glob, type GlobOptions} from 'glob';
6
+
7
+ const PROJECT_CACHE = new Map<string, Project>();
8
+
9
+ export class Project {
10
+ static load(root: string | URL) {
11
+ const resolvedRoot = resolveRoot(root);
12
+
13
+ let project = PROJECT_CACHE.get(resolvedRoot);
14
+
15
+ if (project == null) {
16
+ project = new Project(resolvedRoot);
17
+ PROJECT_CACHE.set(resolvedRoot, project);
18
+ }
19
+
20
+ return project;
21
+ }
22
+
23
+ readonly root: string;
24
+
25
+ get path() {
26
+ return this.root;
27
+ }
28
+
29
+ get name() {
30
+ return this.packageJSON.name;
31
+ }
32
+
33
+ #packageJSON: PackageJSON | undefined;
34
+ get packageJSON() {
35
+ this.#packageJSON ??= new PackageJSON(this.root);
36
+ return this.#packageJSON;
37
+ }
38
+
39
+ constructor(root: string | URL) {
40
+ this.root = resolveRoot(root);
41
+ }
42
+
43
+ resolve(...segments: string[]) {
44
+ return path.resolve(this.root, ...segments);
45
+ }
46
+
47
+ glob(pattern: string | string[], options?: GlobOptions) {
48
+ return glob(pattern, {
49
+ ...options,
50
+ absolute: true,
51
+ cwd: this.root,
52
+ }) as Promise<string[]>;
53
+ }
54
+ }
55
+
56
+ export interface PackageJSONRaw {
57
+ name: string;
58
+ main?: string;
59
+ private?: boolean;
60
+ exports?: Record<string, string | Record<string, string>>;
61
+ dependencies?: Record<string, string>;
62
+ peerDependencies?: Record<string, string>;
63
+ peerDependenciesMeta?: Record<string, {optional?: boolean}>;
64
+ devDependencies?: Record<string, string>;
65
+ [key: string]: unknown;
66
+ }
67
+
68
+ export class PackageJSON {
69
+ readonly raw: PackageJSONRaw;
70
+ readonly path: string;
71
+
72
+ get name() {
73
+ return this.raw.name;
74
+ }
75
+
76
+ constructor(rootOrPath: string) {
77
+ this.path = rootOrPath.endsWith('package.json')
78
+ ? rootOrPath
79
+ : path.join(rootOrPath, 'package.json');
80
+ this.raw = JSON.parse(readFileSync(this.path, 'utf8'));
81
+ }
82
+ }
83
+
84
+ function resolveRoot(root: string | URL) {
85
+ return typeof root === 'string' ? root : fileURLToPath(root);
86
+ }
87
+
88
+ export async function sourceEntriesForProject(project: Project) {
89
+ const {main, exports} = project.packageJSON.raw;
90
+
91
+ const entries: Record<string, string> = {};
92
+
93
+ if (typeof main === 'string') {
94
+ entries['.'] = await resolveTargetFileAsSource(main, project);
95
+ }
96
+
97
+ if (typeof exports === 'string') {
98
+ entries['.'] = await resolveTargetFileAsSource(exports, project);
99
+ return entries;
100
+ } else if (exports == null || typeof exports !== 'object') {
101
+ return entries;
102
+ }
103
+
104
+ for (const [exportPath, exportCondition] of Object.entries(
105
+ exports as Record<string, null | string | Record<string, string>>,
106
+ )) {
107
+ let targetFile: string | null | undefined = null;
108
+
109
+ if (exportCondition == null) continue;
110
+
111
+ if (typeof exportCondition === 'string') {
112
+ targetFile = exportCondition;
113
+ } else {
114
+ targetFile ??=
115
+ exportCondition['source'] ??
116
+ exportCondition['quilt:source'] ??
117
+ exportCondition['quilt:esnext'] ??
118
+ Object.values(exportCondition).find(
119
+ (condition) =>
120
+ typeof condition === 'string' && condition.startsWith('./build/'),
121
+ );
122
+ }
123
+
124
+ if (targetFile == null) continue;
125
+
126
+ const sourceFile = await resolveTargetFileAsSource(targetFile, project);
127
+
128
+ entries[exportPath] = sourceFile;
129
+ }
130
+
131
+ return entries;
132
+ }
133
+
134
+ async function resolveTargetFileAsSource(file: string, project: Project) {
135
+ const sourceFile = file.includes('/build/')
136
+ ? (
137
+ await project.glob(
138
+ file
139
+ .replace(/[/]build[/][^/]+[/]/, '/*/')
140
+ .replace(/(\.d\.ts|\.[\w]+)$/, '.*'),
141
+ {
142
+ absolute: true,
143
+ ignore: [project.resolve(file)],
144
+ },
145
+ )
146
+ )[0]!
147
+ : project.resolve(file);
148
+
149
+ return sourceFile;
150
+ }
@@ -1,16 +0,0 @@
1
- import * as path from 'node:path';
2
- import { fileURLToPath } from 'node:url';
3
- import { readFile } from 'node:fs/promises';
4
-
5
- async function loadPackageJSON(root) {
6
- const file = await readFile(
7
- path.join(
8
- typeof root === "string" ? root : fileURLToPath(root),
9
- "package.json"
10
- ),
11
- "utf8"
12
- );
13
- return JSON.parse(file);
14
- }
15
-
16
- export { loadPackageJSON };
@@ -1,7 +0,0 @@
1
- import { fileURLToPath } from 'node:url';
2
-
3
- function resolveRoot(root) {
4
- return typeof root === "string" ? root : fileURLToPath(root);
5
- }
6
-
7
- export { resolveRoot };
@@ -1,20 +0,0 @@
1
- import * as path from 'path';
2
- import {fileURLToPath} from 'url';
3
- import {readFile} from 'fs/promises';
4
-
5
- export interface PackageJSON {
6
- name?: string;
7
- [key: string]: unknown;
8
- }
9
-
10
- export async function loadPackageJSON(root: string | URL) {
11
- const file = await readFile(
12
- path.join(
13
- typeof root === 'string' ? root : fileURLToPath(root),
14
- 'package.json',
15
- ),
16
- 'utf8',
17
- );
18
-
19
- return JSON.parse(file) as PackageJSON;
20
- }
@@ -1,5 +0,0 @@
1
- import {fileURLToPath} from 'url';
2
-
3
- export function resolveRoot(root: string | URL) {
4
- return typeof root === 'string' ? root : fileURLToPath(root);
5
- }