@metamask/snaps-cli 1.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/.browserslistrc +3 -0
  2. package/CHANGELOG.md +32 -377
  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 +239 -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 +241 -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
@@ -0,0 +1,340 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ SnapsStatsPlugin: function() {
13
+ return SnapsStatsPlugin;
14
+ },
15
+ SnapsWatchPlugin: function() {
16
+ return SnapsWatchPlugin;
17
+ },
18
+ SnapsBuiltInResolver: function() {
19
+ return SnapsBuiltInResolver;
20
+ },
21
+ SnapsBundleWarningsPlugin: function() {
22
+ return SnapsBundleWarningsPlugin;
23
+ }
24
+ });
25
+ const _snapsutils = require("@metamask/snaps-utils");
26
+ const _utils = require("@metamask/utils");
27
+ const _chalk = require("chalk");
28
+ const _module = require("module");
29
+ const _eval = require("../commands/eval");
30
+ const _utils1 = require("../utils");
31
+ const _utils2 = require("./utils");
32
+ function _check_private_redeclaration(obj, privateCollection) {
33
+ if (privateCollection.has(obj)) {
34
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
35
+ }
36
+ }
37
+ function _class_apply_descriptor_get(receiver, descriptor) {
38
+ if (descriptor.get) {
39
+ return descriptor.get.call(receiver);
40
+ }
41
+ return descriptor.value;
42
+ }
43
+ function _class_apply_descriptor_set(receiver, descriptor, value) {
44
+ if (descriptor.set) {
45
+ descriptor.set.call(receiver, value);
46
+ } else {
47
+ if (!descriptor.writable) {
48
+ throw new TypeError("attempted to set read only private field");
49
+ }
50
+ descriptor.value = value;
51
+ }
52
+ }
53
+ function _class_extract_field_descriptor(receiver, privateMap, action) {
54
+ if (!privateMap.has(receiver)) {
55
+ throw new TypeError("attempted to " + action + " private field on non-instance");
56
+ }
57
+ return privateMap.get(receiver);
58
+ }
59
+ function _class_private_field_get(receiver, privateMap) {
60
+ var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
61
+ return _class_apply_descriptor_get(receiver, descriptor);
62
+ }
63
+ function _class_private_field_init(obj, privateMap, value) {
64
+ _check_private_redeclaration(obj, privateMap);
65
+ privateMap.set(obj, value);
66
+ }
67
+ function _class_private_field_set(receiver, privateMap, value) {
68
+ var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
69
+ _class_apply_descriptor_set(receiver, descriptor, value);
70
+ return value;
71
+ }
72
+ function _class_private_method_get(receiver, privateSet, fn) {
73
+ if (!privateSet.has(receiver)) {
74
+ throw new TypeError("attempted to get private field on non-instance");
75
+ }
76
+ return fn;
77
+ }
78
+ function _class_private_method_init(obj, privateSet) {
79
+ _check_private_redeclaration(obj, privateSet);
80
+ privateSet.add(obj);
81
+ }
82
+ function _define_property(obj, key, value) {
83
+ if (key in obj) {
84
+ Object.defineProperty(obj, key, {
85
+ value: value,
86
+ enumerable: true,
87
+ configurable: true,
88
+ writable: true
89
+ });
90
+ } else {
91
+ obj[key] = value;
92
+ }
93
+ return obj;
94
+ }
95
+ var _spinner = /*#__PURE__*/ new WeakMap(), /**
96
+ * Get the error message for the given stats error.
97
+ *
98
+ * @param statsError - The stats error.
99
+ * @returns The error message.
100
+ */ _getStatsErrorMessage = /*#__PURE__*/ new WeakSet();
101
+ class SnapsStatsPlugin {
102
+ /**
103
+ * Apply the plugin to the Webpack compiler.
104
+ *
105
+ * @param compiler - The Webpack compiler.
106
+ */ apply(compiler) {
107
+ compiler.hooks.afterDone.tap(this.constructor.name, (stats)=>{
108
+ if (!stats) {
109
+ return;
110
+ }
111
+ const { modules, time, errors } = stats.toJson();
112
+ (0, _utils.assert)(modules, 'Modules must be defined in stats.');
113
+ (0, _utils.assert)(time, 'Time must be defined in stats.');
114
+ if (errors?.length) {
115
+ const formattedErrors = errors.map(_class_private_method_get(this, _getStatsErrorMessage, getStatsErrorMessage).bind(this)).join('\n\n');
116
+ (0, _utils1.error)(`Compiled ${modules.length} ${(0, _utils2.pluralize)(modules.length, 'file')} in ${time}ms with ${errors.length} ${(0, _utils2.pluralize)(errors.length, 'error')}.\n\n${formattedErrors}\n`, _class_private_field_get(this, _spinner));
117
+ _class_private_field_get(this, _spinner)?.stop();
118
+ process.exitCode = 1;
119
+ return;
120
+ }
121
+ (0, _utils1.info)(`Compiled ${modules.length} ${(0, _utils2.pluralize)(modules.length, 'file')} in ${time}ms.`, _class_private_field_get(this, _spinner));
122
+ if (compiler.watchMode) {
123
+ // The spinner may be restarted by the watch plugin, outside of the
124
+ // `executeSteps` flow, so we stop it here just in case.
125
+ _class_private_field_get(this, _spinner)?.succeed('Done!');
126
+ }
127
+ });
128
+ }
129
+ constructor(options = {
130
+ verbose: false
131
+ }, spinner){
132
+ _class_private_method_init(this, _getStatsErrorMessage);
133
+ /**
134
+ * The options for the plugin.
135
+ */ _define_property(this, "options", void 0);
136
+ /**
137
+ * The spinner to use for logging.
138
+ */ _class_private_field_init(this, _spinner, {
139
+ writable: true,
140
+ value: void 0
141
+ });
142
+ this.options = options;
143
+ _class_private_field_set(this, _spinner, spinner);
144
+ }
145
+ }
146
+ function getStatsErrorMessage(statsError) {
147
+ const baseMessage = this.options.verbose ? (0, _utils1.getErrorMessage)(statsError) : statsError.message;
148
+ const [first, ...rest] = baseMessage.split('\n');
149
+ return [
150
+ (0, _snapsutils.indent)((0, _chalk.red)(`• ${first}`), 2),
151
+ ...rest.map((message)=>(0, _snapsutils.indent)((0, _chalk.red)(message), 4)),
152
+ statsError.details && (0, _snapsutils.indent)((0, _chalk.dim)(statsError.details), 4)
153
+ ].filter(Boolean).join('\n');
154
+ }
155
+ var _spinner1 = /*#__PURE__*/ new WeakMap(), _safeEvaluate = /*#__PURE__*/ new WeakSet();
156
+ class SnapsWatchPlugin {
157
+ /**
158
+ * Apply the plugin to the Webpack compiler.
159
+ *
160
+ * @param compiler - The Webpack compiler.
161
+ */ apply(compiler) {
162
+ compiler.hooks.invalid.tap(this.constructor.name, (file)=>{
163
+ _class_private_field_get(this, _spinner1)?.start();
164
+ (0, _utils1.info)(`Changes detected in ${(0, _chalk.yellow)(file)}, recompiling.`, _class_private_field_get(this, _spinner1));
165
+ });
166
+ compiler.hooks.afterEmit.tapPromise(this.constructor.name, async ({ fileDependencies })=>{
167
+ this.options.files?.forEach(fileDependencies.add.bind(fileDependencies));
168
+ if (this.options.bundle && this.options.evaluate) {
169
+ await _class_private_method_get(this, _safeEvaluate, safeEvaluate).call(this, this.options.bundle);
170
+ }
171
+ });
172
+ }
173
+ constructor(options, spinner){
174
+ /**
175
+ * Safely evaluate the bundle at the given path. If an error occurs, it will
176
+ * be logged to the console, rather than throwing an error.
177
+ *
178
+ * This function should never throw an error.
179
+ *
180
+ * @param bundlePath - The path to the bundle.
181
+ */ _class_private_method_init(this, _safeEvaluate);
182
+ /**
183
+ * The options for the plugin.
184
+ */ _define_property(this, "options", void 0);
185
+ /**
186
+ * The spinner to use for logging.
187
+ */ _class_private_field_init(this, _spinner1, {
188
+ writable: true,
189
+ value: void 0
190
+ });
191
+ this.options = options;
192
+ _class_private_field_set(this, _spinner1, spinner);
193
+ }
194
+ }
195
+ async function safeEvaluate(bundlePath) {
196
+ try {
197
+ await (0, _eval.evaluate)(bundlePath);
198
+ (0, _utils1.info)(`Snap bundle evaluated successfully.`, _class_private_field_get(this, _spinner1));
199
+ } catch (evaluateError) {
200
+ (0, _utils1.error)(evaluateError.message, _class_private_field_get(this, _spinner1));
201
+ }
202
+ }
203
+ var _source = /*#__PURE__*/ new WeakMap(), _spinner2 = /*#__PURE__*/ new WeakMap();
204
+ class SnapsBuiltInResolver {
205
+ /**
206
+ * Apply the plugin to the Webpack resolver.
207
+ *
208
+ * @param resolver - The Webpack resolver.
209
+ */ apply(resolver) {
210
+ resolver.getHook(_class_private_field_get(this, _source)).tapAsync(this.constructor.name, ({ module: isModule, request }, _, callback)=>{
211
+ if (!isModule || !request) {
212
+ return callback();
213
+ }
214
+ const baseRequest = request.split('/')[0];
215
+ if ((0, _module.isBuiltin)(baseRequest) && !this.options.ignore?.includes(baseRequest)) {
216
+ const fallback = resolver.options.fallback.find(({ name })=>name === baseRequest);
217
+ if (fallback && !fallback.alias) {
218
+ this.unresolvedModules.add(baseRequest);
219
+ }
220
+ }
221
+ return callback();
222
+ });
223
+ }
224
+ constructor(options = {
225
+ ignore: []
226
+ }, spinner){
227
+ /**
228
+ * The built-in modules that have been imported, but not resolved.
229
+ */ _define_property(this, "unresolvedModules", new Set());
230
+ /**
231
+ * The name of the resolver hook to tap into.
232
+ */ _class_private_field_init(this, _source, {
233
+ writable: true,
234
+ value: 'described-resolve'
235
+ });
236
+ /**
237
+ * The options for the plugin.
238
+ */ _define_property(this, "options", void 0);
239
+ /**
240
+ * The spinner to use for logging.
241
+ */ _class_private_field_init(this, _spinner2, {
242
+ writable: true,
243
+ value: void 0
244
+ });
245
+ this.options = options;
246
+ _class_private_field_set(this, _spinner2, spinner);
247
+ }
248
+ }
249
+ var _spinner3 = /*#__PURE__*/ new WeakMap(), /**
250
+ * Check if a built-in module is used, but not provided by Webpack's
251
+ * `fallback` configuration.
252
+ *
253
+ * @param compiler - The Webpack compiler.
254
+ */ _checkBuiltIns = /*#__PURE__*/ new WeakSet(), /**
255
+ * Check if the given instance is a `ProvidePlugin`. This is not guaranteed to
256
+ * be accurate, but it's good enough for our purposes. If we were to use
257
+ * `instanceof` instead, it might not work if multiple versions of Webpack are
258
+ * installed.
259
+ *
260
+ * @param instance - The instance to check.
261
+ * @returns Whether the instance is a `ProvidePlugin`, i.e., whether it's an
262
+ * object with the name `ProvidePlugin` and a `definitions` property.
263
+ */ _isProvidePlugin = /*#__PURE__*/ new WeakSet(), /**
264
+ * Check if the `Buffer` global is used, but not provided by Webpack's
265
+ * `DefinePlugin`.
266
+ *
267
+ * @param compiler - The Webpack compiler.
268
+ */ _checkBuffer = /*#__PURE__*/ new WeakSet();
269
+ class SnapsBundleWarningsPlugin {
270
+ /**
271
+ * Apply the plugin to the Webpack compiler.
272
+ *
273
+ * @param compiler - The Webpack compiler.
274
+ */ apply(compiler) {
275
+ if (this.options.builtIns) {
276
+ _class_private_method_get(this, _checkBuiltIns, checkBuiltIns).call(this, compiler);
277
+ }
278
+ if (this.options.buffer) {
279
+ _class_private_method_get(this, _checkBuffer, checkBuffer).call(this, compiler);
280
+ }
281
+ }
282
+ constructor(options = {
283
+ buffer: true,
284
+ builtIns: true
285
+ }, spinner){
286
+ _class_private_method_init(this, _checkBuiltIns);
287
+ _class_private_method_init(this, _isProvidePlugin);
288
+ _class_private_method_init(this, _checkBuffer);
289
+ /**
290
+ * The spinner to use for logging.
291
+ */ _class_private_field_init(this, _spinner3, {
292
+ writable: true,
293
+ value: void 0
294
+ });
295
+ /**
296
+ * The options for the plugin.
297
+ */ _define_property(this, "options", void 0);
298
+ this.options = options;
299
+ _class_private_field_set(this, _spinner3, spinner);
300
+ }
301
+ }
302
+ function checkBuiltIns(compiler) {
303
+ compiler.hooks.afterCompile.tap(this.constructor.name, ()=>{
304
+ if (!this.options.builtInResolver) {
305
+ return;
306
+ }
307
+ const { unresolvedModules } = this.options.builtInResolver;
308
+ if (unresolvedModules.size === 0) {
309
+ return;
310
+ }
311
+ const formattedModules = new Array(...unresolvedModules).map((name)=>(0, _snapsutils.indent)(`• ${name}`, 2)).join('\n');
312
+ (0, _utils1.warn)(`The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided.\n` + `The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${(0, _chalk.yellow)(`polyfills`)} to ${(0, _chalk.yellow)(`true`)} or an object with the builtins to polyfill as the key and ${(0, _chalk.yellow)(`true`)} as the value.\n` + `To disable this warning, set ${(0, _chalk.yellow)('`stats.builtIns`')} to ${(0, _chalk.yellow)('`false`')} in your snap config file, or add the module to the ${(0, _chalk.yellow)('`stats.builtIns.ignore`')} array.\n\n${formattedModules}\n`, _class_private_field_get(this, _spinner3));
313
+ });
314
+ }
315
+ function isProvidePlugin(instance) {
316
+ return (0, _utils.isObject)(instance) && instance.constructor.name === 'ProvidePlugin' && (0, _utils.hasProperty)(instance, 'definitions');
317
+ }
318
+ function checkBuffer(compiler) {
319
+ const plugin = compiler.options.plugins?.find((instance)=>_class_private_method_get(this, _isProvidePlugin, isProvidePlugin).call(this, instance));
320
+ // If the `ProvidePlugin` is configured to provide `Buffer`, then we don't
321
+ // need to warn the user.
322
+ if (plugin) {
323
+ const { definitions } = plugin;
324
+ if (definitions.Buffer) {
325
+ return;
326
+ }
327
+ }
328
+ compiler.hooks.compilation.tap(this.constructor.name, (compilation)=>{
329
+ compilation.hooks.afterProcessAssets.tap(this.constructor.name, (assets)=>{
330
+ // Check if assets use `Buffer`.
331
+ const bufferAssets = Object.entries(assets).filter(([name])=>name.endsWith('.js')).filter(([, asset])=>asset.source().includes('Buffer'));
332
+ if (bufferAssets.length === 0) {
333
+ return;
334
+ }
335
+ (0, _utils1.warn)(`The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default.\n` + `To use the Buffer global, you must polyfill Buffer by setting ${(0, _chalk.yellow)(`buffer`)} to ${(0, _chalk.yellow)(`true`)} in the ${(0, _chalk.yellow)(`polyfills`)} config object in your snap config.\n` + `To disable this warning, set ${(0, _chalk.yellow)('`stats.buffer`')} to ${(0, _chalk.yellow)('`false`')} in your snap config file.`, _class_private_field_get(this, _spinner3));
336
+ });
337
+ });
338
+ }
339
+
340
+ //# sourceMappingURL=plugins.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/webpack/plugins.ts"],"sourcesContent":["import { indent } from '@metamask/snaps-utils';\nimport { assert, hasProperty, isObject } from '@metamask/utils';\nimport { dim, red, yellow } from 'chalk';\nimport { isBuiltin } from 'module';\nimport type { Ora } from 'ora';\nimport type {\n Compiler,\n ProvidePlugin,\n ResolvePluginInstance,\n Resolver,\n StatsError,\n WebpackPluginInstance,\n} from 'webpack';\n\nimport { evaluate } from '../commands/eval';\nimport { error, getErrorMessage, info, warn } from '../utils';\nimport { pluralize } from './utils';\n\nexport type SnapsStatsPluginOptions = {\n /**\n * Whether to log the verbose stats.\n */\n verbose?: boolean;\n};\n\n/**\n * A plugin that logs the stats after compilation. This is useful for logging\n * the number of files compiled, and the time taken to compile them.\n */\nexport class SnapsStatsPlugin implements WebpackPluginInstance {\n /**\n * The options for the plugin.\n */\n readonly options: SnapsStatsPluginOptions;\n\n /**\n * The spinner to use for logging.\n */\n readonly #spinner?: Ora;\n\n constructor(\n options: SnapsStatsPluginOptions = {\n verbose: false,\n },\n spinner?: Ora,\n ) {\n this.options = options;\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n compiler.hooks.afterDone.tap(this.constructor.name, (stats) => {\n if (!stats) {\n return;\n }\n\n const { modules, time, errors } = stats.toJson();\n\n assert(modules, 'Modules must be defined in stats.');\n assert(time, 'Time must be defined in stats.');\n\n if (errors?.length) {\n const formattedErrors = errors\n .map(this.#getStatsErrorMessage.bind(this))\n .join('\\n\\n');\n\n error(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms with ${errors.length} ${pluralize(\n errors.length,\n 'error',\n )}.\\n\\n${formattedErrors}\\n`,\n this.#spinner,\n );\n\n this.#spinner?.stop();\n\n process.exitCode = 1;\n return;\n }\n\n info(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms.`,\n this.#spinner,\n );\n\n if (compiler.watchMode) {\n // The spinner may be restarted by the watch plugin, outside of the\n // `executeSteps` flow, so we stop it here just in case.\n this.#spinner?.succeed('Done!');\n }\n });\n }\n\n /**\n * Get the error message for the given stats error.\n *\n * @param statsError - The stats error.\n * @returns The error message.\n */\n #getStatsErrorMessage(statsError: StatsError) {\n const baseMessage = this.options.verbose\n ? getErrorMessage(statsError)\n : statsError.message;\n\n const [first, ...rest] = baseMessage.split('\\n');\n\n return [\n indent(red(`• ${first}`), 2),\n ...rest.map((message) => indent(red(message), 4)),\n statsError.details && indent(dim(statsError.details), 4),\n ]\n .filter(Boolean)\n .join('\\n');\n }\n}\n\n/**\n * The options for the {@link SnapsWatchPlugin}.\n */\nexport type SnapsWatchPluginOptions = {\n /**\n * The bundle path. This is the file that will be evaluated, if the `evaluate`\n * option is set.\n */\n bundle?: string;\n\n /**\n * Whether to evaluate the bundle. This only applies if the `bundle` option is\n * set.\n */\n evaluate?: boolean;\n\n /**\n * The extra files to watch.\n */\n files?: string[];\n};\n\n/**\n * A plugin that adds extra files to watch. This is useful for watching files\n * that are not imported by the entry point, such as the `snap.manifest.json`\n * file.\n */\nexport class SnapsWatchPlugin implements WebpackPluginInstance {\n /**\n * The options for the plugin.\n */\n readonly options: SnapsWatchPluginOptions;\n\n /**\n * The spinner to use for logging.\n */\n readonly #spinner?: Ora;\n\n constructor(options: SnapsWatchPluginOptions, spinner?: Ora) {\n this.options = options;\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n compiler.hooks.invalid.tap(this.constructor.name, (file) => {\n this.#spinner?.start();\n info(`Changes detected in ${yellow(file)}, recompiling.`, this.#spinner);\n });\n\n compiler.hooks.afterEmit.tapPromise(\n this.constructor.name,\n async ({ fileDependencies }) => {\n this.options.files?.forEach(\n fileDependencies.add.bind(fileDependencies),\n );\n\n if (this.options.bundle && this.options.evaluate) {\n await this.#safeEvaluate(this.options.bundle);\n }\n },\n );\n }\n\n /**\n * Safely evaluate the bundle at the given path. If an error occurs, it will\n * be logged to the console, rather than throwing an error.\n *\n * This function should never throw an error.\n *\n * @param bundlePath - The path to the bundle.\n */\n async #safeEvaluate(bundlePath: string) {\n try {\n await evaluate(bundlePath);\n info(`Snap bundle evaluated successfully.`, this.#spinner);\n } catch (evaluateError) {\n error(evaluateError.message, this.#spinner);\n }\n }\n}\n\n/**\n * The options for the {@link SnapsBuiltInResolver}.\n */\nexport type SnapsBuiltInResolverOptions = {\n /**\n * The built-in modules to ignore.\n */\n ignore?: string[];\n};\n\n/**\n * A plugin that logs a message when a built-in module is not resolved. The\n * MetaMask Snaps CLI does not support built-in modules by default, and this\n * plugin is used to warn the user when they try to import a built-in module,\n * when no fallback is configured.\n */\nexport class SnapsBuiltInResolver implements ResolvePluginInstance {\n /**\n * The built-in modules that have been imported, but not resolved.\n */\n readonly unresolvedModules = new Set<string>();\n\n /**\n * The name of the resolver hook to tap into.\n */\n readonly #source = 'described-resolve';\n\n /**\n * The options for the plugin.\n */\n readonly options: SnapsBuiltInResolverOptions;\n\n /**\n * The spinner to use for logging.\n */\n readonly #spinner?: Ora;\n\n constructor(\n options: SnapsBuiltInResolverOptions = {\n ignore: [],\n },\n spinner?: Ora,\n ) {\n this.options = options;\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack resolver.\n *\n * @param resolver - The Webpack resolver.\n */\n apply(resolver: Resolver) {\n resolver\n .getHook(this.#source)\n .tapAsync(\n this.constructor.name,\n ({ module: isModule, request }, _, callback) => {\n if (!isModule || !request) {\n return callback();\n }\n\n const baseRequest = request.split('/')[0];\n if (\n isBuiltin(baseRequest) &&\n !this.options.ignore?.includes(baseRequest)\n ) {\n const fallback = resolver.options.fallback.find(\n ({ name }) => name === baseRequest,\n );\n\n if (fallback && !fallback.alias) {\n this.unresolvedModules.add(baseRequest);\n }\n }\n\n return callback();\n },\n );\n }\n}\n\n/**\n * The options for the {@link SnapsBundleWarningsPlugin}.\n */\nexport type SnapsBundleWarningsPluginOptions = {\n /**\n * The {@link SnapsBuiltInResolver} instance to use for detecting built-in\n * modules.\n */\n builtInResolver?: SnapsBuiltInResolver | false;\n\n /**\n * Whether to show warnings if built-in modules are used, but not provided by\n * Webpack's `fallback` configuration.\n */\n builtIns?: boolean;\n\n /**\n * Whether to show warnings if the `Buffer` global is used, but not provided\n * by Webpack's `DefinePlugin`.\n */\n buffer?: boolean;\n};\n\n/**\n * A plugin that logs a message when:\n *\n * - A built-in module is not resolved. The MetaMask Snaps CLI does not support\n * built-in modules by default, and this plugin is used to warn the user when\n * they try to import a built-in module, when no fallback is configured.\n * - A snap uses the `Buffer` global. The MetaMask Snaps CLI does not support\n * the `Buffer` global by default, and this plugin is used to warn the user when\n * they try to use the `Buffer` global.\n *\n * We use both a resolver and a plugin, because the resolver is used to detect\n * when a built-in module is imported, and the plugin is used to log a single\n * message when the compilation is complete. We can't do everything in a single\n * plugin, because the resolver doesn't have access to the compilation, and the\n * plugin doesn't have access to the resolver.\n */\n\nexport class SnapsBundleWarningsPlugin implements WebpackPluginInstance {\n /**\n * The spinner to use for logging.\n */\n readonly #spinner?: Ora;\n\n /**\n * The options for the plugin.\n */\n readonly options: SnapsBundleWarningsPluginOptions;\n\n constructor(\n options: SnapsBundleWarningsPluginOptions = {\n buffer: true,\n builtIns: true,\n },\n spinner?: Ora,\n ) {\n this.options = options;\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n if (this.options.builtIns) {\n this.#checkBuiltIns(compiler);\n }\n\n if (this.options.buffer) {\n this.#checkBuffer(compiler);\n }\n }\n\n /**\n * Check if a built-in module is used, but not provided by Webpack's\n * `fallback` configuration.\n *\n * @param compiler - The Webpack compiler.\n */\n #checkBuiltIns(compiler: Compiler) {\n compiler.hooks.afterCompile.tap(this.constructor.name, () => {\n if (!this.options.builtInResolver) {\n return;\n }\n\n const { unresolvedModules } = this.options.builtInResolver;\n if (unresolvedModules.size === 0) {\n return;\n }\n\n const formattedModules = new Array(...unresolvedModules)\n .map((name) => indent(`• ${name}`, 2))\n .join('\\n');\n\n warn(\n `The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided.\\n` +\n `The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${yellow(\n `polyfills`,\n )} to ${yellow(\n `true`,\n )} or an object with the builtins to polyfill as the key and ${yellow(\n `true`,\n )} as the value.\\n` +\n `To disable this warning, set ${yellow(\n '`stats.builtIns`',\n )} to ${yellow(\n '`false`',\n )} in your snap config file, or add the module to the ${yellow(\n '`stats.builtIns.ignore`',\n )} array.\\n\\n${formattedModules}\\n`,\n this.#spinner,\n );\n });\n }\n\n /**\n * Check if the given instance is a `ProvidePlugin`. This is not guaranteed to\n * be accurate, but it's good enough for our purposes. If we were to use\n * `instanceof` instead, it might not work if multiple versions of Webpack are\n * installed.\n *\n * @param instance - The instance to check.\n * @returns Whether the instance is a `ProvidePlugin`, i.e., whether it's an\n * object with the name `ProvidePlugin` and a `definitions` property.\n */\n #isProvidePlugin(instance: unknown): instance is ProvidePlugin {\n return (\n isObject(instance) &&\n instance.constructor.name === 'ProvidePlugin' &&\n hasProperty(instance, 'definitions')\n );\n }\n\n /**\n * Check if the `Buffer` global is used, but not provided by Webpack's\n * `DefinePlugin`.\n *\n * @param compiler - The Webpack compiler.\n */\n #checkBuffer(compiler: Compiler) {\n const plugin = compiler.options.plugins?.find((instance) =>\n this.#isProvidePlugin(instance),\n ) as ProvidePlugin | undefined;\n\n // If the `ProvidePlugin` is configured to provide `Buffer`, then we don't\n // need to warn the user.\n if (plugin) {\n const { definitions } = plugin;\n if (definitions.Buffer) {\n return;\n }\n }\n\n compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {\n compilation.hooks.afterProcessAssets.tap(\n this.constructor.name,\n (assets) => {\n // Check if assets use `Buffer`.\n const bufferAssets = Object.entries(assets)\n .filter(([name]) => name.endsWith('.js'))\n .filter(([, asset]) => asset.source().includes('Buffer'));\n\n if (bufferAssets.length === 0) {\n return;\n }\n\n warn(\n `The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default.\\n` +\n `To use the Buffer global, you must polyfill Buffer by setting ${yellow(\n `buffer`,\n )} to ${yellow(`true`)} in the ${yellow(\n `polyfills`,\n )} config object in your snap config.\\n` +\n `To disable this warning, set ${yellow(\n '`stats.buffer`',\n )} to ${yellow('`false`')} in your snap config file.`,\n this.#spinner,\n );\n },\n );\n });\n }\n}\n"],"names":["SnapsStatsPlugin","SnapsWatchPlugin","SnapsBuiltInResolver","SnapsBundleWarningsPlugin","apply","compiler","hooks","afterDone","tap","constructor","name","stats","modules","time","errors","toJson","assert","length","formattedErrors","map","getStatsErrorMessage","bind","join","error","pluralize","spinner","stop","process","exitCode","info","watchMode","succeed","options","verbose","statsError","baseMessage","getErrorMessage","message","first","rest","split","indent","red","details","dim","filter","Boolean","invalid","file","start","yellow","afterEmit","tapPromise","fileDependencies","files","forEach","add","bundle","evaluate","safeEvaluate","bundlePath","evaluateError","resolver","getHook","source","tapAsync","module","isModule","request","_","callback","baseRequest","isBuiltin","ignore","includes","fallback","find","alias","unresolvedModules","Set","builtIns","checkBuiltIns","buffer","checkBuffer","afterCompile","builtInResolver","size","formattedModules","Array","warn","instance","isObject","hasProperty","plugin","plugins","isProvidePlugin","definitions","Buffer","compilation","afterProcessAssets","assets","bufferAssets","Object","entries","endsWith","asset"],"mappings":";;;;;;;;;;;IA6BaA,gBAAgB;eAAhBA;;IA6HAC,gBAAgB;eAAhBA;;IA2EAC,oBAAoB;eAApBA;;IA0GAC,yBAAyB;eAAzBA;;;4BA/UU;uBACuB;uBACb;wBACP;sBAWD;wBAC0B;wBACzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAsBf,wCAkET;;;;;GAKC,GACD;AAjFK,MAAMH;IAqBX;;;;GAIC,GACDI,MAAMC,QAAkB,EAAE;QACxBA,SAASC,KAAK,CAACC,SAAS,CAACC,GAAG,CAAC,IAAI,CAACC,WAAW,CAACC,IAAI,EAAE,CAACC;YACnD,IAAI,CAACA,OAAO;gBACV;YACF;YAEA,MAAM,EAAEC,OAAO,EAAEC,IAAI,EAAEC,MAAM,EAAE,GAAGH,MAAMI,MAAM;YAE9CC,IAAAA,aAAM,EAACJ,SAAS;YAChBI,IAAAA,aAAM,EAACH,MAAM;YAEb,IAAIC,QAAQG,QAAQ;gBAClB,MAAMC,kBAAkBJ,OACrBK,GAAG,CAAC,0BAAA,IAAI,EAAEC,uBAAAA,sBAAqBC,IAAI,CAAC,IAAI,GACxCC,IAAI,CAAC;gBAERC,IAAAA,aAAK,EACH,CAAC,SAAS,EAAEX,QAAQK,MAAM,CAAC,CAAC,EAAEO,IAAAA,iBAAS,EACrCZ,QAAQK,MAAM,EACd,QACA,IAAI,EAAEJ,KAAK,QAAQ,EAAEC,OAAOG,MAAM,CAAC,CAAC,EAAEO,IAAAA,iBAAS,EAC/CV,OAAOG,MAAM,EACb,SACA,KAAK,EAAEC,gBAAgB,EAAE,CAAC,2BAC5B,IAA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Module", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ BROWSERSLIST_FILE: function() {
13
+ return BROWSERSLIST_FILE;
14
+ },
15
+ WEBPACK_FALLBACKS: function() {
16
+ return WEBPACK_FALLBACKS;
17
+ },
18
+ getDefaultLoader: function() {
19
+ return getDefaultLoader;
20
+ },
21
+ getDevTool: function() {
22
+ return getDevTool;
23
+ },
24
+ getProgressHandler: function() {
25
+ return getProgressHandler;
26
+ },
27
+ getBrowserslistTargets: function() {
28
+ return getBrowserslistTargets;
29
+ },
30
+ pluralize: function() {
31
+ return pluralize;
32
+ },
33
+ getFallbacks: function() {
34
+ return getFallbacks;
35
+ }
36
+ });
37
+ const _chalk = require("chalk");
38
+ const _fs = require("fs");
39
+ const _module = require("module");
40
+ const _path = require("path");
41
+ const BROWSERSLIST_FILE = (0, _path.resolve)((0, _path.dirname)(// eslint-disable-next-line n/no-extraneous-require
42
+ require.resolve('@metamask/snaps-cli/package.json')), '.browserslistrc');
43
+ const WEBPACK_FALLBACKS = {
44
+ assert: require.resolve('assert/'),
45
+ buffer: require.resolve('buffer/'),
46
+ console: require.resolve('console-browserify'),
47
+ constants: require.resolve('constants-browserify'),
48
+ crypto: require.resolve('crypto-browserify'),
49
+ domain: require.resolve('domain-browser'),
50
+ events: require.resolve('events/'),
51
+ http: require.resolve('stream-http'),
52
+ https: require.resolve('https-browserify'),
53
+ os: require.resolve('os-browserify/browser'),
54
+ path: require.resolve('path-browserify'),
55
+ punycode: require.resolve('punycode/'),
56
+ process: require.resolve('process/browser'),
57
+ querystring: require.resolve('querystring-es3'),
58
+ stream: require.resolve('stream-browserify'),
59
+ /* eslint-disable @typescript-eslint/naming-convention */ _stream_duplex: require.resolve('readable-stream/lib/_stream_duplex'),
60
+ _stream_passthrough: require.resolve('readable-stream/lib/_stream_passthrough'),
61
+ _stream_readable: require.resolve('readable-stream/lib/_stream_readable'),
62
+ _stream_transform: require.resolve('readable-stream/lib/_stream_transform'),
63
+ _stream_writable: require.resolve('readable-stream/lib/_stream_writable'),
64
+ string_decoder: require.resolve('string_decoder/'),
65
+ /* eslint-enable @typescript-eslint/naming-convention */ sys: require.resolve('util/'),
66
+ timers: require.resolve('timers-browserify'),
67
+ tty: require.resolve('tty-browserify'),
68
+ url: require.resolve('url/'),
69
+ util: require.resolve('util/'),
70
+ vm: require.resolve('vm-browserify'),
71
+ zlib: require.resolve('browserify-zlib')
72
+ };
73
+ async function getDefaultLoader({ legacy, sourceMap }) {
74
+ if (legacy) {
75
+ return {
76
+ /**
77
+ * If the snap uses the legacy config, we use the custom `browserify`
78
+ * loader. This uses the legacy Browserify config to transpile the code.
79
+ * This is necessary for backwards compatibility with the
80
+ * `bundlerCustomizer` function.
81
+ */ loader: (0, _path.resolve)(__dirname, 'loaders', 'browserify'),
82
+ /**
83
+ * The options for the `browserify` loader. These can be overridden in the
84
+ * snap config.
85
+ */ options: legacy
86
+ };
87
+ }
88
+ const targets = await getBrowserslistTargets();
89
+ return {
90
+ /**
91
+ * We use the `swc-loader` to transpile TypeScript and JavaScript files.
92
+ * This is a Webpack loader that uses the `SWC` compiler, which is a much
93
+ * faster alternative to Babel and TypeScript's own compiler.
94
+ */ loader: 'swc-loader',
95
+ /**
96
+ * The options for the `swc-loader`. These can be overridden in the
97
+ * `.swcrc` file.
98
+ *
99
+ * @see https://swc.rs/docs/configuration/swcrc
100
+ */ options: {
101
+ sync: false,
102
+ /**
103
+ * This tells SWC to generate source maps. We set it to the
104
+ * `sourceMap` value from the config object.
105
+ *
106
+ * This must be enabled if source maps are enabled in the config.
107
+ */ sourceMaps: Boolean(getDevTool(sourceMap)),
108
+ jsc: {
109
+ parser: {
110
+ /**
111
+ * This tells the parser to parse TypeScript files. If you
112
+ * don't need to support TypeScript, you can set this to
113
+ * `ecmascript` instead, but there's no harm in leaving it
114
+ * as `typescript`.
115
+ *
116
+ * @see https://swc.rs/docs/configuration/compilation#jscparser
117
+ */ syntax: 'typescript'
118
+ }
119
+ },
120
+ /**
121
+ * The module configuration. This tells SWC how to output the
122
+ * transpiled code.
123
+ *
124
+ * @see https://swc.rs/docs/configuration/modules
125
+ */ module: {
126
+ /**
127
+ * This tells SWC to output CommonJS modules. MetaMask Snaps
128
+ * doesn't support ES modules yet, so this is necessary.
129
+ *
130
+ * @see https://swc.rs/docs/configuration/modules#commonjs
131
+ */ type: 'commonjs'
132
+ },
133
+ env: {
134
+ targets: targets.join(', ')
135
+ }
136
+ }
137
+ };
138
+ }
139
+ function getDevTool(sourceMap) {
140
+ if (sourceMap === 'inline') {
141
+ return 'inline-source-map';
142
+ }
143
+ if (sourceMap === true) {
144
+ return 'source-map';
145
+ }
146
+ return false;
147
+ }
148
+ function getProgressHandler(spinner, spinnerText) {
149
+ return (percentage)=>{
150
+ if (spinner && spinnerText) {
151
+ spinner.text = `${spinnerText} ${(0, _chalk.dim)(`(${Math.round(percentage * 100)}%)`)}`;
152
+ }
153
+ };
154
+ }
155
+ async function getBrowserslistTargets() {
156
+ const contents = await _fs.promises.readFile(BROWSERSLIST_FILE, 'utf8');
157
+ return contents.split('\n').map((line)=>line.trim()).filter((line)=>line && !line.startsWith('#'));
158
+ }
159
+ function pluralize(count, singular, plural = `${singular}s`) {
160
+ return count === 1 ? singular : plural;
161
+ }
162
+ function getFallbacks(polyfills) {
163
+ if (polyfills === true) {
164
+ return Object.fromEntries(_module.builtinModules.map((name)=>[
165
+ name,
166
+ WEBPACK_FALLBACKS[name] ?? false
167
+ ]));
168
+ }
169
+ if (polyfills === false) {
170
+ return Object.fromEntries(_module.builtinModules.map((name)=>[
171
+ name,
172
+ false
173
+ ]));
174
+ }
175
+ return Object.fromEntries(_module.builtinModules.map((name)=>[
176
+ name,
177
+ polyfills[name] ? WEBPACK_FALLBACKS[name] : false
178
+ ]));
179
+ }
180
+
181
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/webpack/utils.ts"],"sourcesContent":["import { dim } from 'chalk';\nimport { promises as fs } from 'fs';\nimport { builtinModules } from 'module';\nimport type { Ora } from 'ora';\nimport { dirname, resolve } from 'path';\nimport type { Configuration } from 'webpack';\n\nimport type { ProcessedWebpackConfig } from '../config';\n\nexport const BROWSERSLIST_FILE = resolve(\n dirname(\n // eslint-disable-next-line n/no-extraneous-require\n require.resolve('@metamask/snaps-cli/package.json'),\n ),\n '.browserslistrc',\n);\n\nexport const WEBPACK_FALLBACKS = {\n assert: require.resolve('assert/'),\n buffer: require.resolve('buffer/'),\n console: require.resolve('console-browserify'),\n constants: require.resolve('constants-browserify'),\n crypto: require.resolve('crypto-browserify'),\n domain: require.resolve('domain-browser'),\n events: require.resolve('events/'),\n http: require.resolve('stream-http'),\n https: require.resolve('https-browserify'),\n os: require.resolve('os-browserify/browser'),\n path: require.resolve('path-browserify'),\n punycode: require.resolve('punycode/'),\n process: require.resolve('process/browser'),\n querystring: require.resolve('querystring-es3'),\n stream: require.resolve('stream-browserify'),\n /* eslint-disable @typescript-eslint/naming-convention */\n _stream_duplex: require.resolve('readable-stream/lib/_stream_duplex'),\n _stream_passthrough: require.resolve(\n 'readable-stream/lib/_stream_passthrough',\n ),\n _stream_readable: require.resolve('readable-stream/lib/_stream_readable'),\n _stream_transform: require.resolve('readable-stream/lib/_stream_transform'),\n _stream_writable: require.resolve('readable-stream/lib/_stream_writable'),\n string_decoder: require.resolve('string_decoder/'),\n /* eslint-enable @typescript-eslint/naming-convention */\n sys: require.resolve('util/'),\n timers: require.resolve('timers-browserify'),\n tty: require.resolve('tty-browserify'),\n url: require.resolve('url/'),\n util: require.resolve('util/'),\n vm: require.resolve('vm-browserify'),\n zlib: require.resolve('browserify-zlib'),\n};\n\n/**\n * Get the default loader for JavaScript and TypeScript files, based on the\n * config object.\n *\n * - If the `legacy` option is set, we use the custom `browserify` loader. This\n * uses the legacy Browserify config to transpile the code.\n * - Otherwise, we use the `swc-loader`. This is a Webpack loader that uses the\n * `SWC` compiler, which is a much faster alternative to Babel and TypeScript's\n * own compiler.\n *\n * @param config - The processed snap Webpack config.\n * @param config.legacy - The legacy config object, if any.\n * @param config.sourceMap - Whether to generate source maps.\n * @see https://swc.rs/docs/usage/swc-loader\n * @returns The default loader.\n */\nexport async function getDefaultLoader({\n legacy,\n sourceMap,\n}: ProcessedWebpackConfig) {\n if (legacy) {\n return {\n /**\n * If the snap uses the legacy config, we use the custom `browserify`\n * loader. This uses the legacy Browserify config to transpile the code.\n * This is necessary for backwards compatibility with the\n * `bundlerCustomizer` function.\n */\n loader: resolve(__dirname, 'loaders', 'browserify'),\n\n /**\n * The options for the `browserify` loader. These can be overridden in the\n * snap config.\n */\n options: legacy,\n };\n }\n\n const targets = await getBrowserslistTargets();\n return {\n /**\n * We use the `swc-loader` to transpile TypeScript and JavaScript files.\n * This is a Webpack loader that uses the `SWC` compiler, which is a much\n * faster alternative to Babel and TypeScript's own compiler.\n */\n loader: 'swc-loader',\n\n /**\n * The options for the `swc-loader`. These can be overridden in the\n * `.swcrc` file.\n *\n * @see https://swc.rs/docs/configuration/swcrc\n */\n options: {\n sync: false,\n\n /**\n * This tells SWC to generate source maps. We set it to the\n * `sourceMap` value from the config object.\n *\n * This must be enabled if source maps are enabled in the config.\n */\n sourceMaps: Boolean(getDevTool(sourceMap)),\n\n jsc: {\n parser: {\n /**\n * This tells the parser to parse TypeScript files. If you\n * don't need to support TypeScript, you can set this to\n * `ecmascript` instead, but there's no harm in leaving it\n * as `typescript`.\n *\n * @see https://swc.rs/docs/configuration/compilation#jscparser\n */\n syntax: 'typescript',\n },\n },\n\n /**\n * The module configuration. This tells SWC how to output the\n * transpiled code.\n *\n * @see https://swc.rs/docs/configuration/modules\n */\n module: {\n /**\n * This tells SWC to output CommonJS modules. MetaMask Snaps\n * doesn't support ES modules yet, so this is necessary.\n *\n * @see https://swc.rs/docs/configuration/modules#commonjs\n */\n type: 'commonjs',\n },\n\n env: {\n targets: targets.join(', '),\n },\n },\n };\n}\n\n/**\n * Get the Webpack devtool configuration based on the given snap config.\n *\n * - If `sourceMap` is `inline`, return `inline-source-map`.\n * - If `sourceMap` is `true`, return `source-map`.\n * - Otherwise, return `false`.\n *\n * @param sourceMap - The `sourceMap` value from the snap config.\n * @returns The Webpack devtool configuration.\n */\nexport function getDevTool(\n sourceMap: ProcessedWebpackConfig['sourceMap'],\n): Configuration['devtool'] {\n if (sourceMap === 'inline') {\n return 'inline-source-map';\n }\n\n if (sourceMap === true) {\n return 'source-map';\n }\n\n return false;\n}\n\n/**\n * Get a function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n *\n * @param spinner - The spinner to update.\n * @param spinnerText - The initial spinner text. This will be prepended to the\n * percentage.\n * @returns A function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n */\n// Note: This is extracted for testing purposes.\nexport function getProgressHandler(spinner?: Ora, spinnerText?: string) {\n return (percentage: number) => {\n if (spinner && spinnerText) {\n spinner.text = `${spinnerText} ${dim(\n `(${Math.round(percentage * 100)}%)`,\n )}`;\n }\n };\n}\n\n/**\n * Get the targets from the `.browserslistrc` file.\n *\n * @returns The browser targets as an array of strings.\n */\nexport async function getBrowserslistTargets() {\n const contents = await fs.readFile(BROWSERSLIST_FILE, 'utf8');\n return contents\n .split('\\n')\n .map((line) => line.trim())\n .filter((line) => line && !line.startsWith('#'));\n}\n\n/**\n * Get a singular or plural string based on the given count. This is useful for\n * generating messages like \"1 error\" or \"2 errors\". By default, the plural\n * string is the singular string with an \"s\" appended to it.\n *\n * This assumes that the text is in English, and likely won't work for some\n * other languages.\n *\n * @param count - The count.\n * @param singular - The singular string.\n * @param plural - The plural string.\n * @returns The singular or plural string.\n * @example\n * ```typescript\n * pluralize(1, 'error'); // => 'error'\n * pluralize(2, 'error'); // => 'errors'\n * pluralize(1, 'error', 'problem'); // => 'error'\n * pluralize(2, 'error', 'problems'); // => 'problems'\n * ```\n */\nexport function pluralize(\n count: number,\n singular: string,\n plural = `${singular}s`,\n) {\n return count === 1 ? singular : plural;\n}\n\n/**\n * Get an object that can be used as fallback config for Webpack's\n * `fallback` config.\n *\n * @param polyfills - The polyfill object from the snap config.\n * @returns The webpack fallback config.\n */\nexport function getFallbacks(polyfills: ProcessedWebpackConfig['polyfills']): {\n [index: string]: string | false;\n} {\n if (polyfills === true) {\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS] ?? false,\n ]),\n );\n }\n\n if (polyfills === false) {\n return Object.fromEntries(builtinModules.map((name) => [name, false]));\n }\n\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n polyfills[name as keyof ProcessedWebpackConfig['polyfills']]\n ? WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS]\n : false,\n ]),\n );\n}\n"],"names":["BROWSERSLIST_FILE","WEBPACK_FALLBACKS","getDefaultLoader","getDevTool","getProgressHandler","getBrowserslistTargets","pluralize","getFallbacks","resolve","dirname","require","assert","buffer","console","constants","crypto","domain","events","http","https","os","path","punycode","process","querystring","stream","_stream_duplex","_stream_passthrough","_stream_readable","_stream_transform","_stream_writable","string_decoder","sys","timers","tty","url","util","vm","zlib","legacy","sourceMap","loader","__dirname","options","targets","sync","sourceMaps","Boolean","jsc","parser","syntax","module","type","env","join","spinner","spinnerText","percentage","text","dim","Math","round","contents","fs","readFile","split","map","line","trim","filter","startsWith","count","singular","plural","polyfills","Object","fromEntries","builtinModules","name"],"mappings":";;;;;;;;;;;IASaA,iBAAiB;eAAjBA;;IAQAC,iBAAiB;eAAjBA;;IAmDSC,gBAAgB;eAAhBA;;IA+FNC,UAAU;eAAVA;;IAyBAC,kBAAkB;eAAlBA;;IAeMC,sBAAsB;eAAtBA;;IA4BNC,SAAS;eAATA;;IAeAC,YAAY;eAAZA;;;uBAtPI;oBACW;wBACA;sBAEE;AAK1B,MAAMP,oBAAoBQ,IAAAA,aAAO,EACtCC,IAAAA,aAAO,EACL,mDA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