@metamask/snaps-cli 1.0.2 → 2.0.1

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.
Files changed (253) hide show
  1. package/.browserslistrc +3 -0
  2. package/CHANGELOG.md +35 -375
  3. package/README.md +548 -69
  4. package/dist/cjs/builders.js +148 -0
  5. package/dist/cjs/builders.js.map +1 -0
  6. package/dist/cjs/cli.js +35 -0
  7. package/dist/cjs/cli.js.map +1 -0
  8. package/dist/cjs/commands/build/build.js +54 -0
  9. package/dist/cjs/commands/build/build.js.map +1 -0
  10. package/dist/cjs/commands/build/implementation.js +31 -0
  11. package/dist/cjs/commands/build/implementation.js.map +1 -0
  12. package/dist/cjs/commands/build/index.js +45 -0
  13. package/dist/cjs/commands/build/index.js.map +1 -0
  14. package/dist/cjs/commands/eval/eval.js +57 -0
  15. package/dist/cjs/commands/eval/eval.js.map +1 -0
  16. package/dist/cjs/commands/eval/implementation.js +27 -0
  17. package/dist/cjs/commands/eval/implementation.js.map +1 -0
  18. package/dist/cjs/commands/eval/index.js +50 -0
  19. package/dist/cjs/commands/eval/index.js.map +1 -0
  20. package/dist/cjs/commands/index.js +30 -0
  21. package/dist/cjs/commands/index.js.map +1 -0
  22. package/dist/cjs/commands/manifest/implementation.js +34 -0
  23. package/dist/cjs/commands/manifest/implementation.js.map +1 -0
  24. package/dist/cjs/commands/manifest/index.js +50 -0
  25. package/dist/cjs/commands/manifest/index.js.map +1 -0
  26. package/dist/cjs/commands/manifest/manifest.js +55 -0
  27. package/dist/cjs/commands/manifest/manifest.js.map +1 -0
  28. package/dist/cjs/commands/serve/index.js +33 -0
  29. package/dist/cjs/commands/serve/index.js.map +1 -0
  30. package/dist/cjs/commands/serve/serve.js +21 -0
  31. package/dist/cjs/commands/serve/serve.js.map +1 -0
  32. package/dist/cjs/commands/watch/implementation.js +35 -0
  33. package/dist/cjs/commands/watch/implementation.js.map +1 -0
  34. package/dist/cjs/commands/watch/index.js +47 -0
  35. package/dist/cjs/commands/watch/index.js.map +1 -0
  36. package/dist/cjs/commands/watch/watch.js +51 -0
  37. package/dist/cjs/commands/watch/watch.js.map +1 -0
  38. package/dist/cjs/config.js +323 -0
  39. package/dist/cjs/config.js.map +1 -0
  40. package/dist/cjs/errors.js +37 -0
  41. package/dist/cjs/errors.js.map +1 -0
  42. package/dist/cjs/index.js +77 -0
  43. package/dist/cjs/index.js.map +1 -0
  44. package/dist/cjs/main.js +19 -0
  45. package/dist/cjs/main.js.map +1 -0
  46. package/dist/cjs/utils/cli.js +61 -0
  47. package/dist/cjs/utils/cli.js.map +1 -0
  48. package/dist/cjs/utils/errors.js +42 -0
  49. package/dist/cjs/utils/errors.js.map +1 -0
  50. package/dist/cjs/utils/index.js +25 -0
  51. package/dist/cjs/utils/index.js.map +1 -0
  52. package/dist/cjs/utils/legacy.js +48 -0
  53. package/dist/cjs/utils/legacy.js.map +1 -0
  54. package/dist/cjs/utils/logging.js +46 -0
  55. package/dist/cjs/utils/logging.js.map +1 -0
  56. package/dist/cjs/utils/path.js +16 -0
  57. package/dist/cjs/utils/path.js.map +1 -0
  58. package/dist/cjs/utils/steps.js +52 -0
  59. package/dist/cjs/utils/steps.js.map +1 -0
  60. package/dist/cjs/webpack/compiler.js +96 -0
  61. package/dist/cjs/webpack/compiler.js.map +1 -0
  62. package/dist/cjs/webpack/config.js +250 -0
  63. package/dist/cjs/webpack/config.js.map +1 -0
  64. package/dist/cjs/webpack/index.js +22 -0
  65. package/dist/cjs/webpack/index.js.map +1 -0
  66. package/dist/cjs/webpack/loaders/browserify.js +97 -0
  67. package/dist/cjs/webpack/loaders/browserify.js.map +1 -0
  68. package/dist/cjs/webpack/loaders/wasm.js +36 -0
  69. package/dist/cjs/webpack/loaders/wasm.js.map +1 -0
  70. package/dist/cjs/webpack/plugins.js +340 -0
  71. package/dist/cjs/webpack/plugins.js.map +1 -0
  72. package/dist/cjs/webpack/utils.js +181 -0
  73. package/dist/cjs/webpack/utils.js.map +1 -0
  74. package/dist/{builders.js → esm/builders.js} +48 -51
  75. package/dist/esm/builders.js.map +1 -0
  76. package/dist/esm/cli.js +26 -0
  77. package/dist/esm/cli.js.map +1 -0
  78. package/dist/esm/commands/build/build.js +51 -0
  79. package/dist/esm/commands/build/build.js.map +1 -0
  80. package/dist/esm/commands/build/implementation.js +27 -0
  81. package/dist/esm/commands/build/implementation.js.map +1 -0
  82. package/dist/esm/commands/build/index.js +17 -0
  83. package/dist/esm/commands/build/index.js.map +1 -0
  84. package/dist/esm/commands/eval/eval.js +54 -0
  85. package/dist/esm/commands/eval/eval.js.map +1 -0
  86. package/dist/esm/commands/eval/implementation.js +24 -0
  87. package/dist/esm/commands/eval/implementation.js.map +1 -0
  88. package/dist/esm/commands/eval/index.js +22 -0
  89. package/dist/esm/commands/eval/index.js.map +1 -0
  90. package/dist/esm/commands/index.js +15 -0
  91. package/dist/esm/commands/index.js.map +1 -0
  92. package/dist/esm/commands/manifest/implementation.js +33 -0
  93. package/dist/esm/commands/manifest/implementation.js.map +1 -0
  94. package/dist/esm/commands/manifest/index.js +22 -0
  95. package/dist/esm/commands/manifest/index.js.map +1 -0
  96. package/dist/esm/commands/manifest/manifest.js +52 -0
  97. package/dist/esm/commands/manifest/manifest.js.map +1 -0
  98. package/dist/esm/commands/serve/index.js +18 -0
  99. package/dist/esm/commands/serve/index.js.map +1 -0
  100. package/dist/esm/commands/serve/serve.js +17 -0
  101. package/dist/esm/commands/serve/serve.js.map +1 -0
  102. package/dist/esm/commands/watch/implementation.js +34 -0
  103. package/dist/esm/commands/watch/implementation.js.map +1 -0
  104. package/dist/esm/commands/watch/index.js +19 -0
  105. package/dist/esm/commands/watch/index.js.map +1 -0
  106. package/dist/esm/commands/watch/watch.js +50 -0
  107. package/dist/esm/commands/watch/watch.js.map +1 -0
  108. package/dist/esm/config.js +327 -0
  109. package/dist/esm/config.js.map +1 -0
  110. package/dist/esm/errors.js +30 -0
  111. package/dist/esm/errors.js.map +1 -0
  112. package/dist/esm/index.js +6 -0
  113. package/dist/esm/index.js.map +1 -0
  114. package/dist/esm/main.js +10 -0
  115. package/dist/esm/main.js.map +1 -0
  116. package/dist/esm/utils/cli.js +43 -0
  117. package/dist/esm/utils/cli.js.map +1 -0
  118. package/dist/esm/utils/errors.js +43 -0
  119. package/dist/esm/utils/errors.js.map +1 -0
  120. package/dist/esm/utils/index.js +8 -0
  121. package/dist/esm/utils/index.js.map +1 -0
  122. package/dist/esm/utils/legacy.js +47 -0
  123. package/dist/esm/utils/legacy.js.map +1 -0
  124. package/dist/esm/utils/logging.js +40 -0
  125. package/dist/esm/utils/logging.js.map +1 -0
  126. package/dist/esm/utils/path.js +13 -0
  127. package/dist/esm/utils/path.js.map +1 -0
  128. package/dist/esm/utils/steps.js +43 -0
  129. package/dist/esm/utils/steps.js.map +1 -0
  130. package/dist/esm/webpack/compiler.js +89 -0
  131. package/dist/esm/webpack/compiler.js.map +1 -0
  132. package/dist/esm/webpack/config.js +252 -0
  133. package/dist/esm/webpack/config.js.map +1 -0
  134. package/dist/esm/webpack/index.js +5 -0
  135. package/dist/esm/webpack/index.js.map +1 -0
  136. package/dist/esm/webpack/loaders/browserify.js +82 -0
  137. package/dist/esm/webpack/loaders/browserify.js.map +1 -0
  138. package/dist/esm/webpack/loaders/wasm.js +26 -0
  139. package/dist/esm/webpack/loaders/wasm.js.map +1 -0
  140. package/dist/esm/webpack/plugins.js +343 -0
  141. package/dist/esm/webpack/plugins.js.map +1 -0
  142. package/dist/esm/webpack/utils.js +208 -0
  143. package/dist/esm/webpack/utils.js.map +1 -0
  144. package/dist/types/__fixtures__/configs/cjs.d.ts +1 -0
  145. package/dist/types/__fixtures__/configs/esm.d.ts +3 -0
  146. package/dist/types/__fixtures__/configs/invalid.d.ts +3 -0
  147. package/dist/types/__fixtures__/configs/javascript/snap.config.d.ts +0 -0
  148. package/dist/types/__fixtures__/configs/typescript/snap.config.d.ts +3 -0
  149. package/dist/types/builders.d.ts +8 -0
  150. package/dist/{cli.d.ts → types/cli.d.ts} +1 -1
  151. package/dist/types/commands/build/build.d.ts +10 -0
  152. package/dist/types/commands/build/implementation.d.ts +10 -0
  153. package/dist/types/commands/build/index.d.ts +10 -0
  154. package/dist/types/commands/eval/__test__/browserify/bad/snap.config.d.ts +3 -0
  155. package/dist/types/commands/eval/__test__/browserify/good/snap.config.d.ts +3 -0
  156. package/dist/types/commands/eval/__test__/webpack/bad/snap.config.d.ts +3 -0
  157. package/dist/types/commands/eval/__test__/webpack/good/snap.config.d.ts +3 -0
  158. package/dist/types/commands/eval/__test__/webpack/snap.config.d.ts +3 -0
  159. package/dist/types/commands/eval/eval.d.ts +14 -0
  160. package/dist/types/commands/eval/implementation.d.ts +9 -0
  161. package/dist/types/commands/eval/index.d.ts +10 -0
  162. package/dist/types/commands/manifest/implementation.d.ts +12 -0
  163. package/dist/types/commands/manifest/index.d.ts +10 -0
  164. package/dist/types/commands/manifest/manifest.d.ts +14 -0
  165. package/dist/{cmds/build → types/commands/serve}/index.d.ts +4 -4
  166. package/dist/types/commands/serve/serve.d.ts +16 -0
  167. package/dist/types/commands/watch/implementation.d.ts +14 -0
  168. package/dist/types/commands/watch/index.d.ts +10 -0
  169. package/dist/types/commands/watch/watch.d.ts +19 -0
  170. package/dist/types/config.d.ts +792 -0
  171. package/dist/types/errors.d.ts +25 -0
  172. package/dist/types/index.d.ts +5 -0
  173. package/dist/types/utils/cli.d.ts +17 -0
  174. package/dist/types/utils/errors.d.ts +23 -0
  175. package/dist/types/utils/index.d.ts +6 -0
  176. package/dist/types/utils/legacy.d.ts +27 -0
  177. package/dist/types/utils/logging.d.ts +22 -0
  178. package/dist/types/utils/path.d.ts +9 -0
  179. package/dist/types/utils/steps.d.ts +17 -0
  180. package/dist/types/webpack/compiler.d.ts +29 -0
  181. package/dist/types/webpack/config.d.ts +37 -0
  182. package/dist/types/webpack/index.d.ts +3 -0
  183. package/dist/types/webpack/loaders/browserify.d.ts +16 -0
  184. package/dist/types/webpack/loaders/wasm.d.ts +20 -0
  185. package/dist/types/webpack/plugins.d.ts +147 -0
  186. package/dist/types/webpack/utils.d.ts +188 -0
  187. package/package.json +83 -43
  188. package/dist/builders.d.ts +0 -27
  189. package/dist/builders.js.map +0 -1
  190. package/dist/cli.js +0 -51
  191. package/dist/cli.js.map +0 -1
  192. package/dist/cmds/build/buildHandler.d.ts +0 -13
  193. package/dist/cmds/build/buildHandler.js +0 -37
  194. package/dist/cmds/build/buildHandler.js.map +0 -1
  195. package/dist/cmds/build/bundle.d.ts +0 -15
  196. package/dist/cmds/build/bundle.js +0 -80
  197. package/dist/cmds/build/bundle.js.map +0 -1
  198. package/dist/cmds/build/index.js +0 -29
  199. package/dist/cmds/build/index.js.map +0 -1
  200. package/dist/cmds/build/utils.d.ts +0 -56
  201. package/dist/cmds/build/utils.js +0 -100
  202. package/dist/cmds/build/utils.js.map +0 -1
  203. package/dist/cmds/eval/evalHandler.d.ts +0 -9
  204. package/dist/cmds/eval/evalHandler.js +0 -25
  205. package/dist/cmds/eval/evalHandler.js.map +0 -1
  206. package/dist/cmds/eval/index.d.ts +0 -9
  207. package/dist/cmds/eval/index.js +0 -15
  208. package/dist/cmds/eval/index.js.map +0 -1
  209. package/dist/cmds/index.js +0 -14
  210. package/dist/cmds/index.js.map +0 -1
  211. package/dist/cmds/init/index.d.ts +0 -9
  212. package/dist/cmds/init/index.js +0 -34
  213. package/dist/cmds/init/index.js.map +0 -1
  214. package/dist/cmds/init/initHandler.d.ts +0 -26
  215. package/dist/cmds/init/initHandler.js +0 -69
  216. package/dist/cmds/init/initHandler.js.map +0 -1
  217. package/dist/cmds/init/initUtils.d.ts +0 -39
  218. package/dist/cmds/init/initUtils.js +0 -122
  219. package/dist/cmds/init/initUtils.js.map +0 -1
  220. package/dist/cmds/manifest/index.d.ts +0 -9
  221. package/dist/cmds/manifest/index.js +0 -24
  222. package/dist/cmds/manifest/index.js.map +0 -1
  223. package/dist/cmds/manifest/manifestHandler.d.ts +0 -10
  224. package/dist/cmds/manifest/manifestHandler.js +0 -50
  225. package/dist/cmds/manifest/manifestHandler.js.map +0 -1
  226. package/dist/cmds/serve/index.d.ts +0 -9
  227. package/dist/cmds/serve/index.js +0 -15
  228. package/dist/cmds/serve/index.js.map +0 -1
  229. package/dist/cmds/serve/serveHandler.d.ts +0 -10
  230. package/dist/cmds/serve/serveHandler.js +0 -59
  231. package/dist/cmds/serve/serveHandler.js.map +0 -1
  232. package/dist/cmds/serve/serveUtils.d.ts +0 -22
  233. package/dist/cmds/serve/serveUtils.js +0 -39
  234. package/dist/cmds/serve/serveUtils.js.map +0 -1
  235. package/dist/cmds/watch/index.d.ts +0 -9
  236. package/dist/cmds/watch/index.js +0 -32
  237. package/dist/cmds/watch/index.js.map +0 -1
  238. package/dist/cmds/watch/watchHandler.d.ts +0 -14
  239. package/dist/cmds/watch/watchHandler.js +0 -100
  240. package/dist/cmds/watch/watchHandler.js.map +0 -1
  241. package/dist/main.js +0 -15
  242. package/dist/main.js.map +0 -1
  243. package/dist/utils/index.d.ts +0 -2
  244. package/dist/utils/index.js +0 -19
  245. package/dist/utils/index.js.map +0 -1
  246. package/dist/utils/misc.d.ts +0 -53
  247. package/dist/utils/misc.js +0 -156
  248. package/dist/utils/misc.js.map +0 -1
  249. package/dist/utils/snap-config.d.ts +0 -48
  250. package/dist/utils/snap-config.js +0 -117
  251. package/dist/utils/snap-config.js.map +0 -1
  252. /package/dist/{cmds → types/commands}/index.d.ts +0 -0
  253. /package/dist/{main.d.ts → types/main.d.ts} +0 -0
package/README.md CHANGED
@@ -7,10 +7,10 @@ A CLI for developing MetaMask Snaps.
7
7
  Use Node.js `16.0.0` or later.
8
8
  We recommend [nvm](https://github.com/nvm-sh/nvm) for managing Node.js versions.
9
9
 
10
- Install a dev dependency in your snap project or globally using `yarn` or `npm`:
10
+ Install a dev dependency in your snap project using `yarn` or `npm`:
11
11
 
12
- - `npm install -g @metamask/snaps-cli`
13
- - `yarn global add @metamask/snaps-cli`
12
+ - `npm install --save-dev @metamask/snaps-cli`
13
+ - `yarn add -D @metamask/snaps-cli`
14
14
 
15
15
  ## Usage
16
16
 
@@ -18,55 +18,55 @@ Install a dev dependency in your snap project or globally using `yarn` or `npm`:
18
18
  Usage: mm-snap <command> [options]
19
19
 
20
20
  Commands:
21
- mm-snap build Build Snap from source [aliases: b]
22
- mm-snap eval Attempt to evaluate Snap bundle in SES [aliases: e]
23
- mm-snap init [directory] Initialize Snap package [aliases: i]
24
- mm-snap manifest Validate the snap.manifest.json file [aliases: m]
25
- mm-snap serve Locally serve Snap file(s) for testing [aliases: s]
26
- mm-snap watch Build Snap on change [aliases: w]
21
+ mm-snap build Build snap from source [aliases: b]
22
+ mm-snap eval Attempt to evaluate snap bundle in SES [aliases: e]
23
+ mm-snap manifest Validate the snap.manifest.json file [aliases: m]
24
+ mm-snap serve Locally serve Snap file(s) for testing [aliases: s]
25
+ mm-snap watch Build Snap on change [aliases: w]
27
26
 
28
27
  Options:
29
28
  --version Show version number [boolean]
30
- --verboseErrors Display original errors [boolean] [default: true]
31
- --suppressWarnings Whether to suppress warnings[boolean] [default: false]
29
+ -c, --config Path to config file [string]
30
+ --verboseErrors Display original errors [deprecated] [boolean]
31
+ --suppressWarnings Whether to suppress warnings [deprecated] [boolean]
32
32
  -h, --help Show help [boolean]
33
33
 
34
34
  Examples:
35
- mm-snap init Initialize a snap project in the
36
- current directory
37
- mm-snap init my-snap Initialize a snap project in the
38
- 'my-snap' directory
39
- mm-snap build -s src/index.js -d out Build 'src/index.js' as
40
- './out/bundle.js'
41
- mm-snap build -s src/index.js -d out -n Build 'src/index.js' as
42
- snap.js './out/snap.js'
43
- mm-snap serve -r out Serve files in './out' on port 8080
44
- mm-snap serve -r out -p 9000 Serve files in './out' on port 9000
45
- mm-snap watch -s src/index.js -d out Rebuild './out/bundle.js' on changes
46
- to files in 'src/index.js' parent
47
- and child directories
35
+ mm-snap build Build './src/index.js' as
36
+ './dist/bundle.js'
37
+ mm-snap build --config Build './src/index.js' as
38
+ ./snap.config.build.ts './dist/bundle.js' using the config
39
+ in './snap.config.build.ts'
40
+ mm-snap manifest --fix Check the snap manifest, and fix any
41
+ errors
42
+ mm-snap watch --port 8000 The snap input file for changes, and
43
+ serve it on port 8000
44
+ mm-snap serve --port 8000 Serve the snap bundle on port 8000
48
45
  ```
49
46
 
50
47
  ## MetaMask Snaps
51
48
 
52
- MetaMask Snaps enables trustlessly extending the functionality of MetaMask at runtime.
53
- A Snap consist of two things: a JSON manifest and a JavaScript bundle.
54
- At present, Snaps can be published as npm packages on the public npm registry, or hosted locally during development.
55
- In the future, it will be possible to publish snaps on many different platforms, including arbitrary npm registries and IPFS.
49
+ MetaMask Snaps enables trustlessly extending the functionality of MetaMask at
50
+ runtime. A snap consist of two things: a JSON manifest and a JavaScript bundle.
51
+ At present, snaps can be published as npm packages on the public npm registry,
52
+ or hosted locally during development. In the future, it will be possible to
53
+ publish snaps on many different platforms, including arbitrary npm registries
54
+ and IPFS.
56
55
 
57
- We recommend building your Snap using this tool.
58
- You can bundle your Snap using your own tools, but it must run in SES and only use the global APIs that MetaMask exposes at runtime.
59
- Although Snaps currently execute in the browser, some browser APIs are not available to Snaps, and Snaps do not have DOM access.
56
+ We recommend building your snap using this tool. You can bundle your snap using
57
+ your own tools, but it must run in SES and only use the global APIs that
58
+ MetaMask exposes at runtime. Although snaps currently execute in the browser,
59
+ some browser APIs are not available to snaps, and snaps do not have DOM access.
60
60
 
61
61
  ### The Snap Manifest
62
62
 
63
- Your manifest must be named `snap.manifest.json` and located in the root directory of your npm package.
64
- Here's an example manifest:
63
+ Your manifest must be named `snap.manifest.json` and located in the root
64
+ directory of your npm package. Here's an example manifest:
65
65
 
66
66
  ```json
67
67
  {
68
68
  "version": "0.7.0",
69
- "proposedName": "@metamask/template-snap",
69
+ "proposedName": "Dialog Example",
70
70
  "description": "A MetaMask Snap template.",
71
71
  "repository": {
72
72
  "type": "git",
@@ -84,15 +84,21 @@ Here's an example manifest:
84
84
  }
85
85
  },
86
86
  "initialPermissions": {
87
- "snap_confirm": {}
87
+ "snap_dialog": {},
88
+ "endowment:rpc": {
89
+ "dapps": true,
90
+ "snaps": false
91
+ }
88
92
  },
89
93
  "manifestVersion": "0.1"
90
94
  }
91
95
  ```
92
96
 
93
- Refer to [the Snaps publishing specification](https://github.com/MetaMask/specifications/blob/main/snaps/publishing.md) and the [manifest JSON schema](https://github.com/MetaMask/snaps-monorepo/blob/main/packages/controllers/src/snaps/json-schemas/snap-manifest.schema.json) for details.
97
+ Refer to [the Snaps publishing specification](https://github.com/MetaMask/specifications/blob/main/snaps/publishing.md)
98
+ for more information about the manifest.
94
99
 
95
- > **ATTN:** If your Snap is not compatible with the publishing specification, your Snap may not work properly or install at all.
100
+ > **Note**: If your snap is not compatible with the publishing specification,
101
+ > your snap may not work properly or install at all.
96
102
 
97
103
  ### Assumed Project Structure
98
104
 
@@ -109,56 +115,529 @@ snap-project/
109
115
  ├─ ... (all other project files and folders)
110
116
  ```
111
117
 
112
- Source files other than `index.js` are located through its imports.
113
- The defaults can be overwritten using the `snap.config.js` [config file](#configuration-file).
118
+ Source files other than `index.js` are located through its imports. The
119
+ defaults can be overwritten using the `snap.config.js` or (`snap.config.ts`)
120
+ [config file](#configuration-file).
121
+
122
+ ## Configuration
123
+
124
+ The MetaMask Snaps CLI supports a configuration file. It can be either:
114
125
 
115
- ### Configuration File
126
+ - `snap.config.js` - a CommonJS module.
127
+ - `snap.config.ts` - a TypeScript module.
116
128
 
117
- `snap.config.js` should be placed in the project root directory. It can override cli options - the property `cliOptions` should have string keys matching command arguments. Values become argument defaults, which can still be overridden on the command line.
129
+ The file should be placed in the project root directory.
118
130
 
119
- Example:
131
+ ### Example
132
+
133
+ #### JavaScript
120
134
 
121
135
  ```javascript
122
136
  module.exports = {
123
- cliOptions: {
124
- src: 'lib/index.js',
125
- dist: 'out',
137
+ bundler: 'webpack', // default: 'browserify'
138
+ input: 'src/index.js',
139
+ output: {
140
+ path: 'dist',
141
+ },
142
+ server: {
126
143
  port: 9000,
127
144
  },
128
145
  };
129
146
  ```
130
147
 
131
- If you want to customize the Browserify build process, you can provide `bundlerCustomizer` property. It's a function that takes one argument - [browserify object](https://github.com/browserify/browserify#api-example) which we use internally to bundle the snap. You can transform it in any way you want, for example adding plugins.
148
+ #### TypeScript
132
149
 
133
- Example:
150
+ The CLI has full support for TypeScript out of the box. `@metamask/snaps-cli`
151
+ exports a `SnapConfig` type that can be used to type the configuration object.
134
152
 
135
- ```javascript
136
- const brfs = require('brfs');
153
+ ```typescript
154
+ import type { SnapConfig } from '@metamask/snaps-cli';
137
155
 
138
- module.exports = {
139
- cliOptions: {
140
- /* ... */
156
+ const config: SnapConfig = {
157
+ bundler: 'webpack', // default: 'browserify'
158
+ input: 'src/index.js',
159
+ output: {
160
+ path: 'dist',
141
161
  },
142
- bundlerCustomizer: (bundler) => {
143
- bundler.transform(brfs);
162
+ server: {
163
+ port: 9000,
144
164
  },
145
165
  };
166
+
167
+ export default config;
146
168
  ```
147
169
 
148
170
  The configuration file should not be published.
149
171
 
150
- ### Gotchas
151
-
152
- - Commands
153
- - `watch --src ... --dist ...` rebuilds on all changes in the parent directory
154
- of `src` and its children except:
155
- - `node_modules/`
156
- - `test/`, `tests/`, `**/*.test.js`, and `**/*.test.ts`
157
- - The specified `dist` directory
158
- - Dotfiles
159
- - `serve --root ... --port ...` serves the `root` directory on `localhost:port`
160
- - By default, `root` serves the current working directory: `.`
161
- - Arguments
162
- - `src` must be a file path when specified
163
- - `dist` and `root` must be directory paths when specified
164
- - `transpilationMode` must be at least `localOnly` for TypeScript Snaps
172
+ ### Options
173
+
174
+ - [`bundler`](#bundler)
175
+ - [`input`](#input)
176
+ - [`output`](#output)
177
+ - [`output.path`](#outputpath)
178
+ - [`output.filename`](#outputfilename)
179
+ - [`output.clean`](#outputclean)
180
+ - [`output.minimize`](#outputminimize)
181
+ - [`server`](#server)
182
+ - [`server.enabled`](#serverenabled)
183
+ - [`server.root`](#serverroot)
184
+ - [`server.port`](#serverport)
185
+ - [`environment`](#environment)
186
+ - [`stats`](#stats)
187
+ - [`stats.verbose`](#statsverbose)
188
+ - [`stats.builtIns`](#statsbuiltins)
189
+ - [`stats.buffer`](#statsbuffer)
190
+ - [`customizeWebpackConfig`](#customizewebpackconfig)
191
+ - [`experimental`](#experimental)
192
+ - [`experimental.wasm`](#experimentalwasm)
193
+
194
+ #### `bundler`
195
+
196
+ - Type: `"browserify"` | `"webpack"`
197
+ - Default: `"browserify"`
198
+
199
+ The bundler to use. The MetaMask Snaps CLI supports both Browserify and
200
+ Webpack. For backwards compatibility, Browserify is the default, but we
201
+ recommend using Webpack. Browserify support will be removed in the future.
202
+
203
+ This section describes the options for the Webpack bundler. For Browserify,
204
+ refer to the [legacy options](#legacy-options) section.
205
+
206
+ #### `input`
207
+
208
+ - Type: `string`
209
+ - Default: `"src/index.js"`
210
+
211
+ The entry point of your snap. This is the file that will be bundled.
212
+
213
+ #### `output`
214
+
215
+ - Type: `object`
216
+
217
+ The output configuration.
218
+
219
+ ##### `output.path`
220
+
221
+ - Type: `string`
222
+ - Default: `"dist"`
223
+
224
+ The output directory.
225
+
226
+ ##### `output.filename`
227
+
228
+ - Type: `string`
229
+ - Default: `"bundle.js"`
230
+
231
+ The output filename.
232
+
233
+ ##### `output.clean`
234
+
235
+ - Type: `boolean`
236
+ - Default: `false`
237
+
238
+ Whether to clean the output directory before building.
239
+
240
+ ##### `output.minimize`
241
+
242
+ - Type: `boolean`
243
+ - Default: `true`
244
+
245
+ Whether to minimize the bundle. This will remove comments and whitespace from
246
+ the bundle, mangle variable names, and perform other optimizations.
247
+
248
+ #### `sourceMap`
249
+
250
+ - Type: `boolean | "inline"`
251
+ - Default: `true`
252
+
253
+ Whether to generate a source map. If `"inline"`, the source map will be
254
+ inlined in the bundle. Otherwise, it will be written to a separate file.
255
+
256
+ #### `evaluate`
257
+
258
+ - Type: `boolean`
259
+
260
+ Whether to evaluate the bundle. This will check the bundle for compatibility
261
+ issues with the MetaMask Snaps runtime. If there are any issues, the CLI will
262
+ exit with an error.
263
+
264
+ #### `manifest`
265
+
266
+ - Type: `object`
267
+
268
+ The snap manifest configuration.
269
+
270
+ ##### `manifest.path`
271
+
272
+ - Type: `string`
273
+ - Default: `"snap.manifest.json"`
274
+
275
+ The path to the snap manifest, i.e., `snap.manifest.json`.
276
+
277
+ ##### `manifest.update`
278
+
279
+ - Type: `boolean`
280
+ - Default: `true`
281
+
282
+ Whether to update the manifest with the bundle shasum, and any other possible
283
+ updates. If `false`, the manifest will not be updated, and an error will be
284
+ thrown if the manifest is not up-to-date.
285
+
286
+ #### `server`
287
+
288
+ - Type: `object`
289
+
290
+ The development server configuration. The development server is used to test
291
+ your snap during development, using the `watch` and `serve` commands.
292
+
293
+ ##### `server.enabled`
294
+
295
+ - Type: `boolean`
296
+ - Default: `true`
297
+
298
+ Whether to enable the development server. If `false`, the development server
299
+ will not be started when running the `watch` command. This option has no effect
300
+ on the `serve` command.
301
+
302
+ ##### `server.root`
303
+
304
+ - Type: `string`
305
+ - Default: `process.cwd()`
306
+
307
+ The root directory of the development server. This is the directory that will
308
+ be served by the development server.
309
+
310
+ ##### `server.port`
311
+
312
+ - Type: `number`
313
+ - Default: `8081`
314
+
315
+ The port to run the development server on. If set to `0`, a random port will
316
+ be used.
317
+
318
+ #### `environment`
319
+
320
+ - Type: `Record<string, unknown>`
321
+
322
+ The environment configuration. This is used to set environment variables for
323
+ the snap, which can be accessed using `process.env`.
324
+
325
+ In addition to the environment variables set by the user, the following
326
+ environment variables are set by the CLI:
327
+
328
+ - `NODE_ENV` - `"production"`.
329
+ - `NODE_DEBUG` - `false`.
330
+ - `DEBUG` - `false`.
331
+
332
+ #### `stats`
333
+
334
+ - Type: `object`
335
+
336
+ The stats configuration, which controls the log output of the CLI.
337
+
338
+ ##### `stats.verbose`
339
+
340
+ - Type: `boolean`
341
+ - Default: `false`
342
+
343
+ Whether to enable verbose logging. If `true`, the CLI will log more
344
+ information.
345
+
346
+ ##### `stats.builtIns`
347
+
348
+ - Type: `false | object`
349
+ - Default: `{ ignore: [] }`
350
+
351
+ Whether to check for missing built-in modules. The MetaMask Snaps CLI does not
352
+ support Node.js built-ins out of the box, and any used built-ins must be
353
+ provided through the `customizeWebpackConfig` option. When enabled, the CLI
354
+ shows a warning if a built-in module is used, but not provided.
355
+
356
+ ###### `stats.builtIns.ignore`
357
+
358
+ - Type: `string[]`
359
+
360
+ A list of built-in modules to ignore. This is useful if the built-in module is
361
+ not actually used in the snap, but is added by a dependency.
362
+
363
+ ###### `stats.buffer`
364
+
365
+ - Type: `boolean`
366
+ - Default: `true`
367
+
368
+ Whether to show a warning if `Buffer` is used, but not provided. The `Buffer`
369
+ global is not available in the MetaMask Snaps runtime by default, and must
370
+ be provided through the `customizeWebpackConfig` option.
371
+
372
+ #### `customizeWebpackConfig`
373
+
374
+ - Type: `(config: webpack.Configuration) => webpack.Configuration`
375
+
376
+ A function that can be used to customize the Webpack configuration. This is
377
+ useful if you need to add a Webpack plugin, provide a polyfill, add a loader,
378
+ and so on.
379
+
380
+ The function receives the Webpack configuration object, and should return the
381
+ modified configuration object. For convenience, `@metamask/snaps-cli` exports
382
+ a `merge` function (re-exported from `webpack-merge`) that can be used to merge
383
+ the configuration object with the default configuration.
384
+
385
+ The default configuration can be found in [`src/webpack/config.ts`](./src/webpack/config.ts).
386
+
387
+ ```typescript
388
+ import type { SnapConfig } from '@metamask/snaps-cli';
389
+ import { merge } from '@metamask/snaps-cli';
390
+
391
+ const config: SnapConfig = {
392
+ customizeWebpackConfig: (config) =>
393
+ merge(config, {
394
+ plugins: [
395
+ // Add a plugin.
396
+ ],
397
+ module: {
398
+ rules: [
399
+ // Add a loader.
400
+ ],
401
+ },
402
+ }),
403
+ };
404
+
405
+ export default config;
406
+ ```
407
+
408
+ #### `experimental`
409
+
410
+ - Type: `object`
411
+
412
+ Experimental features. These features are not stable, and may change in the
413
+ future.
414
+
415
+ ##### `experimental.wasm`
416
+
417
+ - Type: `boolean`
418
+
419
+ Whether to enable WebAssembly support. When this is enabled, WebAssembly files
420
+ can be imported in the snap, for example:
421
+
422
+ ```typescript
423
+ import program from './program.wasm';
424
+
425
+ const module = await WebAssembly.instantiate(program, {});
426
+ // ...
427
+ ```
428
+
429
+ ### Legacy Options
430
+
431
+ > **Warning**: Using the Browserify-based build system is deprecated, and will
432
+ > be removed in a future version. Please migrate to the Webpack-based build
433
+ > system.
434
+
435
+ - [`bundler`](#bundler)
436
+ - [`cliOptions`](#clioptions)
437
+ - [`cliOptions.bundle`](#clioptionsbundle)
438
+ - [`cliOptions.dist`](#clioptionsdist)
439
+ - [`cliOptions.eval`](#clioptionseval)
440
+ - [`cliOptions.manifest`](#clioptionsmanifest)
441
+ - [`cliOptions.outfileName`](#clioptionsoutfilename)
442
+ - [`cliOptions.port`](#clioptionsport)
443
+ - [`cliOptions.root`](#clioptionsroot)
444
+ - [`cliOptions.sourceMaps`](#clioptionssourcemaps)
445
+ - [`cliOptions.src`](#clioptionssrc)
446
+ - [`cliOptions.stripComments`](#clioptionsstripcomments)
447
+ - [`cliOptions.transpilationMode`](#clioptionstranspilationmode)
448
+ - [`cliOptions.depsToTranspile`](#clioptionsdepstotranspile)
449
+ - [`cliOptions.writeManifest`](#clioptionswritemanifest)
450
+ - [`cliOptions.serve`](#clioptionsserve)
451
+ - [`cliOptions.verboseErrors`](#clioptionsverboseerrors)
452
+ - [`cliOptions.suppressWarnings`](#clioptionssuppresswarnings)
453
+ - [`bundlerCustomizer`](#bundlercustomizer)
454
+
455
+ #### `bundler`
456
+
457
+ - Type: `"browserify"` | `"webpack"`
458
+ - Default: `"browserify"`
459
+
460
+ The bundler to use. The MetaMask Snaps CLI supports both Browserify and
461
+ Webpack. For backwards compatibility, Browserify is the default, but we
462
+ recommend using Webpack. Browserify support will be removed in the future.
463
+
464
+ This section describes the options for the Browserify bundler. For Webpack,
465
+ refer to the [options](#options) section.
466
+
467
+ #### `cliOptions`
468
+
469
+ - Type: `object`
470
+
471
+ The CLI options. These can be specified using the CLI, but can also be
472
+ specified in the configuration file. For example, the `--port` option can be
473
+ specified in the configuration file as follows:
474
+
475
+ ```ts
476
+ export default {
477
+ cliOptions: {
478
+ port: 8081,
479
+ },
480
+ };
481
+ ```
482
+
483
+ But can also be specified using the CLI:
484
+
485
+ ```bash
486
+ $ mm-snap serve --port 8081
487
+ ```
488
+
489
+ If the same option is specified in both the configuration file and the CLI,
490
+ the CLI option takes precedence.
491
+
492
+ ##### `cliOptions.bundle`
493
+
494
+ - Type: `string`
495
+ - Default: `dist/bundle.js`
496
+
497
+ The path to the bundle file. This is where the bundle will be written to.
498
+
499
+ ##### `cliOptions.dist`
500
+
501
+ - Type: `string`
502
+ - Default: `dist`
503
+
504
+ The path to the distribution directory. This is where the bundle file will be
505
+ written to, if the `bundle` option is not specified.
506
+
507
+ ##### `cliOptions.eval`
508
+
509
+ - Type: `boolean`
510
+ - Default: `true`
511
+
512
+ Whether to evaluate the bundle. If `true`, the bundle will be evaluated in the
513
+ MetaMask Snaps runtime, to check for any compatibility issues.
514
+
515
+ ##### `cliOptions.manifest`
516
+
517
+ - Type: `boolean`
518
+ - Default: `true`
519
+
520
+ Whether to validate the snap manifest. If `true`, the snap manifest will be
521
+ validated, and any errors or warnings will be shown.
522
+
523
+ ##### `cliOptions.outfileName`
524
+
525
+ - Type: `string`
526
+ - Default: `bundle.js`
527
+
528
+ The name of the bundle file. This is used to generate the bundle file path,
529
+ using the `dist` option. For example, if `dist` is `dist`, and `outfileName`
530
+ is `bundle.js`, the bundle file path will be `dist/bundle.js`. This option is
531
+ ignored if the `bundle` option is specified.
532
+
533
+ ##### `cliOptions.port`
534
+
535
+ - Type: `number`
536
+ - Default: `8081`
537
+
538
+ The port to use for the development server.
539
+
540
+ ##### `cliOptions.root`
541
+
542
+ - Type: `string`
543
+ - Default: `.`
544
+
545
+ The root directory. This should be the directory that contains the snap
546
+ manifest, and the `dist` directory. This will be served by the development
547
+ server.
548
+
549
+ ##### `cliOptions.sourceMaps`
550
+
551
+ - Type: `boolean`
552
+ - Default: `false`
553
+
554
+ Whether to generate source maps. If `true`, source maps will be generated for
555
+ the bundle.
556
+
557
+ ##### `cliOptions.src`
558
+
559
+ - Type: `string`
560
+ - Default: `src/index.js`
561
+
562
+ The path to the entry file. This is the file that will be bundled.
563
+
564
+ ##### `cliOptions.stripComments`
565
+
566
+ - Type: `boolean`
567
+ - Default: `true`
568
+
569
+ Whether to strip comments from the bundle. If `true`, comments will be stripped
570
+ from the bundle, and the bundle will be minified.
571
+
572
+ ##### `cliOptions.transpilationMode`
573
+
574
+ - Type: `'localAndDeps' | 'localOnly' | 'none'`
575
+ - Default: `'localOnly'`
576
+
577
+ The transpilation mode to use. This determines which files will be transpiled
578
+ by Babel. The possible values are:
579
+
580
+ - `'localAndDeps'`: Transpile all files, including dependencies.
581
+ - `'localOnly'`: Transpile only local files.
582
+ - `'none'`: Do not transpile any files.
583
+
584
+ ##### `cliOptions.depsToTranspile`
585
+
586
+ - Type: `string[]`
587
+ - Default: `[]`
588
+
589
+ If `transpilationMode` is `'localAndDeps'`, this option can be used to specify
590
+ which dependencies should be transpiled. This is useful if you want to transpile
591
+ a limited set of dependencies, but not all of them.
592
+
593
+ ##### `cliOptions.writeManifest`
594
+
595
+ - Type: `boolean`
596
+ - Default: `true`
597
+
598
+ Whether to write the snap manifest. If `true`, the snap manifest will be written
599
+ with any changes made by the CLI, if the manifest is invalid.
600
+
601
+ ##### `cliOptions.serve`
602
+
603
+ - Type: `boolean`
604
+ - Default: `true`
605
+
606
+ Whether to serve the snap. If `true`, the snap will be served by the development
607
+ server, when running `mm-snap watch`.
608
+
609
+ ##### `cliOptions.suppressWarnings`
610
+
611
+ - Type: `boolean`
612
+ - Default: `false`
613
+
614
+ This option exists for backwards compatibility, and is ignored.
615
+
616
+ ##### `cliOptions.verboseErrors`
617
+
618
+ - Type: `boolean`
619
+ - Default: `false`
620
+
621
+ This option exists for backwards compatibility, and is ignored.
622
+
623
+ #### `bundlerCustomizer`
624
+
625
+ - Type: `(bundler: BrowserifyObject) => void`
626
+
627
+ A function that customizes the bundler. This function is called after the
628
+ bundler is created, but before the bundle is written to disk. This can be used
629
+ to customize the bundler, for example to add plugins or transforms.
630
+
631
+ ```typescript
632
+ import type { SnapConfig } from '@metamask/snaps-cli';
633
+ import type { BrowserifyObject } from 'browserify';
634
+
635
+ const config: SnapConfig = {
636
+ bundlerCustomizer: (bundler: BrowserifyObject) => {
637
+ // Add the BRFS transform to Browserify.
638
+ bundler.transform('brfs');
639
+ },
640
+ };
641
+
642
+ export default config;
643
+ ```