Skip to content

Commit 53e8179

Browse files
committed
BC: Properly exclude external packages when preparing this library for web browsers
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 3b7aa2b commit 53e8179

File tree

12 files changed

+174
-79
lines changed

12 files changed

+174
-79
lines changed

Diff for: .eslintignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
**/node_modules/**
22

3-
43
# generated files: dist and docs
4+
/reports/**
55
/dist/**
66
/dist.*/**
77
/docs/api/**
88
/examples/node-typescript/dist/**
9+
/examples/web/dist/**
10+
911

1012
!/src/**
1113
!/libs/**

Diff for: .github/workflows/nodejs.yml

+33-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ jobs:
263263
run: node -- 'example.${{ matrix.js-type }}'
264264
working-directory: ${{ env.EXAMPLE_DIR }}
265265

266-
example-typescript:
266+
example-TS:
267267
needs: [ 'build' ]
268268
name: example TS${{ matrix.typescript-version }} ${{ matrix.js-type }}
269269
runs-on: ubuntu-latest
@@ -326,6 +326,38 @@ jobs:
326326
run: npm run example
327327
working-directory: ${{ env.EXAMPLE_DIR }}
328328

329+
examples-Web:
330+
needs: [ 'build' ]
331+
name: example Web
332+
runs-on: ubuntu-latest
333+
env:
334+
EXAMPLE_DIR: examples/web
335+
steps:
336+
- name: Checkout
337+
# see https://github.com/actions/checkout
338+
uses: actions/checkout@v3
339+
- name: Setup Node.js ${{ env.NODE_ACTIVE_LTS }}
340+
# see https://github.com/actions/setup-node
341+
uses: actions/setup-node@v3
342+
with:
343+
node-version: ${{ env.NODE_ACTIVE_LTS }}
344+
- name: fetch build artifact 'node'
345+
# see https://github.com/actions/download-artifact
346+
uses: actions/download-artifact@v3
347+
with:
348+
name: dist.web
349+
path: dist.web
350+
- name: setup library
351+
run: |
352+
set -ex
353+
npm ci --ignore-scripts --omit=dev --include=optional --loglevel=silly
354+
- name: setup example project
355+
run: npm i --no-save --loglevel=silly
356+
working-directory: ${{ env.EXAMPLE_DIR }}
357+
- name: build example
358+
run: npm run build
359+
working-directory: ${{ env.EXAMPLE_DIR }}
360+
329361
api-doc:
330362
name: api-doc ${{ matrix.target }}
331363
runs-on: "ubuntu-latest"

Diff for: HISTORY.md

+10
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@ All notable changes to this project will be documented in this file.
44

55
## unreleased
66

7+
* BREAKING
8+
* Usage in web browsers might no longer work out of the box (via [#880])
9+
It requires a bundler/packer for web. See the `examples/web` as a best-practice guide utilizing _Webpack_.
10+
This is only a breaking change if you used this library in a web browser.
11+
* Fixed
12+
* Properly exclude external packages when preparing this library for web browsers (via [#880])
13+
* Examples
14+
* Adjusted example for usage in web browsers (via [#880])
715
* Build
816
* Use _TypeScript_ `v5.1.6` now, was `v5.1.5` (via [#866])
917
* Use _Webpack_ `v5.88.1` now, was `v5.88.0` (via [#870])
18+
* Apply wider exclusion for externals to be future-prove (via [#880])
1019

1120
[#866]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/866
1221
[#870]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/870
22+
[#880]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/880
1323

1424
## 3.0.0 -- 2023-06-28
1525

Diff for: examples/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* [`example.cjs`](node-typescript/example.cjs) targets JavaScript, "commonjs" style - from TypeScript `^3.8 || ^4 || ^5`.
1010
* [`example.mjs`](node-typescript/example.cjs) targets JavaScript, "module" style - from TypeScript `^3.8 || ^4 || ^5`.
1111
* `web` showcases the usage in web browser JavaScript
12-
* [`example.html`](web/example.html) targets any web-browser.
12+
* [`src/index.js`](web/src/index.js) will be compiled, to that it can be embedded by
13+
[`dist/index.html`](web/dist/index.html) and run in any web browser.
1314

1415
## Data models
1516

Diff for: examples/web/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*
2+
!/.gitignore
3+
!/package.json
4+
!/webpack.json
5+
!/src
6+
!/src/**
7+
!/dist
8+
!/dist/index.html

Diff for: examples/web/dist/index.html

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<!--
3+
This file is part of CycloneDX JavaScript Library.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
SPDX-License-Identifier: Apache-2.0
18+
Copyright (c) OWASP Foundation. All Rights Reserved.
19+
-->
20+
<html lang="en">
21+
<head>
22+
<title>example</title>
23+
</head>
24+
<body>
25+
<script src="./main.js"></script>
26+
<p>see JavaScript Console output for result.</p>
27+
</body>
28+
</html>

Diff for: examples/web/example.html

-67
This file was deleted.

Diff for: examples/web/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"private": true,
3+
"name": "example",
4+
"license": "Apache-2.0",
5+
"dependencies": {
6+
"@cyclonedx/cyclonedx-library": "file:../.."
7+
},
8+
"devDependencies": {
9+
"webpack": "^5",
10+
"webpack-cli": "^5"
11+
},
12+
"scripts": {
13+
"build": "webpack build",
14+
"example": "open index.html"
15+
}
16+
}

Diff for: examples/web/src/index.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*!
2+
This file is part of CycloneDX JavaScript Library.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache-2.0
17+
Copyright (c) OWASP Foundation. All Rights Reserved.
18+
*/
19+
20+
const CDX = require('@cyclonedx/cyclonedx-library')
21+
22+
/** Example how to serialize a Bom to JSON / XML. */
23+
24+
const lFac = new CDX.Factories.LicenseFactory()
25+
26+
const bom = new CDX.Models.Bom()
27+
bom.metadata.component = new CDX.Models.Component(
28+
CDX.Enums.ComponentType.Application,
29+
'MyProject'
30+
)
31+
bom.metadata.component.licenses.add(lFac.makeFromString('MIT OR Apache-2.0'))
32+
33+
const componentA = new CDX.Models.Component(
34+
CDX.Enums.ComponentType.Library,
35+
'myComponentA'
36+
)
37+
componentA.licenses.add(lFac.makeFromString('Apache-2.0'))
38+
39+
bom.components.add(componentA)
40+
bom.metadata.component.dependencies.add(componentA.bomRef)
41+
42+
const jsonSerializer = new CDX.Serialize.JsonSerializer(
43+
new CDX.Serialize.JSON.Normalize.Factory(
44+
CDX.Spec.Spec1dot4))
45+
const serializedJson = jsonSerializer.serialize(bom)
46+
console.log(serializedJson)
47+
48+
const xmlSerializer = new CDX.Serialize.XmlSerializer(
49+
new CDX.Serialize.XML.Normalize.Factory(
50+
CDX.Spec.Spec1dot5))
51+
const serializedXML = xmlSerializer.serialize(bom)
52+
console.log(serializedXML)

Diff for: package-lock.json

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@
9696
"typedoc-plugin-missing-exports": "^2.0.0",
9797
"typescript": "5.1.6",
9898
"webpack": "5.88.1",
99-
"webpack-cli": "5.1.4"
99+
"webpack-cli": "5.1.4",
100+
"webpack-node-externals": "^3.0.0"
100101
},
101102
"browser": "./dist.web/lib.js",
102103
"types": "./dist.d/index.node.d.ts",

Diff for: webpack.config.js

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

2020
const path = require('path')
2121
const deepmerge = require('deepmerge')
22+
const nodeExternals = require('webpack-node-externals')
2223

2324
/* eslint-disable jsdoc/valid-types */
2425

@@ -55,13 +56,8 @@ const configBase = {
5556
type: 'umd'
5657
}
5758
},
58-
externals: {
59-
ajv: 'ajv',
60-
'ajv-formats': 'ajv-formats',
61-
'ajv-formats-draft2019': 'ajv-formats-draft2019',
62-
'packageurl-js': 'packageurl-js',
63-
'spdx-expression-parse': 'spdx-expression-parse'
64-
}
59+
externalsPresets: { node: true },
60+
externals: [nodeExternals()]
6561
}
6662

6763
module.exports = [

0 commit comments

Comments
 (0)