@metamask/snaps-webpack-plugin 4.3.0 → 5.0.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 CHANGED
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [5.0.0]
11
+
12
+ ### Added
13
+
14
+ - Support scheduling cronjobs with an ISO 8601 duration ([#3421](https://github.com/MetaMask/snaps/pull/3421))
15
+
16
+ ### Changed
17
+
18
+ - **BREAKING:** Drop support for Node.js 18 and 21 ([#3447](https://github.com/MetaMask/snaps/pull/3447))
19
+
10
20
  ## [4.3.0]
11
21
 
12
22
  ### Changed
@@ -128,7 +138,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
128
138
  - The version of the package no longer needs to match the version of all other
129
139
  MetaMask Snaps packages.
130
140
 
131
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.3.0...HEAD
141
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@5.0.0...HEAD
142
+ [5.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.3.0...@metamask/snaps-webpack-plugin@5.0.0
132
143
  [4.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.2.1...@metamask/snaps-webpack-plugin@4.3.0
133
144
  [4.2.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.2.0...@metamask/snaps-webpack-plugin@4.2.1
134
145
  [4.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.1.2...@metamask/snaps-webpack-plugin@4.2.0
package/dist/plugin.cjs CHANGED
@@ -1,19 +1,7 @@
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
- };
13
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
4
  };
16
- var _SnapsWebpackPlugin_spinner;
17
5
  Object.defineProperty(exports, "__esModule", { value: true });
18
6
  const snaps_rpc_methods_1 = require("@metamask/snaps-rpc-methods");
19
7
  const snaps_sdk_1 = require("@metamask/snaps-sdk");
@@ -27,6 +15,8 @@ const webpack_sources_1 = require("webpack-sources");
27
15
  const manifest_1 = require("./manifest.cjs");
28
16
  const PLUGIN_NAME = 'SnapsWebpackPlugin';
29
17
  class SnapsWebpackPlugin {
18
+ options;
19
+ #spinner;
30
20
  /**
31
21
  * Construct an instance of the plugin.
32
22
  *
@@ -43,14 +33,13 @@ class SnapsWebpackPlugin {
43
33
  * @param spinner - The spinner to use for logging. For internal use only.
44
34
  */
45
35
  constructor(options, spinner) {
46
- _SnapsWebpackPlugin_spinner.set(this, void 0);
47
36
  this.options = {
48
37
  eval: true,
49
38
  manifestPath: path_1.default.join(process.cwd(), 'snap.manifest.json'),
50
39
  writeManifest: true,
51
40
  ...options,
52
41
  };
53
- __classPrivateFieldSet(this, _SnapsWebpackPlugin_spinner, spinner, "f");
42
+ this.#spinner = spinner;
54
43
  }
55
44
  /**
56
45
  * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
@@ -113,8 +102,8 @@ class SnapsWebpackPlugin {
113
102
  if (this.options.eval) {
114
103
  try {
115
104
  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();
105
+ this.#spinner?.clear();
106
+ this.#spinner?.frame();
118
107
  (0, node_1.logInfo)(`${(0, chalk_1.blue)('ℹ')} ${(0, chalk_1.dim)('Snap bundle evaluated successfully.')}`);
119
108
  exports = output.exports;
120
109
  }
@@ -161,6 +150,5 @@ class SnapsWebpackPlugin {
161
150
  });
162
151
  }
163
152
  }
164
- _SnapsWebpackPlugin_spinner = new WeakMap();
165
153
  exports.default = SnapsWebpackPlugin;
166
154
  //# sourceMappingURL=plugin.cjs.map
@@ -1 +1 @@
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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,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,sBAAY,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,sBAAY,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,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"]}
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;IACrB,OAAO,CAAmB;IAEjC,QAAQ,CAAsB;IAEvC;;;;;;;;;;;;;;OAcG;IACH,YAAY,OAA0B,EAAE,OAAiB;QACvD,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,IAAI,CAAC,QAAQ,GAAG,OAAO,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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C,IAAI,CACvB,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,sBAAY,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,sBAAY,CAAC,GAAG,OAAO,UAAU,CAAC,CAAC,CAClE,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA1MD,qCA0MC","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.mjs CHANGED
@@ -1,15 +1,3 @@
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
1
  import { handlerEndowments } from "@metamask/snaps-rpc-methods";
14
2
  import { getErrorMessage } from "@metamask/snaps-sdk";
15
3
  import { checkManifest, evalBundle, logInfo, postProcessBundle, SnapEvalError, useTemporaryFile } from "@metamask/snaps-utils/node";
@@ -24,7 +12,9 @@ import $webpacksources from "webpack-sources";
24
12
  const { RawSource, SourceMapSource } = $webpacksources;
25
13
  import { writeManifest } from "./manifest.mjs";
26
14
  const PLUGIN_NAME = 'SnapsWebpackPlugin';
27
- class SnapsWebpackPlugin {
15
+ export default class SnapsWebpackPlugin {
16
+ options;
17
+ #spinner;
28
18
  /**
29
19
  * Construct an instance of the plugin.
30
20
  *
@@ -41,14 +31,13 @@ class SnapsWebpackPlugin {
41
31
  * @param spinner - The spinner to use for logging. For internal use only.
42
32
  */
43
33
  constructor(options, spinner) {
44
- _SnapsWebpackPlugin_spinner.set(this, void 0);
45
34
  this.options = {
46
35
  eval: true,
47
36
  manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),
48
37
  writeManifest: true,
49
38
  ...options,
50
39
  };
51
- __classPrivateFieldSet(this, _SnapsWebpackPlugin_spinner, spinner, "f");
40
+ this.#spinner = spinner;
52
41
  }
53
42
  /**
54
43
  * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
@@ -111,8 +100,8 @@ class SnapsWebpackPlugin {
111
100
  if (this.options.eval) {
112
101
  try {
113
102
  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();
103
+ this.#spinner?.clear();
104
+ this.#spinner?.frame();
116
105
  logInfo(`${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`);
117
106
  exports = output.exports;
118
107
  }
@@ -159,6 +148,4 @@ class SnapsWebpackPlugin {
159
148
  });
160
149
  }
161
150
  }
162
- _SnapsWebpackPlugin_spinner = new WeakMap();
163
- export default SnapsWebpackPlugin;
164
151
  //# sourceMappingURL=plugin.mjs.map
@@ -1 +1 @@
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,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ACnC,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;;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"]}
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,MAAM,CAAC,OAAO,OAAO,kBAAkB;IACrB,OAAO,CAAmB;IAEjC,QAAQ,CAAsB;IAEvC;;;;;;;;;;;;;;OAcG;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AAC,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 { 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.0",
3
+ "version": "5.0.0",
4
4
  "description": "A Webpack plugin to build MetaMask Snaps with Webpack",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -57,18 +57,18 @@
57
57
  "test:watch": "jest --watch"
58
58
  },
59
59
  "dependencies": {
60
- "@metamask/snaps-rpc-methods": "^12.4.0",
61
- "@metamask/snaps-sdk": "^7.1.0",
62
- "@metamask/snaps-utils": "^9.4.0",
60
+ "@metamask/snaps-rpc-methods": "^13.2.0",
61
+ "@metamask/snaps-sdk": "^9.0.0",
62
+ "@metamask/snaps-utils": "^11.0.0",
63
63
  "@metamask/utils": "^11.4.0",
64
64
  "chalk": "^4.1.2",
65
65
  "webpack-sources": "^3.2.3"
66
66
  },
67
67
  "devDependencies": {
68
- "@lavamoat/allow-scripts": "^3.3.3",
68
+ "@lavamoat/allow-scripts": "^3.3.4",
69
69
  "@metamask/auto-changelog": "^5.0.2",
70
- "@swc/core": "1.3.78",
71
- "@swc/jest": "^0.2.26",
70
+ "@swc/core": "1.11.31",
71
+ "@swc/jest": "^0.2.38",
72
72
  "@ts-bridge/cli": "^0.6.1",
73
73
  "@types/jest": "^27.5.1",
74
74
  "@types/webpack-sources": "^3.2.0",
@@ -84,7 +84,7 @@
84
84
  "webpack": "^5.97.1"
85
85
  },
86
86
  "engines": {
87
- "node": "^18.16 || >=20"
87
+ "node": "^20 || >=22"
88
88
  },
89
89
  "publishConfig": {
90
90
  "access": "public",