Skip to content

Commit de2a09c

Browse files
committed
Add CommonJS/ESM interoperability documentation
1 parent ecaeb49 commit de2a09c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/package-url-builder.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ import { PackageURL } from './package-url.js'
2727

2828
import type { QualifiersObject } from './purl-component.js'
2929

30+
/**
31+
* Known Limitation: instanceof checks with ESM/CommonJS interop
32+
* ==============================================================
33+
*
34+
* When using PackageURLBuilder in environments that mix ESM and CommonJS modules
35+
* (such as Vitest tests importing CommonJS-compiled code as ESM), the instanceof
36+
* operator may not work reliably for checking if the built objects are instances
37+
* of PackageURL.
38+
*
39+
* This occurs because:
40+
* - PackageURLBuilder internally imports PackageURL using CommonJS require()
41+
* - External code may import PackageURL using ESM import
42+
* - Node.js creates different wrapper objects for the same class
43+
* - The instanceof check fails due to different object identities
44+
*
45+
* Workaround:
46+
* Instead of: `purl instanceof PackageURL`
47+
* Use: `purl.constructor.name === 'PackageURL'` or check for expected properties/methods
48+
*
49+
* This limitation only affects instanceof checks, not the actual functionality
50+
* of the created PackageURL objects.
51+
*/
52+
3053
/**
3154
* Builder class for constructing PackageURL instances using a fluent API.
3255
*

src/package-url.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ SOFTWARE.
2222

2323
/**
2424
* @fileoverview Package URL parsing and construction utilities.
25+
*
26+
* Note on instanceof checks:
27+
* When this module is compiled to CommonJS and imported from ESM contexts,
28+
* instanceof checks may fail due to module system interoperability issues.
29+
* See package-url-builder.ts for detailed explanation and workarounds.
2530
*/
2631
import { decodePurlComponent } from './decode.js'
2732
import { PurlError } from './error.js'

0 commit comments

Comments
 (0)