@metamask/snaps-rollup-plugin 0.24.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/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2022 MetaMask
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # @metamask/snaps-rollup-plugin
2
+
3
+ A plugin for developing [MetaMask Snaps](https://docs.metamask.io/guide/snaps.html) using [Rollup](https://rollupjs.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
+
5
+ ## Installation
6
+
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
+
9
+ Install a dependency in your snap project using `yarn` or `npm`:
10
+
11
+ - `npm install @metamask/snaps-rollup-plugin`
12
+ - `yarn add @metamask/snaps-rollup-plugin`
13
+
14
+ ## Usage
15
+
16
+ Add the plugin to the `plugins` array in your Rollup configuration:
17
+
18
+ ```ts
19
+ // rollup.config.js
20
+
21
+ import snaps from '@metamask/snaps-rollup-plugin';
22
+
23
+ export default {
24
+ plugins: [snaps(options)],
25
+ };
26
+ ```
27
+
28
+ ### Options
29
+
30
+ All options are optional, and default to `true`.
31
+
32
+ ```ts
33
+ import { Options } from '@metamask/snaps-rollup-plugin';
34
+
35
+ const options: Options = {
36
+ /**
37
+ * Whether to strip all comments from the bundle.
38
+ */
39
+ stripComments: true,
40
+
41
+ /**
42
+ * Whether to evaluate the bundle with SES, to ensure SES compatibility.
43
+ */
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,
59
+ };
60
+ ```
@@ -0,0 +1 @@
1
+ declare const foo = "bar";
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ // This file is only used for testing source map generation.
3
+ // eslint-disable-next-line import/unambiguous
4
+ const foo = 'bar';
5
+ console.log(foo);
6
+ //# sourceMappingURL=source-map.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"source-map.js","sourceRoot":"","sources":["../../src/__fixtures__/source-map.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAE5D,8CAA8C;AAC9C,MAAM,GAAG,GAAG,KAAK,CAAC;AAClB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC","sourcesContent":["// This file is only used for testing source map generation.\n\n// eslint-disable-next-line import/unambiguous\nconst foo = 'bar';\nconsole.log(foo);\n"]}
@@ -0,0 +1,2 @@
1
+ export { default } from './plugin';
2
+ export type { Options } from './plugin';
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
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
@@ -0,0 +1 @@
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"]}
@@ -0,0 +1,24 @@
1
+ import { PostProcessOptions } from '@metamask/snaps-utils';
2
+ import { Plugin } from 'rollup';
3
+ declare type PluginOptions = {
4
+ eval?: boolean;
5
+ manifestPath?: string;
6
+ writeManifest?: boolean;
7
+ };
8
+ export declare type Options = PluginOptions & Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;
9
+ /**
10
+ * Creates a Snaps Rollup plugin instance.
11
+ *
12
+ * @param options - The plugin options.
13
+ * @param options.stripComments - Whether to strip comments. Defaults to `true`.
14
+ * @param options.eval - Whether to evaluate the bundle to test SES
15
+ * compatibility. Defaults to `true`.
16
+ * @param options.manifestPath - The path to the manifest file. If provided,
17
+ * the manifest will be validated. Defaults to
18
+ * `process.cwd() + '/snap.manifest.json'`.
19
+ * @param options.writeManifest - Whether to fix the manifest. Defaults to
20
+ * `true`.
21
+ * @returns The Rollup plugin object.
22
+ */
23
+ export default function snaps(options?: Partial<Options>): Plugin;
24
+ export {};
package/dist/plugin.js ADDED
@@ -0,0 +1,59 @@
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 fs_1 = require("fs");
7
+ const path_1 = __importDefault(require("path"));
8
+ const snaps_utils_1 = require("@metamask/snaps-utils");
9
+ /**
10
+ * Creates a Snaps Rollup plugin instance.
11
+ *
12
+ * @param options - The plugin options.
13
+ * @param options.stripComments - Whether to strip comments. Defaults to `true`.
14
+ * @param options.eval - Whether to evaluate the bundle to test SES
15
+ * compatibility. Defaults to `true`.
16
+ * @param options.manifestPath - The path to the manifest file. If provided,
17
+ * the manifest will be validated. Defaults to
18
+ * `process.cwd() + '/snap.manifest.json'`.
19
+ * @param options.writeManifest - Whether to fix the manifest. Defaults to
20
+ * `true`.
21
+ * @returns The Rollup plugin object.
22
+ */
23
+ function snaps(options) {
24
+ const defaultOptions = Object.assign({ eval: true, manifestPath: path_1.default.join(process.cwd(), 'snap.manifest.json'), writeManifest: true }, options);
25
+ return {
26
+ name: '@metamask/snaps-rollup-plugin',
27
+ renderChunk(code) {
28
+ // Rollup internally merges the new source map with the old one, so there
29
+ // is no need to pass the current source map to the function.
30
+ const result = (0, snaps_utils_1.postProcessBundle)(code, Object.assign(Object.assign({}, defaultOptions), { sourceMap: true }));
31
+ if (result.warnings.length > 0) {
32
+ this.warn(`Bundle Warning: Processing of the Snap bundle completed with warnings.\n${result.warnings.join('\n')}`);
33
+ }
34
+ return { code: result.code, map: result.sourceMap };
35
+ },
36
+ async writeBundle(output) {
37
+ if (!output.file) {
38
+ this.warn('No output file specified, skipping bundle validation.');
39
+ return;
40
+ }
41
+ if (defaultOptions.eval) {
42
+ await (0, snaps_utils_1.evalBundle)(output.file).catch((error) => {
43
+ this.error(error);
44
+ });
45
+ }
46
+ if (defaultOptions.manifestPath) {
47
+ const { errors, warnings } = await (0, snaps_utils_1.checkManifest)(path_1.default.dirname(defaultOptions.manifestPath), defaultOptions.writeManifest, await fs_1.promises.readFile(output.file, 'utf8'));
48
+ if (!defaultOptions.writeManifest && errors.length > 0) {
49
+ this.error(`Manifest Error: The manifest is invalid.\n${errors.join('\n')}`);
50
+ }
51
+ if (warnings.length > 0) {
52
+ this.warn(`Manifest Warning: Validation of snap.manifest.json completed with warnings.\n${warnings.join('\n')}`);
53
+ }
54
+ }
55
+ },
56
+ };
57
+ }
58
+ exports.default = snaps;
59
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;;;AAAA,2BAAoC;AACpC,gDAA6B;AAC7B,uDAK+B;AAY/B;;;;;;;;;;;;;GAaG;AACH,SAAwB,KAAK,CAAC,OAA0B;IACtD,MAAM,cAAc,mBAClB,IAAI,EAAE,IAAI,EACV,YAAY,EAAE,cAAS,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,oBAAoB,CAAC,EACjE,aAAa,EAAE,IAAI,IAChB,OAAO,CACX,CAAC;IAEF,OAAO;QACL,IAAI,EAAE,+BAA+B;QAErC,WAAW,CAAC,IAAY;YACtB,yEAAyE;YACzE,6DAA6D;YAC7D,MAAM,MAAM,GAAG,IAAA,+BAAiB,EAAC,IAAI,kCAChC,cAAc,KACjB,SAAS,EAAE,IAAI,IACf,CAAC;YAEH,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;gBAC9B,IAAI,CAAC,IAAI,CACP,2EAA2E,MAAM,CAAC,QAAQ,CAAC,IAAI,CAC7F,IAAI,CACL,EAAE,CACJ,CAAC;aACH;YAED,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,SAAS,EAAE,CAAC;QACtD,CAAC;QAED,KAAK,CAAC,WAAW,CAAC,MAAM;YACtB,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE;gBAChB,IAAI,CAAC,IAAI,CAAC,uDAAuD,CAAC,CAAC;gBACnE,OAAO;aACR;YAED,IAAI,cAAc,CAAC,IAAI,EAAE;gBACvB,MAAM,IAAA,wBAAU,EAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;oBAC5C,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBACpB,CAAC,CAAC,CAAC;aACJ;YAED,IAAI,cAAc,CAAC,YAAY,EAAE;gBAC/B,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,IAAA,2BAAa,EAC9C,cAAS,CAAC,OAAO,CAAC,cAAc,CAAC,YAAY,CAAC,EAC9C,cAAc,CAAC,aAAa,EAC5B,MAAM,aAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CACvC,CAAC;gBAEF,IAAI,CAAC,cAAc,CAAC,aAAa,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;oBACtD,IAAI,CAAC,KAAK,CACR,6CAA6C,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CACjE,CAAC;iBACH;gBAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;oBACvB,IAAI,CAAC,IAAI,CACP,gFAAgF,QAAQ,CAAC,IAAI,CAC3F,IAAI,CACL,EAAE,CACJ,CAAC;iBACH;aACF;QACH,CAAC;KACF,CAAC;AACJ,CAAC;AAjED,wBAiEC","sourcesContent":["import { promises as fs } from 'fs';\nimport pathUtils from 'path';\nimport {\n checkManifest,\n evalBundle,\n postProcessBundle,\n PostProcessOptions,\n} from '@metamask/snaps-utils';\nimport { Plugin, SourceMapInput } from 'rollup';\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/**\n * Creates a Snaps Rollup plugin instance.\n *\n * @param options - The plugin options.\n * @param options.stripComments - Whether to strip comments. Defaults to `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. Defaults to\n * `true`.\n * @returns The Rollup plugin object.\n */\nexport default function snaps(options?: Partial<Options>): Plugin {\n const defaultOptions = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n\n return {\n name: '@metamask/snaps-rollup-plugin',\n\n renderChunk(code: string): { code: string; map?: SourceMapInput } | null {\n // Rollup internally merges the new source map with the old one, so there\n // is no need to pass the current source map to the function.\n const result = postProcessBundle(code, {\n ...defaultOptions,\n sourceMap: true,\n });\n\n if (result.warnings.length > 0) {\n this.warn(\n `Bundle Warning: Processing of the Snap bundle completed with warnings.\\n${result.warnings.join(\n '\\n',\n )}`,\n );\n }\n\n return { code: result.code, map: result.sourceMap };\n },\n\n async writeBundle(output): Promise<void> {\n if (!output.file) {\n this.warn('No output file specified, skipping bundle validation.');\n return;\n }\n\n if (defaultOptions.eval) {\n await evalBundle(output.file).catch((error) => {\n this.error(error);\n });\n }\n\n if (defaultOptions.manifestPath) {\n const { errors, warnings } = await checkManifest(\n pathUtils.dirname(defaultOptions.manifestPath),\n defaultOptions.writeManifest,\n await fs.readFile(output.file, 'utf8'),\n );\n\n if (!defaultOptions.writeManifest && errors.length > 0) {\n this.error(\n `Manifest Error: The manifest is invalid.\\n${errors.join('\\n')}`,\n );\n }\n\n if (warnings.length > 0) {\n this.warn(\n `Manifest Warning: Validation of snap.manifest.json completed with warnings.\\n${warnings.join(\n '\\n',\n )}`,\n );\n }\n }\n },\n };\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@metamask/snaps-rollup-plugin",
3
+ "version": "0.24.0",
4
+ "keywords": [
5
+ "rollup",
6
+ "rollup-plugin"
7
+ ],
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/MetaMask/snaps-monorepo.git"
11
+ },
12
+ "main": "dist/index.js",
13
+ "files": [
14
+ "dist/"
15
+ ],
16
+ "scripts": {
17
+ "test": "jest && yarn posttest",
18
+ "posttest": "jest-it-up",
19
+ "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",
23
+ "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",
27
+ "build:clean": "yarn clean && yarn build",
28
+ "clean": "rimraf '*.tsbuildinfo' 'dist/*'",
29
+ "publish:package": "../../scripts/publish-package.sh"
30
+ },
31
+ "dependencies": {
32
+ "@metamask/snaps-utils": "^0.24.0"
33
+ },
34
+ "devDependencies": {
35
+ "@lavamoat/allow-scripts": "^2.0.3",
36
+ "@metamask/auto-changelog": "^2.6.0",
37
+ "@metamask/eslint-config": "^9.0.0",
38
+ "@metamask/eslint-config-jest": "^9.0.0",
39
+ "@metamask/eslint-config-nodejs": "^9.0.0",
40
+ "@metamask/eslint-config-typescript": "^9.0.1",
41
+ "@rollup/plugin-virtual": "^2.1.0",
42
+ "@types/jest": "^27.5.1",
43
+ "deepmerge": "^4.2.2",
44
+ "eslint": "^7.30.0",
45
+ "eslint-config-prettier": "^8.3.0",
46
+ "eslint-plugin-import": "^2.23.4",
47
+ "eslint-plugin-jest": "^24.4.0",
48
+ "eslint-plugin-jsdoc": "^36.1.0",
49
+ "eslint-plugin-node": "^11.1.0",
50
+ "eslint-plugin-prettier": "^3.4.0",
51
+ "jest": "^29.0.2",
52
+ "jest-it-up": "^2.0.0",
53
+ "memfs": "^3.4.7",
54
+ "prettier": "^2.3.2",
55
+ "prettier-plugin-packagejson": "^2.2.11",
56
+ "rimraf": "^3.0.2",
57
+ "rollup": "^2.72.1",
58
+ "ts-jest": "^29.0.0",
59
+ "typescript": "^4.4.0"
60
+ },
61
+ "engines": {
62
+ "node": ">=16.0.0"
63
+ },
64
+ "publishConfig": {
65
+ "access": "public",
66
+ "registry": "https://registry.npmjs.org/"
67
+ }
68
+ }