Skip to content
Draft
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
38 changes: 31 additions & 7 deletions bindings/wasm/build/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,42 @@ const entryFileNode = fs.readFileSync(entryFilePathNode).toString()
lintBigInt(entryFileNode);

let changedFileNode = entryFileNode.replace(
"let imports = {};",
`if (!globalThis.fetch) {
const fetch = require('node-fetch')
globalThis.Headers = fetch.Headers
globalThis.Request = fetch.Request
globalThis.Response = fetch.Response
"wasm.__wbindgen_start();",
`import crypto from 'crypto'
if (!globalThis.crypto) {
globalThis.crypto = crypto
}
import fetch, {Headers, Request, Response} from 'node-fetch'
globalThis.crypto = crypto
if (!globalThis.fetch) {
globalThis.Headers = Headers
globalThis.Request = Request
globalThis.Response = Response
globalThis.fetch = fetch
}
let imports = {};`
wasm.__wbindgen_start();`
)
fs.writeFileSync(
entryFilePathNode,
changedFileNode
)

const bgFilePathNode = path.join(__dirname, '../node/identity_wasm_bg.js')
const bgFileNode = fs.readFileSync(bgFilePathNode).toString()

lintBigInt(bgFileNode);

let changedBgFileNode = bgFileNode.replace(
"var ret = module.require(getStringFromWasm0(arg0, arg1));",
`
console.log(getStringFromWasm0(arg0, arg1));
console.log(globalThis);
if (getStringFromWasm0(arg0, arg1) === "crypto") {
return addHeapObject(globalThis.crypto)
}
var ret = module.require(getStringFromWasm0(arg0, arg1));`
)
fs.writeFileSync(
bgFilePathNode,
changedBgFileNode
)
51 changes: 27 additions & 24 deletions bindings/wasm/examples/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
const path = require('path');
const CopyWebPlugin = require('copy-webpack-plugin');
const serverConfig = {
target: 'node',
target: 'node16',
entry: './examples/src/node.js',
devtool: "source-map",
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'node.js',
filename: 'node.mjs',
library: {
type: 'module',
},
},
externals: [
function ({ context, request }, callback) {
if (/^@iota\/identity-wasm$/.test(request)) {
// Externalize to a commonjs module
return callback(null, 'commonjs ' + path.resolve(__dirname, '../node/identity_wasm.js'));
}

// Continue without externalizing the import
callback();
experiments: {
asyncWebAssembly: true,
outputModule: true,
},
resolve: {
alias: {
'@iota/identity-wasm': path.resolve(__dirname, '../node/identity_wasm.js'),
},
],
},
};

const serverTestConfig = {
target: 'node',
target: 'node16',
entry: './examples/src/test.js',
devtool: "source-map",
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'test.js',
filename: 'test.mjs',
library: {
type: 'module',
},
},
externals: [
function ({ context, request }, callback) {
if (/^@iota\/identity-wasm$/.test(request)) {
// Externalize to a commonjs module
return callback(null, 'commonjs ' + path.resolve(__dirname, '../node/identity_wasm.js'));
}

// Continue without externalizing the import
callback();
experiments: {
asyncWebAssembly: true,
outputModule: true,
},
resolve: {
alias: {
'@iota/identity-wasm': path.resolve(__dirname, '../node/identity_wasm.js'),
},
],
},
};

const clientConfig = {
Expand All @@ -56,6 +58,7 @@ const clientConfig = {
experiments: {
topLevelAwait: true,
outputModule: true,
asyncWebAssembly: true,
},
resolve: {
alias: {
Expand Down
Loading