@davidwells/extract-deps 0.0.5 → 0.0.7

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 (34) hide show
  1. package/.claude/settings.local.json +7 -1
  2. package/examples/verify-basic.js +2 -0
  3. package/examples/verify-package-entries.js +74 -0
  4. package/package.json +2 -2
  5. package/src/dep-graph.js +82 -14
  6. package/src/get-cjs-exports.js +112 -7
  7. package/src/get-imports.js +1 -0
  8. package/src/utils.js +1 -1
  9. package/src/vendor/inspect-parameters-declaration.js +282 -0
  10. package/src/verify-package-entries.js +228 -0
  11. package/tests/dep-graph.test.js +172 -0
  12. package/tests/fixtures/cjs-broken-example/index.js +8 -0
  13. package/tests/fixtures/cjs-broken-example/one.js +9 -0
  14. package/tests/fixtures/cjs-broken-example/package.json +7 -0
  15. package/tests/fixtures/cjs-broken-example/sub-folder/sub-sub-folder/three.js +6 -0
  16. package/tests/fixtures/cjs-broken-example/sub-folder/two.js +8 -0
  17. package/tests/fixtures/cjs-deep-example/package.json +15 -0
  18. package/tests/fixtures/cjs-example/package.json +7 -0
  19. package/tests/fixtures/dir-index-example/entry.js +6 -0
  20. package/tests/fixtures/dir-index-example/shared-pkg/index.js +4 -0
  21. package/tests/fixtures/esm-broken-fixture/index.js +8 -0
  22. package/tests/fixtures/esm-broken-fixture/one.js +9 -0
  23. package/tests/fixtures/esm-broken-fixture/package.json +15 -0
  24. package/tests/fixtures/esm-broken-fixture/sub-folder/sub-sub-folder/three.js +6 -0
  25. package/tests/fixtures/esm-broken-fixture/sub-folder/sub-sub-folder-two/deep.js +6 -0
  26. package/tests/fixtures/esm-broken-fixture/sub-folder/sub-sub-folder-two/even-deeper/wow-deep.js +5 -0
  27. package/tests/fixtures/esm-broken-fixture/sub-folder/two.js +8 -0
  28. package/tests/fixtures/esm-broken-fixture/valid.js +7 -0
  29. package/tests/fixtures/esm-fixture/package.json +11 -0
  30. package/tests/get-cjs-exports.test.js +171 -0
  31. package/tests/get-imports.test.js +4 -1
  32. package/tests/manual/broken-cjs.js +38 -0
  33. package/tests/manual/broken-esm.js +37 -0
  34. package/tests/verify-package-entries.test.js +272 -0
@@ -0,0 +1,272 @@
1
+ // Test package.json entry point verification
2
+ const { test } = require('uvu')
3
+ const assert = require('uvu/assert')
4
+ const path = require('path')
5
+ const { getPackageEntryPoints, verifyPackageEntries, collectEntryPoints } = require('../src/verify-package-entries')
6
+
7
+ const CJS_EXAMPLE_PKG = path.resolve(__dirname, './fixtures/cjs-example/package.json')
8
+ const ESM_FIXTURE_PKG = path.resolve(__dirname, './fixtures/esm-fixture/package.json')
9
+ const CJS_DEEP_EXAMPLE_PKG = path.resolve(__dirname, './fixtures/cjs-deep-example/package.json')
10
+
11
+ const packageStub = {
12
+ "name": "my-package",
13
+ "version": "1.0.0",
14
+ "description": "Example package with all entry points",
15
+ "main": "./dist/index.cjs",
16
+ "module": "./dist/index.mjs",
17
+ "browser": "./dist/browser.js",
18
+ "types": "./dist/index.d.ts",
19
+ "bin": {
20
+ "my-cli": "./bin/cli.js"
21
+ },
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.mjs",
26
+ "require": "./dist/index.cjs",
27
+ "browser": "./dist/browser.js"
28
+ },
29
+ "./dep-graph": "./src/dep-graph.js",
30
+ "./package.json": "./package.json"
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "bin"
35
+ ]
36
+ }
37
+
38
+ test('getPackageEntryPoints should extract all entry points from package.json', () => {
39
+ const entryPoints = collectEntryPoints(packageStub, 'dir-path')
40
+ console.log('entryPoints', entryPoints)
41
+ assert.ok(Array.isArray(entryPoints), 'Should return an array')
42
+ assert.ok(entryPoints.length > 0, 'Should have at least one entry point')
43
+ })
44
+
45
+ // getPackageEntryPoints tests
46
+ test('getPackageEntryPoints should extract main field', () => {
47
+ const entryPoints = getPackageEntryPoints(CJS_EXAMPLE_PKG)
48
+ console.log('entryPoints', entryPoints)
49
+
50
+ assert.ok(Array.isArray(entryPoints), 'Should return an array')
51
+ assert.ok(entryPoints.length > 0, 'Should have at least one entry point')
52
+
53
+ const mainEntry = entryPoints.find(ep => ep.field === 'main')
54
+ assert.ok(mainEntry, 'Should have main entry point')
55
+ assert.is(mainEntry.path, './index.js', 'Main path should be ./index.js')
56
+ })
57
+
58
+ test('getPackageEntryPoints should extract multiple fields', () => {
59
+ const entryPoints = getPackageEntryPoints(ESM_FIXTURE_PKG)
60
+
61
+ const fields = entryPoints.map(ep => ep.field)
62
+ assert.ok(fields.includes('main'), 'Should include main')
63
+ assert.ok(fields.includes('module'), 'Should include module')
64
+ })
65
+
66
+ test('getPackageEntryPoints should extract exports field', () => {
67
+ const entryPoints = getPackageEntryPoints(ESM_FIXTURE_PKG)
68
+
69
+ const exportsEntry = entryPoints.find(ep => ep.field.startsWith('exports'))
70
+ assert.ok(exportsEntry, 'Should have exports entry point')
71
+ })
72
+
73
+ test('getPackageEntryPoints should handle complex exports with conditionals', () => {
74
+ const entryPoints = getPackageEntryPoints(CJS_DEEP_EXAMPLE_PKG)
75
+
76
+ const fields = entryPoints.map(ep => ep.field)
77
+
78
+ // Should extract conditional exports
79
+ assert.ok(fields.some(f => f.includes('require')), 'Should extract require conditional')
80
+ assert.ok(fields.some(f => f.includes('import')), 'Should extract import conditional')
81
+ })
82
+
83
+ test('getPackageEntryPoints should skip .d.ts files', () => {
84
+ const entryPoints = getPackageEntryPoints(ESM_FIXTURE_PKG)
85
+
86
+ const hasDts = entryPoints.some(ep => ep.path.endsWith('.d.ts'))
87
+ assert.not(hasDts, 'Should not include .d.ts files')
88
+ })
89
+
90
+ test('getPackageEntryPoints should skip .json files', () => {
91
+ const entryPoints = getPackageEntryPoints(ESM_FIXTURE_PKG)
92
+
93
+ const hasJson = entryPoints.some(ep => ep.path.endsWith('.json'))
94
+ assert.not(hasJson, 'Should not include .json files')
95
+ })
96
+
97
+ test('getPackageEntryPoints should resolve absolute paths', () => {
98
+ const entryPoints = getPackageEntryPoints(CJS_EXAMPLE_PKG)
99
+
100
+ entryPoints.forEach(ep => {
101
+ assert.ok(path.isAbsolute(ep.resolvedPath), 'Resolved path should be absolute')
102
+ })
103
+ })
104
+
105
+ test('getPackageEntryPoints should extract nested exports', () => {
106
+ const entryPoints = getPackageEntryPoints(CJS_DEEP_EXAMPLE_PKG)
107
+
108
+ const fields = entryPoints.map(ep => ep.field)
109
+
110
+ // Should have nested paths like exports./one, exports./sub
111
+ assert.ok(fields.some(f => f.includes('one')), 'Should extract nested export "./one"')
112
+ assert.ok(fields.some(f => f.includes('sub')), 'Should extract nested export "./sub"')
113
+ })
114
+
115
+ // verifyPackageEntries tests
116
+ test('verifyPackageEntries should return correct structure', () => {
117
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG)
118
+ console.log('results', results)
119
+
120
+ assert.ok(results.entryPoints, 'Should have entryPoints')
121
+ assert.ok(results.sourceFiles, 'Should have sourceFiles')
122
+ assert.ok(results.errors, 'Should have errors')
123
+
124
+ assert.ok(Array.isArray(results.entryPoints), 'entryPoints should be array')
125
+ assert.ok(results.sourceFiles instanceof Set, 'sourceFiles should be Set')
126
+ assert.ok(Array.isArray(results.errors), 'errors should be array')
127
+ })
128
+
129
+ test('verifyPackageEntries should verify valid entry points', () => {
130
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG)
131
+
132
+ const validCount = results.entryPoints.filter(ep => ep.valid).length
133
+ assert.ok(validCount > 0, 'Should have at least one valid entry point')
134
+ })
135
+
136
+ test('verifyPackageEntries should collect dependencies by default', () => {
137
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG)
138
+
139
+ results.entryPoints.forEach(ep => {
140
+ if (ep.valid) {
141
+ assert.ok(ep.sourceFiles, 'Valid entry should have sourceFiles')
142
+ assert.ok(Array.isArray(ep.sourceFiles), 'sourceFiles should be array')
143
+ }
144
+ })
145
+ })
146
+
147
+ test('verifyPackageEntries should deduplicate dependencies in sourceFiles', () => {
148
+ const results = verifyPackageEntries(CJS_DEEP_EXAMPLE_PKG)
149
+
150
+ // Count total sourceFiles from all entry points
151
+ let totalDeps = 0
152
+ results.entryPoints.forEach(ep => {
153
+ if (ep.sourceFiles) {
154
+ totalDeps += ep.sourceFiles.length
155
+ }
156
+ })
157
+
158
+ // sourceFiles should be less than or equal to total (deduplication)
159
+ assert.ok(results.sourceFiles.size <= totalDeps, 'sourceFiles should deduplicate')
160
+ })
161
+
162
+ test('verifyPackageEntries should work with collectDeps option', () => {
163
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG, { collectDeps: true })
164
+
165
+ assert.ok(results.sourceFiles.size > 0, 'Should collect dependencies')
166
+ })
167
+
168
+ test('verifyPackageEntries should skip sourceFiles when collectDeps is false', () => {
169
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG, { collectDeps: false })
170
+ console.log('results', results)
171
+ results.entryPoints.forEach(ep => {
172
+ console.log('ep', ep)
173
+ assert.equal(ep.sourceFiles, [], 'Should not collect sourceFiles when collectDeps is false')
174
+ })
175
+
176
+ assert.is(results.sourceFiles.size, 0, 'sourceFiles should be empty')
177
+ })
178
+
179
+ test('verifyPackageEntries should work with verifyGraph option', () => {
180
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG, { verifyGraph: true })
181
+
182
+ // Should complete without throwing
183
+ assert.ok(results, 'Should return results')
184
+ })
185
+
186
+ test('verifyPackageEntries should skip verification when verifyGraph is false', () => {
187
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG, { verifyGraph: false })
188
+
189
+ // Should still collect sourceFiles but skip verification
190
+ assert.ok(results.sourceFiles.size > 0, 'Should still collect sourceFiles')
191
+ })
192
+
193
+ test('verifyPackageEntries should collect all errors', () => {
194
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG)
195
+
196
+ // With valid fixtures, should have 0 errors
197
+ assert.is(results.errors.length, 0, 'Valid fixtures should have no errors')
198
+ })
199
+
200
+ test('verifyPackageEntries should mark entry as invalid on error', () => {
201
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG)
202
+
203
+ results.entryPoints.forEach(ep => {
204
+ if (!ep.valid) {
205
+ assert.ok(ep.error, 'Invalid entry should have error')
206
+ }
207
+ })
208
+ })
209
+
210
+ test('verifyPackageEntries should process all entries even if one fails', () => {
211
+ const results = verifyPackageEntries(CJS_DEEP_EXAMPLE_PKG)
212
+
213
+ // Should process all 6 entry points from deep example
214
+ assert.ok(results.entryPoints.length >= 6, 'Should process all entry points')
215
+ })
216
+
217
+ test('verifyPackageEntries should collect sourceFiles for each entry separately', () => {
218
+ const results = verifyPackageEntries(CJS_DEEP_EXAMPLE_PKG)
219
+
220
+ const validEntries = results.entryPoints.filter(ep => ep.valid)
221
+
222
+ validEntries.forEach(ep => {
223
+ assert.ok(ep.sourceFiles, 'Each entry should have its own sourceFiles')
224
+ assert.ok(Array.isArray(ep.sourceFiles), 'sourceFiles should be array')
225
+ assert.ok(ep.sourceFiles.length > 0, 'sourceFiles should not be empty')
226
+ })
227
+ })
228
+
229
+ test('verifyPackageEntries should work with simple package', () => {
230
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG)
231
+
232
+ assert.is(results.errors.length, 0, 'Should have no errors')
233
+ assert.ok(results.sourceFiles.size > 0, 'Should have dependencies')
234
+ })
235
+
236
+ test('verifyPackageEntries should work with ESM package', () => {
237
+ const results = verifyPackageEntries(ESM_FIXTURE_PKG)
238
+
239
+ assert.is(results.errors.length, 0, 'Should have no errors')
240
+ assert.ok(results.sourceFiles.size > 0, 'Should have dependencies')
241
+ })
242
+
243
+ test('verifyPackageEntries should work with complex package', () => {
244
+ const results = verifyPackageEntries(CJS_DEEP_EXAMPLE_PKG)
245
+
246
+ assert.is(results.errors.length, 0, 'Should have no errors')
247
+ assert.ok(results.sourceFiles.size > 0, 'Should have dependencies')
248
+ assert.ok(results.entryPoints.length >= 6, 'Should have multiple entry points')
249
+ })
250
+
251
+ test('verifyPackageEntries should include entry point file in sourceFiles', () => {
252
+ const results = verifyPackageEntries(CJS_EXAMPLE_PKG)
253
+
254
+ const mainEntry = results.entryPoints.find(ep => ep.field === 'main')
255
+ assert.ok(mainEntry, 'Should have main entry')
256
+ assert.ok(mainEntry.sourceFiles, 'Should have sourceFiles')
257
+
258
+ // Entry point file should be in its sourceFiles (as it's the last file in build order)
259
+ const hasEntryInDeps = mainEntry.sourceFiles.some(dep => dep === mainEntry.resolvedPath)
260
+ assert.ok(hasEntryInDeps, 'Entry point should be in its own sourceFiles list')
261
+ })
262
+
263
+ test('verifyPackageEntries should collect unique sourceFiles across all entries', () => {
264
+ const results = verifyPackageEntries(CJS_DEEP_EXAMPLE_PKG)
265
+
266
+ const sourceFilesArray = Array.from(results.sourceFiles)
267
+ const uniqueDeps = [...new Set(sourceFilesArray)]
268
+
269
+ assert.is(sourceFilesArray.length, uniqueDeps.length, 'sourceFiles should not have duplicates')
270
+ })
271
+
272
+ test.run()