Skip to content

Commit ac99376

Browse files
fix
1 parent 22d46fa commit ac99376

File tree

2 files changed

+28229
-27879
lines changed

2 files changed

+28229
-27879
lines changed

extension/lib/README.md

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,76 @@ Copy `dist/forge.js` to `extension/lib/forge.js` in this repository.
3939

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

42-
Install rollup and create a rollup configuration:
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`.
4343

44-
```bash
45-
npm install --save-dev rollup @rollup/plugin-node-resolve @rollup/plugin-commonjs
46-
```
47-
48-
Create `rollup.config.js`:
44+
Create a script `convert-forge-to-mjs.js`:
4945

5046
```javascript
51-
import resolve from '@rollup/plugin-node-resolve';
52-
import commonjs from '@rollup/plugin-commonjs';
53-
54-
export default {
55-
input: 'lib/index.js',
56-
output: {
57-
file: 'dist/forge.mjs',
58-
format: 'es',
59-
sourcemap: true
60-
},
61-
plugins: [
62-
resolve(),
63-
commonjs()
64-
]
65-
};
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+
// Find where the webpack bundle starts
57+
// It starts with the webpack bootstrap function
58+
let startLine = lines.findIndex(line => line.includes('/******/ (function(modules) { // webpackBootstrap'));
59+
60+
if (startLine === -1) {
61+
throw new Error('Could not find webpack bundle start');
62+
}
63+
64+
// Find the end - the webpack bundle ends with "/******/ });"
65+
// This closes the modules object passed to the bootstrap function
66+
let endLine = -1;
67+
for (let i = lines.length - 1; i >= 0; i--) {
68+
if (lines[i].trim() === '/******/ });') {
69+
endLine = i + 1; // Include this line
70+
break;
71+
}
72+
}
73+
74+
if (endLine === -1) {
75+
throw new Error('Could not find bundle end');
76+
}
77+
78+
console.log(`Extracting lines ${startLine} to ${endLine}`);
79+
80+
// Extract just the webpack bundle
81+
const bundleLines = lines.slice(startLine, endLine);
82+
83+
// Remove 'return ' from the first line if present (from UMD wrapper)
84+
// The line looks like: return /******/ (function(modules) { // webpackBootstrap
85+
bundleLines[0] = bundleLines[0].replace(/^\s*return\s+/, '');
86+
87+
// Prepend 'var forge = ' to capture the return value
88+
bundleLines[0] = 'var forge = ' + bundleLines[0];
89+
90+
const bundle = bundleLines.join('\n');
91+
92+
// Create ES module wrapper
93+
const esModule = `const globalThis = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
94+
95+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
96+
97+
${bundle}
98+
99+
var lib = forge;
100+
101+
export { lib as default };
102+
`;
103+
104+
console.log('Writing forge.mjs...');
105+
fs.writeFileSync(targetFile, esModule, 'utf8');
66106
```
67107

68-
Build the ES module:
108+
Run the script:
69109

70110
```bash
71-
npx rollup -c rollup.config.js
111+
node convert-forge-to-mjs.js
72112
```
73113

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

0 commit comments

Comments
 (0)