@metamask/snaps-webpack-plugin 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -6,183 +6,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
- ## [1.0.1]
9
+ ## [2.0.0]
10
10
  ### Changed
11
- - No changes this release.
11
+ - Initial stable release from main branch ([#1757](https://github.com/MetaMask/snaps/pull/1757))
12
12
 
13
- ## [1.0.0]
13
+ ## [0.37.4-flask.1]
14
14
  ### Changed
15
- - No changes this release.
15
+ - Bump `metamask/utils` and `metamask/snaps-registry` ([#1738](https://github.com/MetaMask/snaps/pull/1738), [#1694](https://github.com/MetaMask/snaps/pull/1694))
16
16
 
17
- ## [1.0.0-prerelease.1]
18
- ### Changed
19
- - No changes this release.
20
-
21
- ## [0.32.2]
22
- ### Changed
23
- - No changes this release.
24
-
25
- ## [0.32.1]
26
- ### Changed
27
- - No changes this release.
28
-
29
- ## [0.32.0]
30
- ### Changed
31
- - No changes this release.
32
-
33
- ## [0.31.0]
34
- ### Changed
35
- - **BREAKING:** Target `ES2020` ([#1247](https://github.com/MetaMask/snaps-monorepo/pull/1247))
36
-
37
- ### Fixed
38
- - Fix a few smaller issues when using the Webpack plugin with in-memory files ([#1225](https://github.com/MetaMask/snaps-monorepo/pull/1225))
39
-
40
- ## [0.30.0]
41
- ### Changed
42
- - No changes this release.
43
-
44
- ## [0.29.0]
45
- ### Changed
46
- - No changes this release.
47
-
48
- ## [0.28.0]
49
- ### Changed
50
- - No changes this release.
51
-
52
- ## [0.27.1]
53
- ### Changed
54
- - No changes this release.
55
-
56
- ## [0.27.0]
57
- ### Changed
58
- - No changes this release.
59
-
60
- ## [0.26.2]
61
- ### Changed
62
- - No changes this release.
63
-
64
- ## [0.26.1]
65
- ### Changed
66
- - No changes this release.
67
-
68
- ## [0.26.0]
69
- ### Changed
70
- - No changes this release.
71
-
72
- ## [0.25.0]
73
- ### Changed
74
- - No changes this release.
75
-
76
- ## [0.24.1]
77
- ### Changed
78
- - No changes this release.
79
-
80
- ## [0.24.0]
81
- ### Added
82
- - Add compile-time warning when using `Math.random` ([#950](https://github.com/MetaMask/snaps-monorepo/pull/950))
83
-
84
- ## [0.23.0]
85
- ### Fixed
86
- - Fix Webpack plugin issue with existing files ([#858](https://github.com/MetaMask/snaps-monorepo/pull/858))
87
-
88
- ## [0.22.3]
89
- ### Changed
90
- - No changes this release.
91
-
92
- ## [0.22.2]
93
- ### Changed
94
- - No changes this release.
95
-
96
- ## [0.22.1]
97
- ### Changed
98
- - No changes this release.
99
-
100
- ## [0.22.0]
101
- ### Changed
102
- - No changes this release.
103
-
104
- ## [0.21.0]
105
- ### Added
106
- - Run eval and fix manifest in bundler plugins ([#731](https://github.com/MetaMask/snaps-monorepo/pull/731))
107
-
108
- ## [0.20.0]
109
- ### Changed
110
- - No changes this release.
111
-
112
- ## [0.19.1]
113
- ### Added
114
- - Generate source maps from modified code ([#615](https://github.com/MetaMask/snaps-monorepo/pull/615))
115
-
116
- ## [0.19.0]
117
- ### Changed
118
- - No changes this release.
119
-
120
- ## [0.18.1]
121
- ### Changed
122
- - No changes this release.
123
-
124
- ## [0.18.0]
125
- ### Changed
126
- - Reduce TypeScript compilation target to ES2017 ([#628](https://github.com/MetaMask/snaps-monorepo/pull/628))
127
-
128
- ## [0.17.0]
129
- ### Changed
130
- - **BREAKING:** Bump minimum Node version to 16 ([#601](https://github.com/MetaMask/snaps-monorepo/pull/601))
131
-
132
- ## [0.16.0]
133
- ### Changed
134
- - No changes this release.
135
-
136
- ## [0.15.0]
17
+ ## [0.37.3-flask.1]
137
18
  ### Fixed
138
- - Fix some typing issues ([#462](https://github.com/MetaMask/snaps-monorepo/pull/462))
19
+ - Remove unused dependencies ([#1680](https://github.com/MetaMask/snaps/pull/1680))
139
20
 
140
- ## [0.14.0]
141
- ### Fixed
142
- - Actually publish package contents ([#449](https://github.com/MetaMask/snaps-monorepo/pull/449))
143
- - Package contents were omitted from the previous version due to a build failure.
144
-
145
- ## [0.13.0]
21
+ ## [0.37.2-flask.1]
146
22
  ### Changed
147
- - No changes this release.
148
-
149
- ## [0.12.0]
150
- ### Added
151
- - Initial release ([#420](https://github.com/MetaMask/snaps-monorepo/pull/420))
23
+ - Release package independently ([#1600](https://github.com/MetaMask/snaps/pull/1600))
24
+ - The version of the package no longer needs to match the version of all other
25
+ MetaMask Snaps packages.
152
26
 
153
- [Unreleased]: https://github.com/MetaMask/snaps/compare/v1.0.1...HEAD
154
- [1.0.1]: https://github.com/MetaMask/snaps/compare/v1.0.0...v1.0.1
155
- [1.0.0]: https://github.com/MetaMask/snaps/compare/v1.0.0-prerelease.1...v1.0.0
156
- [1.0.0-prerelease.1]: https://github.com/MetaMask/snaps/compare/v0.32.2...v1.0.0-prerelease.1
157
- [0.32.2]: https://github.com/MetaMask/snaps/compare/v0.32.1...v0.32.2
158
- [0.32.1]: https://github.com/MetaMask/snaps/compare/v0.32.0...v0.32.1
159
- [0.32.0]: https://github.com/MetaMask/snaps/compare/v0.31.0...v0.32.0
160
- [0.31.0]: https://github.com/MetaMask/snaps/compare/v0.30.0...v0.31.0
161
- [0.30.0]: https://github.com/MetaMask/snaps/compare/v0.29.0...v0.30.0
162
- [0.29.0]: https://github.com/MetaMask/snaps/compare/v0.28.0...v0.29.0
163
- [0.28.0]: https://github.com/MetaMask/snaps/compare/v0.27.1...v0.28.0
164
- [0.27.1]: https://github.com/MetaMask/snaps/compare/v0.27.0...v0.27.1
165
- [0.27.0]: https://github.com/MetaMask/snaps/compare/v0.26.2...v0.27.0
166
- [0.26.2]: https://github.com/MetaMask/snaps/compare/v0.26.1...v0.26.2
167
- [0.26.1]: https://github.com/MetaMask/snaps/compare/v0.26.0...v0.26.1
168
- [0.26.0]: https://github.com/MetaMask/snaps/compare/v0.25.0...v0.26.0
169
- [0.25.0]: https://github.com/MetaMask/snaps/compare/v0.24.1...v0.25.0
170
- [0.24.1]: https://github.com/MetaMask/snaps/compare/v0.24.0...v0.24.1
171
- [0.24.0]: https://github.com/MetaMask/snaps/compare/v0.23.0...v0.24.0
172
- [0.23.0]: https://github.com/MetaMask/snaps/compare/v0.22.3...v0.23.0
173
- [0.22.3]: https://github.com/MetaMask/snaps/compare/v0.22.2...v0.22.3
174
- [0.22.2]: https://github.com/MetaMask/snaps/compare/v0.22.1...v0.22.2
175
- [0.22.1]: https://github.com/MetaMask/snaps/compare/v0.22.0...v0.22.1
176
- [0.22.0]: https://github.com/MetaMask/snaps/compare/v0.21.0...v0.22.0
177
- [0.21.0]: https://github.com/MetaMask/snaps/compare/v0.20.0...v0.21.0
178
- [0.20.0]: https://github.com/MetaMask/snaps/compare/v0.19.1...v0.20.0
179
- [0.19.1]: https://github.com/MetaMask/snaps/compare/v0.19.0...v0.19.1
180
- [0.19.0]: https://github.com/MetaMask/snaps/compare/v0.18.1...v0.19.0
181
- [0.18.1]: https://github.com/MetaMask/snaps/compare/v0.18.0...v0.18.1
182
- [0.18.0]: https://github.com/MetaMask/snaps/compare/v0.17.0...v0.18.0
183
- [0.17.0]: https://github.com/MetaMask/snaps/compare/v0.16.0...v0.17.0
184
- [0.16.0]: https://github.com/MetaMask/snaps/compare/v0.15.0...v0.16.0
185
- [0.15.0]: https://github.com/MetaMask/snaps/compare/v0.14.0...v0.15.0
186
- [0.14.0]: https://github.com/MetaMask/snaps/compare/v0.13.0...v0.14.0
187
- [0.13.0]: https://github.com/MetaMask/snaps/compare/v0.12.0...v0.13.0
188
- [0.12.0]: https://github.com/MetaMask/snaps/releases/tag/v0.12.0
27
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@2.0.0...HEAD
28
+ [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
+ [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
+ [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
31
+ [0.37.2-flask.1]: https://github.com/MetaMask/snaps/releases/tag/@metamask/snaps-webpack-plugin@0.37.2-flask.1
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return _plugin.default;
9
+ }
10
+ });
11
+ const _plugin = /*#__PURE__*/ _interop_require_default(require("./plugin"));
12
+ function _interop_require_default(obj) {
13
+ return obj && obj.__esModule ? obj : {
14
+ default: obj
15
+ };
16
+ }
17
+
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export { default } from './plugin';\nexport type { Options } from './plugin';\n"],"names":["default"],"mappings":";;;;+BAASA;;;eAAAA,eAAO;;;+DAAQ"}
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return SnapsWebpackPlugin;
9
+ }
10
+ });
11
+ const _snapsutils = require("@metamask/snaps-utils");
12
+ const _utils = require("@metamask/utils");
13
+ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
14
+ const _util = require("util");
15
+ const _webpack = require("webpack");
16
+ const _webpacksources = require("webpack-sources");
17
+ function _define_property(obj, key, value) {
18
+ if (key in obj) {
19
+ Object.defineProperty(obj, key, {
20
+ value: value,
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true
24
+ });
25
+ } else {
26
+ obj[key] = value;
27
+ }
28
+ return obj;
29
+ }
30
+ function _interop_require_default(obj) {
31
+ return obj && obj.__esModule ? obj : {
32
+ default: obj
33
+ };
34
+ }
35
+ const PLUGIN_NAME = 'SnapsWebpackPlugin';
36
+ class SnapsWebpackPlugin {
37
+ /**
38
+ * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
39
+ * stage to process the bundle.
40
+ *
41
+ * @param compiler - The Webpack compiler.
42
+ */ apply(compiler) {
43
+ const { devtool } = compiler.options;
44
+ compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation)=>{
45
+ compilation.hooks.processAssets.tap({
46
+ name: PLUGIN_NAME,
47
+ stage: _webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,
48
+ additionalAssets: true
49
+ }, (assets)=>{
50
+ Object.keys(assets).filter((assetName)=>assetName.endsWith('.js')).forEach((assetName)=>{
51
+ const asset = assets[assetName];
52
+ const source = asset.source();
53
+ const sourceMap = asset.map();
54
+ const processed = (0, _snapsutils.postProcessBundle)(source, {
55
+ ...this.options,
56
+ sourceMap: Boolean(devtool),
57
+ inputSourceMap: devtool ? sourceMap : undefined
58
+ });
59
+ if (processed.warnings.length > 0) {
60
+ compilation.warnings.push(new _webpack.WebpackError(`${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\n${processed.warnings.join('\n')}`));
61
+ }
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
+ });
67
+ });
68
+ });
69
+ compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation)=>{
70
+ const file = compilation.getAssets().find((asset)=>asset.name.endsWith('.js'));
71
+ (0, _utils.assert)(file);
72
+ (0, _utils.assert)(compilation.outputOptions.path);
73
+ const outputPath = compilation.outputOptions.path;
74
+ const filePath = _path.default.join(outputPath, file.name);
75
+ const bundleFile = await (0, _util.promisify)(compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem))(filePath);
76
+ (0, _utils.assert)(bundleFile);
77
+ const bundleContent = bundleFile.toString();
78
+ if (this.options.eval) {
79
+ await (0, _snapsutils.useTemporaryFile)('snaps-bundle.js', bundleContent, (path)=>(0, _snapsutils.evalBundle)(path));
80
+ }
81
+ if (this.options.manifestPath) {
82
+ const { errors, warnings } = await (0, _snapsutils.checkManifest)(_path.default.dirname(this.options.manifestPath), this.options.writeManifest, bundleContent, (0, _util.promisify)(compiler.outputFileSystem.writeFile.bind(compiler.outputFileSystem)));
83
+ if (!this.options.writeManifest && errors.length > 0) {
84
+ throw new Error(`Manifest Error: The manifest is invalid.\n${errors.join('\n')}`);
85
+ }
86
+ if (warnings.length > 0) {
87
+ compilation.warnings.push(new _webpack.WebpackError(`${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\n${warnings.join('\n')}`));
88
+ }
89
+ }
90
+ });
91
+ }
92
+ /**
93
+ * Construct an instance of the plugin.
94
+ *
95
+ * @param options - The post-process options.
96
+ * @param options.stripComments - Whether to strip comments. Defaults to
97
+ * `true`.
98
+ * @param options.eval - Whether to evaluate the bundle to test SES
99
+ * compatibility. Defaults to `true`.
100
+ * @param options.manifestPath - The path to the manifest file. If provided,
101
+ * the manifest will be validated. Defaults to
102
+ * `process.cwd() + '/snap.manifest.json'`.
103
+ * @param options.writeManifest - Whether to fix the manifest.
104
+ * Defaults to `true`.
105
+ */ constructor(options){
106
+ _define_property(this, "options", void 0);
107
+ this.options = {
108
+ eval: true,
109
+ manifestPath: _path.default.join(process.cwd(), 'snap.manifest.json'),
110
+ writeManifest: true,
111
+ ...options
112
+ };
113
+ }
114
+ }
115
+
116
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +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 ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool ? (sourceMap as SourceMap) : 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 });\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","afterEmit","tapPromise","file","getAssets","find","assert","outputOptions","path","outputPath","filePath","pathUtils","bundleFile","promisify","outputFileSystem","readFile","bind","bundleContent","toString","eval","useTemporaryFile","evalBundle","manifestPath","errors","checkManifest","dirname","writeManifest","writeFile","Error","constructor","process","cwd"],"mappings":";;;;;;;eAyBqBA;;;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,GAAG,CACjC;gBACEE,MAAMT;gBACNU,OAAOC,oBAAW,CAACC,2CAA2C;gBAC9DC,kBAAkB;YACpB,GACA,CAACC;gBACCC,OAAOC,IAAI,CAACF,QACTG,MAAM,CAAC,CAACC,YAAcA,UAAUC,QAAQ,CAAC,QACzCC,OAAO,CAAC,CAACF;oBACR,MAAMG,QAAQP,MAAM,CAACI,UAAU;oBAC/B,MAAMI,SAASD,MAAMC,MAAM;oBAC3B,MAAMC,YAAYF,MAAMG,GAAG;oBAE3B,MAAMC,YAAYC,IAAAA,6BAAiB,EAACJ,QAAQ;wBAC1C,GAAG,IAAI,CAAClB,OAAO;wBACfmB,WAAWI,QAAQxB;wBACnByB,gBAAgBzB,UAAWoB,YAA0BM;oBACvD;oBAEA,IAAIJ,UAAUK,QAAQ,CAACC,MAAM,GAAG,GAAG;wBACjCzB,YAAYwB,QAAQ,CAACE,IAAI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D,OAAO,CAACyD,YAAY,GAC3C,IAAI,CAACzD,OAAO,CAAC6D,aAAa,EAC1BT,eACAJ,IAAAA,eAAS,EACPlD,SAASmD,gBAAgB,CAACa,SAAS,CAACX,IAAI,CAACrD,SAASmD,gBAAgB;gBAItE,IAAI,CAAC,IAAI,CAACjD,OAAO,CAAC6D,aAAa,IAAIH,OAAO/B,MAAM,GAAG,GAAG;oBACpD,MAAM,IAAIoC,MACR,CAAC,0CAA0C,EAAEL,OAAO5B,IAAI,CAAC,MAAM,CAAC;gBAEpE;gBAEA,IAAIJ,SAASC,MAAM,GAAG,GAAG;oBACvBzB,YAAYwB,QAAQ,CAACE,IAAI,CACvB,IAAIC,qBAAY,CACd,CAAC,EAAEjC,YAAY,+EAA+E,EAAE8B,SAASI,IAAI,CAC3G,MACA,CAAC;gBAGT;YACF;QACF;IACF;IArIA;;;;;;;;;;;;;GAaC,GACDkC,YAAYhE,OAA0B,CAAE;QAhBxC,uBAAgBA,WAAhB,KAAA;QAiBE,IAAI,CAACA,OAAO,GAAG;YACbsD,MAAM;YACNG,cAAcX,aAAS,CAAChB,IAAI,CAACmC,QAAQC,GAAG,IAAI;YAC5CL,eAAe;YACf,GAAG7D,OAAO;QACZ;IACF;AAiHF"}
@@ -0,0 +1,3 @@
1
+ export { default } from './plugin';
2
+
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export { default } from './plugin';\nexport type { Options } from './plugin';\n"],"names":["default"],"mappings":"AAAA,SAASA,OAAO,QAAQ,WAAW"}
@@ -0,0 +1,102 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value: value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
14
+ import { checkManifest, evalBundle, postProcessBundle, useTemporaryFile } from '@metamask/snaps-utils';
15
+ import { assert } from '@metamask/utils';
16
+ import pathUtils from 'path';
17
+ import { promisify } from 'util';
18
+ import { Compilation, WebpackError } from 'webpack';
19
+ import { RawSource, SourceMapSource } from 'webpack-sources';
20
+ const PLUGIN_NAME = 'SnapsWebpackPlugin';
21
+ class SnapsWebpackPlugin {
22
+ /**
23
+ * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
24
+ * stage to process the bundle.
25
+ *
26
+ * @param compiler - The Webpack compiler.
27
+ */ apply(compiler) {
28
+ const { devtool } = compiler.options;
29
+ compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation)=>{
30
+ compilation.hooks.processAssets.tap({
31
+ name: PLUGIN_NAME,
32
+ stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,
33
+ additionalAssets: true
34
+ }, (assets)=>{
35
+ Object.keys(assets).filter((assetName)=>assetName.endsWith('.js')).forEach((assetName)=>{
36
+ const asset = assets[assetName];
37
+ const source = asset.source();
38
+ const sourceMap = asset.map();
39
+ const processed = postProcessBundle(source, {
40
+ ...this.options,
41
+ sourceMap: Boolean(devtool),
42
+ inputSourceMap: devtool ? sourceMap : undefined
43
+ });
44
+ if (processed.warnings.length > 0) {
45
+ compilation.warnings.push(new WebpackError(`${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\n${processed.warnings.join('\n')}`));
46
+ }
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
+ });
52
+ });
53
+ });
54
+ compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation)=>{
55
+ const file = compilation.getAssets().find((asset)=>asset.name.endsWith('.js'));
56
+ assert(file);
57
+ assert(compilation.outputOptions.path);
58
+ const outputPath = compilation.outputOptions.path;
59
+ const filePath = pathUtils.join(outputPath, file.name);
60
+ const bundleFile = await promisify(compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem))(filePath);
61
+ assert(bundleFile);
62
+ const bundleContent = bundleFile.toString();
63
+ if (this.options.eval) {
64
+ await useTemporaryFile('snaps-bundle.js', bundleContent, (path)=>evalBundle(path));
65
+ }
66
+ if (this.options.manifestPath) {
67
+ const { errors, warnings } = await checkManifest(pathUtils.dirname(this.options.manifestPath), this.options.writeManifest, bundleContent, promisify(compiler.outputFileSystem.writeFile.bind(compiler.outputFileSystem)));
68
+ if (!this.options.writeManifest && errors.length > 0) {
69
+ throw new Error(`Manifest Error: The manifest is invalid.\n${errors.join('\n')}`);
70
+ }
71
+ if (warnings.length > 0) {
72
+ compilation.warnings.push(new WebpackError(`${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\n${warnings.join('\n')}`));
73
+ }
74
+ }
75
+ });
76
+ }
77
+ /**
78
+ * Construct an instance of the plugin.
79
+ *
80
+ * @param options - The post-process options.
81
+ * @param options.stripComments - Whether to strip comments. Defaults to
82
+ * `true`.
83
+ * @param options.eval - Whether to evaluate the bundle to test SES
84
+ * compatibility. Defaults to `true`.
85
+ * @param options.manifestPath - The path to the manifest file. If provided,
86
+ * the manifest will be validated. Defaults to
87
+ * `process.cwd() + '/snap.manifest.json'`.
88
+ * @param options.writeManifest - Whether to fix the manifest.
89
+ * Defaults to `true`.
90
+ */ constructor(options){
91
+ _define_property(this, "options", void 0);
92
+ this.options = {
93
+ eval: true,
94
+ manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),
95
+ writeManifest: true,
96
+ ...options
97
+ };
98
+ }
99
+ }
100
+ export { SnapsWebpackPlugin as default };
101
+
102
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +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 ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool ? (sourceMap as SourceMap) : 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 });\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","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","afterEmit","tapPromise","file","getAssets","find","outputOptions","path","outputPath","filePath","bundleFile","outputFileSystem","readFile","bind","bundleContent","toString","eval","manifestPath","errors","dirname","writeManifest","writeFile","Error","constructor","process","cwd"],"mappings":";;;;;;;;;;;;;AACA,SACEA,aAAa,EACbC,UAAU,EACVC,iBAAiB,EACjBC,gBAAgB,QACX,wBAAwB;AAC/B,SAASC,MAAM,QAAQ,kBAAkB;AACzC,OAAOC,eAAe,OAAO;AAC7B,SAASC,SAAS,QAAQ,OAAO;AAEjC,SAASC,WAAW,EAAEC,YAAY,QAAQ,UAAU;AACpD,SAASC,SAAS,EAAEC,eAAe,QAAQ,kBAAkB;AAE7D,MAAMC,cAAc;AAWL,MAAMC;IA0BnB;;;;;GAKC,GACDC,MAAMC,QAAkB,EAAE;QACxB,MAAM,EAAEC,OAAO,EAAE,GAAGD,SAASE,OAAO;QAEpCF,SAASG,KAAK,CAACC,WAAW,CAACC,GAAG,CAACR,aAAa,CAACO;YAC3CA,YAAYD,KAAK,CAACG,aAAa,CAACD,GAAG,CACjC;gBACEE,MAAMV;gBACNW,OAAOf,YAAYgB,2CAA2C;gBAC9DC,kBAAkB;YACpB,GACA,CAACC;gBACCC,OAAOC,IAAI,CAACF,QACTG,MAAM,CAAC,CAACC,YAAcA,UAAUC,QAAQ,CAAC,QACzCC,OAAO,CAAC,CAACF;oBACR,MAAMG,QAAQP,MAAM,CAACI,UAAU;oBAC/B,MAAMI,SAASD,MAAMC,MAAM;oBAC3B,MAAMC,YAAYF,MAAMG,GAAG;oBAE3B,MAAMC,YAAYlC,kBAAkB+B,QAAQ;wBAC1C,GAAG,IAAI,CAACjB,OAAO;wBACfkB,WAAWG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ACjCK,UAAU4D,OAAO,CAAC,IAAI,CAACjD,OAAO,CAAC+C,YAAY,GAC3C,IAAI,CAAC/C,OAAO,CAACkD,aAAa,EAC1BN,eACAtD,UACEQ,SAAS2C,gBAAgB,CAACU,SAAS,CAACR,IAAI,CAAC7C,SAAS2C,gBAAgB;gBAItE,IAAI,CAAC,IAAI,CAACzC,OAAO,CAACkD,aAAa,IAAIF,OAAOvB,MAAM,GAAG,GAAG;oBACpD,MAAM,IAAI2B,MACR,CAAC,0CAA0C,EAAEJ,OAAOrB,IAAI,CAAC,MAAM,CAAC;gBAEpE;gBAEA,IAAIH,SAASC,MAAM,GAAG,GAAG;oBACvBvB,YAAYsB,QAAQ,CAACE,IAAI,CACvB,IAAIlC,aACF,CAAC,EAAEG,YAAY,+EAA+E,EAAE6B,SAASG,IAAI,CAC3G,MACA,CAAC;gBAGT;YACF;QACF;IACF;IArIA;;;;;;;;;;;;;GAaC,GACD0B,YAAYrD,OAA0B,CAAE;QAhBxC,uBAAgBA,WAAhB,KAAA;QAiBE,IAAI,CAACA,OAAO,GAAG;YACb8C,MAAM;YACNC,cAAc1D,UAAUsC,IAAI,CAAC2B,QAAQC,GAAG,IAAI;YAC5CL,eAAe;YACf,GAAGlD,OAAO;QACZ;IACF;AAiHF;AAzIA,SAAqBJ,gCAyIpB"}
@@ -1,5 +1,5 @@
1
- import { PostProcessOptions } from '@metamask/snaps-utils';
2
- import { Compiler } from 'webpack';
1
+ import type { PostProcessOptions } from '@metamask/snaps-utils';
2
+ import type { Compiler } from 'webpack';
3
3
  declare type PluginOptions = {
4
4
  eval?: boolean;
5
5
  manifestPath?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-webpack-plugin",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "keywords": [
5
5
  "webpack",
6
6
  "plugin"
@@ -9,60 +9,73 @@
9
9
  "type": "git",
10
10
  "url": "https://github.com/MetaMask/snaps.git"
11
11
  },
12
- "main": "dist/index.js",
12
+ "sideEffects": false,
13
+ "main": "./dist/cjs/index.js",
14
+ "module": "./dist/esm/index.js",
15
+ "types": "./dist/types/index.d.ts",
13
16
  "files": [
14
- "dist/"
17
+ "dist/cjs/**",
18
+ "dist/esm/**",
19
+ "dist/types/**"
15
20
  ],
16
21
  "scripts": {
17
22
  "test": "jest && yarn posttest",
18
23
  "posttest": "jest-it-up",
19
24
  "test:ci": "yarn test",
20
- "lint:eslint": "eslint . --cache --ext js,ts",
21
- "lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' --ignore-path ../../.gitignore",
22
- "lint": "yarn lint:eslint && yarn lint:misc --check",
25
+ "lint:eslint": "eslint . --cache --ext js,ts,jsx,tsx",
26
+ "lint:misc": "prettier --no-error-on-unmatched-pattern --loglevel warn \"**/*.json\" \"**/*.md\" \"**/*.html\" \"!CHANGELOG.md\" --ignore-path ../../.gitignore",
27
+ "lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:changelog && yarn lint:dependencies",
23
28
  "lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
24
- "lint:changelog": "yarn auto-changelog validate",
25
- "build:tsc": "tsc --project tsconfig.build.json",
26
- "build": "yarn build:tsc",
29
+ "lint:changelog": "../../scripts/validate-changelog.sh @metamask/snaps-webpack-plugin",
30
+ "build": "yarn build:source && yarn build:types",
31
+ "build:source": "yarn build:esm && yarn build:cjs",
32
+ "build:types": "tsc --project tsconfig.build.json",
33
+ "build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
34
+ "build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
27
35
  "build:clean": "yarn clean && yarn build",
28
- "clean": "rimraf '*.tsbuildinfo' 'dist/*'",
36
+ "clean": "rimraf '*.tsbuildinfo' 'dist'",
29
37
  "prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
30
38
  "publish:preview": "yarn npm publish --tag preview",
31
- "publish:package": "../../scripts/publish-package.sh"
39
+ "lint:ci": "yarn lint",
40
+ "lint:dependencies": "depcheck"
32
41
  },
33
42
  "dependencies": {
34
- "@metamask/snaps-utils": "^1.0.1",
35
- "@metamask/utils": "^6.0.1",
43
+ "@metamask/snaps-utils": "^2.0.0",
44
+ "@metamask/utils": "^8.1.0",
36
45
  "webpack-sources": "^3.2.3"
37
46
  },
38
47
  "devDependencies": {
39
- "@lavamoat/allow-scripts": "^2.0.3",
48
+ "@lavamoat/allow-scripts": "^2.3.1",
40
49
  "@metamask/auto-changelog": "^3.1.0",
41
- "@metamask/eslint-config": "^11.0.0",
42
- "@metamask/eslint-config-jest": "^11.0.0",
43
- "@metamask/eslint-config-nodejs": "^11.0.1",
44
- "@metamask/eslint-config-typescript": "^11.0.0",
50
+ "@metamask/eslint-config": "^12.1.0",
51
+ "@metamask/eslint-config-jest": "^12.1.0",
52
+ "@metamask/eslint-config-nodejs": "^12.1.0",
53
+ "@metamask/eslint-config-typescript": "^12.1.0",
54
+ "@swc/cli": "^0.1.62",
55
+ "@swc/core": "1.3.78",
56
+ "@swc/jest": "^0.2.26",
45
57
  "@types/jest": "^27.5.1",
46
58
  "@types/webpack-sources": "^3.2.0",
47
59
  "@typescript-eslint/eslint-plugin": "^5.42.1",
48
60
  "@typescript-eslint/parser": "^5.42.1",
49
61
  "deepmerge": "^4.2.2",
62
+ "depcheck": "^1.4.5",
50
63
  "eslint": "^8.27.0",
51
64
  "eslint-config-prettier": "^8.5.0",
52
65
  "eslint-plugin-import": "^2.26.0",
53
66
  "eslint-plugin-jest": "^27.1.5",
54
67
  "eslint-plugin-jsdoc": "^39.6.2",
55
- "eslint-plugin-node": "^11.1.0",
68
+ "eslint-plugin-n": "^15.7.0",
56
69
  "eslint-plugin-prettier": "^4.2.1",
70
+ "eslint-plugin-promise": "^6.1.1",
57
71
  "jest": "^29.0.2",
58
72
  "jest-it-up": "^2.0.0",
59
- "memfs": "^3.4.1",
73
+ "memfs": "^3.4.13",
60
74
  "prettier": "^2.7.1",
61
75
  "prettier-plugin-packagejson": "^2.2.11",
62
- "rimraf": "^3.0.2",
63
- "ts-jest": "^29.0.0",
76
+ "rimraf": "^4.1.2",
64
77
  "typescript": "~4.8.4",
65
- "webpack": "^5.72.1"
78
+ "webpack": "^5.88.0"
66
79
  },
67
80
  "engines": {
68
81
  "node": ">=16.0.0"
package/dist/index.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.default = void 0;
7
- var plugin_1 = require("./plugin");
8
- Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(plugin_1).default; } });
9
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,mCAAmC;AAA1B,kHAAA,OAAO,OAAA","sourcesContent":["export { default } from './plugin';\nexport type { Options } from './plugin';\n"]}
package/dist/plugin.js DELETED
@@ -1,94 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const snaps_utils_1 = require("@metamask/snaps-utils");
7
- const utils_1 = require("@metamask/utils");
8
- const path_1 = __importDefault(require("path"));
9
- const util_1 = require("util");
10
- const webpack_1 = require("webpack");
11
- const webpack_sources_1 = require("webpack-sources");
12
- const PLUGIN_NAME = 'SnapsWebpackPlugin';
13
- class SnapsWebpackPlugin {
14
- /**
15
- * Construct an instance of the plugin.
16
- *
17
- * @param options - The post-process options.
18
- * @param options.stripComments - Whether to strip comments. Defaults to
19
- * `true`.
20
- * @param options.eval - Whether to evaluate the bundle to test SES
21
- * compatibility. Defaults to `true`.
22
- * @param options.manifestPath - The path to the manifest file. If provided,
23
- * the manifest will be validated. Defaults to
24
- * `process.cwd() + '/snap.manifest.json'`.
25
- * @param options.writeManifest - Whether to fix the manifest.
26
- * Defaults to `true`.
27
- */
28
- constructor(options) {
29
- this.options = {
30
- eval: true,
31
- manifestPath: path_1.default.join(process.cwd(), 'snap.manifest.json'),
32
- writeManifest: true,
33
- ...options,
34
- };
35
- }
36
- /**
37
- * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
38
- * stage to process the bundle.
39
- *
40
- * @param compiler - The Webpack compiler.
41
- */
42
- apply(compiler) {
43
- const { devtool } = compiler.options;
44
- compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
45
- compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets) => {
46
- Object.keys(assets)
47
- .filter((assetName) => assetName.endsWith('.js'))
48
- .forEach((assetName) => {
49
- const asset = assets[assetName];
50
- const processed = (0, snaps_utils_1.postProcessBundle)(asset.source(), {
51
- ...this.options,
52
- sourceMap: Boolean(devtool),
53
- inputSourceMap: devtool ? asset.map() : undefined,
54
- });
55
- if (processed.warnings.length > 0) {
56
- compilation.warnings.push(new webpack_1.WebpackError(`${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\n${processed.warnings.join('\n')}`));
57
- }
58
- const replacement = processed.sourceMap
59
- ? new webpack_sources_1.SourceMapSource(processed.code, assetName, processed.sourceMap)
60
- : new webpack_sources_1.RawSource(processed.code);
61
- // For some reason the type of `RawSource` is not compatible with Webpack's own
62
- // `Source`, but works fine when casting it to `any`.
63
- compilation.updateAsset(assetName, replacement);
64
- });
65
- });
66
- });
67
- compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {
68
- const file = compilation
69
- .getAssets()
70
- .find((asset) => asset.name.endsWith('.js'));
71
- (0, utils_1.assert)(file);
72
- (0, utils_1.assert)(compilation.outputOptions.path);
73
- const outputPath = compilation.outputOptions.path;
74
- const filePath = path_1.default.join(outputPath, file.name);
75
- const bundleFile = await (0, util_1.promisify)(compiler.outputFileSystem.readFile)(filePath);
76
- (0, utils_1.assert)(bundleFile);
77
- const bundleContent = bundleFile.toString();
78
- if (this.options.eval) {
79
- await (0, snaps_utils_1.useTemporaryFile)('snaps-bundle.js', bundleContent, (path) => (0, snaps_utils_1.evalBundle)(path));
80
- }
81
- if (this.options.manifestPath) {
82
- const { errors, warnings } = await (0, snaps_utils_1.checkManifest)(path_1.default.dirname(this.options.manifestPath), this.options.writeManifest, bundleContent);
83
- if (!this.options.writeManifest && errors.length > 0) {
84
- throw new Error(`Manifest Error: The manifest is invalid.\n${errors.join('\n')}`);
85
- }
86
- if (warnings.length > 0) {
87
- compilation.warnings.push(new webpack_1.WebpackError(`${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\n${warnings.join('\n')}`));
88
- }
89
- }
90
- });
91
- }
92
- }
93
- exports.default = SnapsWebpackPlugin;
94
- //# sourceMappingURL=plugin.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;;;AAAA,uDAO+B;AAC/B,2CAAyC;AACzC,gDAA6B;AAC7B,+BAAiC;AACjC,qCAAiD;AACjD,qDAA6D;AAE7D,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAWzC,MAAqB,kBAAkB;IAGrC;;;;;;;;;;;;;OAaG;IACH,YAAY,OAA0B;QACpC,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;IACJ,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,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,GAAG,CAAC,EAAE;oBACpD,MAAM,IAAI,KAAK,CACb,6CAA6C,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CACjE,CAAC;iBACH;gBAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;oBACvB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,IAAI,sBAAY,CACd,GAAG,WAAW,kFAAkF,QAAQ,CAAC,IAAI,CAC3G,IAAI,CACL,EAAE,CACJ,CACF,CAAC;iBACH;aACF;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA1HD,qCA0HC","sourcesContent":["import {\n checkManifest,\n evalBundle,\n postProcessBundle,\n PostProcessOptions,\n SourceMap,\n useTemporaryFile,\n} from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport { Compiler, 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(PLUGIN_NAME, (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const processed = postProcessBundle(asset.source() as string, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool ? (asset.map() as SourceMap) : 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 )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with Webpack's own\n // `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\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(compiler.outputFileSystem.readFile)(\n filePath,\n );\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 );\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"]}
File without changes