@metamask/snaps-webpack-plugin 5.1.0 → 5.1.2

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,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [5.1.2]
11
+
12
+ ### Changed
13
+
14
+ - Bump `@metamask/snaps-utils` from `11.7.1` to `12.0.0` ([#3793](https://github.com/MetaMask/snaps/pull/3793))
15
+
16
+ ## [5.1.1]
17
+
18
+ ### Fixed
19
+
20
+ - Add `prettier` as a runtime dependency ([#3615](https://github.com/MetaMask/snaps/pull/3615))
21
+
10
22
  ## [5.1.0]
11
23
 
12
24
  ### Added
@@ -148,7 +160,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
148
160
  - The version of the package no longer needs to match the version of all other
149
161
  MetaMask Snaps packages.
150
162
 
151
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@5.1.0...HEAD
163
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@5.1.2...HEAD
164
+ [5.1.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@5.1.1...@metamask/snaps-webpack-plugin@5.1.2
165
+ [5.1.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@5.1.0...@metamask/snaps-webpack-plugin@5.1.1
152
166
  [5.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@5.0.0...@metamask/snaps-webpack-plugin@5.1.0
153
167
  [5.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.3.0...@metamask/snaps-webpack-plugin@5.0.0
154
168
  [4.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.2.1...@metamask/snaps-webpack-plugin@4.3.0
package/dist/plugin.cjs CHANGED
@@ -48,108 +48,121 @@ class SnapsWebpackPlugin {
48
48
  * @param compiler - The Webpack compiler.
49
49
  */
50
50
  apply(compiler) {
51
- const { devtool } = compiler.options;
52
51
  compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
53
52
  compilation.hooks.processAssets.tap({
54
53
  name: PLUGIN_NAME,
55
54
  stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,
56
- additionalAssets: true,
57
55
  }, (assets) => {
58
- Object.keys(assets)
59
- .filter((assetName) => assetName.endsWith('.js'))
60
- .forEach((assetName) => {
61
- const asset = assets[assetName];
62
- const source = asset.source();
63
- const sourceMap = asset.map();
64
- try {
65
- const processed = (0, node_1.postProcessBundle)(source, {
66
- ...this.options,
67
- sourceMap: Boolean(devtool),
68
- inputSourceMap: devtool
69
- ? sourceMap
70
- : undefined,
71
- });
72
- if (processed.warnings.length > 0) {
73
- const webpackErrors = processed.warnings.map((warning) => new webpack_1.WebpackError(warning));
74
- compilation.warnings.push(...webpackErrors);
75
- }
76
- const replacement = processed.sourceMap
77
- ? new webpack_sources_1.SourceMapSource(processed.code, assetName, processed.sourceMap, source, sourceMap)
78
- : new webpack_sources_1.RawSource(processed.code);
79
- // For some reason the type of `RawSource` is not compatible with
80
- // Webpack's own `Source`, but works fine when casting it to `any`.
81
- compilation.updateAsset(assetName, replacement);
82
- }
83
- catch (error) {
84
- compilation.errors.push(new webpack_1.WebpackError((0, snaps_sdk_1.getErrorMessage)(error)));
85
- }
86
- });
56
+ this.#applyPostProcessing(compiler, compilation, assets);
57
+ });
58
+ compilation.hooks.processAssets.tapPromise({
59
+ name: PLUGIN_NAME,
60
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ANALYSE,
61
+ }, async () => {
62
+ await this.#applyValidation(compiler, compilation);
87
63
  });
88
64
  });
89
- compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {
90
- const file = compilation
91
- .getAssets()
92
- .find((asset) => asset.name.endsWith('.js'));
93
- (0, utils_1.assert)(file);
94
- (0, utils_1.assert)(compilation.outputOptions.path);
95
- const outputPath = compilation.outputOptions.path;
96
- const filePath = path_1.default.join(outputPath, file.name);
97
- (0, utils_1.assert)(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
98
- const bundleFile = await (0, util_1.promisify)(compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem))(filePath);
99
- (0, utils_1.assert)(bundleFile);
100
- const bundleContent = bundleFile.toString();
101
- let exports;
102
- if (this.options.eval) {
103
- try {
104
- const output = await (0, node_1.useTemporaryFile)('snaps-bundle.js', bundleContent, async (path) => (0, node_1.evalBundle)(path));
105
- this.#spinner?.clear();
106
- this.#spinner?.frame();
107
- (0, node_1.logInfo)(`${(0, chalk_1.blue)('ℹ')} ${(0, chalk_1.dim)('Snap bundle evaluated successfully.')}`);
108
- exports = output.exports;
109
- }
110
- catch (error) {
111
- 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)}`);
112
- if (error instanceof node_1.SnapEvalError) {
113
- // The constructor for `WebpackError` doesn't accept the details
114
- // property, so we need to set it manually.
115
- webpackError.details = error.output.stderr;
116
- }
117
- compilation.errors.push(webpackError);
118
- }
119
- }
120
- if (this.options.manifestPath) {
121
- const { reports } = await (0, node_1.checkManifest)(path_1.default.dirname(this.options.manifestPath), {
122
- updateAndWriteManifest: this.options.writeManifest,
123
- sourceCode: bundleContent,
124
- exports,
125
- handlerEndowments: snaps_rpc_methods_1.handlerEndowments,
126
- watchMode: compiler.watchMode,
127
- writeFileFn: async (path, data) => {
128
- (0, utils_1.assert)(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
129
- return (0, manifest_1.writeManifest)(path, data, (0, util_1.promisify)(compiler.outputFileSystem.writeFile));
130
- },
65
+ }
66
+ /**
67
+ * Apply post-processing to the generated assets.
68
+ *
69
+ * @param compiler - The Webpack compiler.
70
+ * @param compilation - The Webpack compilation.
71
+ * @param assets - The Webpack assets.
72
+ */
73
+ #applyPostProcessing(compiler, compilation, assets) {
74
+ const { devtool } = compiler.options;
75
+ Object.keys(assets)
76
+ .filter((assetName) => assetName.endsWith('.js'))
77
+ .forEach((assetName) => {
78
+ const asset = assets[assetName];
79
+ const source = asset.source();
80
+ const sourceMap = asset.map();
81
+ try {
82
+ const processed = (0, node_1.postProcessBundle)(source, {
83
+ ...this.options,
84
+ sourceMap: Boolean(devtool),
85
+ inputSourceMap: devtool ? sourceMap : undefined,
131
86
  });
132
- const errors = reports
133
- .filter((report) => report.severity === 'error' && !report.wasFixed)
134
- .map((report) => report.message);
135
- const warnings = reports
136
- .filter((report) => report.severity === 'warning' && !report.wasFixed)
137
- .map((report) => report.message);
138
- const fixed = reports
139
- .filter((report) => report.wasFixed)
140
- .map((report) => report.message);
141
- if (errors.length > 0) {
142
- compilation.errors.push(...errors.map((error) => new webpack_1.WebpackError(error)));
143
- }
144
- if (warnings.length > 0) {
145
- compilation.warnings.push(...warnings.map((warning) => new webpack_1.WebpackError(warning)));
146
- }
147
- if (fixed.length > 0) {
148
- compilation.warnings.push(...fixed.map((problem) => new webpack_1.WebpackError(`${problem} (fixed)`)));
87
+ if (processed.warnings.length > 0) {
88
+ const webpackErrors = processed.warnings.map((warning) => new webpack_1.WebpackError(warning));
89
+ compilation.warnings.push(...webpackErrors);
149
90
  }
91
+ const replacement = processed.sourceMap
92
+ ? new webpack_sources_1.SourceMapSource(processed.code, assetName, processed.sourceMap, source, sourceMap)
93
+ : new webpack_sources_1.RawSource(processed.code);
94
+ // For some reason the type of `RawSource` is not compatible with
95
+ // Webpack's own `Source`, but works fine when casting it to `any`.
96
+ compilation.updateAsset(assetName, replacement);
97
+ }
98
+ catch (error) {
99
+ compilation.errors.push(new webpack_1.WebpackError((0, snaps_sdk_1.getErrorMessage)(error)));
150
100
  }
151
101
  });
152
102
  }
103
+ /**
104
+ * Apply validation to the generated bundle.
105
+ *
106
+ * @param compiler - The Webpack compiler.
107
+ * @param compilation - The Webpack compilation.
108
+ */
109
+ async #applyValidation(compiler, compilation) {
110
+ const file = compilation
111
+ .getAssets()
112
+ .find((asset) => asset.name.endsWith('.js'));
113
+ (0, utils_1.assert)(file, 'Expected to find a JavaScript asset in the compilation.');
114
+ const bundleContent = file.source.source().toString();
115
+ let exports;
116
+ if (this.options.eval) {
117
+ try {
118
+ const output = await (0, node_1.useTemporaryFile)('snaps-bundle.js', bundleContent, async (path) => (0, node_1.evalBundle)(path));
119
+ this.#spinner?.clear();
120
+ this.#spinner?.frame();
121
+ (0, node_1.logInfo)(`${(0, chalk_1.blue)('ℹ')} ${(0, chalk_1.dim)('Snap bundle evaluated successfully.')}`);
122
+ exports = output.exports;
123
+ }
124
+ catch (error) {
125
+ 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)}`);
126
+ if (error instanceof node_1.SnapEvalError) {
127
+ // The constructor for `WebpackError` doesn't accept the details
128
+ // property, so we need to set it manually.
129
+ webpackError.details = error.output.stderr;
130
+ }
131
+ compilation.errors.push(webpackError);
132
+ }
133
+ }
134
+ if (this.options.manifestPath) {
135
+ const { reports } = await (0, node_1.checkManifest)(this.options.manifestPath, {
136
+ updateAndWriteManifest: this.options.writeManifest,
137
+ sourceCode: bundleContent,
138
+ exports,
139
+ handlerEndowments: snaps_rpc_methods_1.handlerEndowments,
140
+ watchMode: compiler.watchMode,
141
+ writeFileFn: async (path, data) => {
142
+ (0, utils_1.assert)(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
143
+ return (0, manifest_1.writeManifest)(path, data, (0, util_1.promisify)(compiler.outputFileSystem.writeFile));
144
+ },
145
+ });
146
+ const errors = reports
147
+ .filter((report) => report.severity === 'error' && !report.wasFixed)
148
+ .map((report) => report.message);
149
+ const warnings = reports
150
+ .filter((report) => report.severity === 'warning' && !report.wasFixed)
151
+ .map((report) => report.message);
152
+ const fixed = reports
153
+ .filter((report) => report.wasFixed)
154
+ .map((report) => report.message);
155
+ if (errors.length > 0) {
156
+ compilation.errors.push(...errors.map((error) => new webpack_1.WebpackError(error)));
157
+ }
158
+ if (warnings.length > 0) {
159
+ compilation.warnings.push(...warnings.map((warning) => new webpack_1.WebpackError(warning)));
160
+ }
161
+ if (fixed.length > 0) {
162
+ compilation.warnings.push(...fixed.map((problem) => new webpack_1.WebpackError(`${problem} (fixed)`)));
163
+ }
164
+ }
165
+ }
153
166
  }
154
167
  exports.default = SnapsWebpackPlugin;
155
168
  //# 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;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,CAA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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;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
+ {"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,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;aAC/D,EACD,CAAC,MAAM,EAAE,EAAE;gBACT,IAAI,CAAC,oBAAoB,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vB,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,sBAAY,CAAC,GAAG,OAAO,UAAU,CAAC,CAAC,CAClE,CAAC;YACJ,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAlND,qCAkNC","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 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 },\n (assets) => {\n this.#applyPostProcessing(compiler, compilation, assets);\n },\n );\n\n compilation.hooks.processAssets.tapPromise(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_ANALYSE,\n },\n async () => {\n await this.#applyValidation(compiler, compilation);\n },\n );\n });\n }\n\n /**\n * Apply post-processing to the generated assets.\n *\n * @param compiler - The Webpack compiler.\n * @param compilation - The Webpack compilation.\n * @param assets - The Webpack assets.\n */\n #applyPostProcessing(\n compiler: Compiler,\n compilation: Compilation,\n assets: Compilation['assets'],\n ) {\n const { devtool } = compiler.options;\n\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 ? (sourceMap as SourceMap) : 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(new WebpackError(getErrorMessage(error)));\n }\n });\n }\n\n /**\n * Apply validation to the generated bundle.\n *\n * @param compiler - The Webpack compiler.\n * @param compilation - The Webpack compilation.\n */\n async #applyValidation(compiler: Compiler, compilation: Compilation) {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file, 'Expected to find a JavaScript asset in the compilation.');\n\n const bundleContent = file.source.source().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(`${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`);\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(this.options.manifestPath, {\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 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"]}
@@ -1 +1 @@
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;CAsKzB"}
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,GA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,"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;CA6KzB"}
package/dist/plugin.mjs CHANGED
@@ -46,107 +46,120 @@ export default class SnapsWebpackPlugin {
46
46
  * @param compiler - The Webpack compiler.
47
47
  */
48
48
  apply(compiler) {
49
- const { devtool } = compiler.options;
50
49
  compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
51
50
  compilation.hooks.processAssets.tap({
52
51
  name: PLUGIN_NAME,
53
52
  stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,
54
- additionalAssets: true,
55
53
  }, (assets) => {
56
- Object.keys(assets)
57
- .filter((assetName) => assetName.endsWith('.js'))
58
- .forEach((assetName) => {
59
- const asset = assets[assetName];
60
- const source = asset.source();
61
- const sourceMap = asset.map();
62
- try {
63
- const processed = postProcessBundle(source, {
64
- ...this.options,
65
- sourceMap: Boolean(devtool),
66
- inputSourceMap: devtool
67
- ? sourceMap
68
- : undefined,
69
- });
70
- if (processed.warnings.length > 0) {
71
- const webpackErrors = processed.warnings.map((warning) => new WebpackError(warning));
72
- compilation.warnings.push(...webpackErrors);
73
- }
74
- const replacement = processed.sourceMap
75
- ? new SourceMapSource(processed.code, assetName, processed.sourceMap, source, sourceMap)
76
- : new RawSource(processed.code);
77
- // For some reason the type of `RawSource` is not compatible with
78
- // Webpack's own `Source`, but works fine when casting it to `any`.
79
- compilation.updateAsset(assetName, replacement);
80
- }
81
- catch (error) {
82
- compilation.errors.push(new WebpackError(getErrorMessage(error)));
83
- }
84
- });
54
+ this.#applyPostProcessing(compiler, compilation, assets);
55
+ });
56
+ compilation.hooks.processAssets.tapPromise({
57
+ name: PLUGIN_NAME,
58
+ stage: Compilation.PROCESS_ASSETS_STAGE_ANALYSE,
59
+ }, async () => {
60
+ await this.#applyValidation(compiler, compilation);
85
61
  });
86
62
  });
87
- compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {
88
- const file = compilation
89
- .getAssets()
90
- .find((asset) => asset.name.endsWith('.js'));
91
- assert(file);
92
- assert(compilation.outputOptions.path);
93
- const outputPath = compilation.outputOptions.path;
94
- const filePath = pathUtils.join(outputPath, file.name);
95
- assert(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
96
- const bundleFile = await promisify(compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem))(filePath);
97
- assert(bundleFile);
98
- const bundleContent = bundleFile.toString();
99
- let exports;
100
- if (this.options.eval) {
101
- try {
102
- const output = await useTemporaryFile('snaps-bundle.js', bundleContent, async (path) => evalBundle(path));
103
- this.#spinner?.clear();
104
- this.#spinner?.frame();
105
- logInfo(`${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`);
106
- exports = output.exports;
107
- }
108
- catch (error) {
109
- 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)}`);
110
- if (error instanceof SnapEvalError) {
111
- // The constructor for `WebpackError` doesn't accept the details
112
- // property, so we need to set it manually.
113
- webpackError.details = error.output.stderr;
114
- }
115
- compilation.errors.push(webpackError);
116
- }
117
- }
118
- if (this.options.manifestPath) {
119
- const { reports } = await checkManifest(pathUtils.dirname(this.options.manifestPath), {
120
- updateAndWriteManifest: this.options.writeManifest,
121
- sourceCode: bundleContent,
122
- exports,
123
- handlerEndowments,
124
- watchMode: compiler.watchMode,
125
- writeFileFn: async (path, data) => {
126
- assert(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
127
- return writeManifest(path, data, promisify(compiler.outputFileSystem.writeFile));
128
- },
63
+ }
64
+ /**
65
+ * Apply post-processing to the generated assets.
66
+ *
67
+ * @param compiler - The Webpack compiler.
68
+ * @param compilation - The Webpack compilation.
69
+ * @param assets - The Webpack assets.
70
+ */
71
+ #applyPostProcessing(compiler, compilation, assets) {
72
+ const { devtool } = compiler.options;
73
+ Object.keys(assets)
74
+ .filter((assetName) => assetName.endsWith('.js'))
75
+ .forEach((assetName) => {
76
+ const asset = assets[assetName];
77
+ const source = asset.source();
78
+ const sourceMap = asset.map();
79
+ try {
80
+ const processed = postProcessBundle(source, {
81
+ ...this.options,
82
+ sourceMap: Boolean(devtool),
83
+ inputSourceMap: devtool ? sourceMap : undefined,
129
84
  });
130
- const errors = reports
131
- .filter((report) => report.severity === 'error' && !report.wasFixed)
132
- .map((report) => report.message);
133
- const warnings = reports
134
- .filter((report) => report.severity === 'warning' && !report.wasFixed)
135
- .map((report) => report.message);
136
- const fixed = reports
137
- .filter((report) => report.wasFixed)
138
- .map((report) => report.message);
139
- if (errors.length > 0) {
140
- compilation.errors.push(...errors.map((error) => new WebpackError(error)));
141
- }
142
- if (warnings.length > 0) {
143
- compilation.warnings.push(...warnings.map((warning) => new WebpackError(warning)));
144
- }
145
- if (fixed.length > 0) {
146
- compilation.warnings.push(...fixed.map((problem) => new WebpackError(`${problem} (fixed)`)));
85
+ if (processed.warnings.length > 0) {
86
+ const webpackErrors = processed.warnings.map((warning) => new WebpackError(warning));
87
+ compilation.warnings.push(...webpackErrors);
147
88
  }
89
+ const replacement = processed.sourceMap
90
+ ? new SourceMapSource(processed.code, assetName, processed.sourceMap, source, sourceMap)
91
+ : new RawSource(processed.code);
92
+ // For some reason the type of `RawSource` is not compatible with
93
+ // Webpack's own `Source`, but works fine when casting it to `any`.
94
+ compilation.updateAsset(assetName, replacement);
95
+ }
96
+ catch (error) {
97
+ compilation.errors.push(new WebpackError(getErrorMessage(error)));
148
98
  }
149
99
  });
150
100
  }
101
+ /**
102
+ * Apply validation to the generated bundle.
103
+ *
104
+ * @param compiler - The Webpack compiler.
105
+ * @param compilation - The Webpack compilation.
106
+ */
107
+ async #applyValidation(compiler, compilation) {
108
+ const file = compilation
109
+ .getAssets()
110
+ .find((asset) => asset.name.endsWith('.js'));
111
+ assert(file, 'Expected to find a JavaScript asset in the compilation.');
112
+ const bundleContent = file.source.source().toString();
113
+ let exports;
114
+ if (this.options.eval) {
115
+ try {
116
+ const output = await useTemporaryFile('snaps-bundle.js', bundleContent, async (path) => evalBundle(path));
117
+ this.#spinner?.clear();
118
+ this.#spinner?.frame();
119
+ logInfo(`${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`);
120
+ exports = output.exports;
121
+ }
122
+ catch (error) {
123
+ 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)}`);
124
+ if (error instanceof SnapEvalError) {
125
+ // The constructor for `WebpackError` doesn't accept the details
126
+ // property, so we need to set it manually.
127
+ webpackError.details = error.output.stderr;
128
+ }
129
+ compilation.errors.push(webpackError);
130
+ }
131
+ }
132
+ if (this.options.manifestPath) {
133
+ const { reports } = await checkManifest(this.options.manifestPath, {
134
+ updateAndWriteManifest: this.options.writeManifest,
135
+ sourceCode: bundleContent,
136
+ exports,
137
+ handlerEndowments,
138
+ watchMode: compiler.watchMode,
139
+ writeFileFn: async (path, data) => {
140
+ assert(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
141
+ return writeManifest(path, data, promisify(compiler.outputFileSystem.writeFile));
142
+ },
143
+ });
144
+ const errors = reports
145
+ .filter((report) => report.severity === 'error' && !report.wasFixed)
146
+ .map((report) => report.message);
147
+ const warnings = reports
148
+ .filter((report) => report.severity === 'warning' && !report.wasFixed)
149
+ .map((report) => report.message);
150
+ const fixed = reports
151
+ .filter((report) => report.wasFixed)
152
+ .map((report) => report.message);
153
+ if (errors.length > 0) {
154
+ compilation.errors.push(...errors.map((error) => new WebpackError(error)));
155
+ }
156
+ if (warnings.length > 0) {
157
+ compilation.warnings.push(...warnings.map((warning) => new WebpackError(warning)));
158
+ }
159
+ if (fixed.length > 0) {
160
+ compilation.warnings.push(...fixed.map((problem) => new WebpackError(`${problem} (fixed)`)));
161
+ }
162
+ }
163
+ }
151
164
  }
152
165
  //# 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,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;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,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 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
+ {"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,IAAI,EAAE,IAAI;YACV,YAAY,EAAE,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,oBAAoB,CAAC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ED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACxB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC,CACxD,CAAC;YACJ,CAAC;YAED,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACrB,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;YACJ,CAAC;QACH,CAAC;IACH,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 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 },\n (assets) => {\n this.#applyPostProcessing(compiler, compilation, assets);\n },\n );\n\n compilation.hooks.processAssets.tapPromise(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_ANALYSE,\n },\n async () => {\n await this.#applyValidation(compiler, compilation);\n },\n );\n });\n }\n\n /**\n * Apply post-processing to the generated assets.\n *\n * @param compiler - The Webpack compiler.\n * @param compilation - The Webpack compilation.\n * @param assets - The Webpack assets.\n */\n #applyPostProcessing(\n compiler: Compiler,\n compilation: Compilation,\n assets: Compilation['assets'],\n ) {\n const { devtool } = compiler.options;\n\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 ? (sourceMap as SourceMap) : 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(new WebpackError(getErrorMessage(error)));\n }\n });\n }\n\n /**\n * Apply validation to the generated bundle.\n *\n * @param compiler - The Webpack compiler.\n * @param compilation - The Webpack compilation.\n */\n async #applyValidation(compiler: Compiler, compilation: Compilation) {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file, 'Expected to find a JavaScript asset in the compilation.');\n\n const bundleContent = file.source.source().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(`${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`);\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(this.options.manifestPath, {\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 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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-webpack-plugin",
3
- "version": "5.1.0",
3
+ "version": "5.1.2",
4
4
  "description": "A Webpack plugin to build MetaMask Snaps with Webpack",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -57,15 +57,16 @@
57
57
  "test:watch": "jest --watch"
58
58
  },
59
59
  "dependencies": {
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",
60
+ "@metamask/snaps-rpc-methods": "^14.2.0",
61
+ "@metamask/snaps-sdk": "^10.4.0",
62
+ "@metamask/snaps-utils": "^12.0.0",
63
+ "@metamask/utils": "^11.9.0",
64
64
  "chalk": "^4.1.2",
65
+ "prettier": "^3.3.3",
65
66
  "webpack-sources": "^3.2.3"
66
67
  },
67
68
  "devDependencies": {
68
- "@lavamoat/allow-scripts": "^3.3.4",
69
+ "@lavamoat/allow-scripts": "^3.4.2",
69
70
  "@metamask/auto-changelog": "^5.0.2",
70
71
  "@swc/core": "1.11.31",
71
72
  "@swc/jest": "^0.2.38",
@@ -79,7 +80,6 @@
79
80
  "jest-it-up": "^2.0.0",
80
81
  "jest-silent-reporter": "^0.6.0",
81
82
  "memfs": "^3.4.13",
82
- "prettier": "^3.3.3",
83
83
  "typescript": "~5.3.3",
84
84
  "webpack": "^5.97.1"
85
85
  },