From 973a88b78369cf7ce51af92e8571ffc9a39ef1a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eike=20Ha=C3=9F?= Date: Tue, 1 Feb 2022 18:44:21 +0100 Subject: [PATCH] enable esm output for node --- bindings/wasm/build/node.js | 38 +++- bindings/wasm/examples/webpack.config.js | 51 +++--- bindings/wasm/package-lock.json | 212 ++++++++++++++++------- bindings/wasm/package.json | 13 +- bindings/wasm/webpack.config.js | 58 ++++--- 5 files changed, 255 insertions(+), 117 deletions(-) diff --git a/bindings/wasm/build/node.js b/bindings/wasm/build/node.js index 38b3aa722e..787e5d4f1d 100644 --- a/bindings/wasm/build/node.js +++ b/bindings/wasm/build/node.js @@ -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 +) diff --git a/bindings/wasm/examples/webpack.config.js b/bindings/wasm/examples/webpack.config.js index 377fe3b392..10f33d584c 100644 --- a/bindings/wasm/examples/webpack.config.js +++ b/bindings/wasm/examples/webpack.config.js @@ -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 = { @@ -56,6 +58,7 @@ const clientConfig = { experiments: { topLevelAwait: true, outputModule: true, + asyncWebAssembly: true, }, resolve: { alias: { diff --git a/bindings/wasm/package-lock.json b/bindings/wasm/package-lock.json index f869b17fe3..0ec217020f 100644 --- a/bindings/wasm/package-lock.json +++ b/bindings/wasm/package-lock.json @@ -9,7 +9,7 @@ "version": "0.5.0-dev.3", "license": "Apache-2.0", "dependencies": { - "node-fetch": "^2.6.7" + "node-fetch": "^3.2.0" }, "devDependencies": { "@wasm-tool/wasm-pack-plugin": "^1.6.0", @@ -22,7 +22,8 @@ "wasm-pack": "0.10.1", "webpack": "^5.67.0", "webpack-cli": "^4.9.2", - "webpack-dev-server": "^4.7.3" + "webpack-dev-server": "^4.7.3", + "webpack-plugin-replace": "^1.2.0" }, "engines": { "node": ">=16" @@ -2005,6 +2006,14 @@ "node": ">=0.10" } }, + "node_modules/data-uri-to-buffer": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz", + "integrity": "sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==", + "engines": { + "node": ">= 12" + } + }, "node_modules/dayjs": { "version": "1.10.7", "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.7.tgz", @@ -2657,6 +2666,28 @@ "pend": "~1.2.0" } }, + "node_modules/fetch-blob": { + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.1.4.tgz", + "integrity": "sha512-Eq5Xv5+VlSrYWEqKrusxY1C3Hm/hjeAsCGVG3ft7pZahlUAChpGZT/Ms1WmSLnEAisEXszjzu/s+ce6HZB2VHA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, "node_modules/figures": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz", @@ -2801,6 +2832,17 @@ "node": ">= 0.12" } }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -2867,6 +2909,20 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "dev": true }, + "node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", @@ -4908,42 +4964,39 @@ "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", "dev": true }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "engines": { + "node": ">=10.5.0" + } + }, "node_modules/node-fetch": { - "version": "2.6.7", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", - "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.2.0.tgz", + "integrity": "sha512-8xeimMwMItMw8hRrOl3C9/xzU49HV/yE6ORew/l+dxWimO5A4Ra8ld2rerlJvc/O7et5Z1zrWsPX43v1QBjCxw==", "dependencies": { - "whatwg-url": "^5.0.0" + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" }, "engines": { - "node": "4.x || >=6.0.0" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, - "peerDependencies": { - "encoding": "^0.1.0" - }, - "peerDependenciesMeta": { - "encoding": { - "optional": true - } - } - }, - "node_modules/node-fetch/node_modules/tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" - }, - "node_modules/node-fetch/node_modules/webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=" - }, - "node_modules/node-fetch/node_modules/whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", - "dependencies": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" } }, "node_modules/node-forge": { @@ -6778,6 +6831,14 @@ "minimalistic-assert": "^1.0.0" } }, + "node_modules/web-streams-polyfill": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.0.tgz", + "integrity": "sha512-EqPmREeOzttaLRm5HS7io98goBgZ7IVz79aDvqjD0kYXLtFZTc0T/U6wHTPKyIjb+MdN7DFIIX6hgdBEpWmfPA==", + "engines": { + "node": ">= 8" + } + }, "node_modules/webpack": { "version": "5.67.0", "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.67.0.tgz", @@ -7097,6 +7158,15 @@ "node": ">=10.0.0" } }, + "node_modules/webpack-plugin-replace": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/webpack-plugin-replace/-/webpack-plugin-replace-1.2.0.tgz", + "integrity": "sha512-1HA3etHpJW55qonJqv84o5w5GY7iqF8fqSHpTWdNwarj1llkkt4jT4QSvYs1hoaU8Lu5akDnq/spHHO5mXwo1w==", + "dev": true, + "engines": { + "node": ">=4" + } + }, "node_modules/webpack-sources": { "version": "3.2.3", "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-3.2.3.tgz", @@ -8861,6 +8931,11 @@ "assert-plus": "^1.0.0" } }, + "data-uri-to-buffer": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz", + "integrity": "sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==" + }, "dayjs": { "version": "1.10.7", "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.7.tgz", @@ -9391,6 +9466,15 @@ "pend": "~1.2.0" } }, + "fetch-blob": { + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.1.4.tgz", + "integrity": "sha512-Eq5Xv5+VlSrYWEqKrusxY1C3Hm/hjeAsCGVG3ft7pZahlUAChpGZT/Ms1WmSLnEAisEXszjzu/s+ce6HZB2VHA==", + "requires": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + } + }, "figures": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz", @@ -9490,6 +9574,14 @@ "mime-types": "^2.1.12" } }, + "formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "requires": { + "fetch-blob": "^3.1.2" + } + }, "forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -9541,6 +9633,13 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "dev": true }, + "fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "optional": true + }, "function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", @@ -11071,33 +11170,19 @@ "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", "dev": true }, + "node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==" + }, "node-fetch": { - "version": "2.6.7", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", - "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.2.0.tgz", + "integrity": "sha512-8xeimMwMItMw8hRrOl3C9/xzU49HV/yE6ORew/l+dxWimO5A4Ra8ld2rerlJvc/O7et5Z1zrWsPX43v1QBjCxw==", "requires": { - "whatwg-url": "^5.0.0" - }, - "dependencies": { - "tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" - }, - "webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=" - }, - "whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", - "requires": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } - } + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" } }, "node-forge": { @@ -12520,6 +12605,11 @@ "minimalistic-assert": "^1.0.0" } }, + "web-streams-polyfill": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.0.tgz", + "integrity": "sha512-EqPmREeOzttaLRm5HS7io98goBgZ7IVz79aDvqjD0kYXLtFZTc0T/U6wHTPKyIjb+MdN7DFIIX6hgdBEpWmfPA==" + }, "webpack": { "version": "5.67.0", "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.67.0.tgz", @@ -12746,6 +12836,12 @@ "wildcard": "^2.0.0" } }, + "webpack-plugin-replace": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/webpack-plugin-replace/-/webpack-plugin-replace-1.2.0.tgz", + "integrity": "sha512-1HA3etHpJW55qonJqv84o5w5GY7iqF8fqSHpTWdNwarj1llkkt4jT4QSvYs1hoaU8Lu5akDnq/spHHO5mXwo1w==", + "dev": true + }, "webpack-sources": { "version": "3.2.3", "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-3.2.3.tgz", diff --git a/bindings/wasm/package.json b/bindings/wasm/package.json index 60023323a6..010b209fe7 100644 --- a/bindings/wasm/package.json +++ b/bindings/wasm/package.json @@ -10,9 +10,9 @@ "example": "examples" }, "scripts": { - "build:nodejs": "wasm-pack build --target nodejs --release --out-dir node && node ./build/node", + "build:nodejs": "wasm-pack build --target bundler --release --out-dir node && node ./build/node", "build:web": "wasm-pack build --target web --release --out-dir web && node ./build/web", - "build-dev:nodejs": "wasm-pack build --target nodejs --dev --out-dir node && node ./build/node", + "build-dev:nodejs": "wasm-pack build --target bundler --dev --out-dir node && node ./build/node", "build-dev:web": "wasm-pack build --target web --dev --out-dir web && node ./build/web", "build:docs": "node ./build/docs", "build:examples": "webpack --config ./examples/webpack.config.js --mode=production", @@ -21,9 +21,9 @@ "pretest": "npm run build:nodejs", "prepublishOnly": "npm run build", "serve": "webpack serve", - "example:node": "node examples/dist/node.js", + "example:node": "node examples/dist/node.mjs", "example:browser": "http-server ./examples/dist/ -c-1 -o ", - "test:node": "mocha ./examples/dist/test.js --exit", + "test:node": "mocha ./examples/dist/test.mjs --exit", "test:browser": "cypress run --headless", "test:readme": "txm README.md", "test:unit": "wasm-pack test --node", @@ -59,10 +59,11 @@ "wasm-pack": "0.10.1", "webpack": "^5.67.0", "webpack-cli": "^4.9.2", - "webpack-dev-server": "^4.7.3" + "webpack-dev-server": "^4.7.3", + "webpack-plugin-replace": "^1.2.0" }, "dependencies": { - "node-fetch": "^2.6.7" + "node-fetch": "^3.2.0" }, "engines": { "node": ">=16" diff --git a/bindings/wasm/webpack.config.js b/bindings/wasm/webpack.config.js index dd6cdc78bc..b85501f4f0 100644 --- a/bindings/wasm/webpack.config.js +++ b/bindings/wasm/webpack.config.js @@ -1,41 +1,55 @@ const path = require("path"); const CopyWebPlugin = require('copy-webpack-plugin'); -const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin"); +const nodeExternals = require('webpack-node-externals'); const dist = path.resolve(__dirname, "dist"); module.exports = { mode: "production", entry: { - index: "./examples/web.js" + index: "./node/identity_wasm.js" }, output: { path: dist, - filename: "[name].js" + filename: 'index.mjs', + module: true, + chunkFormat: 'module', + library: { + type: 'module' + }, + environment: { + module: true + } }, - devServer: { - contentBase: dist, + target: 'node16', + //externalsType: 'module', + // resolve: { + // fallback: { "crypto": false } + // }, + externalsPresets: { node: true }, + externals: { + crypto: 'crypto', + 'node-fetch': 'node-fetch', }, + //externals: [nodeExternals({importType: "module"})], // in order to ignore all modules in node_modules folder plugins: [ - new CopyWebPlugin({ - patterns: [ - { - from: path.resolve(__dirname, "static") - } - ] - }), + // new CopyWebPlugin({ + // patterns: [ + // { + // from: path.resolve(__dirname, "static") + // } + // ] + // }), - new WasmPackPlugin({ - crateDirectory: __dirname, - }), ], - // Makes the output less verbose - stats: 'minimal', - // Removes the asset size warning - performance: { - hints: false, - }, + // // Makes the output less verbose + // stats: 'minimal', + // // Removes the asset size warning + // performance: { + // hints: false, + // }, experiments: { - asyncWebAssembly: true + asyncWebAssembly: true, + outputModule: true, } };