@metamask/snaps-webpack-plugin 5.0.0 → 5.1.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.1.0]
11
+
12
+ ### Added
13
+
14
+ - Add support for eval in watch mode for Snaps CLI ([#3553](https://github.com/MetaMask/snaps/pull/3553))
15
+
16
+ ### Changed
17
+
18
+ - Bump @metamask/utils from `11.4.0` to `11.4.2` ([#3516](https://github.com/MetaMask/snaps/pull/3516), [#3526](https://github.com/MetaMask/snaps/pull/3526))
19
+
10
20
  ## [5.0.0]
11
21
 
12
22
  ### Added
@@ -138,7 +148,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
138
148
  - The version of the package no longer needs to match the version of all other
139
149
  MetaMask Snaps packages.
140
150
 
141
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@5.0.0...HEAD
151
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@5.1.0...HEAD
152
+ [5.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@5.0.0...@metamask/snaps-webpack-plugin@5.1.0
142
153
  [5.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.3.0...@metamask/snaps-webpack-plugin@5.0.0
143
154
  [4.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.2.1...@metamask/snaps-webpack-plugin@4.3.0
144
155
  [4.2.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.2.0...@metamask/snaps-webpack-plugin@4.2.1
package/dist/plugin.cjs CHANGED
@@ -123,6 +123,7 @@ class SnapsWebpackPlugin {
123
123
  sourceCode: bundleContent,
124
124
  exports,
125
125
  handlerEndowments: snaps_rpc_methods_1.handlerEndowments,
126
+ watchMode: compiler.watchMode,
126
127
  writeFileFn: async (path, data) => {
127
128
  (0, utils_1.assert)(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
128
129
  return (0, manifest_1.writeManifest)(path, data, (0, util_1.promisify)(compiler.outputFileSystem.writeFile));
@@ -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;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,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,EAAE,EAAE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AC,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 }\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C,CAAC,CAClE,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA3MD,qCA2MC","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 watchMode: compiler.watchMode,\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 +1 @@
1
- {"version":3,"file":"plugin.d.cts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,kBAAkB,EAAa,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AAO,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;CAsKzB"}
@@ -1 +1 @@
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"}
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;CAsKzB"}
package/dist/plugin.mjs CHANGED
@@ -121,6 +121,7 @@ export default class SnapsWebpackPlugin {
121
121
  sourceCode: bundleContent,
122
122
  exports,
123
123
  handlerEndowments,
124
+ watchMode: compiler.watchMode,
124
125
  writeFileFn: async (path, data) => {
125
126
  assert(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
126
127
  return writeManifest(path, data, promisify(compiler.outputFileSystem.writeFile));
@@ -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,OAAO,EAAE,aAAa,EAAE,uBAAmB;AAE3C,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAiBzC,M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,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 { 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;IACH,YAAY,OAA0B,EAAE,OAAiB;QACvD,IAAI,CAAC,OAAO,GAAG;YACb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AC,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 watchMode: compiler.watchMode,\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": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "A Webpack plugin to build MetaMask Snaps with Webpack",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -57,10 +57,10 @@
57
57
  "test:watch": "jest --watch"
58
58
  },
59
59
  "dependencies": {
60
- "@metamask/snaps-rpc-methods": "^13.2.0",
61
- "@metamask/snaps-sdk": "^9.0.0",
62
- "@metamask/snaps-utils": "^11.0.0",
63
- "@metamask/utils": "^11.4.0",
60
+ "@metamask/snaps-rpc-methods": "^13.5.0",
61
+ "@metamask/snaps-sdk": "^9.3.0",
62
+ "@metamask/snaps-utils": "^11.4.0",
63
+ "@metamask/utils": "^11.4.2",
64
64
  "chalk": "^4.1.2",
65
65
  "webpack-sources": "^3.2.3"
66
66
  },