@metamask/snaps-cli 0.37.0-flask.1 → 0.38.0-flask.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 (256) hide show
  1. package/.browserslistrc +3 -0
  2. package/CHANGELOG.md +20 -426
  3. package/README.md +539 -63
  4. package/dist/cjs/builders.js +40 -41
  5. package/dist/cjs/builders.js.map +1 -1
  6. package/dist/cjs/cli.js +14 -14
  7. package/dist/cjs/cli.js.map +1 -1
  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/{cmds → commands}/build/index.js +18 -5
  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.map +1 -0
  21. package/dist/cjs/commands/manifest/implementation.js +34 -0
  22. package/dist/cjs/commands/manifest/implementation.js.map +1 -0
  23. package/dist/cjs/commands/manifest/index.js +50 -0
  24. package/dist/cjs/commands/manifest/index.js.map +1 -0
  25. package/dist/cjs/commands/manifest/manifest.js +55 -0
  26. package/dist/cjs/commands/manifest/manifest.js.map +1 -0
  27. package/dist/cjs/{cmds → commands}/serve/index.js +4 -2
  28. package/dist/cjs/commands/serve/index.js.map +1 -0
  29. package/dist/cjs/commands/serve/serve.js +21 -0
  30. package/dist/cjs/commands/serve/serve.js.map +1 -0
  31. package/dist/cjs/commands/watch/implementation.js +35 -0
  32. package/dist/cjs/commands/watch/implementation.js.map +1 -0
  33. package/dist/cjs/{cmds → commands}/watch/index.js +19 -4
  34. package/dist/cjs/commands/watch/index.js.map +1 -0
  35. package/dist/cjs/commands/watch/watch.js +51 -0
  36. package/dist/cjs/commands/watch/watch.js.map +1 -0
  37. package/dist/cjs/config.js +290 -0
  38. package/dist/cjs/config.js.map +1 -0
  39. package/dist/cjs/errors.js +37 -0
  40. package/dist/cjs/errors.js.map +1 -0
  41. package/dist/cjs/index.js +28 -3
  42. package/dist/cjs/index.js.map +1 -1
  43. package/dist/cjs/main.js +6 -7
  44. package/dist/cjs/main.js.map +1 -1
  45. package/dist/cjs/utils/cli.js +61 -0
  46. package/dist/cjs/utils/cli.js.map +1 -0
  47. package/dist/cjs/utils/errors.js +42 -0
  48. package/dist/cjs/utils/errors.js.map +1 -0
  49. package/dist/cjs/utils/index.js +6 -2
  50. package/dist/cjs/utils/index.js.map +1 -1
  51. package/dist/cjs/utils/legacy.js +48 -0
  52. package/dist/cjs/utils/legacy.js.map +1 -0
  53. package/dist/cjs/utils/logging.js +46 -0
  54. package/dist/cjs/utils/logging.js.map +1 -0
  55. package/dist/cjs/utils/path.js +16 -0
  56. package/dist/cjs/utils/path.js.map +1 -0
  57. package/dist/cjs/utils/steps.js +52 -0
  58. package/dist/cjs/utils/steps.js.map +1 -0
  59. package/dist/cjs/webpack/compiler.js +96 -0
  60. package/dist/cjs/webpack/compiler.js.map +1 -0
  61. package/dist/cjs/webpack/config.js +234 -0
  62. package/dist/cjs/webpack/config.js.map +1 -0
  63. package/dist/cjs/webpack/index.js +22 -0
  64. package/dist/cjs/webpack/index.js.map +1 -0
  65. package/dist/cjs/webpack/loaders/browserify.js +97 -0
  66. package/dist/cjs/webpack/loaders/browserify.js.map +1 -0
  67. package/dist/cjs/webpack/loaders/wasm.js +36 -0
  68. package/dist/cjs/webpack/loaders/wasm.js.map +1 -0
  69. package/dist/cjs/webpack/plugins.js +340 -0
  70. package/dist/cjs/webpack/plugins.js.map +1 -0
  71. package/dist/cjs/webpack/utils.js +133 -0
  72. package/dist/cjs/webpack/utils.js.map +1 -0
  73. package/dist/esm/builders.js +40 -41
  74. package/dist/esm/builders.js.map +1 -1
  75. package/dist/esm/cli.js +15 -15
  76. package/dist/esm/cli.js.map +1 -1
  77. package/dist/esm/commands/build/build.js +51 -0
  78. package/dist/esm/commands/build/build.js.map +1 -0
  79. package/dist/esm/commands/build/implementation.js +27 -0
  80. package/dist/esm/commands/build/implementation.js.map +1 -0
  81. package/dist/esm/{cmds → commands}/build/index.js +5 -5
  82. package/dist/esm/commands/build/index.js.map +1 -0
  83. package/dist/esm/commands/eval/eval.js +54 -0
  84. package/dist/esm/commands/eval/eval.js.map +1 -0
  85. package/dist/esm/commands/eval/implementation.js +24 -0
  86. package/dist/esm/commands/eval/implementation.js.map +1 -0
  87. package/dist/esm/commands/eval/index.js +22 -0
  88. package/dist/esm/commands/eval/index.js.map +1 -0
  89. package/dist/esm/commands/index.js +15 -0
  90. package/dist/esm/commands/index.js.map +1 -0
  91. package/dist/esm/commands/manifest/implementation.js +33 -0
  92. package/dist/esm/commands/manifest/implementation.js.map +1 -0
  93. package/dist/esm/commands/manifest/index.js +22 -0
  94. package/dist/esm/commands/manifest/index.js.map +1 -0
  95. package/dist/esm/commands/manifest/manifest.js +52 -0
  96. package/dist/esm/commands/manifest/manifest.js.map +1 -0
  97. package/dist/esm/{cmds → commands}/serve/index.js +4 -2
  98. package/dist/esm/commands/serve/index.js.map +1 -0
  99. package/dist/esm/commands/serve/serve.js +17 -0
  100. package/dist/esm/commands/serve/serve.js.map +1 -0
  101. package/dist/esm/commands/watch/implementation.js +34 -0
  102. package/dist/esm/commands/watch/implementation.js.map +1 -0
  103. package/dist/esm/{cmds → commands}/watch/index.js +6 -4
  104. package/dist/esm/commands/watch/index.js.map +1 -0
  105. package/dist/esm/commands/watch/watch.js +50 -0
  106. package/dist/esm/commands/watch/watch.js.map +1 -0
  107. package/dist/esm/config.js +294 -0
  108. package/dist/esm/config.js.map +1 -0
  109. package/dist/esm/errors.js +30 -0
  110. package/dist/esm/errors.js.map +1 -0
  111. package/dist/esm/index.js +3 -0
  112. package/dist/esm/index.js.map +1 -1
  113. package/dist/esm/main.js +6 -7
  114. package/dist/esm/main.js.map +1 -1
  115. package/dist/esm/utils/cli.js +43 -0
  116. package/dist/esm/utils/cli.js.map +1 -0
  117. package/dist/esm/utils/errors.js +43 -0
  118. package/dist/esm/utils/errors.js.map +1 -0
  119. package/dist/esm/utils/index.js +6 -2
  120. package/dist/esm/utils/index.js.map +1 -1
  121. package/dist/esm/utils/legacy.js +47 -0
  122. package/dist/esm/utils/legacy.js.map +1 -0
  123. package/dist/esm/utils/logging.js +40 -0
  124. package/dist/esm/utils/logging.js.map +1 -0
  125. package/dist/esm/utils/path.js +13 -0
  126. package/dist/esm/utils/path.js.map +1 -0
  127. package/dist/esm/utils/steps.js +43 -0
  128. package/dist/esm/utils/steps.js.map +1 -0
  129. package/dist/esm/webpack/compiler.js +89 -0
  130. package/dist/esm/webpack/compiler.js.map +1 -0
  131. package/dist/esm/webpack/config.js +236 -0
  132. package/dist/esm/webpack/config.js.map +1 -0
  133. package/dist/esm/webpack/index.js +5 -0
  134. package/dist/esm/webpack/index.js.map +1 -0
  135. package/dist/esm/webpack/loaders/browserify.js +82 -0
  136. package/dist/esm/webpack/loaders/browserify.js.map +1 -0
  137. package/dist/esm/webpack/loaders/wasm.js +26 -0
  138. package/dist/esm/webpack/loaders/wasm.js.map +1 -0
  139. package/dist/esm/webpack/plugins.js +343 -0
  140. package/dist/esm/webpack/plugins.js.map +1 -0
  141. package/dist/esm/webpack/utils.js +160 -0
  142. package/dist/esm/webpack/utils.js.map +1 -0
  143. package/dist/types/__fixtures__/configs/cjs.d.ts +1 -0
  144. package/dist/types/__fixtures__/configs/esm.d.ts +3 -0
  145. package/dist/types/__fixtures__/configs/invalid.d.ts +3 -0
  146. package/dist/types/__fixtures__/configs/javascript/snap.config.d.ts +0 -0
  147. package/dist/types/__fixtures__/configs/typescript/snap.config.d.ts +3 -0
  148. package/dist/types/builders.d.ts +2 -21
  149. package/dist/types/cli.d.ts +1 -1
  150. package/dist/types/commands/build/build.d.ts +10 -0
  151. package/dist/types/commands/build/implementation.d.ts +10 -0
  152. package/dist/types/{cmds/eval → commands/build}/index.d.ts +1 -0
  153. package/dist/types/commands/eval/__test__/browserify/bad/snap.config.d.ts +3 -0
  154. package/dist/types/commands/eval/__test__/browserify/good/snap.config.d.ts +3 -0
  155. package/dist/types/commands/eval/__test__/webpack/bad/snap.config.d.ts +3 -0
  156. package/dist/types/commands/eval/__test__/webpack/good/snap.config.d.ts +3 -0
  157. package/dist/types/commands/eval/__test__/webpack/snap.config.d.ts +3 -0
  158. package/dist/types/commands/eval/eval.d.ts +14 -0
  159. package/dist/types/commands/eval/implementation.d.ts +9 -0
  160. package/dist/types/{cmds/serve → commands/eval}/index.d.ts +1 -0
  161. package/dist/types/commands/manifest/implementation.d.ts +12 -0
  162. package/dist/types/{cmds → commands}/manifest/index.d.ts +1 -0
  163. package/dist/types/commands/manifest/manifest.d.ts +14 -0
  164. package/dist/types/commands/serve/serve.d.ts +16 -0
  165. package/dist/types/commands/watch/implementation.d.ts +14 -0
  166. package/dist/types/commands/watch/index.d.ts +10 -0
  167. package/dist/types/commands/watch/watch.d.ts +19 -0
  168. package/dist/types/config.d.ts +626 -0
  169. package/dist/types/errors.d.ts +25 -0
  170. package/dist/types/index.d.ts +4 -0
  171. package/dist/types/utils/cli.d.ts +17 -0
  172. package/dist/types/utils/errors.d.ts +23 -0
  173. package/dist/types/utils/index.d.ts +6 -2
  174. package/dist/types/utils/legacy.d.ts +27 -0
  175. package/dist/types/utils/logging.d.ts +22 -0
  176. package/dist/types/utils/path.d.ts +9 -0
  177. package/dist/types/utils/steps.d.ts +17 -0
  178. package/dist/types/webpack/compiler.d.ts +29 -0
  179. package/dist/types/webpack/config.d.ts +37 -0
  180. package/dist/types/webpack/index.d.ts +3 -0
  181. package/dist/types/webpack/loaders/browserify.d.ts +16 -0
  182. package/dist/types/webpack/loaders/wasm.d.ts +20 -0
  183. package/dist/types/webpack/plugins.d.ts +147 -0
  184. package/dist/types/webpack/utils.d.ts +156 -0
  185. package/package.json +15 -7
  186. package/dist/cjs/cmds/build/buildHandler.js +0 -36
  187. package/dist/cjs/cmds/build/buildHandler.js.map +0 -1
  188. package/dist/cjs/cmds/build/bundle.js +0 -83
  189. package/dist/cjs/cmds/build/bundle.js.map +0 -1
  190. package/dist/cjs/cmds/build/index.js.map +0 -1
  191. package/dist/cjs/cmds/build/utils.js +0 -79
  192. package/dist/cjs/cmds/build/utils.js.map +0 -1
  193. package/dist/cjs/cmds/eval/evalHandler.js +0 -24
  194. package/dist/cjs/cmds/eval/evalHandler.js.map +0 -1
  195. package/dist/cjs/cmds/eval/index.js +0 -31
  196. package/dist/cjs/cmds/eval/index.js.map +0 -1
  197. package/dist/cjs/cmds/index.js.map +0 -1
  198. package/dist/cjs/cmds/manifest/index.js +0 -44
  199. package/dist/cjs/cmds/manifest/index.js.map +0 -1
  200. package/dist/cjs/cmds/manifest/manifestHandler.js +0 -47
  201. package/dist/cjs/cmds/manifest/manifestHandler.js.map +0 -1
  202. package/dist/cjs/cmds/serve/index.js.map +0 -1
  203. package/dist/cjs/cmds/serve/serveHandler.js +0 -62
  204. package/dist/cjs/cmds/serve/serveHandler.js.map +0 -1
  205. package/dist/cjs/cmds/serve/serveUtils.js +0 -37
  206. package/dist/cjs/cmds/serve/serveUtils.js.map +0 -1
  207. package/dist/cjs/cmds/watch/index.js.map +0 -1
  208. package/dist/cjs/cmds/watch/watchHandler.js +0 -91
  209. package/dist/cjs/cmds/watch/watchHandler.js.map +0 -1
  210. package/dist/cjs/utils/misc.js +0 -142
  211. package/dist/cjs/utils/misc.js.map +0 -1
  212. package/dist/cjs/utils/snap-config.js +0 -104
  213. package/dist/cjs/utils/snap-config.js.map +0 -1
  214. package/dist/esm/cmds/build/buildHandler.js +0 -36
  215. package/dist/esm/cmds/build/buildHandler.js.map +0 -1
  216. package/dist/esm/cmds/build/bundle.js +0 -81
  217. package/dist/esm/cmds/build/bundle.js.map +0 -1
  218. package/dist/esm/cmds/build/index.js.map +0 -1
  219. package/dist/esm/cmds/build/utils.js +0 -88
  220. package/dist/esm/cmds/build/utils.js.map +0 -1
  221. package/dist/esm/cmds/eval/evalHandler.js +0 -20
  222. package/dist/esm/cmds/eval/evalHandler.js.map +0 -1
  223. package/dist/esm/cmds/eval/index.js +0 -16
  224. package/dist/esm/cmds/eval/index.js.map +0 -1
  225. package/dist/esm/cmds/index.js +0 -15
  226. package/dist/esm/cmds/index.js.map +0 -1
  227. package/dist/esm/cmds/manifest/index.js +0 -29
  228. package/dist/esm/cmds/manifest/index.js.map +0 -1
  229. package/dist/esm/cmds/manifest/manifestHandler.js +0 -44
  230. package/dist/esm/cmds/manifest/manifestHandler.js.map +0 -1
  231. package/dist/esm/cmds/serve/index.js.map +0 -1
  232. package/dist/esm/cmds/serve/serveHandler.js +0 -54
  233. package/dist/esm/cmds/serve/serveHandler.js.map +0 -1
  234. package/dist/esm/cmds/serve/serveUtils.js +0 -30
  235. package/dist/esm/cmds/serve/serveUtils.js.map +0 -1
  236. package/dist/esm/cmds/watch/index.js.map +0 -1
  237. package/dist/esm/cmds/watch/watchHandler.js +0 -87
  238. package/dist/esm/cmds/watch/watchHandler.js.map +0 -1
  239. package/dist/esm/utils/misc.js +0 -139
  240. package/dist/esm/utils/misc.js.map +0 -1
  241. package/dist/esm/utils/snap-config.js +0 -107
  242. package/dist/esm/utils/snap-config.js.map +0 -1
  243. package/dist/types/cmds/build/buildHandler.d.ts +0 -13
  244. package/dist/types/cmds/build/bundle.d.ts +0 -15
  245. package/dist/types/cmds/build/utils.d.ts +0 -56
  246. package/dist/types/cmds/eval/evalHandler.d.ts +0 -9
  247. package/dist/types/cmds/manifest/manifestHandler.d.ts +0 -10
  248. package/dist/types/cmds/serve/serveHandler.d.ts +0 -10
  249. package/dist/types/cmds/serve/serveUtils.d.ts +0 -22
  250. package/dist/types/cmds/watch/index.d.ts +0 -9
  251. package/dist/types/cmds/watch/watchHandler.d.ts +0 -14
  252. package/dist/types/utils/misc.d.ts +0 -53
  253. package/dist/types/utils/snap-config.d.ts +0 -48
  254. /package/dist/cjs/{cmds → commands}/index.js +0 -0
  255. /package/dist/types/{cmds → commands}/index.d.ts +0 -0
  256. /package/dist/types/{cmds/build → commands/serve}/index.d.ts +0 -0
@@ -0,0 +1,236 @@
1
+ import SnapsWebpackPlugin from '@metamask/snaps-webpack-plugin';
2
+ import { builtinModules } from 'module';
3
+ import { resolve } from 'path';
4
+ import TerserPlugin from 'terser-webpack-plugin';
5
+ import { EnvironmentPlugin, ProgressPlugin } from 'webpack';
6
+ import { SnapsBuiltInResolver, SnapsBundleWarningsPlugin, SnapsStatsPlugin, SnapsWatchPlugin } from './plugins';
7
+ import { BROWSERSLIST_FILE, getDefaultLoader, getDevTool, getProgressHandler } from './utils';
8
+ /**
9
+ * Get the default Webpack configuration. This is the configuration that will
10
+ * be used if the user doesn't provide a custom Webpack configuration. The
11
+ * configuration is based on the snap config.
12
+ *
13
+ * The default configuration includes:
14
+ *
15
+ * - `SWC` to transpile TypeScript and JavaScript files.
16
+ * - `TerserPlugin` to minify the bundle.
17
+ * - `SnapsWebpackPlugin` to validate the bundle and update the manifest.
18
+ *
19
+ * It can be customized through the `customizeWebpackConfig` function in the
20
+ * snap config, but in most cases, you shouldn't need to do that.
21
+ *
22
+ * @param config - The processed snap Webpack config.
23
+ * @param options - The Webpack options.
24
+ * @returns The default Webpack configuration.
25
+ */ export async function getDefaultConfiguration(config, options = {
26
+ evaluate: config.evaluate
27
+ }) {
28
+ const spinnerText = options.spinner?.text;
29
+ const builtInResolver = config.stats.builtIns && new SnapsBuiltInResolver(config.stats.builtIns, options.spinner);
30
+ return {
31
+ /**
32
+ * The target is set to `browserslist` so that Webpack will compile the
33
+ * bundle to support the browsers specified in the `.browserslistrc` file.
34
+ * This Browserslist file contains the browsers that are supported by
35
+ * MetaMask Snaps.
36
+ *
37
+ * @see https://webpack.js.org/configuration/target/
38
+ */ target: `browserslist:${BROWSERSLIST_FILE}`,
39
+ /**
40
+ * The mode is set to `production` by default, so that Webpack will minify
41
+ * and optimize the bundle.
42
+ *
43
+ * @see https://webpack.js.org/configuration/mode/
44
+ */ mode: 'production',
45
+ /**
46
+ * The entry point is set to the `input` value from the config object.
47
+ *
48
+ * @see https://webpack.js.org/configuration/entry-context/
49
+ */ entry: config.input,
50
+ /**
51
+ * The devtool option controls how source maps are generated. We set it to
52
+ * the `sourceMap` value from the config object.
53
+ *
54
+ * @see https://webpack.js.org/configuration/devtool/
55
+ */ devtool: getDevTool(config.sourceMap),
56
+ /**
57
+ * The stats option controls how much information is printed to the console
58
+ * when Webpack is running. We set it to `none` so that we can control the
59
+ * output ourselves.
60
+ *
61
+ * @see https://webpack.js.org/configuration/stats/
62
+ */ stats: 'none',
63
+ /**
64
+ * The output options.
65
+ *
66
+ * @see https://webpack.js.org/configuration/output/
67
+ */ output: {
68
+ /**
69
+ * This indicates whether Webpack should clear the output directory
70
+ * before building. We set it to the `clean` value from the config
71
+ * object.
72
+ *
73
+ * @see https://webpack.js.org/configuration/output/#outputclean
74
+ */ clean: config.output.clean,
75
+ /**
76
+ * The filename of the bundle. We set it to the `filename` value from
77
+ * the config object.
78
+ *
79
+ * @see https://webpack.js.org/configuration/output/#outputfilename
80
+ */ filename: config.output.filename,
81
+ /**
82
+ * The path to the output directory. We set it to the `path` value from
83
+ * the config object.
84
+ *
85
+ * @see https://webpack.js.org/configuration/output/#outputpath
86
+ */ path: config.output.path,
87
+ /**
88
+ * The public path of the bundle. We set it to `/` by default, so that
89
+ * the bundle can be loaded from the root of the server.
90
+ *
91
+ * @see https://webpack.js.org/configuration/output/#outputpublicpath
92
+ */ publicPath: '/',
93
+ /**
94
+ * The library configuration. This tells Webpack how to export the
95
+ * bundle.
96
+ *
97
+ * @see https://webpack.js.org/configuration/output/#outputlibrary
98
+ */ library: {
99
+ /**
100
+ * This tells Webpack to export the bundle as a CommonJS module. This
101
+ * is necessary for MetaMask Snaps.
102
+ *
103
+ * @see https://webpack.js.org/configuration/output/#outputlibrarytarget
104
+ */ type: 'commonjs'
105
+ }
106
+ },
107
+ /**
108
+ * The module configuration. This is where we tell Webpack how to handle
109
+ * different types of files.
110
+ *
111
+ * @see https://webpack.js.org/configuration/module/
112
+ */ module: {
113
+ rules: [
114
+ {
115
+ test: /\.[tj]sx?$/u,
116
+ exclude: /node_modules/u,
117
+ use: await getDefaultLoader(config)
118
+ },
119
+ config.experimental.wasm && {
120
+ test: /\.wasm$/u,
121
+ use: {
122
+ loader: resolve(__dirname, 'loaders', 'wasm')
123
+ }
124
+ }
125
+ ]
126
+ },
127
+ /**
128
+ * The resolve configuration. This tells Webpack how to resolve imports.
129
+ * We set it to resolve `.js` and `.ts` files.
130
+ *
131
+ * @see https://webpack.js.org/configuration/resolve/
132
+ */ resolve: {
133
+ /**
134
+ * The extensions to resolve. We set it to resolve `.js` and `.ts`
135
+ * files.
136
+ */ extensions: [
137
+ '.js',
138
+ '.ts'
139
+ ],
140
+ /**
141
+ * The fallback options. This tells Webpack how to handle imports that
142
+ * aren't resolved. By default, we set Node.js built-ins to `false`, so
143
+ * that they are ignored.
144
+ */ fallback: Object.fromEntries(builtinModules.map((name)=>[
145
+ name,
146
+ false
147
+ ])),
148
+ /**
149
+ * The plugins to use. We use the {@link SnapsBuiltInResolver} to show
150
+ * warnings about using Node.js built-ins, when no fallback is specified.
151
+ */ plugins: [
152
+ builtInResolver
153
+ ]
154
+ },
155
+ /**
156
+ * The plugins to use.
157
+ *
158
+ * @see https://webpack.js.org/configuration/plugins/
159
+ */ plugins: [
160
+ /**
161
+ * The `SnapsWebpackPlugin` is a Webpack plugin that checks and updates
162
+ * the manifest file, and evaluates the bundle in SES. While not strictly
163
+ * required, it's highly recommended to use this plugin.
164
+ */ new SnapsWebpackPlugin({
165
+ manifestPath: config.manifest.path,
166
+ writeManifest: config.manifest.update,
167
+ eval: !options.watch && options.evaluate
168
+ }),
169
+ /**
170
+ * The `SnapsStatsPlugin` is a Webpack plugin that handles the stats
171
+ * output. It's used to show the stats in the terminal, in a format that
172
+ * is easy to read.
173
+ */ new SnapsStatsPlugin({
174
+ verbose: config.stats.verbose
175
+ }, options.spinner),
176
+ /**
177
+ * The `EnvironmentPlugin` is a Webpack plugin that adds environment
178
+ * variables to the bundle. We use it to add the `NODE_ENV` and `DEBUG`
179
+ * environment variables.
180
+ */ new EnvironmentPlugin(config.environment),
181
+ /**
182
+ * The `ProgressPlugin` is a Webpack plugin that logs the progress of
183
+ * the build. We set it to log the progress to the spinner.
184
+ */ new ProgressPlugin({
185
+ handler: getProgressHandler(options.spinner, spinnerText)
186
+ }),
187
+ /**
188
+ * The `SnapsBundleWarningPlugin` is a Webpack plugin that shows a
189
+ * warning when the bundle is potentially incompatible with MetaMask
190
+ * Snaps.
191
+ */ new SnapsBundleWarningsPlugin({
192
+ builtInResolver,
193
+ builtIns: Boolean(config.stats.builtIns),
194
+ buffer: config.stats.buffer
195
+ }, options.spinner),
196
+ /**
197
+ * The `WatchPlugin` is a Webpack plugin that adds extra files to watch
198
+ * for changes. This is useful for rebuilding the bundle when the
199
+ * manifest file changes.
200
+ */ options.watch && new SnapsWatchPlugin({
201
+ bundle: resolve(config.output.path, config.output.filename),
202
+ evaluate: options.evaluate,
203
+ files: [
204
+ config.manifest.path
205
+ ]
206
+ }, options.spinner)
207
+ ].filter(Boolean),
208
+ /**
209
+ * The optimization configuration. This tells Webpack how to optimize the
210
+ * bundle. Most of the time, you won't need to change this, as the default
211
+ * options set by the `mode` option are sufficient.
212
+ */ optimization: {
213
+ minimize: config.output.minimize,
214
+ /**
215
+ * The minimizer to use. We set it to use the `TerserPlugin`.
216
+ */ minimizer: [
217
+ new TerserPlugin({
218
+ parallel: true
219
+ })
220
+ ]
221
+ },
222
+ /**
223
+ * The infrastructure logging configuration. This tells Webpack how to
224
+ * log messages.
225
+ *
226
+ * @see https://webpack.js.org/configuration/infrastructure-logging
227
+ */ infrastructureLogging: {
228
+ /**
229
+ * The level of logging to use. We set it to `none`, so that we can
230
+ * control the output ourselves.
231
+ */ level: 'none'
232
+ }
233
+ };
234
+ }
235
+
236
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/webpack/config.ts"],"sourcesContent":["import SnapsWebpackPlugin from '@metamask/snaps-webpack-plugin';\nimport { builtinModules } from 'module';\nimport type { Ora } from 'ora';\nimport { resolve } from 'path';\nimport TerserPlugin from 'terser-webpack-plugin';\nimport type { Configuration } from 'webpack';\nimport { EnvironmentPlugin, ProgressPlugin } from 'webpack';\n\nimport type { ProcessedWebpackConfig } from '../config';\nimport {\n SnapsBuiltInResolver,\n SnapsBundleWarningsPlugin,\n SnapsStatsPlugin,\n SnapsWatchPlugin,\n} from './plugins';\nimport {\n BROWSERSLIST_FILE,\n getDefaultLoader,\n getDevTool,\n getProgressHandler,\n} from './utils';\n\nexport type WebpackOptions = {\n /**\n * Whether to watch for changes.\n */\n watch?: boolean;\n\n /**\n * Whether to evaluate the bundle. If this is set, it will override the\n * `evaluate` option in the config object.\n */\n evaluate?: boolean;\n\n /**\n * The spinner to use for logging.\n */\n spinner?: Ora;\n};\n\n/**\n * Get the default Webpack configuration. This is the configuration that will\n * be used if the user doesn't provide a custom Webpack configuration. The\n * configuration is based on the snap config.\n *\n * The default configuration includes:\n *\n * - `SWC` to transpile TypeScript and JavaScript files.\n * - `TerserPlugin` to minify the bundle.\n * - `SnapsWebpackPlugin` to validate the bundle and update the manifest.\n *\n * It can be customized through the `customizeWebpackConfig` function in the\n * snap config, but in most cases, you shouldn't need to do that.\n *\n * @param config - The processed snap Webpack config.\n * @param options - The Webpack options.\n * @returns The default Webpack configuration.\n */\nexport async function getDefaultConfiguration(\n config: ProcessedWebpackConfig,\n options: WebpackOptions = {\n evaluate: config.evaluate,\n },\n): Promise<Configuration> {\n const spinnerText = options.spinner?.text;\n const builtInResolver =\n config.stats.builtIns &&\n new SnapsBuiltInResolver(config.stats.builtIns, options.spinner);\n\n return {\n /**\n * The target is set to `browserslist` so that Webpack will compile the\n * bundle to support the browsers specified in the `.browserslistrc` file.\n * This Browserslist file contains the browsers that are supported by\n * MetaMask Snaps.\n *\n * @see https://webpack.js.org/configuration/target/\n */\n target: `browserslist:${BROWSERSLIST_FILE}`,\n\n /**\n * The mode is set to `production` by default, so that Webpack will minify\n * and optimize the bundle.\n *\n * @see https://webpack.js.org/configuration/mode/\n */\n mode: 'production',\n\n /**\n * The entry point is set to the `input` value from the config object.\n *\n * @see https://webpack.js.org/configuration/entry-context/\n */\n entry: config.input,\n\n /**\n * The devtool option controls how source maps are generated. We set it to\n * the `sourceMap` value from the config object.\n *\n * @see https://webpack.js.org/configuration/devtool/\n */\n devtool: getDevTool(config.sourceMap),\n\n /**\n * The stats option controls how much information is printed to the console\n * when Webpack is running. We set it to `none` so that we can control the\n * output ourselves.\n *\n * @see https://webpack.js.org/configuration/stats/\n */\n stats: 'none',\n\n /**\n * The output options.\n *\n * @see https://webpack.js.org/configuration/output/\n */\n output: {\n /**\n * This indicates whether Webpack should clear the output directory\n * before building. We set it to the `clean` value from the config\n * object.\n *\n * @see https://webpack.js.org/configuration/output/#outputclean\n */\n clean: config.output.clean,\n\n /**\n * The filename of the bundle. We set it to the `filename` value from\n * the config object.\n *\n * @see https://webpack.js.org/configuration/output/#outputfilename\n */\n filename: config.output.filename,\n\n /**\n * The path to the output directory. We set it to the `path` value from\n * the config object.\n *\n * @see https://webpack.js.org/configuration/output/#outputpath\n */\n path: config.output.path,\n\n /**\n * The public path of the bundle. We set it to `/` by default, so that\n * the bundle can be loaded from the root of the server.\n *\n * @see https://webpack.js.org/configuration/output/#outputpublicpath\n */\n publicPath: '/',\n\n /**\n * The library configuration. This tells Webpack how to export the\n * bundle.\n *\n * @see https://webpack.js.org/configuration/output/#outputlibrary\n */\n library: {\n /**\n * This tells Webpack to export the bundle as a CommonJS module. This\n * is necessary for MetaMask Snaps.\n *\n * @see https://webpack.js.org/configuration/output/#outputlibrarytarget\n */\n type: 'commonjs',\n },\n },\n\n /**\n * The module configuration. This is where we tell Webpack how to handle\n * different types of files.\n *\n * @see https://webpack.js.org/configuration/module/\n */\n module: {\n rules: [\n {\n test: /\\.[tj]sx?$/u,\n exclude: /node_modules/u,\n use: await getDefaultLoader(config),\n },\n\n config.experimental.wasm && {\n test: /\\.wasm$/u,\n use: {\n loader: resolve(__dirname, 'loaders', 'wasm'),\n },\n },\n ],\n },\n\n /**\n * The resolve configuration. This tells Webpack how to resolve imports.\n * We set it to resolve `.js` and `.ts` files.\n *\n * @see https://webpack.js.org/configuration/resolve/\n */\n resolve: {\n /**\n * The extensions to resolve. We set it to resolve `.js` and `.ts`\n * files.\n */\n extensions: ['.js', '.ts'],\n\n /**\n * The fallback options. This tells Webpack how to handle imports that\n * aren't resolved. By default, we set Node.js built-ins to `false`, so\n * that they are ignored.\n */\n fallback: Object.fromEntries(builtinModules.map((name) => [name, false])),\n\n /**\n * The plugins to use. We use the {@link SnapsBuiltInResolver} to show\n * warnings about using Node.js built-ins, when no fallback is specified.\n */\n plugins: [builtInResolver],\n },\n\n /**\n * The plugins to use.\n *\n * @see https://webpack.js.org/configuration/plugins/\n */\n plugins: [\n /**\n * The `SnapsWebpackPlugin` is a Webpack plugin that checks and updates\n * the manifest file, and evaluates the bundle in SES. While not strictly\n * required, it's highly recommended to use this plugin.\n */\n new SnapsWebpackPlugin({\n manifestPath: config.manifest.path,\n writeManifest: config.manifest.update,\n eval: !options.watch && options.evaluate,\n }),\n\n /**\n * The `SnapsStatsPlugin` is a Webpack plugin that handles the stats\n * output. It's used to show the stats in the terminal, in a format that\n * is easy to read.\n */\n new SnapsStatsPlugin({ verbose: config.stats.verbose }, options.spinner),\n\n /**\n * The `EnvironmentPlugin` is a Webpack plugin that adds environment\n * variables to the bundle. We use it to add the `NODE_ENV` and `DEBUG`\n * environment variables.\n */\n new EnvironmentPlugin(config.environment),\n\n /**\n * The `ProgressPlugin` is a Webpack plugin that logs the progress of\n * the build. We set it to log the progress to the spinner.\n */\n new ProgressPlugin({\n handler: getProgressHandler(options.spinner, spinnerText),\n }),\n\n /**\n * The `SnapsBundleWarningPlugin` is a Webpack plugin that shows a\n * warning when the bundle is potentially incompatible with MetaMask\n * Snaps.\n */\n new SnapsBundleWarningsPlugin(\n {\n builtInResolver,\n builtIns: Boolean(config.stats.builtIns),\n buffer: config.stats.buffer,\n },\n options.spinner,\n ),\n\n /**\n * The `WatchPlugin` is a Webpack plugin that adds extra files to watch\n * for changes. This is useful for rebuilding the bundle when the\n * manifest file changes.\n */\n options.watch &&\n new SnapsWatchPlugin(\n {\n bundle: resolve(config.output.path, config.output.filename),\n evaluate: options.evaluate,\n files: [config.manifest.path],\n },\n options.spinner,\n ),\n ].filter(Boolean),\n\n /**\n * The optimization configuration. This tells Webpack how to optimize the\n * bundle. Most of the time, you won't need to change this, as the default\n * options set by the `mode` option are sufficient.\n */\n optimization: {\n minimize: config.output.minimize,\n\n /**\n * The minimizer to use. We set it to use the `TerserPlugin`.\n */\n minimizer: [\n new TerserPlugin({\n parallel: true,\n }),\n ],\n },\n\n /**\n * The infrastructure logging configuration. This tells Webpack how to\n * log messages.\n *\n * @see https://webpack.js.org/configuration/infrastructure-logging\n */\n infrastructureLogging: {\n /**\n * The level of logging to use. We set it to `none`, so that we can\n * control the output ourselves.\n */\n level: 'none',\n },\n };\n}\n"],"names":["SnapsWebpackPlugin","builtinModules","resolve","TerserPlugin","EnvironmentPlugin","ProgressPlugin","SnapsBuiltInResolver","SnapsBundleWarningsPlugin","SnapsStatsPlugin","SnapsWatchPlugin","BROWSERSLIST_FILE","getDefaultLoader","getDevTool","getProgressHandler","getDefaultConfiguration","config","options","evaluate","spinnerText","spinner","text","builtInResolver","stats","builtIns","target","mode","entry","input","devtool","sourceMap","output","clean","filename","path","publicPath","library","type","module","rules","test","exclude","use","experimental","wasm","loader","__dirname","extensions","fallback","Object","fromEntries","map","name","plugins","manifestPath","manifest","writeManifest","update","eval","watch","verbose","environment","handler","Boolean","buffer","bundle","files","filter","optimization","minimize","minimizer","parallel","infrastructureLogging","level"],"mappings":"AAAA,OAAOA,wBAAwB,iCAAiC;AAChE,SAASC,cAAc,QAAQ,SAAS;AAExC,SAASC,OAAO,QAAQ,OAAO;AAC/B,OAAOC,kBAAkB,wBAAwB;AAEjD,SAASC,iBAAiB,EAAEC,cAAc,QAAQ,UAAU;AAG5D,SACEC,oBAAoB,EACpBC,yBAAyB,EACzBC,gBAAgB,EAChBC,gBAAgB,QACX,YAAY;AACnB,SACEC,iBAAiB,EACjBC,gBAAgB,EAChBC,UAAU,EACVC,kBAAkB,QACb,UAAU;AAoBjB;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,eAAeC,wBACpBC,MAA8B,EAC9BC,UAA0B;IACxBC,UAAUF,OAAOE,QAAQ;AAC3B,CAAC;IAED,MAAMC,cAAcF,QAAQG,OAAO,EAAEC;IACrC,MAAMC,kBACJN,OAAOO,KAAK,CAACC,QAAQ,IACrB,IAAIjB,qBAAqBS,OAAOO,KAAK,CAACC,QAAQ,EAAEP,QAAQG,OAAO;IAEjE,OAAO;QACL;;;;;;;KAOC,GACDK,QAAQ,CAAC,aAAa,EAAEd,kBAAkB,CAAC;QAE3C;;;;;KAKC,GACDe,MAAM;QAEN;;;;KAIC,GACDC,OAAOX,OAAOY,KAAK;QAEnB;;;;;KAKC,GACDC,SAAShB,WAAWG,OAAOc,SAAS;QAEpC;;;;;;KAMC,GACDP,OAAO;QAEP;;;;KAIC,GACDQ,QAAQ;YACN;;;;;;OAMC,GACDC,OAAOhB,OAAOe,MAAM,CAACC,KAAK;YAE1B;;;;;OAKC,GACDC,UAAUjB,OAAOe,MAAM,CAACE,QAAQ;YAEhC;;;;;OAKC,GACDC,MAAMlB,OAAOe,MAAM,CAACG,IAAI;YAExB;;;;;OAKC,GACDC,YAAY;YAEZ;;;;;OAKC,GACDC,SAAS;gBACP;;;;;SAKC,GACDC,MAAM;YACR;QACF;QAEA;;;;;KAKC,GACDC,QAAQ;YACNC,OAAO;gBACL;oBACEC,MAAM;oBACNC,SAAS;oBACTC,KAAK,MAAM9B,iBAAiBI;gBAC9B;gBAEAA,OAAO2B,YAAY,CAACC,IAAI,IAAI;oBAC1BJ,MAAM;oBACNE,KAAK;wBACHG,QAAQ1C,QAAQ2C,WAAW,WAAW;oBACxC;gBACF;aACD;QACH;QAEA;;;;;KAKC,GACD3C,SAAS;YACP;;;OAGC,GACD4C,YAAY;gBAAC;gBAAO;aAAM;YAE1B;;;;OAIC,GACDC,UAAUC,OAAOC,WAAW,CAAChD,eAAeiD,GAAG,CAAC,CAACC,OAAS;oBAACA;oBAAM;iBAAM;YAEvE;;;OAGC,GACDC,SAAS;gBAAC/B;aAAgB;QAC5B;QAEA;;;;KAIC,GACD+B,SAAS;YACP;;;;OAIC,GACD,IAAIpD,mBAAmB;gBACrBqD,cAActC,OAAOuC,QAAQ,CAACrB,IAAI;gBAClCsB,eAAexC,OAAOuC,QAAQ,CAACE,MAAM;gBACrCC,MAAM,CAACzC,QAAQ0C,KAAK,IAAI1C,QAAQC,QAAQ;YAC1C;YAEA;;;;OAIC,GACD,IAAIT,iBAAiB;gBAAEmD,SAAS5C,OAAOO,KAAK,CAACqC,OAAO;YAAC,GAAG3C,QAAQG,OAAO;YAEvE;;;;OAIC,GACD,IAAIf,kBAAkBW,OAAO6C,WAAW;YAExC;;;OAGC,GACD,IAAIvD,eAAe;gBACjBwD,SAAShD,mBAAmBG,QAAQG,OAAO,EAAED;YAC/C;YAEA;;;;OAIC,GACD,IAAIX,0BACF;gBACEc;gBACAE,UAAUuC,QAAQ/C,OAAOO,KAAK,CAACC,QAAQ;gBACvCwC,QAAQhD,OAAOO,KAAK,CAACyC,MAAM;YAC7B,GACA/C,QAAQG,OAAO;YAGjB;;;;OAIC,GACDH,QAAQ0C,KAAK,IACX,IAAIjD,iBACF;gBACEuD,QAAQ9D,QAAQa,OAAOe,MAAM,CAACG,IAAI,EAAElB,OAAOe,MAAM,CAACE,QAAQ;gBAC1Df,UAAUD,QAAQC,QAAQ;gBAC1BgD,OAAO;oBAAClD,OAAOuC,QAAQ,CAACrB,IAAI;iBAAC;YAC/B,GACAjB,QAAQG,OAAO;SAEpB,CAAC+C,MAAM,CAACJ;QAET;;;;KAIC,GACDK,cAAc;YACZC,UAAUrD,OAAOe,MAAM,CAACsC,QAAQ;YAEhC;;OAEC,GACDC,WAAW;gBACT,IAAIlE,aAAa;oBACfmE,UAAU;gBACZ;aACD;QACH;QAEA;;;;;KAKC,GACDC,uBAAuB;YACrB;;;OAGC,GACDC,OAAO;QACT;IACF;AACF"}
@@ -0,0 +1,5 @@
1
+ export * from './compiler';
2
+ export * from './config';
3
+ export * from './plugins';
4
+
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/webpack/index.ts"],"sourcesContent":["export * from './compiler';\nexport * from './config';\nexport * from './plugins';\n"],"names":[],"mappings":"AAAA,cAAc,aAAa;AAC3B,cAAc,WAAW;AACzB,cAAc,YAAY"}
@@ -0,0 +1,82 @@
1
+ import browserify from 'browserify';
2
+ import { Readable } from 'stream';
3
+ import { TranspilationModes } from '../../builders';
4
+ import { processDependencies } from '../../utils';
5
+ import { getBrowserslistTargets } from '../utils';
6
+ /**
7
+ * A Browserify loader for Webpack. This exists for backwards compatibility with
8
+ * the legacy configuration format, in order to support the `bundlerCustomizer`
9
+ * function.
10
+ *
11
+ * When this loader is used, the input file will be processed by Browserify, and
12
+ * written to disk by Webpack. Most processing will be handled by Browserify, so
13
+ * there are no benefits like tree-shaking.
14
+ *
15
+ * @param content - The input file contents as a string.
16
+ * @param sourceMap - The source map of the input file.
17
+ */ const loader = async function(content, sourceMap) {
18
+ const config = this.getOptions();
19
+ const { transpilationMode } = config;
20
+ const bundler = browserify({
21
+ extensions: [
22
+ '.js',
23
+ '.ts'
24
+ ],
25
+ debug: Boolean(sourceMap),
26
+ standalone: '<snap>'
27
+ });
28
+ if (transpilationMode !== TranspilationModes.None) {
29
+ const babelifyOptions = processDependencies(config);
30
+ // We need to statically import all Browserify transforms, and all Babel
31
+ // presets and plugins, and calling `require` is the sanest way to do that.
32
+ /* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires, n/global-require */ bundler.transform(require('babelify'), {
33
+ global: transpilationMode === TranspilationModes.LocalAndDeps,
34
+ extensions: [
35
+ '.js',
36
+ '.ts'
37
+ ],
38
+ presets: [
39
+ require('@babel/preset-typescript'),
40
+ [
41
+ require('@babel/preset-env'),
42
+ {
43
+ targets: {
44
+ browsers: await getBrowserslistTargets()
45
+ }
46
+ }
47
+ ]
48
+ ],
49
+ plugins: [
50
+ require('@babel/plugin-transform-runtime'),
51
+ require('@babel/plugin-proposal-class-properties'),
52
+ require('@babel/plugin-proposal-private-methods'),
53
+ require('@babel/plugin-proposal-class-static-block'),
54
+ require('@babel/plugin-proposal-private-property-in-object')
55
+ ],
56
+ ...babelifyOptions
57
+ });
58
+ /* eslint-enable @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires, n/global-require */ }
59
+ config.bundlerCustomizer?.(bundler);
60
+ // Browserify doesn't accept a string as an entry point, so we need to convert
61
+ // it to a stream.
62
+ const stream = new Readable();
63
+ stream.push(content);
64
+ stream.push(null);
65
+ bundler.add(stream, {
66
+ file: this.resourcePath
67
+ });
68
+ return new Promise((resolve, reject)=>{
69
+ bundler.bundle((bundleError, buffer)=>{
70
+ if (bundleError) {
71
+ reject(bundleError);
72
+ return;
73
+ }
74
+ // Browserify inlines the source map, so we just pass the output buffer back
75
+ // to Webpack.
76
+ resolve(buffer);
77
+ });
78
+ });
79
+ };
80
+ export default loader;
81
+
82
+ //# sourceMappingURL=browserify.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/webpack/loaders/browserify.ts"],"sourcesContent":["import browserify from 'browserify';\nimport { Readable } from 'stream';\nimport type { LoaderDefinitionFunction } from 'webpack';\n\nimport { TranspilationModes } from '../../builders';\nimport type { LegacyOptions } from '../../config';\nimport { processDependencies } from '../../utils';\nimport { getBrowserslistTargets } from '../utils';\n\n/**\n * A Browserify loader for Webpack. This exists for backwards compatibility with\n * the legacy configuration format, in order to support the `bundlerCustomizer`\n * function.\n *\n * When this loader is used, the input file will be processed by Browserify, and\n * written to disk by Webpack. Most processing will be handled by Browserify, so\n * there are no benefits like tree-shaking.\n *\n * @param content - The input file contents as a string.\n * @param sourceMap - The source map of the input file.\n */\nconst loader: LoaderDefinitionFunction<LegacyOptions> = async function (\n content,\n sourceMap,\n) {\n const config = this.getOptions();\n\n const { transpilationMode } = config;\n\n const bundler = browserify({\n extensions: ['.js', '.ts'],\n debug: Boolean(sourceMap),\n standalone: '<snap>',\n });\n\n if (transpilationMode !== TranspilationModes.None) {\n const babelifyOptions = processDependencies(config);\n\n // We need to statically import all Browserify transforms, and all Babel\n // presets and plugins, and calling `require` is the sanest way to do that.\n /* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires, n/global-require */\n bundler.transform(require('babelify'), {\n global: transpilationMode === TranspilationModes.LocalAndDeps,\n extensions: ['.js', '.ts'],\n presets: [\n require('@babel/preset-typescript'),\n [\n require('@babel/preset-env'),\n {\n targets: {\n browsers: await getBrowserslistTargets(),\n },\n },\n ],\n ],\n plugins: [\n require('@babel/plugin-transform-runtime'),\n require('@babel/plugin-proposal-class-properties'),\n require('@babel/plugin-proposal-private-methods'),\n require('@babel/plugin-proposal-class-static-block'),\n require('@babel/plugin-proposal-private-property-in-object'),\n ],\n ...(babelifyOptions as any),\n });\n /* eslint-enable @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires, n/global-require */\n }\n\n config.bundlerCustomizer?.(bundler);\n\n // Browserify doesn't accept a string as an entry point, so we need to convert\n // it to a stream.\n const stream = new Readable();\n stream.push(content);\n stream.push(null);\n\n bundler.add(stream, {\n file: this.resourcePath,\n });\n\n return new Promise<Buffer>((resolve, reject) => {\n bundler.bundle((bundleError, buffer: Buffer) => {\n if (bundleError) {\n reject(bundleError);\n return;\n }\n\n // Browserify inlines the source map, so we just pass the output buffer back\n // to Webpack.\n resolve(buffer);\n });\n });\n};\n\nexport default loader;\n"],"names":["browserify","Readable","TranspilationModes","processDependencies","getBrowserslistTargets","loader","content","sourceMap","config","getOptions","transpilationMode","bundler","extensions","debug","Boolean","standalone","None","babelifyOptions","transform","require","global","LocalAndDeps","presets","targets","browsers","plugins","bundlerCustomizer","stream","push","add","file","resourcePath","Promise","resolve","reject","bundle","bundleError","buffer"],"mappings":"AAAA,OAAOA,gBAAgB,aAAa;AACpC,SAASC,QAAQ,QAAQ,SAAS;AAGlC,SAASC,kBAAkB,QAAQ,iBAAiB;AAEpD,SAASC,mBAAmB,QAAQ,cAAc;AAClD,SAASC,sBAAsB,QAAQ,WAAW;AAElD;;;;;;;;;;;CAWC,GACD,MAAMC,SAAkD,eACtDC,OAAO,EACPC,SAAS;IAET,MAAMC,SAAS,IAAI,CAACC,UAAU;IAE9B,MAAM,EAAEC,iBAAiB,EAAE,GAAGF;IAE9B,MAAMG,UAAUX,WAAW;QACzBY,YAAY;YAAC;YAAO;SAAM;QAC1BC,OAAOC,QAAQP;QACfQ,YAAY;IACd;IAEA,IAAIL,sBAAsBR,mBAAmBc,IAAI,EAAE;QACjD,MAAMC,kBAAkBd,oBAAoBK;QAE5C,wEAAwE;QACxE,2EAA2E;QAC3E,8GAA8G,GAC9GG,QAAQO,SAAS,CAACC,QAAQ,aAAa;YACrCC,QAAQV,sBAAsBR,mBAAmBmB,YAAY;YAC7DT,YAAY;gBAAC;gBAAO;aAAM;YAC1BU,SAAS;gBACPH,QAAQ;gBACR;oBACEA,QAAQ;oBACR;wBACEI,SAAS;4BACPC,UAAU,MAAMpB;wBAClB;oBACF;iBACD;aACF;YACDqB,SAAS;gBACPN,QAAQ;gBACRA,QAAQ;gBACRA,QAAQ;gBACRA,QAAQ;gBACRA,QAAQ;aACT;YACD,GAAIF,eAAe;QACrB;IACA,6GAA6G,GAC/G;IAEAT,OAAOkB,iBAAiB,GAAGf;IAE3B,8EAA8E;IAC9E,kBAAkB;IAClB,MAAMgB,SAAS,IAAI1B;IACnB0B,OAAOC,IAAI,CAACtB;IACZqB,OAAOC,IAAI,CAAC;IAEZjB,QAAQkB,GAAG,CAACF,QAAQ;QAClBG,MAAM,IAAI,CAACC,YAAY;IACzB;IAEA,OAAO,IAAIC,QAAgB,CAACC,SAASC;QACnCvB,QAAQwB,MAAM,CAAC,CAACC,aAAaC;YAC3B,IAAID,aAAa;gBACfF,OAAOE;gBACP;YACF;YAEA,4EAA4E;YAC5E,cAAc;YACdH,QAAQI;QACV;IACF;AACF;AAEA,eAAehC,OAAO"}
@@ -0,0 +1,26 @@
1
+ import { assert, stringToBytes } from '@metamask/utils';
2
+ /**
3
+ * A Webpack loader that inlines the WASM module as a `Uint8Array`. This makes
4
+ * it possible to import the WASM module directly, and use it with the
5
+ * `WebAssembly.instantiate` function.
6
+ *
7
+ * This is useful, because snaps are not allowed to import assets from outside
8
+ * of their package. This loader allows you to inline the WASM module as a
9
+ * `Uint8Array`, which can then be passed to `WebAssembly.instantiate`.
10
+ *
11
+ * @param source - The WASM module as a string.
12
+ * @returns A string that exports the WASM module as a `Uint8Array`.
13
+ * @example
14
+ * ```ts
15
+ * import wasm from './program.wasm';
16
+ *
17
+ * const { instance } = await WebAssembly.instantiate(wasm, {});
18
+ * // Do something with the WASM module...
19
+ * ```
20
+ */ export default function loader(source) {
21
+ assert(typeof source === 'string', 'Expected source to be a string.');
22
+ const bytes = stringToBytes(source);
23
+ return `export default new Uint8Array(${JSON.stringify(Array.from(bytes))});`;
24
+ }
25
+
26
+ //# sourceMappingURL=wasm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/webpack/loaders/wasm.ts"],"sourcesContent":["import { assert, stringToBytes } from '@metamask/utils';\n\n/**\n * A Webpack loader that inlines the WASM module as a `Uint8Array`. This makes\n * it possible to import the WASM module directly, and use it with the\n * `WebAssembly.instantiate` function.\n *\n * This is useful, because snaps are not allowed to import assets from outside\n * of their package. This loader allows you to inline the WASM module as a\n * `Uint8Array`, which can then be passed to `WebAssembly.instantiate`.\n *\n * @param source - The WASM module as a string.\n * @returns A string that exports the WASM module as a `Uint8Array`.\n * @example\n * ```ts\n * import wasm from './program.wasm';\n *\n * const { instance } = await WebAssembly.instantiate(wasm, {});\n * // Do something with the WASM module...\n * ```\n */\nexport default function loader(source: unknown) {\n assert(typeof source === 'string', 'Expected source to be a string.');\n\n const bytes = stringToBytes(source);\n return `export default new Uint8Array(${JSON.stringify(Array.from(bytes))});`;\n}\n"],"names":["assert","stringToBytes","loader","source","bytes","JSON","stringify","Array","from"],"mappings":"AAAA,SAASA,MAAM,EAAEC,aAAa,QAAQ,kBAAkB;AAExD;;;;;;;;;;;;;;;;;;CAkBC,GACD,eAAe,SAASC,OAAOC,MAAe;IAC5CH,OAAO,OAAOG,WAAW,UAAU;IAEnC,MAAMC,QAAQH,cAAcE;IAC5B,OAAO,CAAC,8BAA8B,EAAEE,KAAKC,SAAS,CAACC,MAAMC,IAAI,CAACJ,QAAQ,EAAE,CAAC;AAC/E"}