@metamask/snaps-webpack-plugin 4.2.1 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -1
- package/dist/manifest.cjs +31 -9
- package/dist/manifest.cjs.map +1 -1
- package/dist/manifest.d.cts +0 -3
- package/dist/manifest.d.cts.map +1 -1
- package/dist/manifest.d.mts +0 -3
- package/dist/manifest.d.mts.map +1 -1
- package/dist/manifest.mjs +8 -11
- package/dist/manifest.mjs.map +1 -1
- package/dist/plugin.cjs +40 -2
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +7 -1
- package/dist/plugin.d.cts.map +1 -1
- package/dist/plugin.d.mts +7 -1
- package/dist/plugin.d.mts.map +1 -1
- package/dist/plugin.mjs +44 -4
- package/dist/plugin.mjs.map +1 -1
- package/package.json +13 -25
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [4.3.0]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Improve readability of eval errors ([#3335](https://github.com/MetaMask/snaps/pull/3335))
|
|
15
|
+
- Bump `@metamask/utils` from `11.0.1` to `11.3.0` ([#3050](https://github.com/MetaMask/snaps/pull/3050), [#3091](https://github.com/MetaMask/snaps/pull/3091),[#3232](https://github.com/MetaMask/snaps/pull/3232))
|
|
16
|
+
|
|
10
17
|
## [4.2.1]
|
|
11
18
|
|
|
12
19
|
### Changed
|
|
@@ -121,7 +128,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
121
128
|
- The version of the package no longer needs to match the version of all other
|
|
122
129
|
MetaMask Snaps packages.
|
|
123
130
|
|
|
124
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.
|
|
131
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.3.0...HEAD
|
|
132
|
+
[4.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.2.1...@metamask/snaps-webpack-plugin@4.3.0
|
|
125
133
|
[4.2.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.2.0...@metamask/snaps-webpack-plugin@4.2.1
|
|
126
134
|
[4.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.1.2...@metamask/snaps-webpack-plugin@4.2.0
|
|
127
135
|
[4.1.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.1.1...@metamask/snaps-webpack-plugin@4.1.2
|
package/dist/manifest.cjs
CHANGED
|
@@ -1,27 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
exports.writeManifest = void 0;
|
|
4
27
|
const fs_1 = require("fs");
|
|
5
|
-
const
|
|
28
|
+
const babel = __importStar(require("prettier/plugins/babel"));
|
|
29
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
30
|
+
// eslint-disable-next-line import-x/namespace
|
|
31
|
+
const estree = __importStar(require("prettier/plugins/estree"));
|
|
32
|
+
const standalone_1 = require("prettier/standalone");
|
|
6
33
|
/**
|
|
7
34
|
* Format the manifest data with Prettier and write it to disk.
|
|
8
35
|
*
|
|
9
|
-
* It uses the Prettier configuration found in the project directory (if any),
|
|
10
|
-
* or the default Prettier configuration if none is found.
|
|
11
|
-
*
|
|
12
36
|
* @param path - The path to write the manifest to.
|
|
13
37
|
* @param data - The manifest data.
|
|
14
38
|
* @param writeFileFn - The function to use to write the manifest.
|
|
15
39
|
* @returns A promise that resolves when the manifest has been written.
|
|
16
40
|
*/
|
|
17
41
|
async function writeManifest(path, data, writeFileFn = fs_1.promises.writeFile) {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
});
|
|
21
|
-
const formattedManifest = (0, prettier_1.format)(data, {
|
|
22
|
-
...config,
|
|
42
|
+
const formattedManifest = await (0, standalone_1.format)(data, {
|
|
43
|
+
tabWidth: 2,
|
|
23
44
|
parser: 'json',
|
|
24
45
|
filepath: path,
|
|
46
|
+
plugins: [babel, estree],
|
|
25
47
|
});
|
|
26
48
|
await writeFileFn(path, formattedManifest);
|
|
27
49
|
}
|
package/dist/manifest.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.cjs","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"manifest.cjs","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,2BAAoC;AACpC,8DAAgD;AAChD,gFAAgF;AAChF,8CAA8C;AAC9C,gEAAkD;AAClD,oDAA6C;AAE7C;;;;;;;GAOG;AACI,KAAK,UAAU,aAAa,CACjC,IAAY,EACZ,IAAY,EACZ,cAAiC,aAAE,CAAC,SAAS;IAE7C,MAAM,iBAAiB,GAAG,MAAM,IAAA,mBAAM,EAAC,IAAI,EAAE;QAC3C,QAAQ,EAAE,CAAC;QACX,MAAM,EAAE,MAAM;QACd,QAAQ,EAAE,IAAI;QACd,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC;KACzB,CAAC,CAAC;IAEH,MAAM,WAAW,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAC7C,CAAC;AAbD,sCAaC","sourcesContent":["import type { WriteFileFunction } from '@metamask/snaps-utils/node';\nimport { promises as fs } from 'fs';\nimport * as babel from 'prettier/plugins/babel';\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line import-x/namespace\nimport * as estree from 'prettier/plugins/estree';\nimport { format } from 'prettier/standalone';\n\n/**\n * Format the manifest data with Prettier and write it to disk.\n *\n * @param path - The path to write the manifest to.\n * @param data - The manifest data.\n * @param writeFileFn - The function to use to write the manifest.\n * @returns A promise that resolves when the manifest has been written.\n */\nexport async function writeManifest(\n path: string,\n data: string,\n writeFileFn: WriteFileFunction = fs.writeFile,\n) {\n const formattedManifest = await format(data, {\n tabWidth: 2,\n parser: 'json',\n filepath: path,\n plugins: [babel, estree],\n });\n\n await writeFileFn(path, formattedManifest);\n}\n"]}
|
package/dist/manifest.d.cts
CHANGED
|
@@ -2,9 +2,6 @@ import type { WriteFileFunction } from "@metamask/snaps-utils/node";
|
|
|
2
2
|
/**
|
|
3
3
|
* Format the manifest data with Prettier and write it to disk.
|
|
4
4
|
*
|
|
5
|
-
* It uses the Prettier configuration found in the project directory (if any),
|
|
6
|
-
* or the default Prettier configuration if none is found.
|
|
7
|
-
*
|
|
8
5
|
* @param path - The path to write the manifest to.
|
|
9
6
|
* @param data - The manifest data.
|
|
10
7
|
* @param writeFileFn - The function to use to write the manifest.
|
package/dist/manifest.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.d.cts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mCAAmC;
|
|
1
|
+
{"version":3,"file":"manifest.d.cts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mCAAmC;AAQpE;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,WAAW,GAAE,iBAAgC,iBAU9C"}
|
package/dist/manifest.d.mts
CHANGED
|
@@ -2,9 +2,6 @@ import type { WriteFileFunction } from "@metamask/snaps-utils/node";
|
|
|
2
2
|
/**
|
|
3
3
|
* Format the manifest data with Prettier and write it to disk.
|
|
4
4
|
*
|
|
5
|
-
* It uses the Prettier configuration found in the project directory (if any),
|
|
6
|
-
* or the default Prettier configuration if none is found.
|
|
7
|
-
*
|
|
8
5
|
* @param path - The path to write the manifest to.
|
|
9
6
|
* @param data - The manifest data.
|
|
10
7
|
* @param writeFileFn - The function to use to write the manifest.
|
package/dist/manifest.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.d.mts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mCAAmC;
|
|
1
|
+
{"version":3,"file":"manifest.d.mts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mCAAmC;AAQpE;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,WAAW,GAAE,iBAAgC,iBAU9C"}
|
package/dist/manifest.mjs
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
import { promises as fs } from "fs";
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import * as babel from "prettier/plugins/babel";
|
|
3
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
4
|
+
// eslint-disable-next-line import-x/namespace
|
|
5
|
+
import * as estree from "prettier/plugins/estree";
|
|
6
|
+
import { format } from "prettier/standalone";
|
|
5
7
|
/**
|
|
6
8
|
* Format the manifest data with Prettier and write it to disk.
|
|
7
9
|
*
|
|
8
|
-
* It uses the Prettier configuration found in the project directory (if any),
|
|
9
|
-
* or the default Prettier configuration if none is found.
|
|
10
|
-
*
|
|
11
10
|
* @param path - The path to write the manifest to.
|
|
12
11
|
* @param data - The manifest data.
|
|
13
12
|
* @param writeFileFn - The function to use to write the manifest.
|
|
14
13
|
* @returns A promise that resolves when the manifest has been written.
|
|
15
14
|
*/
|
|
16
15
|
export async function writeManifest(path, data, writeFileFn = fs.writeFile) {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
const formattedManifest = format(data, {
|
|
21
|
-
...config,
|
|
16
|
+
const formattedManifest = await format(data, {
|
|
17
|
+
tabWidth: 2,
|
|
22
18
|
parser: 'json',
|
|
23
19
|
filepath: path,
|
|
20
|
+
plugins: [babel, estree],
|
|
24
21
|
});
|
|
25
22
|
await writeFileFn(path, formattedManifest);
|
|
26
23
|
}
|
package/dist/manifest.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.mjs","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,WAAW
|
|
1
|
+
{"version":3,"file":"manifest.mjs","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,WAAW;AACpC,OAAO,KAAK,KAAK,+BAA+B;AAChD,gFAAgF;AAChF,8CAA8C;AAC9C,OAAO,KAAK,MAAM,gCAAgC;AAClD,OAAO,EAAE,MAAM,EAAE,4BAA4B;AAE7C;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,IAAY,EACZ,IAAY,EACZ,cAAiC,EAAE,CAAC,SAAS;IAE7C,MAAM,iBAAiB,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE;QAC3C,QAAQ,EAAE,CAAC;QACX,MAAM,EAAE,MAAM;QACd,QAAQ,EAAE,IAAI;QACd,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC;KACzB,CAAC,CAAC;IAEH,MAAM,WAAW,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAC7C,CAAC","sourcesContent":["import type { WriteFileFunction } from '@metamask/snaps-utils/node';\nimport { promises as fs } from 'fs';\nimport * as babel from 'prettier/plugins/babel';\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line import-x/namespace\nimport * as estree from 'prettier/plugins/estree';\nimport { format } from 'prettier/standalone';\n\n/**\n * Format the manifest data with Prettier and write it to disk.\n *\n * @param path - The path to write the manifest to.\n * @param data - The manifest data.\n * @param writeFileFn - The function to use to write the manifest.\n * @returns A promise that resolves when the manifest has been written.\n */\nexport async function writeManifest(\n path: string,\n data: string,\n writeFileFn: WriteFileFunction = fs.writeFile,\n) {\n const formattedManifest = await format(data, {\n tabWidth: 2,\n parser: 'json',\n filepath: path,\n plugins: [babel, estree],\n });\n\n await writeFileFn(path, formattedManifest);\n}\n"]}
|
package/dist/plugin.cjs
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
2
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
15
|
};
|
|
16
|
+
var _SnapsWebpackPlugin_spinner;
|
|
5
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
const snaps_rpc_methods_1 = require("@metamask/snaps-rpc-methods");
|
|
6
19
|
const snaps_sdk_1 = require("@metamask/snaps-sdk");
|
|
7
20
|
const node_1 = require("@metamask/snaps-utils/node");
|
|
8
21
|
const utils_1 = require("@metamask/utils");
|
|
22
|
+
const chalk_1 = require("chalk");
|
|
9
23
|
const path_1 = __importDefault(require("path"));
|
|
10
24
|
const util_1 = require("util");
|
|
11
25
|
const webpack_1 = require("webpack");
|
|
@@ -26,14 +40,17 @@ class SnapsWebpackPlugin {
|
|
|
26
40
|
* `process.cwd() + '/snap.manifest.json'`.
|
|
27
41
|
* @param options.writeManifest - Whether to fix the manifest.
|
|
28
42
|
* Defaults to `true`.
|
|
43
|
+
* @param spinner - The spinner to use for logging. For internal use only.
|
|
29
44
|
*/
|
|
30
|
-
constructor(options) {
|
|
45
|
+
constructor(options, spinner) {
|
|
46
|
+
_SnapsWebpackPlugin_spinner.set(this, void 0);
|
|
31
47
|
this.options = {
|
|
32
48
|
eval: true,
|
|
33
49
|
manifestPath: path_1.default.join(process.cwd(), 'snap.manifest.json'),
|
|
34
50
|
writeManifest: true,
|
|
35
51
|
...options,
|
|
36
52
|
};
|
|
53
|
+
__classPrivateFieldSet(this, _SnapsWebpackPlugin_spinner, spinner, "f");
|
|
37
54
|
}
|
|
38
55
|
/**
|
|
39
56
|
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
@@ -88,17 +105,37 @@ class SnapsWebpackPlugin {
|
|
|
88
105
|
(0, utils_1.assert)(compilation.outputOptions.path);
|
|
89
106
|
const outputPath = compilation.outputOptions.path;
|
|
90
107
|
const filePath = path_1.default.join(outputPath, file.name);
|
|
108
|
+
(0, utils_1.assert)(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
|
|
91
109
|
const bundleFile = await (0, util_1.promisify)(compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem))(filePath);
|
|
92
110
|
(0, utils_1.assert)(bundleFile);
|
|
93
111
|
const bundleContent = bundleFile.toString();
|
|
112
|
+
let exports;
|
|
94
113
|
if (this.options.eval) {
|
|
95
|
-
|
|
114
|
+
try {
|
|
115
|
+
const output = await (0, node_1.useTemporaryFile)('snaps-bundle.js', bundleContent, async (path) => (0, node_1.evalBundle)(path));
|
|
116
|
+
__classPrivateFieldGet(this, _SnapsWebpackPlugin_spinner, "f")?.clear();
|
|
117
|
+
__classPrivateFieldGet(this, _SnapsWebpackPlugin_spinner, "f")?.frame();
|
|
118
|
+
(0, node_1.logInfo)(`${(0, chalk_1.blue)('ℹ')} ${(0, chalk_1.dim)('Snap bundle evaluated successfully.')}`);
|
|
119
|
+
exports = output.exports;
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
const webpackError = new webpack_1.WebpackError(`Failed to evaluate Snap bundle in SES. This is likely due to an incompatibility with the SES environment in your Snap: ${(0, snaps_sdk_1.getErrorMessage)(error)}`);
|
|
123
|
+
if (error instanceof node_1.SnapEvalError) {
|
|
124
|
+
// The constructor for `WebpackError` doesn't accept the details
|
|
125
|
+
// property, so we need to set it manually.
|
|
126
|
+
webpackError.details = error.output.stderr;
|
|
127
|
+
}
|
|
128
|
+
compilation.errors.push(webpackError);
|
|
129
|
+
}
|
|
96
130
|
}
|
|
97
131
|
if (this.options.manifestPath) {
|
|
98
132
|
const { reports } = await (0, node_1.checkManifest)(path_1.default.dirname(this.options.manifestPath), {
|
|
99
133
|
updateAndWriteManifest: this.options.writeManifest,
|
|
100
134
|
sourceCode: bundleContent,
|
|
135
|
+
exports,
|
|
136
|
+
handlerEndowments: snaps_rpc_methods_1.handlerEndowments,
|
|
101
137
|
writeFileFn: async (path, data) => {
|
|
138
|
+
(0, utils_1.assert)(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
|
|
102
139
|
return (0, manifest_1.writeManifest)(path, data, (0, util_1.promisify)(compiler.outputFileSystem.writeFile));
|
|
103
140
|
},
|
|
104
141
|
});
|
|
@@ -124,5 +161,6 @@ class SnapsWebpackPlugin {
|
|
|
124
161
|
});
|
|
125
162
|
}
|
|
126
163
|
}
|
|
164
|
+
_SnapsWebpackPlugin_spinner = new WeakMap();
|
|
127
165
|
exports.default = SnapsWebpackPlugin;
|
|
128
166
|
//# sourceMappingURL=plugin.cjs.map
|
package/dist/plugin.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.cjs","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;;;AAAA,mDAAsD;AACtD,qDAKoC;AAEpC,2CAAyC;AACzC,gDAA6B;AAC7B,+BAAiC;AAEjC,qCAAoD;AACpD,qDAA6D;AAE7D,6CAA2C;AAE3C,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAWzC,MAAqB,kBAAkB;IAGrC;;;;;;;;;;;;;OAaG;IACH,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port {\n checkManifest,\n evalBundle,\n postProcessBundle,\n useTemporaryFile,\n} from '@metamask/snaps-utils/node';\nimport type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils/node';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nimport { writeManifest } from './manifest';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n */\n constructor(options?: Partial<Options>) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n try {\n const processed = postProcessBundle(source, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool\n ? (sourceMap as SourceMap)\n : undefined,\n });\n\n if (processed.warnings.length > 0) {\n const webpackErrors = processed.warnings.map(\n (warning) => new WebpackError(warning),\n );\n\n compilation.warnings.push(...webpackErrors);\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n source,\n sourceMap as SourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with\n // Webpack's own `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\n } catch (error) {\n compilation.errors.push(\n new WebpackError(getErrorMessage(error)),\n );\n }\n });\n },\n );\n });\n\n compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file);\n\n assert(compilation.outputOptions.path);\n const outputPath = compilation.outputOptions.path;\n\n const filePath = pathUtils.join(outputPath, file.name);\n\n const bundleFile = await promisify(\n compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem),\n )(filePath);\n assert(bundleFile);\n\n const bundleContent = bundleFile.toString();\n\n if (this.options.eval) {\n await useTemporaryFile('snaps-bundle.js', bundleContent, (path) =>\n evalBundle(path),\n );\n }\n\n if (this.options.manifestPath) {\n const { reports } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n {\n updateAndWriteManifest: this.options.writeManifest,\n sourceCode: bundleContent,\n writeFileFn: async (path, data) => {\n return writeManifest(\n path,\n data,\n promisify(compiler.outputFileSystem.writeFile),\n );\n },\n },\n );\n\n const errors = reports\n .filter((report) => report.severity === 'error' && !report.wasFixed)\n .map((report) => report.message);\n const warnings = reports\n .filter((report) => report.severity === 'warning' && !report.wasFixed)\n .map((report) => report.message);\n const fixed = reports\n .filter((report) => report.wasFixed)\n .map((report) => report.message);\n\n if (errors.length > 0) {\n compilation.errors.push(\n ...errors.map((error) => new WebpackError(error)),\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n ...warnings.map((warning) => new WebpackError(warning)),\n );\n }\n\n if (fixed.length > 0) {\n compilation.warnings.push(\n ...fixed.map((problem) => new WebpackError(`${problem} (fixed)`)),\n );\n }\n }\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"plugin.cjs","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mEAAgE;AAChE,mDAAsD;AACtD,qDAOoC;AAEpC,2CAAyC;AACzC,iCAAkC;AAClC,gDAA6B;AAC7B,+BAAiC;AAEjC,qCAAoD;AACpD,qDAA6D;AAE7D,6CAA2C;AAE3C,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAiBzC,MAAqB,kBAAkB;IAKrC;;;;;;;;;;;;;;OAcG;IACH,YAAY,OAA0B,EAAE,OAAiB;QAjBhD,8CAA8B;QAkBrC,IAAI,CAAC,OAAO,GAAG;YACb,IAAI,EAAE,IAAI;YACV,YAAY,EAAE,cAAS,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,oBAAoB,CAAC;YACjE,aAAa,EAAE,IAAI;YACnB,GAAG,OAAO;SACX,CAAC;QAEF,uBAAA,IAAI,+BAAY,OAAO,MAAA,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,QAAkB;QACtB,MAAM,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC,OAAO,CAAC;QAErC,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,WAAW,EAAE,EAAE;YAC1D,WAAW,CAAC,KAAK,CAAC,aAAa,CAAC,GAAG,CACjC;gBACE,IAAI,EAAE,WAAW;gBACjB,KAAK,EAAE,qBAAW,CAAC,2CAA2C;gBAC9D,gBAAgB,EAAE,IAAI;aACvB,EACD,CAAC,MAAM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MAAM,GAAG,CAAC,EAAE,CAAC;oBACrB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,sBAAY,CAAC,GAAG,OAAO,UAAU,CAAC,CAAC,CAClE,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;;kBA1MoB,kBAAkB","sourcesContent":["import { handlerEndowments } from '@metamask/snaps-rpc-methods';\nimport { getErrorMessage } from '@metamask/snaps-sdk';\nimport {\n checkManifest,\n evalBundle,\n logInfo,\n postProcessBundle,\n SnapEvalError,\n useTemporaryFile,\n} from '@metamask/snaps-utils/node';\nimport type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils/node';\nimport { assert } from '@metamask/utils';\nimport { blue, dim } from 'chalk';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nimport { writeManifest } from './manifest';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\n// Partial copy of `ora` types to avoid a dependency on `ora` in the plugin.\ntype Spinner = {\n clear(): void;\n frame(): void;\n};\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n readonly #spinner: Spinner | undefined;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n * @param spinner - The spinner to use for logging. For internal use only.\n */\n constructor(options?: Partial<Options>, spinner?: Spinner) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n try {\n const processed = postProcessBundle(source, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool\n ? (sourceMap as SourceMap)\n : undefined,\n });\n\n if (processed.warnings.length > 0) {\n const webpackErrors = processed.warnings.map(\n (warning) => new WebpackError(warning),\n );\n\n compilation.warnings.push(...webpackErrors);\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n source,\n sourceMap as SourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with\n // Webpack's own `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\n } catch (error) {\n compilation.errors.push(\n new WebpackError(getErrorMessage(error)),\n );\n }\n });\n },\n );\n });\n\n compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file);\n\n assert(compilation.outputOptions.path);\n const outputPath = compilation.outputOptions.path;\n\n const filePath = pathUtils.join(outputPath, file.name);\n\n assert(\n compiler.outputFileSystem,\n 'Expected compiler to have an output file system.',\n );\n const bundleFile = await promisify(\n compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem),\n )(filePath);\n assert(bundleFile);\n\n const bundleContent = bundleFile.toString();\n let exports: string[] | undefined;\n\n if (this.options.eval) {\n try {\n const output = await useTemporaryFile(\n 'snaps-bundle.js',\n bundleContent,\n async (path) => evalBundle(path),\n );\n\n this.#spinner?.clear();\n this.#spinner?.frame();\n\n logInfo(\n `${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`,\n );\n\n exports = output.exports;\n } catch (error) {\n const webpackError = new WebpackError(\n `Failed to evaluate Snap bundle in SES. This is likely due to an incompatibility with the SES environment in your Snap: ${getErrorMessage(error)}`,\n );\n\n if (error instanceof SnapEvalError) {\n // The constructor for `WebpackError` doesn't accept the details\n // property, so we need to set it manually.\n webpackError.details = error.output.stderr;\n }\n\n compilation.errors.push(webpackError);\n }\n }\n\n if (this.options.manifestPath) {\n const { reports } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n {\n updateAndWriteManifest: this.options.writeManifest,\n sourceCode: bundleContent,\n exports,\n handlerEndowments,\n writeFileFn: async (path, data) => {\n assert(\n compiler.outputFileSystem,\n 'Expected compiler to have an output file system.',\n );\n return writeManifest(\n path,\n data,\n promisify(compiler.outputFileSystem.writeFile),\n );\n },\n },\n );\n\n const errors = reports\n .filter((report) => report.severity === 'error' && !report.wasFixed)\n .map((report) => report.message);\n const warnings = reports\n .filter((report) => report.severity === 'warning' && !report.wasFixed)\n .map((report) => report.message);\n const fixed = reports\n .filter((report) => report.wasFixed)\n .map((report) => report.message);\n\n if (errors.length > 0) {\n compilation.errors.push(\n ...errors.map((error) => new WebpackError(error)),\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n ...warnings.map((warning) => new WebpackError(warning)),\n );\n }\n\n if (fixed.length > 0) {\n compilation.warnings.push(\n ...fixed.map((problem) => new WebpackError(`${problem} (fixed)`)),\n );\n }\n }\n });\n }\n}\n"]}
|
package/dist/plugin.d.cts
CHANGED
|
@@ -6,7 +6,12 @@ type PluginOptions = {
|
|
|
6
6
|
writeManifest?: boolean;
|
|
7
7
|
};
|
|
8
8
|
export type Options = PluginOptions & Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;
|
|
9
|
+
type Spinner = {
|
|
10
|
+
clear(): void;
|
|
11
|
+
frame(): void;
|
|
12
|
+
};
|
|
9
13
|
export default class SnapsWebpackPlugin {
|
|
14
|
+
#private;
|
|
10
15
|
readonly options: Partial<Options>;
|
|
11
16
|
/**
|
|
12
17
|
* Construct an instance of the plugin.
|
|
@@ -21,8 +26,9 @@ export default class SnapsWebpackPlugin {
|
|
|
21
26
|
* `process.cwd() + '/snap.manifest.json'`.
|
|
22
27
|
* @param options.writeManifest - Whether to fix the manifest.
|
|
23
28
|
* Defaults to `true`.
|
|
29
|
+
* @param spinner - The spinner to use for logging. For internal use only.
|
|
24
30
|
*/
|
|
25
|
-
constructor(options?: Partial<Options
|
|
31
|
+
constructor(options?: Partial<Options>, spinner?: Spinner);
|
|
26
32
|
/**
|
|
27
33
|
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
28
34
|
* stage to process the bundle.
|
package/dist/plugin.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.cts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"plugin.d.cts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,kBAAkB,EAAa,mCAAmC;AAKhF,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB;AAQxC,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG,aAAa,GACjC,IAAI,CAAC,kBAAkB,EAAE,WAAW,GAAG,gBAAgB,CAAC,CAAC;AAG3D,KAAK,OAAO,GAAG;IACb,KAAK,IAAI,IAAI,CAAC;IACd,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,kBAAkB;;IACrC,SAAgB,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAI1C;;;;;;;;;;;;;;OAcG;gBACS,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO;IAWzD;;;;;OAKG;IACH,KAAK,CAAC,QAAQ,EAAE,QAAQ;CAqKzB"}
|
package/dist/plugin.d.mts
CHANGED
|
@@ -6,7 +6,12 @@ type PluginOptions = {
|
|
|
6
6
|
writeManifest?: boolean;
|
|
7
7
|
};
|
|
8
8
|
export type Options = PluginOptions & Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;
|
|
9
|
+
type Spinner = {
|
|
10
|
+
clear(): void;
|
|
11
|
+
frame(): void;
|
|
12
|
+
};
|
|
9
13
|
export default class SnapsWebpackPlugin {
|
|
14
|
+
#private;
|
|
10
15
|
readonly options: Partial<Options>;
|
|
11
16
|
/**
|
|
12
17
|
* Construct an instance of the plugin.
|
|
@@ -21,8 +26,9 @@ export default class SnapsWebpackPlugin {
|
|
|
21
26
|
* `process.cwd() + '/snap.manifest.json'`.
|
|
22
27
|
* @param options.writeManifest - Whether to fix the manifest.
|
|
23
28
|
* Defaults to `true`.
|
|
29
|
+
* @param spinner - The spinner to use for logging. For internal use only.
|
|
24
30
|
*/
|
|
25
|
-
constructor(options?: Partial<Options
|
|
31
|
+
constructor(options?: Partial<Options>, spinner?: Spinner);
|
|
26
32
|
/**
|
|
27
33
|
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
28
34
|
* stage to process the bundle.
|
package/dist/plugin.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.mts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"plugin.d.mts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,kBAAkB,EAAa,mCAAmC;AAKhF,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB;AAQxC,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG,aAAa,GACjC,IAAI,CAAC,kBAAkB,EAAE,WAAW,GAAG,gBAAgB,CAAC,CAAC;AAG3D,KAAK,OAAO,GAAG;IACb,KAAK,IAAI,IAAI,CAAC;IACd,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,kBAAkB;;IACrC,SAAgB,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAI1C;;;;;;;;;;;;;;OAcG;gBACS,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO;IAWzD;;;;;OAKG;IACH,KAAK,CAAC,QAAQ,EAAE,QAAQ;CAqKzB"}
|
package/dist/plugin.mjs
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _SnapsWebpackPlugin_spinner;
|
|
13
|
+
import { handlerEndowments } from "@metamask/snaps-rpc-methods";
|
|
1
14
|
import { getErrorMessage } from "@metamask/snaps-sdk";
|
|
2
|
-
import { checkManifest, evalBundle, postProcessBundle, useTemporaryFile } from "@metamask/snaps-utils/node";
|
|
15
|
+
import { checkManifest, evalBundle, logInfo, postProcessBundle, SnapEvalError, useTemporaryFile } from "@metamask/snaps-utils/node";
|
|
3
16
|
import { assert } from "@metamask/utils";
|
|
17
|
+
import $chalk from "chalk";
|
|
18
|
+
const { blue, dim } = $chalk;
|
|
4
19
|
import pathUtils from "path";
|
|
5
20
|
import { promisify } from "util";
|
|
6
21
|
import $webpack from "webpack";
|
|
@@ -9,7 +24,7 @@ import $webpacksources from "webpack-sources";
|
|
|
9
24
|
const { RawSource, SourceMapSource } = $webpacksources;
|
|
10
25
|
import { writeManifest } from "./manifest.mjs";
|
|
11
26
|
const PLUGIN_NAME = 'SnapsWebpackPlugin';
|
|
12
|
-
|
|
27
|
+
class SnapsWebpackPlugin {
|
|
13
28
|
/**
|
|
14
29
|
* Construct an instance of the plugin.
|
|
15
30
|
*
|
|
@@ -23,14 +38,17 @@ export default class SnapsWebpackPlugin {
|
|
|
23
38
|
* `process.cwd() + '/snap.manifest.json'`.
|
|
24
39
|
* @param options.writeManifest - Whether to fix the manifest.
|
|
25
40
|
* Defaults to `true`.
|
|
41
|
+
* @param spinner - The spinner to use for logging. For internal use only.
|
|
26
42
|
*/
|
|
27
|
-
constructor(options) {
|
|
43
|
+
constructor(options, spinner) {
|
|
44
|
+
_SnapsWebpackPlugin_spinner.set(this, void 0);
|
|
28
45
|
this.options = {
|
|
29
46
|
eval: true,
|
|
30
47
|
manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),
|
|
31
48
|
writeManifest: true,
|
|
32
49
|
...options,
|
|
33
50
|
};
|
|
51
|
+
__classPrivateFieldSet(this, _SnapsWebpackPlugin_spinner, spinner, "f");
|
|
34
52
|
}
|
|
35
53
|
/**
|
|
36
54
|
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
@@ -85,17 +103,37 @@ export default class SnapsWebpackPlugin {
|
|
|
85
103
|
assert(compilation.outputOptions.path);
|
|
86
104
|
const outputPath = compilation.outputOptions.path;
|
|
87
105
|
const filePath = pathUtils.join(outputPath, file.name);
|
|
106
|
+
assert(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
|
|
88
107
|
const bundleFile = await promisify(compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem))(filePath);
|
|
89
108
|
assert(bundleFile);
|
|
90
109
|
const bundleContent = bundleFile.toString();
|
|
110
|
+
let exports;
|
|
91
111
|
if (this.options.eval) {
|
|
92
|
-
|
|
112
|
+
try {
|
|
113
|
+
const output = await useTemporaryFile('snaps-bundle.js', bundleContent, async (path) => evalBundle(path));
|
|
114
|
+
__classPrivateFieldGet(this, _SnapsWebpackPlugin_spinner, "f")?.clear();
|
|
115
|
+
__classPrivateFieldGet(this, _SnapsWebpackPlugin_spinner, "f")?.frame();
|
|
116
|
+
logInfo(`${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`);
|
|
117
|
+
exports = output.exports;
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
const webpackError = new WebpackError(`Failed to evaluate Snap bundle in SES. This is likely due to an incompatibility with the SES environment in your Snap: ${getErrorMessage(error)}`);
|
|
121
|
+
if (error instanceof SnapEvalError) {
|
|
122
|
+
// The constructor for `WebpackError` doesn't accept the details
|
|
123
|
+
// property, so we need to set it manually.
|
|
124
|
+
webpackError.details = error.output.stderr;
|
|
125
|
+
}
|
|
126
|
+
compilation.errors.push(webpackError);
|
|
127
|
+
}
|
|
93
128
|
}
|
|
94
129
|
if (this.options.manifestPath) {
|
|
95
130
|
const { reports } = await checkManifest(pathUtils.dirname(this.options.manifestPath), {
|
|
96
131
|
updateAndWriteManifest: this.options.writeManifest,
|
|
97
132
|
sourceCode: bundleContent,
|
|
133
|
+
exports,
|
|
134
|
+
handlerEndowments,
|
|
98
135
|
writeFileFn: async (path, data) => {
|
|
136
|
+
assert(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
|
|
99
137
|
return writeManifest(path, data, promisify(compiler.outputFileSystem.writeFile));
|
|
100
138
|
},
|
|
101
139
|
});
|
|
@@ -121,4 +159,6 @@ export default class SnapsWebpackPlugin {
|
|
|
121
159
|
});
|
|
122
160
|
}
|
|
123
161
|
}
|
|
162
|
+
_SnapsWebpackPlugin_spinner = new WeakMap();
|
|
163
|
+
export default SnapsWebpackPlugin;
|
|
124
164
|
//# sourceMappingURL=plugin.mjs.map
|
package/dist/plugin.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.mjs","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,4BAA4B;AACtD,OAAO,EACL,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,gBAAgB,EACjB,mCAAmC;AAEpC,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,SAAS,aAAa;AAC7B,OAAO,EAAE,SAAS,EAAE,aAAa;;;;;AAKjC,OAAO,EAAE,aAAa,EAAE,uBAAmB;AAE3C,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAWzC,MAAM,CAAC,OAAO,OAAO,kBAAkB;IAGrC;;;;;;;;;;;;;OAaG;IACH,YAAY,OAA0B;QACpC,IAAI,CAAC,OAAO,G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AAC,QAAQ,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC;qBACrE,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBACnC,MAAM,KAAK,GAAG,OAAO;qBAClB,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC;qBACnC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBAEnC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACtB,WAAW,CAAC,MAAM,CAAC,IAAI,CACrB,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,KAAK,CAAC,CAAC,CAClD,CAAC;gBACJ,CAAC;gBAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACxB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC,CACxD,CAAC;gBACJ,CAAC;gBAED,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACrB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,GAAG,OAAO,UAAU,CAAC,CAAC,CAClE,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport {\n checkManifest,\n evalBundle,\n postProcessBundle,\n useTemporaryFile,\n} from '@metamask/snaps-utils/node';\nimport type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils/node';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nimport { writeManifest } from './manifest';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n */\n constructor(options?: Partial<Options>) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n try {\n const processed = postProcessBundle(source, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool\n ? (sourceMap as SourceMap)\n : undefined,\n });\n\n if (processed.warnings.length > 0) {\n const webpackErrors = processed.warnings.map(\n (warning) => new WebpackError(warning),\n );\n\n compilation.warnings.push(...webpackErrors);\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n source,\n sourceMap as SourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with\n // Webpack's own `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\n } catch (error) {\n compilation.errors.push(\n new WebpackError(getErrorMessage(error)),\n );\n }\n });\n },\n );\n });\n\n compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file);\n\n assert(compilation.outputOptions.path);\n const outputPath = compilation.outputOptions.path;\n\n const filePath = pathUtils.join(outputPath, file.name);\n\n const bundleFile = await promisify(\n compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem),\n )(filePath);\n assert(bundleFile);\n\n const bundleContent = bundleFile.toString();\n\n if (this.options.eval) {\n await useTemporaryFile('snaps-bundle.js', bundleContent, (path) =>\n evalBundle(path),\n );\n }\n\n if (this.options.manifestPath) {\n const { reports } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n {\n updateAndWriteManifest: this.options.writeManifest,\n sourceCode: bundleContent,\n writeFileFn: async (path, data) => {\n return writeManifest(\n path,\n data,\n promisify(compiler.outputFileSystem.writeFile),\n );\n },\n },\n );\n\n const errors = reports\n .filter((report) => report.severity === 'error' && !report.wasFixed)\n .map((report) => report.message);\n const warnings = reports\n .filter((report) => report.severity === 'warning' && !report.wasFixed)\n .map((report) => report.message);\n const fixed = reports\n .filter((report) => report.wasFixed)\n .map((report) => report.message);\n\n if (errors.length > 0) {\n compilation.errors.push(\n ...errors.map((error) => new WebpackError(error)),\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n ...warnings.map((warning) => new WebpackError(warning)),\n );\n }\n\n if (fixed.length > 0) {\n compilation.warnings.push(\n ...fixed.map((problem) => new WebpackError(`${problem} (fixed)`)),\n );\n }\n }\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"plugin.mjs","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,iBAAiB,EAAE,oCAAoC;AAChE,OAAO,EAAE,eAAe,EAAE,4BAA4B;AACtD,OAAO,EACL,aAAa,EACb,UAAU,EACV,OAAO,EACP,iBAAiB,EACjB,aAAa,EACb,gBAAgB,EACjB,mCAAmC;AAEpC,OAAO,EAAE,MAAM,EAAE,wBAAwB;;;AAEzC,OAAO,SAAS,aAAa;AAC7B,OAAO,EAAE,SAAS,EAAE,aAAa;;;;;AAKjC,OAAO,EAAE,aAAa,EAAE,uBAAmB;AAE3C,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAiBzC,MAAqB,kBAAkB;IAKrC;;;;;;;;;;;;;;OAcG;IACH,YAAY,OAA0B,EAAE,OAAiB;QAjBhD,8CAA8B;QAkBrC,IAAI,CAAC,OAAO,GAAG;YACb,IAAI,EAAE,IAAI;YACV,YAAY,EAAE,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,oBAAoB,CAAC;YACjE,aAAa,EAAE,IAAI;YACnB,GAAG,OAAO;SACX,CAAC;QAEF,uBAAA,IAAI,+BAAY,OAAO,MAAA,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,QAAkB;QACtB,MAAM,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC,OAAO,CAAC;QAErC,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,WAAW,EAAE,EAAE;YAC1D,WAAW,CAAC,KAAK,CAAC,aAAa,CAAC,GAAG,CACjC;gBACE,IAAI,EAAE,WAAW;gBACjB,KAAK,EAAE,WAAW,CAAC,2CAA2C;gBAC9D,gBAAgB,E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,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,GAAG,OAAO,UAAU,CAAC,CAAC,CAClE,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;;eA1MoB,kBAAkB","sourcesContent":["import { handlerEndowments } from '@metamask/snaps-rpc-methods';\nimport { getErrorMessage } from '@metamask/snaps-sdk';\nimport {\n checkManifest,\n evalBundle,\n logInfo,\n postProcessBundle,\n SnapEvalError,\n useTemporaryFile,\n} from '@metamask/snaps-utils/node';\nimport type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils/node';\nimport { assert } from '@metamask/utils';\nimport { blue, dim } from 'chalk';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nimport { writeManifest } from './manifest';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\n// Partial copy of `ora` types to avoid a dependency on `ora` in the plugin.\ntype Spinner = {\n clear(): void;\n frame(): void;\n};\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n readonly #spinner: Spinner | undefined;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n * @param spinner - The spinner to use for logging. For internal use only.\n */\n constructor(options?: Partial<Options>, spinner?: Spinner) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n try {\n const processed = postProcessBundle(source, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool\n ? (sourceMap as SourceMap)\n : undefined,\n });\n\n if (processed.warnings.length > 0) {\n const webpackErrors = processed.warnings.map(\n (warning) => new WebpackError(warning),\n );\n\n compilation.warnings.push(...webpackErrors);\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n source,\n sourceMap as SourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with\n // Webpack's own `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\n } catch (error) {\n compilation.errors.push(\n new WebpackError(getErrorMessage(error)),\n );\n }\n });\n },\n );\n });\n\n compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file);\n\n assert(compilation.outputOptions.path);\n const outputPath = compilation.outputOptions.path;\n\n const filePath = pathUtils.join(outputPath, file.name);\n\n assert(\n compiler.outputFileSystem,\n 'Expected compiler to have an output file system.',\n );\n const bundleFile = await promisify(\n compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem),\n )(filePath);\n assert(bundleFile);\n\n const bundleContent = bundleFile.toString();\n let exports: string[] | undefined;\n\n if (this.options.eval) {\n try {\n const output = await useTemporaryFile(\n 'snaps-bundle.js',\n bundleContent,\n async (path) => evalBundle(path),\n );\n\n this.#spinner?.clear();\n this.#spinner?.frame();\n\n logInfo(\n `${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`,\n );\n\n exports = output.exports;\n } catch (error) {\n const webpackError = new WebpackError(\n `Failed to evaluate Snap bundle in SES. This is likely due to an incompatibility with the SES environment in your Snap: ${getErrorMessage(error)}`,\n );\n\n if (error instanceof SnapEvalError) {\n // The constructor for `WebpackError` doesn't accept the details\n // property, so we need to set it manually.\n webpackError.details = error.output.stderr;\n }\n\n compilation.errors.push(webpackError);\n }\n }\n\n if (this.options.manifestPath) {\n const { reports } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n {\n updateAndWriteManifest: this.options.writeManifest,\n sourceCode: bundleContent,\n exports,\n handlerEndowments,\n writeFileFn: async (path, data) => {\n assert(\n compiler.outputFileSystem,\n 'Expected compiler to have an output file system.',\n );\n return writeManifest(\n path,\n data,\n promisify(compiler.outputFileSystem.writeFile),\n );\n },\n },\n );\n\n const errors = reports\n .filter((report) => report.severity === 'error' && !report.wasFixed)\n .map((report) => report.message);\n const warnings = reports\n .filter((report) => report.severity === 'warning' && !report.wasFixed)\n .map((report) => report.message);\n const fixed = reports\n .filter((report) => report.wasFixed)\n .map((report) => report.message);\n\n if (errors.length > 0) {\n compilation.errors.push(\n ...errors.map((error) => new WebpackError(error)),\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n ...warnings.map((warning) => new WebpackError(warning)),\n );\n }\n\n if (fixed.length > 0) {\n compilation.warnings.push(\n ...fixed.map((problem) => new WebpackError(`${problem} (fixed)`)),\n );\n }\n }\n });\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-webpack-plugin",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "A Webpack plugin to build MetaMask Snaps with Webpack",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -45,9 +45,9 @@
|
|
|
45
45
|
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn changelog:validate && yarn lint:dependencies",
|
|
46
46
|
"lint:ci": "yarn lint",
|
|
47
47
|
"lint:dependencies": "depcheck",
|
|
48
|
-
"lint:eslint": "eslint . --cache
|
|
48
|
+
"lint:eslint": "eslint . --cache",
|
|
49
49
|
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
|
|
50
|
-
"lint:misc": "prettier --no-error-on-unmatched-pattern --
|
|
50
|
+
"lint:misc": "prettier --no-error-on-unmatched-pattern --log-level warn \"**/*.json\" \"**/*.md\" \"**/*.html\" \"!CHANGELOG.md\" --ignore-path ../../.gitignore",
|
|
51
51
|
"publish:preview": "yarn npm publish --tag preview",
|
|
52
52
|
"since-latest-release": "../../scripts/since-latest-release.sh",
|
|
53
53
|
"test": "jest --reporters=jest-silent-reporter",
|
|
@@ -57,43 +57,31 @@
|
|
|
57
57
|
"test:watch": "jest --watch"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@metamask/snaps-
|
|
61
|
-
"@metamask/snaps-
|
|
62
|
-
"@metamask/utils": "^
|
|
63
|
-
"
|
|
60
|
+
"@metamask/snaps-rpc-methods": "^12.4.0",
|
|
61
|
+
"@metamask/snaps-sdk": "^7.1.0",
|
|
62
|
+
"@metamask/snaps-utils": "^9.4.0",
|
|
63
|
+
"@metamask/utils": "^11.4.0",
|
|
64
|
+
"chalk": "^4.1.2",
|
|
64
65
|
"webpack-sources": "^3.2.3"
|
|
65
66
|
},
|
|
66
67
|
"devDependencies": {
|
|
67
|
-
"@lavamoat/allow-scripts": "^3.
|
|
68
|
-
"@metamask/auto-changelog": "^
|
|
69
|
-
"@metamask/eslint-config": "^12.1.0",
|
|
70
|
-
"@metamask/eslint-config-jest": "^12.1.0",
|
|
71
|
-
"@metamask/eslint-config-nodejs": "^12.1.0",
|
|
72
|
-
"@metamask/eslint-config-typescript": "^12.1.0",
|
|
68
|
+
"@lavamoat/allow-scripts": "^3.3.3",
|
|
69
|
+
"@metamask/auto-changelog": "^5.0.2",
|
|
73
70
|
"@swc/core": "1.3.78",
|
|
74
71
|
"@swc/jest": "^0.2.26",
|
|
75
72
|
"@ts-bridge/cli": "^0.6.1",
|
|
76
73
|
"@types/jest": "^27.5.1",
|
|
77
74
|
"@types/webpack-sources": "^3.2.0",
|
|
78
|
-
"@typescript-eslint/eslint-plugin": "^5.42.1",
|
|
79
|
-
"@typescript-eslint/parser": "^6.21.0",
|
|
80
75
|
"deepmerge": "^4.2.2",
|
|
81
76
|
"depcheck": "^1.4.7",
|
|
82
|
-
"eslint": "^
|
|
83
|
-
"eslint-config-prettier": "^8.5.0",
|
|
84
|
-
"eslint-plugin-import": "^2.26.0",
|
|
85
|
-
"eslint-plugin-jest": "^27.1.5",
|
|
86
|
-
"eslint-plugin-jsdoc": "^41.1.2",
|
|
87
|
-
"eslint-plugin-n": "^15.7.0",
|
|
88
|
-
"eslint-plugin-prettier": "^4.2.1",
|
|
89
|
-
"eslint-plugin-promise": "^6.1.1",
|
|
77
|
+
"eslint": "^9.11.0",
|
|
90
78
|
"jest": "^29.0.2",
|
|
91
79
|
"jest-it-up": "^2.0.0",
|
|
92
80
|
"jest-silent-reporter": "^0.6.0",
|
|
93
81
|
"memfs": "^3.4.13",
|
|
94
|
-
"prettier
|
|
82
|
+
"prettier": "^3.3.3",
|
|
95
83
|
"typescript": "~5.3.3",
|
|
96
|
-
"webpack": "^5.
|
|
84
|
+
"webpack": "^5.97.1"
|
|
97
85
|
},
|
|
98
86
|
"engines": {
|
|
99
87
|
"node": "^18.16 || >=20"
|