Skip to content

Commit 19a6a49

Browse files
authored
feat: NodeJS exports submodules as subpaths (#1066)
## Added * Explicitly export own first-level submodules via package manifest (via [#1066]) When used with bundlers/packers downstream, this might enable better tree shaking due to scoped imports. ## Refactor * Ease internal tree shaking (via [#1066]) [#1066]: #1066 --------- Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 07cb79f commit 19a6a49

26 files changed

+279
-112
lines changed

HISTORY.md

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ All notable changes to this project will be documented in this file.
66

77
<!-- add unreleased items here -->
88

9+
* Added
10+
* Explicitly export own first-level submodules via package manifest (via [#1066])
11+
When used with bundlers/packers downstream, this might enable better tree shaking due to scoped imports.
12+
* Refactor
13+
* Ease internal tree shaking (via [#1066])
14+
15+
[#1066]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/1066
16+
917
## 6.7.2 -- 2024-05-07
1018

1119
* Changed

package.json

+46-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,52 @@
106106
"types": "./dist.d/index.node.d.ts",
107107
"main": "./dist.node/index.node.js",
108108
"exports": {
109-
"types": "./dist.d/index.node.d.ts",
110-
"browser": "./dist.web/lib.js",
111-
"default": "./dist.node/index.node.js"
109+
".": {
110+
"types": "./dist.d/index.node.d.ts",
111+
"browser": "./dist.web/lib.js",
112+
"default": "./dist.node/index.node.js"
113+
},
114+
"./package.json": "./package.json",
115+
"./Builders": {
116+
"types": "./dist.d/builders/index.node.d.ts",
117+
"default": "./dist.node/builders/index.node.js"
118+
},
119+
"./Enums": {
120+
"types": "./dist.d/enums/index.d.ts",
121+
"default": "./dist.node/enums/index.js"
122+
},
123+
"./Factories": {
124+
"types": "./dist.d/factories/index.node.d.ts",
125+
"default": "./dist.node/factories/index.node.js"
126+
},
127+
"./Models": {
128+
"types": "./dist.d/models/index.d.ts",
129+
"default": "./dist.node/models/index.js"
130+
},
131+
"./Serialize": {
132+
"types": "./dist.d/serialize/index.node.d.ts",
133+
"default": "./dist.node/serialize/index.node.js"
134+
},
135+
"./Spec": {
136+
"types": "./dist.d/spec/index.d.ts",
137+
"default": "./dist.node/spec/index.js"
138+
},
139+
"./Types": {
140+
"types": "./dist.d/types/index.d.ts",
141+
"default": "./dist.node/types/index.js"
142+
},
143+
"./Utils": {
144+
"types": "./dist.d/utils/index.d.ts",
145+
"default": "./dist.node/utils/index.js"
146+
},
147+
"./Validation": {
148+
"types": "./dist.d/validation/index.node.d.ts",
149+
"default": "./dist.node/validation/index.node.js"
150+
},
151+
"./SPDX": {
152+
"types": "./dist.d/spdx.d.ts",
153+
"default": "./dist.node/spdx.js"
154+
}
112155
},
113156
"directories": {
114157
"doc": "./docs",

src/builders/fromNodePackageJson.node.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2828

2929
import type { PackageJson } from '../_helpers/packageJson'
3030
import { splitNameGroup } from '../_helpers/packageJson'
31-
import * as Enums from '../enums'
31+
import { ComponentType } from '../enums/componentType'
3232
import type * as Factories from '../factories/index.node'
33-
import * as Models from '../models'
33+
import { Component } from '../models/component'
34+
import { ExternalReferenceRepository } from '../models/externalReference'
35+
import { LicenseRepository } from '../models/license'
36+
import { Tool } from '../models/tool'
3437

3538
/**
3639
* Node-specific ToolBuilder.
@@ -48,18 +51,18 @@ export class ToolBuilder {
4851

4952
// Current implementation does not return `undefined` yet, but it is an option for future implementation.
5053
// To prevent future breaking changes, it is declared to return `undefined`.
51-
makeTool (data: PackageJson): Models.Tool | undefined {
54+
makeTool (data: PackageJson): Tool | undefined {
5255
const [name, vendor] = typeof data.name === 'string'
5356
? splitNameGroup(data.name)
5457
: []
5558

56-
return new Models.Tool({
59+
return new Tool({
5760
vendor,
5861
name,
5962
version: (typeof data.version === 'string')
6063
? data.version
6164
: undefined,
62-
externalReferences: new Models.ExternalReferenceRepository(this.#extRefFactory.makeExternalReferences(data))
65+
externalReferences: new ExternalReferenceRepository(this.#extRefFactory.makeExternalReferences(data))
6366
})
6467
}
6568
}
@@ -87,7 +90,7 @@ export class ComponentBuilder {
8790
return this.#licenseFactory
8891
}
8992

90-
makeComponent (data: PackageJson, type: Enums.ComponentType = Enums.ComponentType.Library): Models.Component | undefined {
93+
makeComponent (data: PackageJson, type: ComponentType = ComponentType.Library): Component | undefined {
9194
if (typeof data.name !== 'string') {
9295
return undefined
9396
}
@@ -116,7 +119,7 @@ export class ComponentBuilder {
116119

117120
const externalReferences = this.#extRefFactory.makeExternalReferences(data)
118121

119-
const licenses = new Models.LicenseRepository()
122+
const licenses = new LicenseRepository()
120123
if (typeof data.license === 'string') {
121124
/* see https://docs.npmjs.com/cli/v9/configuring-npm/package-json#license */
122125
licenses.add(this.#licenseFactory.makeFromString(data.license))
@@ -134,10 +137,10 @@ export class ComponentBuilder {
134137
}
135138
}
136139

137-
return new Models.Component(type, name, {
140+
return new Component(type, name, {
138141
author,
139142
description,
140-
externalReferences: new Models.ExternalReferenceRepository(externalReferences),
143+
externalReferences: new ExternalReferenceRepository(externalReferences),
141144
group,
142145
licenses,
143146
version

src/factories/fromNodePackageJson.node.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ import type { PackageURL } from 'packageurl-js'
3131
import { isNotUndefined } from '../_helpers/notUndefined'
3232
import type { PackageJson } from '../_helpers/packageJson'
3333
import { PackageUrlQualifierNames } from '../_helpers/packageUrl'
34-
import * as Enums from '../enums'
35-
import * as Models from '../models'
34+
import { ExternalReferenceType } from '../enums/externalReferenceType'
35+
import type { Component } from '../models/component'
36+
import { ExternalReference } from '../models/externalReference'
3637
import { PackageUrlFactory as PlainPackageUrlFactory } from './packageUrl'
3738

3839
/**
3940
* Node-specific ExternalReferenceFactory.
4041
*/
4142
export class ExternalReferenceFactory {
42-
makeExternalReferences (data: PackageJson): Models.ExternalReference[] {
43-
const refs: Array<Models.ExternalReference | undefined> = []
43+
makeExternalReferences (data: PackageJson): ExternalReference[] {
44+
const refs: Array<ExternalReference | undefined> = []
4445

4546
try { refs.push(this.makeVcs(data)) } catch { /* pass */ }
4647
try { refs.push(this.makeHomepage(data)) } catch { /* pass */ }
@@ -49,7 +50,7 @@ export class ExternalReferenceFactory {
4950
return refs.filter(isNotUndefined)
5051
}
5152

52-
makeVcs (data: PackageJson): Models.ExternalReference | undefined {
53+
makeVcs (data: PackageJson): ExternalReference | undefined {
5354
/* see https://docs.npmjs.com/cli/v9/configuring-npm/package-json#repositoryc */
5455
const repository = data.repository
5556
let url
@@ -67,21 +68,21 @@ export class ExternalReferenceFactory {
6768
comment = 'as detected from PackageJson property "repository"'
6869
}
6970
return typeof url === 'string' && url.length > 0
70-
? new Models.ExternalReference(url, Enums.ExternalReferenceType.VCS, { comment })
71+
? new ExternalReference(url, ExternalReferenceType.VCS, { comment })
7172
: undefined
7273
}
7374

74-
makeHomepage (data: PackageJson): Models.ExternalReference | undefined {
75+
makeHomepage (data: PackageJson): ExternalReference | undefined {
7576
/* see https://docs.npmjs.com/cli/v9/configuring-npm/package-json#homepage */
7677
const url = data.homepage
7778
return typeof url === 'string' && url.length > 0
78-
? new Models.ExternalReference(
79-
url, Enums.ExternalReferenceType.Website,
79+
? new ExternalReference(
80+
url, ExternalReferenceType.Website,
8081
{ comment: 'as detected from PackageJson property "homepage"' })
8182
: undefined
8283
}
8384

84-
makeIssueTracker (data: PackageJson): Models.ExternalReference | undefined {
85+
makeIssueTracker (data: PackageJson): ExternalReference | undefined {
8586
/* see https://docs.npmjs.com/cli/v9/configuring-npm/package-json#bugs */
8687
const bugs = data.bugs
8788
let url
@@ -94,7 +95,7 @@ export class ExternalReferenceFactory {
9495
comment = 'as detected from PackageJson property "bugs"'
9596
}
9697
return typeof url === 'string' && url.length > 0
97-
? new Models.ExternalReference(url, Enums.ExternalReferenceType.IssueTracker, { comment })
98+
? new ExternalReference(url, ExternalReferenceType.IssueTracker, { comment })
9899
: undefined
99100
}
100101
}
@@ -105,7 +106,7 @@ const npmDefaultRegistryMatcher = /^https?:\/\/registry\.npmjs\.org/
105106
* Node-specific PackageUrlFactory.
106107
*/
107108
export class PackageUrlFactory extends PlainPackageUrlFactory {
108-
override makeFromComponent (component: Models.Component, sort: boolean = false): PackageURL | undefined {
109+
override makeFromComponent (component: Component, sort: boolean = false): PackageURL | undefined {
109110
const purl = super.makeFromComponent(component, sort)
110111
return purl === undefined
111112
? undefined

src/factories/license.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ SPDX-License-Identifier: Apache-2.0
1717
Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

20-
import type { DisjunctiveLicense, License } from '../models'
21-
import { LicenseExpression, NamedLicense, SpdxLicense } from '../models'
20+
import type { DisjunctiveLicense, License } from '../models/license'
21+
import { LicenseExpression, NamedLicense, SpdxLicense } from '../models/license'
2222
import { fixupSpdxId, isValidSpdxLicenseExpression } from '../spdx'
2323

2424
export class LicenseFactory {

src/factories/packageUrl.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
2020
import { PackageURL } from 'packageurl-js'
2121

2222
import { PackageUrlQualifierNames } from '../_helpers/packageUrl'
23-
import { ExternalReferenceType } from '../enums'
24-
import type { Component } from '../models'
23+
import { ExternalReferenceType } from '../enums/externalReferenceType'
24+
import type { Component } from '../models/component'
2525

2626
export class PackageUrlFactory {
2727
readonly #type: PackageURL['type']

src/index.node.ts

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ SPDX-License-Identifier: Apache-2.0
1717
Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

20+
/* REMEMBER:
21+
ALL non-internal exports in here have to be set as `exports` in `package.json`
22+
*/
23+
2024
export * from './index.common'
2125

2226
// region node-specifics

src/models/attachment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

2020
import type { Stringable } from '../_helpers/stringable'
21-
import type { AttachmentEncoding } from '../enums'
21+
import type { AttachmentEncoding } from '../enums/attachmentEncoding'
2222

2323
export interface OptionalAttachmentProperties {
2424
contentType?: Attachment['contentType']

src/models/bom.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ SPDX-License-Identifier: Apache-2.0
1717
Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

20-
import type { PositiveInteger } from '../types'
21-
import { isPositiveInteger } from '../types'
20+
import type { PositiveInteger } from '../types/integer'
21+
import { isPositiveInteger } from '../types/integer'
2222
import { ComponentRepository } from './component'
2323
import { Metadata } from './metadata'
24-
import { VulnerabilityRepository } from './vulnerability'
24+
import { VulnerabilityRepository } from './vulnerability/vulnerability'
2525

2626
export interface OptionalBomProperties {
2727
metadata?: Bom['metadata']

src/models/component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import { SortableComparables, SortableStringables } from '../_helpers/sortable'
2424
import type { Stringable } from '../_helpers/stringable'
2525
import { treeIteratorSymbol } from '../_helpers/tree'
2626
import type { ComponentScope, ComponentType } from '../enums'
27-
import type { CPE } from '../types'
28-
import { isCPE } from '../types'
27+
import type { CPE } from '../types/cpe'
28+
import { isCPE } from '../types/cpe'
2929
import { BomRef, BomRefRepository } from './bomRef'
3030
import { ExternalReferenceRepository } from './externalReference'
3131
import { HashDictionary } from './hash'

src/models/externalReference.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
1919

2020
import type { Comparable } from '../_helpers/sortable'
2121
import { SortableComparables } from '../_helpers/sortable'
22-
import type { ExternalReferenceType } from '../enums'
22+
import type { ExternalReferenceType } from '../enums/externalReferenceType'
2323
import type { BomLink } from './bomLink'
2424
import { HashDictionary } from './hash'
2525

src/models/hash.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

2020
import type { Sortable } from '../_helpers/sortable'
21-
import type { HashAlgorithm } from '../enums'
21+
import type { HashAlgorithm } from '../enums/hashAlogorithm'
2222

2323
// no regex for the HashContent in here. It applies at runtime of a normalization/serialization process.
2424
export type HashContent = string

src/models/license.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

2020
import type { Sortable } from '../_helpers/sortable'
21-
import type { LicenseAcknowledgement } from '../enums'
21+
import type { LicenseAcknowledgement } from '../enums/licenseAcknowledgement'
2222
import type { SpdxId } from '../spdx'
2323
import type { Attachment } from './attachment'
2424

src/models/lifecycle.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

2020
import type { Comparable, Sortable } from '../_helpers/sortable'
21-
import type { LifecyclePhase } from '../enums'
21+
import type { LifecyclePhase } from '../enums/lifecyclePhase'
2222

2323
export interface OptionalNamedLifecycleProperties {
2424
description?: NamedLifecycle['description']

src/models/swid.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ SPDX-License-Identifier: Apache-2.0
1717
Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

20-
import type { NonNegativeInteger } from '../types'
21-
import { isNonNegativeInteger } from '../types'
20+
import type { NonNegativeInteger } from '../types/integer'
21+
import { isNonNegativeInteger } from '../types/integer'
2222
import type { Attachment } from './attachment'
2323

2424
export interface OptionalSWIDProperties {

src/models/vulnerability/analysis.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

2020
import type { AnalysisJustification, AnalysisState } from '../../enums/vulnerability'
21-
import { AnalysisResponseRepository } from '../../enums/vulnerability'
21+
import { AnalysisResponseRepository } from '../../enums/vulnerability/analysisResponse'
2222

2323
export interface OptionalAnalysisProperties {
2424
state?: Analysis['state']

src/models/vulnerability/vulnerability.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
1919

2020
import type { Comparable } from '../../_helpers/sortable'
2121
import { SortableComparables } from '../../_helpers/sortable'
22-
import { CweRepository } from '../../types'
22+
import { CweRepository } from '../../types/cwe'
2323
import { BomRef } from '../bomRef'
2424
import { PropertyRepository } from '../property'
2525
import { ToolRepository } from '../tool'

0 commit comments

Comments
 (0)