@metamask/snaps-webpack-plugin 0.20.0 → 0.21.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/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # @metamask/snaps-webpack-plugin
2
2
 
3
- A plugin for developing [MetaMask Snaps](https://docs.metamask.io/guide/snaps.html) using [Webpack](https://webpack.js.org/). This can be used as alternative to the `mm-snap` CLI `build` command. It transforms the bundle to fix common issues with SES. For a list of changes the plugin makes, you can refer to [the source code](../utils/src/bundle.ts).
3
+ A plugin for developing [MetaMask Snaps](https://docs.metamask.io/guide/snaps.html) using [Webpack](https://webpack.js.org/). This can be used as alternative to the `mm-snap` CLI `build` command. It transforms the bundle to fix common issues with SES. For a list of changes the plugin makes, you can refer to [the source code](../utils/src/post-process.ts).
4
4
 
5
5
  ## Installation
6
6
 
7
- Use Node.js `14.0.0` or later. We recommend using [nvm](https://github.com/nvm-sh/nvm) for managing Node.js versions.
7
+ Use Node.js `16.0.0` or later. We recommend using [nvm](https://github.com/nvm-sh/nvm) for managing Node.js versions.
8
8
 
9
9
  Install a dependency in your snap project using `yarn` or `npm`:
10
10
 
@@ -39,9 +39,22 @@ const options: Options = {
39
39
  stripComments: true,
40
40
 
41
41
  /**
42
- * Whether to break up tokens that could be parsed as HTML comment terminators. This may change
43
- * the behaviour of programs that contain HTML comment terminators in string literals.
42
+ * Whether to evaluate the bundle with SES, to ensure SES compatibility.
44
43
  */
45
- transformHtmlComments: true,
44
+ eval: true,
45
+
46
+ /**
47
+ * The path to the Snap manifest file. If set, it will be checked and automatically updated with
48
+ * the bundle's hash, if `writeManifest` is enabled. Defaults to `snap/manifest.json` in the
49
+ * current working directory.
50
+ */
51
+ manifestPath: './snap.manifest.json',
52
+
53
+ /**
54
+ * Whether to write the updated Snap manifest file to disk. If `manifestPath` is not set, this
55
+ * option has no effect. If this is disabled, an error will be thrown if the manifest file is
56
+ * invalid.
57
+ */
58
+ writeManifest: true,
46
59
  };
47
60
  ```
package/dist/plugin.d.ts CHANGED
@@ -1,14 +1,25 @@
1
1
  import { PostProcessOptions } from '@metamask/snap-utils';
2
2
  import { Compiler } from 'webpack';
3
- export declare type Options = PostProcessOptions;
3
+ declare type PluginOptions = {
4
+ eval?: boolean;
5
+ manifestPath?: string;
6
+ writeManifest?: boolean;
7
+ };
8
+ export declare type Options = PluginOptions & Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;
4
9
  export default class SnapsWebpackPlugin {
5
10
  readonly options: Partial<Options>;
6
11
  /**
7
12
  * Construct an instance of the plugin.
8
13
  *
9
14
  * @param options - The post-process options.
10
- * @param options.stripComments - Whether to strip comments. Defaults to `true`.
11
- * @param options.transformHtmlComments - Whether to transform HTML comments.
15
+ * @param options.stripComments - Whether to strip comments. Defaults to
16
+ * `true`.
17
+ * @param options.eval - Whether to evaluate the bundle to test SES
18
+ * compatibility. Defaults to `true`.
19
+ * @param options.manifestPath - The path to the manifest file. If provided,
20
+ * the manifest will be validated. Defaults to
21
+ * `process.cwd() + '/snap.manifest.json'`.
22
+ * @param options.writeManifest - Whether to fix the manifest.
12
23
  * Defaults to `true`.
13
24
  */
14
25
  constructor(options?: Partial<Options>);
@@ -20,3 +31,4 @@ export default class SnapsWebpackPlugin {
20
31
  */
21
32
  apply(compiler: Compiler): void;
22
33
  }
34
+ export {};
package/dist/plugin.js CHANGED
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const path_1 = __importDefault(require("path"));
3
7
  const snap_utils_1 = require("@metamask/snap-utils");
8
+ const webpack_1 = require("webpack");
4
9
  const webpack_sources_1 = require("webpack-sources");
5
10
  const PLUGIN_NAME = 'SnapsWebpackPlugin';
6
11
  class SnapsWebpackPlugin {
@@ -8,12 +13,18 @@ class SnapsWebpackPlugin {
8
13
  * Construct an instance of the plugin.
9
14
  *
10
15
  * @param options - The post-process options.
11
- * @param options.stripComments - Whether to strip comments. Defaults to `true`.
12
- * @param options.transformHtmlComments - Whether to transform HTML comments.
16
+ * @param options.stripComments - Whether to strip comments. Defaults to
17
+ * `true`.
18
+ * @param options.eval - Whether to evaluate the bundle to test SES
19
+ * compatibility. Defaults to `true`.
20
+ * @param options.manifestPath - The path to the manifest file. If provided,
21
+ * the manifest will be validated. Defaults to
22
+ * `process.cwd() + '/snap.manifest.json'`.
23
+ * @param options.writeManifest - Whether to fix the manifest.
13
24
  * Defaults to `true`.
14
25
  */
15
- constructor(options = {}) {
16
- this.options = options;
26
+ constructor(options) {
27
+ this.options = Object.assign({ eval: true, manifestPath: path_1.default.join(process.cwd(), 'snap.manifest.json'), writeManifest: true }, options);
17
28
  }
18
29
  /**
19
30
  * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
@@ -28,17 +39,33 @@ class SnapsWebpackPlugin {
28
39
  Object.keys(assets).forEach((assetName) => {
29
40
  const asset = assets[assetName];
30
41
  const processed = (0, snap_utils_1.postProcessBundle)(asset.source(), Object.assign(Object.assign({}, this.options), { sourceMap: Boolean(devtool), inputSourceMap: devtool ? asset.map() : undefined }));
31
- if (processed) {
32
- const replacement = processed.sourceMap
33
- ? new webpack_sources_1.SourceMapSource(processed.code, assetName, processed.sourceMap)
34
- : new webpack_sources_1.RawSource(processed.code);
35
- // For some reason the type of `RawSource` is not compatible with Webpack's own
36
- // `Source`, but works fine when casting it to `any`.
37
- compilation.updateAsset(assetName, replacement);
38
- }
42
+ const replacement = processed.sourceMap
43
+ ? new webpack_sources_1.SourceMapSource(processed.code, assetName, processed.sourceMap)
44
+ : new webpack_sources_1.RawSource(processed.code);
45
+ // For some reason the type of `RawSource` is not compatible with Webpack's own
46
+ // `Source`, but works fine when casting it to `any`.
47
+ compilation.updateAsset(assetName, replacement);
39
48
  });
40
49
  });
41
50
  });
51
+ compiler.hooks.assetEmitted.tapPromise(PLUGIN_NAME, async (file, { outputPath, content, compilation }) => {
52
+ if (!file.endsWith('.js')) {
53
+ return;
54
+ }
55
+ const filePath = path_1.default.join(outputPath, file);
56
+ if (this.options.eval) {
57
+ await (0, snap_utils_1.evalBundle)(filePath);
58
+ }
59
+ if (this.options.manifestPath) {
60
+ const { errors, warnings } = await (0, snap_utils_1.checkManifest)(path_1.default.dirname(this.options.manifestPath), this.options.writeManifest, content.toString('utf-8'));
61
+ if (!this.options.writeManifest && errors.length > 0) {
62
+ throw new Error(`Manifest Error: The manifest is invalid.\n${errors.join('\n')}`);
63
+ }
64
+ if (warnings.length > 0) {
65
+ compilation.warnings.push(new webpack_1.WebpackError(`${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\n${warnings.join('\n')}`));
66
+ }
67
+ }
68
+ });
42
69
  }
43
70
  }
44
71
  exports.default = SnapsWebpackPlugin;
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;AAAA,qDAI8B;AAE9B,qDAA6D;AAE7D,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAIzC,MAAqB,kBAAkB;IAGrC;;;;;;;OAOG;IACH,YAAY,UAA4B,EAAE;QACxC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,QAAkB;QACtB,MAAM,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC,OAAO,CAAC;QAErC,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,WAAW,EAAE,EAAE;YAC1D,WAAW,CAAC,KAAK,CAAC,aAAa,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,MAAM,EAAE,EAAE;gBAC1D,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE;oBACxC,MAAM,KAAK,GAAG,MAAM,C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AC,CAAC;YAElD,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE;gBACrB,MAAM,IAAA,uBAAU,EAAC,QAAQ,CAAC,CAAC;aAC5B;YAED,IAAI,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE;gBAC7B,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,IAAA,0BAAa,EAC9C,cAAS,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,EAC5C,IAAI,CAAC,OAAO,CAAC,aAAa,EAC1B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAC1B,CAAC;gBAEF,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,IAAI,MAAM,CAAC,MAAM,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,CACF,CAAC;IACJ,CAAC;CACF;AAnGD,qCAmGC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-webpack-plugin",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "keywords": [
5
5
  "webpack",
6
6
  "plugin"
@@ -29,7 +29,7 @@
29
29
  "publish:package": "../../scripts/publish-package.sh"
30
30
  },
31
31
  "dependencies": {
32
- "@metamask/snap-utils": "^0.20.0",
32
+ "@metamask/snap-utils": "^0.21.0",
33
33
  "webpack-sources": "^3.2.3"
34
34
  },
35
35
  "devDependencies": {
@@ -48,13 +48,13 @@
48
48
  "eslint-plugin-jsdoc": "^36.1.0",
49
49
  "eslint-plugin-node": "^11.1.0",
50
50
  "eslint-plugin-prettier": "^3.4.0",
51
- "jest": "^27.5.1",
51
+ "jest": "^29.0.2",
52
52
  "jest-it-up": "^2.0.0",
53
53
  "memfs": "^3.4.1",
54
54
  "prettier": "^2.3.2",
55
55
  "prettier-plugin-packagejson": "^2.2.11",
56
56
  "rimraf": "^3.0.2",
57
- "ts-jest": "^27.1.5",
57
+ "ts-jest": "^29.0.0",
58
58
  "typescript": "^4.4.0",
59
59
  "webpack": "^5.72.1"
60
60
  },