@metamask/snaps-webpack-plugin 2.0.0 → 3.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 +11 -1
- package/dist/cjs/plugin.js +15 -11
- package/dist/cjs/plugin.js.map +1 -1
- package/dist/esm/plugin.js +16 -12
- package/dist/esm/plugin.js.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [3.0.0]
|
|
10
|
+
### Changed
|
|
11
|
+
- **BREAKING:** Bump minimum Node.js version to `^18.16.0` ([#1741](https://github.com/MetaMask/snaps/pull/1741))
|
|
12
|
+
|
|
13
|
+
## [2.0.1]
|
|
14
|
+
### Changed
|
|
15
|
+
- Improve error messaging ([#1798](https://github.com/MetaMask/snaps/pull/1798))
|
|
16
|
+
|
|
9
17
|
## [2.0.0]
|
|
10
18
|
### Changed
|
|
11
19
|
- Initial stable release from main branch ([#1757](https://github.com/MetaMask/snaps/pull/1757))
|
|
@@ -24,7 +32,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
24
32
|
- The version of the package no longer needs to match the version of all other
|
|
25
33
|
MetaMask Snaps packages.
|
|
26
34
|
|
|
27
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@
|
|
35
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@3.0.0...HEAD
|
|
36
|
+
[3.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@2.0.1...@metamask/snaps-webpack-plugin@3.0.0
|
|
37
|
+
[2.0.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@2.0.0...@metamask/snaps-webpack-plugin@2.0.1
|
|
28
38
|
[2.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@0.37.4-flask.1...@metamask/snaps-webpack-plugin@2.0.0
|
|
29
39
|
[0.37.4-flask.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@0.37.3-flask.1...@metamask/snaps-webpack-plugin@0.37.4-flask.1
|
|
30
40
|
[0.37.3-flask.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@0.37.2-flask.1...@metamask/snaps-webpack-plugin@0.37.3-flask.1
|
package/dist/cjs/plugin.js
CHANGED
|
@@ -51,18 +51,22 @@ class SnapsWebpackPlugin {
|
|
|
51
51
|
const asset = assets[assetName];
|
|
52
52
|
const source = asset.source();
|
|
53
53
|
const sourceMap = asset.map();
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
54
|
+
try {
|
|
55
|
+
const processed = (0, _snapsutils.postProcessBundle)(source, {
|
|
56
|
+
...this.options,
|
|
57
|
+
sourceMap: Boolean(devtool),
|
|
58
|
+
inputSourceMap: devtool ? sourceMap : undefined
|
|
59
|
+
});
|
|
60
|
+
if (processed.warnings.length > 0) {
|
|
61
|
+
compilation.warnings.push(new _webpack.WebpackError(`${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\n${processed.warnings.join('\n')}`));
|
|
62
|
+
}
|
|
63
|
+
const replacement = processed.sourceMap ? new _webpacksources.SourceMapSource(processed.code, assetName, processed.sourceMap, source, sourceMap) : new _webpacksources.RawSource(processed.code);
|
|
64
|
+
// For some reason the type of `RawSource` is not compatible with
|
|
65
|
+
// Webpack's own `Source`, but works fine when casting it to `any`.
|
|
66
|
+
compilation.updateAsset(assetName, replacement);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
compilation.errors.push(new _webpack.WebpackError((0, _snapsutils.getErrorMessage)(error)));
|
|
61
69
|
}
|
|
62
|
-
const replacement = processed.sourceMap ? new _webpacksources.SourceMapSource(processed.code, assetName, processed.sourceMap, source, sourceMap) : new _webpacksources.RawSource(processed.code);
|
|
63
|
-
// For some reason the type of `RawSource` is not compatible with
|
|
64
|
-
// Webpack's own `Source`, but works fine when casting it to `any`.
|
|
65
|
-
compilation.updateAsset(assetName, replacement);
|
|
66
70
|
});
|
|
67
71
|
});
|
|
68
72
|
});
|
package/dist/cjs/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/plugin.ts"],"sourcesContent":["import type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils';\nimport {\n checkManifest,\n evalBundle,\n postProcessBundle,\n useTemporaryFile,\n} from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n */\n constructor(options?: Partial<Options>) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n const processed = postProcessBundle(source, {\n
|
|
1
|
+
{"version":3,"sources":["../../src/plugin.ts"],"sourcesContent":["import type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils';\nimport {\n checkManifest,\n evalBundle,\n getErrorMessage,\n postProcessBundle,\n useTemporaryFile,\n} from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n */\n constructor(options?: Partial<Options>) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n try {\n const processed = postProcessBundle(source, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool\n ? (sourceMap as SourceMap)\n : undefined,\n });\n\n if (processed.warnings.length > 0) {\n compilation.warnings.push(\n new WebpackError(\n `${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\\n${processed.warnings.join(\n '\\n',\n )}`,\n ),\n );\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n source,\n sourceMap as SourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with\n // Webpack's own `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\n } catch (error) {\n compilation.errors.push(\n new WebpackError(getErrorMessage(error)),\n );\n }\n });\n },\n );\n });\n\n compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file);\n\n assert(compilation.outputOptions.path);\n const outputPath = compilation.outputOptions.path;\n\n const filePath = pathUtils.join(outputPath, file.name);\n\n const bundleFile = await promisify(\n compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem),\n )(filePath);\n assert(bundleFile);\n\n const bundleContent = bundleFile.toString();\n\n if (this.options.eval) {\n await useTemporaryFile('snaps-bundle.js', bundleContent, (path) =>\n evalBundle(path),\n );\n }\n\n if (this.options.manifestPath) {\n const { errors, warnings } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n this.options.writeManifest,\n bundleContent,\n promisify(\n compiler.outputFileSystem.writeFile.bind(compiler.outputFileSystem),\n ),\n );\n\n if (!this.options.writeManifest && errors.length > 0) {\n throw new Error(\n `Manifest Error: The manifest is invalid.\\n${errors.join('\\n')}`,\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n new WebpackError(\n `${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\\n${warnings.join(\n '\\n',\n )}`,\n ),\n );\n }\n }\n });\n }\n}\n"],"names":["SnapsWebpackPlugin","PLUGIN_NAME","apply","compiler","devtool","options","hooks","compilation","tap","processAssets","name","stage","Compilation","PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY","additionalAssets","assets","Object","keys","filter","assetName","endsWith","forEach","asset","source","sourceMap","map","processed","postProcessBundle","Boolean","inputSourceMap","undefined","warnings","length","push","WebpackError","join","replacement","SourceMapSource","code","RawSource","updateAsset","error","errors","getErrorMessage","afterEmit","tapPromise","file","getAssets","find","assert","outputOptions","path","outputPath","filePath","pathUtils","bundleFile","promisify","outputFileSystem","readFile","bind","bundleContent","toString","eval","useTemporaryFile","evalBundle","manifestPath","checkManifest","dirname","writeManifest","writeFile","Error","constructor","process","cwd"],"mappings":";;;;;;;eA0BqBA;;;4BAnBd;uBACgB;6DACD;sBACI;yBAEgB;gCACC;;;;;;;;;;;;;;;;;;;AAE3C,MAAMC,cAAc;AAWL,MAAMD;IA0BnB;;;;;GAKC,GACDE,MAAMC,QAAkB,EAAE;QACxB,MAAM,EAAEC,OAAO,EAAE,GAAGD,SAASE,OAAO;QAEpCF,SAASG,KAAK,CAACC,WAAW,CAACC,GAAG,CAACP,aAAa,CAACM;YAC3CA,YAAYD,KAAK,CAACG,aAAa,CAACD,GA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;GAaC,GACDoC,YAAYlE,OAA0B,CAAE;QAhBxC,uBAAgBA,WAAhB,KAAA;QAiBE,IAAI,CAACA,OAAO,GAAG;YACbyD,MAAM;YACNG,cAAcX,aAAS,CAACnB,IAAI,CAACqC,QAAQC,GAAG,IAAI;YAC5CL,eAAe;YACf,GAAG/D,OAAO;QACZ;IACF;AAyHF"}
|
package/dist/esm/plugin.js
CHANGED
|
@@ -11,7 +11,7 @@ function _define_property(obj, key, value) {
|
|
|
11
11
|
}
|
|
12
12
|
return obj;
|
|
13
13
|
}
|
|
14
|
-
import { checkManifest, evalBundle, postProcessBundle, useTemporaryFile } from '@metamask/snaps-utils';
|
|
14
|
+
import { checkManifest, evalBundle, getErrorMessage, postProcessBundle, useTemporaryFile } from '@metamask/snaps-utils';
|
|
15
15
|
import { assert } from '@metamask/utils';
|
|
16
16
|
import pathUtils from 'path';
|
|
17
17
|
import { promisify } from 'util';
|
|
@@ -36,18 +36,22 @@ class SnapsWebpackPlugin {
|
|
|
36
36
|
const asset = assets[assetName];
|
|
37
37
|
const source = asset.source();
|
|
38
38
|
const sourceMap = asset.map();
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
try {
|
|
40
|
+
const processed = postProcessBundle(source, {
|
|
41
|
+
...this.options,
|
|
42
|
+
sourceMap: Boolean(devtool),
|
|
43
|
+
inputSourceMap: devtool ? sourceMap : undefined
|
|
44
|
+
});
|
|
45
|
+
if (processed.warnings.length > 0) {
|
|
46
|
+
compilation.warnings.push(new WebpackError(`${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\n${processed.warnings.join('\n')}`));
|
|
47
|
+
}
|
|
48
|
+
const replacement = processed.sourceMap ? new SourceMapSource(processed.code, assetName, processed.sourceMap, source, sourceMap) : new RawSource(processed.code);
|
|
49
|
+
// For some reason the type of `RawSource` is not compatible with
|
|
50
|
+
// Webpack's own `Source`, but works fine when casting it to `any`.
|
|
51
|
+
compilation.updateAsset(assetName, replacement);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
compilation.errors.push(new WebpackError(getErrorMessage(error)));
|
|
46
54
|
}
|
|
47
|
-
const replacement = processed.sourceMap ? new SourceMapSource(processed.code, assetName, processed.sourceMap, source, sourceMap) : new RawSource(processed.code);
|
|
48
|
-
// For some reason the type of `RawSource` is not compatible with
|
|
49
|
-
// Webpack's own `Source`, but works fine when casting it to `any`.
|
|
50
|
-
compilation.updateAsset(assetName, replacement);
|
|
51
55
|
});
|
|
52
56
|
});
|
|
53
57
|
});
|
package/dist/esm/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/plugin.ts"],"sourcesContent":["import type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils';\nimport {\n checkManifest,\n evalBundle,\n postProcessBundle,\n useTemporaryFile,\n} from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n */\n constructor(options?: Partial<Options>) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n const processed = postProcessBundle(source, {\n
|
|
1
|
+
{"version":3,"sources":["../../src/plugin.ts"],"sourcesContent":["import type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils';\nimport {\n checkManifest,\n evalBundle,\n getErrorMessage,\n postProcessBundle,\n useTemporaryFile,\n} from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n */\n constructor(options?: Partial<Options>) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n try {\n const processed = postProcessBundle(source, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool\n ? (sourceMap as SourceMap)\n : undefined,\n });\n\n if (processed.warnings.length > 0) {\n compilation.warnings.push(\n new WebpackError(\n `${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\\n${processed.warnings.join(\n '\\n',\n )}`,\n ),\n );\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n source,\n sourceMap as SourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with\n // Webpack's own `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\n } catch (error) {\n compilation.errors.push(\n new WebpackError(getErrorMessage(error)),\n );\n }\n });\n },\n );\n });\n\n compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file);\n\n assert(compilation.outputOptions.path);\n const outputPath = compilation.outputOptions.path;\n\n const filePath = pathUtils.join(outputPath, file.name);\n\n const bundleFile = await promisify(\n compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem),\n )(filePath);\n assert(bundleFile);\n\n const bundleContent = bundleFile.toString();\n\n if (this.options.eval) {\n await useTemporaryFile('snaps-bundle.js', bundleContent, (path) =>\n evalBundle(path),\n );\n }\n\n if (this.options.manifestPath) {\n const { errors, warnings } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n this.options.writeManifest,\n bundleContent,\n promisify(\n compiler.outputFileSystem.writeFile.bind(compiler.outputFileSystem),\n ),\n );\n\n if (!this.options.writeManifest && errors.length > 0) {\n throw new Error(\n `Manifest Error: The manifest is invalid.\\n${errors.join('\\n')}`,\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n new WebpackError(\n `${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\\n${warnings.join(\n '\\n',\n )}`,\n ),\n );\n }\n }\n });\n }\n}\n"],"names":["checkManifest","evalBundle","getErrorMessage","postProcessBundle","useTemporaryFile","assert","pathUtils","promisify","Compilation","WebpackError","RawSource","SourceMapSource","PLUGIN_NAME","SnapsWebpackPlugin","apply","compiler","devtool","options","hooks","compilation","tap","processAssets","name","stage","PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY","additionalAssets","assets","Object","keys","filter","assetName","endsWith","forEach","asset","source","sourceMap","map","processed","Boolean","inputSourceMap","undefined","warnings","length","push","join","replacement","code","updateAsset","error","errors","afterEmit","tapPromise","file","getAssets","find","outputOptions","path","outputPath","filePath","bundleFile","outputFileSystem","readFile","bind","bundleContent","toString","eval","manifestPath","dirname","writeManifest","writeFile","Error","constructor","process","cwd"],"mappings":";;;;;;;;;;;;;AACA,SACEA,aAAa,EACbC,UAAU,EACVC,eAAe,EACfC,iBAAiB,EACjBC,gBAAgB,QACX,wBAAwB;AAC/B,SAASC,MAAM,QAAQ,kBAAkB;AACzC,OAAOC,eAAe,OAAO;AAC7B,SAASC,SAAS,QA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AG;oBACvBvB,YAAYsB,QAAQ,CAACE,IAAI,CACvB,IAAIlC,aACF,CAAC,EAAEG,YAAY,+EAA+E,EAAE6B,SAASG,IAAI,CAC3G,MACA,CAAC;gBAGT;YACF;QACF;IACF;IA7IA;;;;;;;;;;;;;GAaC,GACD2B,YAAYtD,OAA0B,CAAE;QAhBxC,uBAAgBA,WAAhB,KAAA;QAiBE,IAAI,CAACA,OAAO,GAAG;YACbgD,MAAM;YACNC,cAAc5D,UAAUsC,IAAI,CAAC4B,QAAQC,GAAG,IAAI;YAC5CL,eAAe;YACf,GAAGnD,OAAO;QACZ;IACF;AAyHF;AAjJA,SAAqBJ,gCAiJpB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-webpack-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"webpack",
|
|
6
6
|
"plugin"
|
|
@@ -40,13 +40,13 @@
|
|
|
40
40
|
"lint:dependencies": "depcheck"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@metamask/snaps-utils": "^
|
|
43
|
+
"@metamask/snaps-utils": "^3.0.0",
|
|
44
44
|
"@metamask/utils": "^8.1.0",
|
|
45
45
|
"webpack-sources": "^3.2.3"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@lavamoat/allow-scripts": "^2.
|
|
49
|
-
"@metamask/auto-changelog": "^3.
|
|
48
|
+
"@lavamoat/allow-scripts": "^2.5.1",
|
|
49
|
+
"@metamask/auto-changelog": "^3.3.0",
|
|
50
50
|
"@metamask/eslint-config": "^12.1.0",
|
|
51
51
|
"@metamask/eslint-config-jest": "^12.1.0",
|
|
52
52
|
"@metamask/eslint-config-nodejs": "^12.1.0",
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
"webpack": "^5.88.0"
|
|
79
79
|
},
|
|
80
80
|
"engines": {
|
|
81
|
-
"node": ">=
|
|
81
|
+
"node": "^18.16 || >=20"
|
|
82
82
|
},
|
|
83
83
|
"publishConfig": {
|
|
84
84
|
"access": "public",
|