Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 102 additions & 9 deletions extension/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,113 @@ Libraries that remain here verbatim are planned to use this mechanism in the fut

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

## How to update a non-minimzed forge.js
1) Checkout the needed tag, e.g.
```
git clone -b v1.3.1 https://github.com/digitalbazaar/forge
## How to update forge.js and forge.mjs

Both `forge.js` and `forge.mjs` need to be rebuilt when updating node-forge to a new version.

### 1) Clone the forge repository at the desired version

```bash
git clone -b v1.3.2 https://github.com/digitalbazaar/forge
cd forge
```

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.
Replace `v1.3.2` with the desired version tag.

3) Build the package
```
sudo apt install webpack
### 2) Install dependencies

```bash
npm install
```

### 3) Build forge.js (UMD bundle with source maps)

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.

Then build:

```bash
npm run build
```

4) A newly-generated `forge.js` should appear in `dist` subfolder, copy it over the file in this repo.
Copy `dist/forge.js` to `extension/lib/forge.js` in this repository.

### 4) Build forge.mjs (ES module)

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`.

Create a script `convert-forge-to-mjs.js`:

```javascript
const fs = require('fs');
const path = require('path');

const sourceFile = path.join(__dirname, 'dist', 'forge.js');
const targetFile = path.join(__dirname, 'dist', 'forge.mjs');

console.log('Reading forge.js...');
let content = fs.readFileSync(sourceFile, 'utf8');

// Split content into lines
const lines = content.split('\n');

// Find where the webpack bundle starts
// It starts with the webpack bootstrap function
let startLine = lines.findIndex(line => line.includes('/******/ (function(modules) { // webpackBootstrap'));

if (startLine === -1) {
throw new Error('Could not find webpack bundle start');
}

// Find the end - the webpack bundle ends with "/******/ });"
// This closes the modules object passed to the bootstrap function
let endLine = -1;
for (let i = lines.length - 1; i >= 0; i--) {
if (lines[i].trim() === '/******/ });') {
endLine = i + 1; // Include this line
break;
}
}

if (endLine === -1) {
throw new Error('Could not find bundle end');
}

console.log(`Extracting lines ${startLine} to ${endLine}`);

// Extract just the webpack bundle
const bundleLines = lines.slice(startLine, endLine);

// Remove 'return ' from the first line if present (from UMD wrapper)
// The line looks like: return /******/ (function(modules) { // webpackBootstrap
bundleLines[0] = bundleLines[0].replace(/^\s*return\s+/, '');

// Prepend 'var forge = ' to capture the return value
bundleLines[0] = 'var forge = ' + bundleLines[0];

const bundle = bundleLines.join('\n');

// Create ES module wrapper
const esModule = `const globalThis = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};

var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};

${bundle}

var lib = forge;

export { lib as default };
`;

console.log('Writing forge.mjs...');
fs.writeFileSync(targetFile, esModule, 'utf8');
```

Run the script:

```bash
node convert-forge-to-mjs.js
```

Copy `dist/forge.mjs` to `extension/lib/forge.mjs` in this repository.

Loading
Loading