Skip to content

Commit 547e7d5

Browse files
#6124 Updated forge.mjs and readme for build proecess (#6126)
* feat: updated forge.mjs and readme * fix * fix: missing lines * fix: stablize test * fix: unstable test * fix: security * fix: security * fix: lint issue
1 parent 711de1f commit 547e7d5

File tree

7 files changed

+56269
-54500
lines changed

7 files changed

+56269
-54500
lines changed

extension/lib/README.md

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,113 @@ Libraries that remain here verbatim are planned to use this mechanism in the fut
66

77
Only add a copy of a library here if it's not published as a npm module.
88

9-
## How to update a non-minimzed forge.js
10-
1) Checkout the needed tag, e.g.
11-
```
12-
git clone -b v1.3.1 https://github.com/digitalbazaar/forge
9+
## How to update forge.js and forge.mjs
10+
11+
Both `forge.js` and `forge.mjs` need to be rebuilt when updating node-forge to a new version.
12+
13+
### 1) Clone the forge repository at the desired version
14+
15+
```bash
16+
git clone -b v1.3.2 https://github.com/digitalbazaar/forge
1317
cd forge
1418
```
1519

16-
2) In `webpack.config.js` add a line `devtool: 'cheap-module-source-map'` for the plain unoptimized unminified bundle configuration similar to what there is in the optimized and minified bundle configuration.
20+
Replace `v1.3.2` with the desired version tag.
1721

18-
3) Build the package
19-
```
20-
sudo apt install webpack
22+
### 2) Install dependencies
23+
24+
```bash
2125
npm install
26+
```
27+
28+
### 3) Build forge.js (UMD bundle with source maps)
29+
30+
In `webpack.config.js`, add a line `devtool: 'cheap-module-source-map'` in the plain unoptimized unminified bundle configuration (around line 82), similar to what exists in the optimized bundle configuration.
31+
32+
Then build:
33+
34+
```bash
2235
npm run build
2336
```
2437

25-
4) A newly-generated `forge.js` should appear in `dist` subfolder, copy it over the file in this repo.
38+
Copy `dist/forge.js` to `extension/lib/forge.js` in this repository.
39+
40+
### 4) Build forge.mjs (ES module)
41+
42+
Since `node-forge` uses webpack and has Node.js built-ins that can be tricky to bundle with rollup for the browser, the most stable way to generate the ES module is to convert the already-built `forge.js`.
43+
44+
Create a script `convert-forge-to-mjs.js`:
45+
46+
```javascript
47+
const fs = require('fs');
48+
const path = require('path');
49+
50+
const sourceFile = path.join(__dirname, 'dist', 'forge.js');
51+
const targetFile = path.join(__dirname, 'dist', 'forge.mjs');
52+
53+
console.log('Reading forge.js...');
54+
let content = fs.readFileSync(sourceFile, 'utf8');
55+
56+
// Split content into lines
57+
const lines = content.split('\n');
58+
59+
// Find where the webpack bundle starts
60+
// It starts with the webpack bootstrap function
61+
let startLine = lines.findIndex(line => line.includes('/******/ (function(modules) { // webpackBootstrap'));
62+
63+
if (startLine === -1) {
64+
throw new Error('Could not find webpack bundle start');
65+
}
66+
67+
// Find the end - the webpack bundle ends with "/******/ });"
68+
// This closes the modules object passed to the bootstrap function
69+
let endLine = -1;
70+
for (let i = lines.length - 1; i >= 0; i--) {
71+
if (lines[i].trim() === '/******/ });') {
72+
endLine = i + 1; // Include this line
73+
break;
74+
}
75+
}
76+
77+
if (endLine === -1) {
78+
throw new Error('Could not find bundle end');
79+
}
80+
81+
console.log(`Extracting lines ${startLine} to ${endLine}`);
82+
83+
// Extract just the webpack bundle
84+
const bundleLines = lines.slice(startLine, endLine);
85+
86+
// Remove 'return ' from the first line if present (from UMD wrapper)
87+
// The line looks like: return /******/ (function(modules) { // webpackBootstrap
88+
bundleLines[0] = bundleLines[0].replace(/^\s*return\s+/, '');
89+
90+
// Prepend 'var forge = ' to capture the return value
91+
bundleLines[0] = 'var forge = ' + bundleLines[0];
92+
93+
const bundle = bundleLines.join('\n');
94+
95+
// Create ES module wrapper
96+
const esModule = `const globalThis = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
97+
98+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
99+
100+
${bundle}
101+
102+
var lib = forge;
103+
104+
export { lib as default };
105+
`;
106+
107+
console.log('Writing forge.mjs...');
108+
fs.writeFileSync(targetFile, esModule, 'utf8');
109+
```
110+
111+
Run the script:
112+
113+
```bash
114+
node convert-forge-to-mjs.js
115+
```
116+
117+
Copy `dist/forge.mjs` to `extension/lib/forge.mjs` in this repository.
118+

0 commit comments

Comments
 (0)