@metamask/snaps-cli 7.2.0 → 8.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.
package/.browserslistrc CHANGED
@@ -1,3 +1,3 @@
1
1
  # The minimum browser versions supported by MetaMask.
2
- chrome >= 90
3
- firefox >= 91
2
+ chrome >= 113
3
+ firefox >= 115
package/CHANGELOG.md CHANGED
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [8.0.0]
11
+
12
+ ### Changed
13
+
14
+ - **BREAKING:** Drop support for Node.js 18 and 21 ([#3447](https://github.com/MetaMask/snaps/pull/3447))
15
+ - Bump minimum supported browser versions ([#3441](https://github.com/MetaMask/snaps/pull/3441))
16
+ - The minimum supported browser versions are now:
17
+ - Chrome 113
18
+ - Firefox 115
19
+ - Bump `@swc/core` from `1.3.78` to `1.11.31` ([#3442](https://github.com/MetaMask/snaps/pull/3442))
20
+ - Bump `swc-loader` from `0.2.3` to `0.2.6` ([#3442](https://github.com/MetaMask/snaps/pull/3442))
21
+
10
22
  ## [7.2.0]
11
23
 
12
24
  ### Added
@@ -369,7 +381,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
369
381
  - The version of the package no longer needs to match the version of all other
370
382
  MetaMask Snaps packages.
371
383
 
372
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@7.2.0...HEAD
384
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@8.0.0...HEAD
385
+ [8.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@7.2.0...@metamask/snaps-cli@8.0.0
373
386
  [7.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@7.1.0...@metamask/snaps-cli@7.2.0
374
387
  [7.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@7.0.0...@metamask/snaps-cli@7.1.0
375
388
  [7.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-cli@6.7.0...@metamask/snaps-cli@7.0.0
@@ -68,8 +68,7 @@ const loader = async function loader(source) {
68
68
  const wasmModule = await WebAssembly.compile(source);
69
69
  const exports = WebAssembly.Module.exports(wasmModule);
70
70
  const imports = WebAssembly.Module.imports(wasmModule).reduce((target, descriptor) => {
71
- var _a;
72
- target[_a = descriptor.module] ?? (target[_a] = []);
71
+ target[descriptor.module] ??= [];
73
72
  target[descriptor.module].push(descriptor.name);
74
73
  return target;
75
74
  }, {});
@@ -1 +1 @@
1
- {"version":3,"file":"wasm.cjs","sourceRoot":"","sources":["../../../src/webpack/loaders/wasm.ts"],"names":[],"mappings":";;;AAAA,2CAAwD;AACxD,+BAAwC;AAGxC;;;;;;GAMG;AACH,SAAgB,UAAU,CAAC,SAAmC;IAC5D,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC;SAC7B,GAAG,CACF,CAAC,CAAC,UAAU,EAAE,WAAW,CAAC,EAAE,EAAE,CAC5B,YAAY,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,SAAS,CACzD,UAAU,CACX,GAAG,CACP;SACA,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC;AATD,gCASC;AAED;;;;;;GAMG;AACH,SAAgB,gBAAgB,CAAC,SAAmC;IAClE,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC;SAC7B,GAAG,CACF,CAAC,CAAC,UAAU,EAAE,WAAW,CAAC,EAAE,EAAE,CAC5B,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,OAAO,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAClE;SACA,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC;AAPD,4CAOC;AAED;;;;;;;GAOG;AACH,SAAgB,UAAU,CAAC,WAAiD;IAC1E,OAAO,WAAW;SACf,GAAG,CAAC,CAAC,U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AA,cAAO,EAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO;MACH,UAAU,CAAC,OAAO,CAAC;;kBAEP,IAAI,CAAC,SAAS,CAAC,IAAA,qBAAa,EAAC,MAAM,CAAC,CAAC;;;;;;;;;;;;;QAa/C,gBAAgB,CAAC,OAAO,CAAC;;;;MAI3B,UAAU,CAAC,OAAO,CAAC;GACtB,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,MAAM,CAAC;AAEtB,gFAAgF;AAChF,6EAA6E;AAC7E,qEAAqE;AACxD,QAAA,GAAG,GAAG,IAAI,CAAC","sourcesContent":["import { assert, bytesToBase64 } from '@metamask/utils';\nimport { dirname, resolve } from 'path';\nimport type { LoaderDefinitionFunction } from 'webpack';\n\n/**\n * Get the imports code for the WASM module. This code imports each of the\n * imports from the WASM module.\n *\n * @param importMap - The import map for the WASM module.\n * @returns The imports code for the WASM module.\n */\nexport function getImports(importMap: Record<string, string[]>) {\n return Object.entries(importMap)\n .map(\n ([moduleName, exportNames]) =>\n `import { ${exportNames.join(', ')} } from ${JSON.stringify(\n moduleName,\n )};`,\n )\n .join('\\n');\n}\n\n/**\n * Get the imports code to use in `WebAssembly.Instance`. This code adds each of\n * the imports to the `imports` object.\n *\n * @param importMap - The import map for the WASM module.\n * @returns The imports code for the WASM module.\n */\nexport function getModuleImports(importMap: Record<string, string[]>) {\n return Object.entries(importMap)\n .map(\n ([moduleName, exportNames]) =>\n `${JSON.stringify(moduleName)}: { ${exportNames.join(', ')} },`,\n )\n .join('\\n');\n}\n\n/**\n * Get the exports code for the WASM module. This code exports each of the\n * exports from the WASM module as a variable. This function assumes that the\n * exports are available in a variable named `exports`.\n *\n * @param descriptors - The export descriptors from the WASM module.\n * @returns The exports code for the WASM module.\n */\nexport function getExports(descriptors: WebAssembly.ModuleExportDescriptor[]) {\n return descriptors\n .map((descriptor) => {\n if (descriptor.name === 'default') {\n return `export default exports[${JSON.stringify(descriptor.name)}];`;\n }\n\n return `export const ${descriptor.name} = exports[${JSON.stringify(\n descriptor.name,\n )}];`;\n })\n .join('\\n');\n}\n\n/**\n * A Webpack loader that synchronously loads the WASM module. This makes it\n * possible to import the WASM module directly.\n *\n * @param source - The WASM module as `Uint8Array`.\n * @returns The WASM module as a JavaScript string.\n * @example\n * ```ts\n * import * as wasm from './program.wasm';\n *\n * // Do something with the WASM module...\n * ```\n */\n// Note: This function needs to be defined like this, so that Webpack can bind\n// `this` to the loader context, and TypeScript can infer the type of `this`.\nconst loader: LoaderDefinitionFunction = async function loader(\n source: unknown,\n) {\n assert(source instanceof Uint8Array, 'Expected source to be a Uint8Array.');\n\n const wasmModule = await WebAssembly.compile(source);\n\n const exports = WebAssembly.Module.exports(wasmModule);\n const imports = WebAssembly.Module.imports(wasmModule).reduce<\n Record<string, string[]>\n >((target, descriptor) => {\n target[descriptor.module] ??= [];\n target[descriptor.module].push(descriptor.name);\n\n return target;\n }, {});\n\n // Add the WASM import as a dependency so that Webpack will watch it for\n // changes.\n const path = dirname(this.resourcePath);\n for (const name of Object.keys(imports)) {\n this.addDependency(resolve(path, name));\n }\n\n return `\n ${getImports(imports)}\n\n const b64 = ${JSON.stringify(bytesToBase64(source))};\n\n function decode(encoded) {\n const str = atob(encoded);\n const bytes = new Uint8Array(str.length);\n for (let i = 0; i < str.length; i++) {\n bytes[i] = str.charCodeAt(i);\n }\n return bytes;\n }\n\n const bytes = decode(b64);\n const { instance } = await WebAssembly.instantiate(bytes, {\n ${getModuleImports(imports)}\n });\n\n const exports = instance.exports;\n ${getExports(exports)}\n `;\n};\n\nexport default loader;\n\n// By setting `raw` to `true`, we are telling Webpack to provide the source as a\n// `Uint8Array` instead of converting it to a string. This allows us to avoid\n// having to convert the source back to a `Uint8Array` in the loader.\nexport const raw = true;\n"]}
1
+ {"version":3,"file":"wasm.cjs","sourceRoot":"","sources":["../../../src/webpack/loaders/wasm.ts"],"names":[],"mappings":";;;AAAA,2CAAwD;AACxD,+BAAwC;AAG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,UAAU,EAAE,qCAAqC,CAAC,CAAC;IAE5E,MAAM,UAAU,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAErD,MAAM,OAAO,GAAG,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACvD,MAAM,OAAO,GAAG,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,MAAM,CAE3D,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE;QACvB,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACjC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAEhD,OAAO,MAAM,CAAC;IAChB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,wEAAwE;IACxE,WAAW;IACX,MAAM,IAAI,GAAG,IAAA,cAAO,EAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACxC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QACxC,IAAI,CAAC,aAAa,CAAC,IAAA,cAAO,EAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO;MACH,UAAU,CAAC,OAAO,CAAC;;kBAEP,IAAI,CAAC,SAAS,CAAC,IAAA,qBAAa,EAAC,MAAM,CAAC,CAAC;;;;;;;;;;;;;QAa/C,gBAAgB,CAAC,OAAO,CAAC;;;;MAI3B,UAAU,CAAC,OAAO,CAAC;GACtB,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,MAAM,CAAC;AAEtB,gFAAgF;AAChF,6EAA6E;AAC7E,qEAAqE;AACxD,QAAA,GAAG,GAAG,IAAI,CAAC","sourcesContent":["import { assert, bytesToBase64 } from '@metamask/utils';\nimport { dirname, resolve } from 'path';\nimport type { LoaderDefinitionFunction } from 'webpack';\n\n/**\n * Get the imports code for the WASM module. This code imports each of the\n * imports from the WASM module.\n *\n * @param importMap - The import map for the WASM module.\n * @returns The imports code for the WASM module.\n */\nexport function getImports(importMap: Record<string, string[]>) {\n return Object.entries(importMap)\n .map(\n ([moduleName, exportNames]) =>\n `import { ${exportNames.join(', ')} } from ${JSON.stringify(\n moduleName,\n )};`,\n )\n .join('\\n');\n}\n\n/**\n * Get the imports code to use in `WebAssembly.Instance`. This code adds each of\n * the imports to the `imports` object.\n *\n * @param importMap - The import map for the WASM module.\n * @returns The imports code for the WASM module.\n */\nexport function getModuleImports(importMap: Record<string, string[]>) {\n return Object.entries(importMap)\n .map(\n ([moduleName, exportNames]) =>\n `${JSON.stringify(moduleName)}: { ${exportNames.join(', ')} },`,\n )\n .join('\\n');\n}\n\n/**\n * Get the exports code for the WASM module. This code exports each of the\n * exports from the WASM module as a variable. This function assumes that the\n * exports are available in a variable named `exports`.\n *\n * @param descriptors - The export descriptors from the WASM module.\n * @returns The exports code for the WASM module.\n */\nexport function getExports(descriptors: WebAssembly.ModuleExportDescriptor[]) {\n return descriptors\n .map((descriptor) => {\n if (descriptor.name === 'default') {\n return `export default exports[${JSON.stringify(descriptor.name)}];`;\n }\n\n return `export const ${descriptor.name} = exports[${JSON.stringify(\n descriptor.name,\n )}];`;\n })\n .join('\\n');\n}\n\n/**\n * A Webpack loader that synchronously loads the WASM module. This makes it\n * possible to import the WASM module directly.\n *\n * @param source - The WASM module as `Uint8Array`.\n * @returns The WASM module as a JavaScript string.\n * @example\n * ```ts\n * import * as wasm from './program.wasm';\n *\n * // Do something with the WASM module...\n * ```\n */\n// Note: This function needs to be defined like this, so that Webpack can bind\n// `this` to the loader context, and TypeScript can infer the type of `this`.\nconst loader: LoaderDefinitionFunction = async function loader(\n source: unknown,\n) {\n assert(source instanceof Uint8Array, 'Expected source to be a Uint8Array.');\n\n const wasmModule = await WebAssembly.compile(source);\n\n const exports = WebAssembly.Module.exports(wasmModule);\n const imports = WebAssembly.Module.imports(wasmModule).reduce<\n Record<string, string[]>\n >((target, descriptor) => {\n target[descriptor.module] ??= [];\n target[descriptor.module].push(descriptor.name);\n\n return target;\n }, {});\n\n // Add the WASM import as a dependency so that Webpack will watch it for\n // changes.\n const path = dirname(this.resourcePath);\n for (const name of Object.keys(imports)) {\n this.addDependency(resolve(path, name));\n }\n\n return `\n ${getImports(imports)}\n\n const b64 = ${JSON.stringify(bytesToBase64(source))};\n\n function decode(encoded) {\n const str = atob(encoded);\n const bytes = new Uint8Array(str.length);\n for (let i = 0; i < str.length; i++) {\n bytes[i] = str.charCodeAt(i);\n }\n return bytes;\n }\n\n const bytes = decode(b64);\n const { instance } = await WebAssembly.instantiate(bytes, {\n ${getModuleImports(imports)}\n });\n\n const exports = instance.exports;\n ${getExports(exports)}\n `;\n};\n\nexport default loader;\n\n// By setting `raw` to `true`, we are telling Webpack to provide the source as a\n// `Uint8Array` instead of converting it to a string. This allows us to avoid\n// having to convert the source back to a `Uint8Array` in the loader.\nexport const raw = true;\n"]}
@@ -62,8 +62,7 @@ const loader = async function loader(source) {
62
62
  const wasmModule = await WebAssembly.compile(source);
63
63
  const exports = WebAssembly.Module.exports(wasmModule);
64
64
  const imports = WebAssembly.Module.imports(wasmModule).reduce((target, descriptor) => {
65
- var _a;
66
- target[_a = descriptor.module] ?? (target[_a] = []);
65
+ target[descriptor.module] ??= [];
67
66
  target[descriptor.module].push(descriptor.name);
68
67
  return target;
69
68
  }, {});
@@ -1 +1 @@
1
- {"version":3,"file":"wasm.mjs","sourceRoot":"","sources":["../../../src/webpack/loaders/wasm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,wBAAwB;AACxD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa;AAGxC;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,SAAmC;IAC5D,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC;SAC7B,GAAG,CACF,CAAC,CAAC,UAAU,EAAE,WAAW,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,CAAC;IACvD,MAAM,OAAO,GAAG,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,MAAM,CAE3D,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE;;QACvB,MAAM,MAAC,UAAU,CAAC,MAAM,MAAxB,MAAM,OAAwB,EAAE,EAAC;QACjC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAEhD,OAAO,MAAM,CAAC;IAChB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,wEAAwE;IACxE,WAAW;IACX,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACxC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QACxC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO;MACH,UAAU,CAAC,OAAO,CAAC;;kBAEP,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;;;;;;;;;;;;;QAa/C,gBAAgB,CAAC,OAAO,CAAC;;;;MAI3B,UAAU,CAAC,OAAO,CAAC;GACtB,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,MAAM,CAAC;AAEtB,gFAAgF;AAChF,6EAA6E;AAC7E,qEAAqE;AACrE,MAAM,CAAC,MAAM,GAAG,GAAG,IAAI,CAAC","sourcesContent":["import { assert, bytesToBase64 } from '@metamask/utils';\nimport { dirname, resolve } from 'path';\nimport type { LoaderDefinitionFunction } from 'webpack';\n\n/**\n * Get the imports code for the WASM module. This code imports each of the\n * imports from the WASM module.\n *\n * @param importMap - The import map for the WASM module.\n * @returns The imports code for the WASM module.\n */\nexport function getImports(importMap: Record<string, string[]>) {\n return Object.entries(importMap)\n .map(\n ([moduleName, exportNames]) =>\n `import { ${exportNames.join(', ')} } from ${JSON.stringify(\n moduleName,\n )};`,\n )\n .join('\\n');\n}\n\n/**\n * Get the imports code to use in `WebAssembly.Instance`. This code adds each of\n * the imports to the `imports` object.\n *\n * @param importMap - The import map for the WASM module.\n * @returns The imports code for the WASM module.\n */\nexport function getModuleImports(importMap: Record<string, string[]>) {\n return Object.entries(importMap)\n .map(\n ([moduleName, exportNames]) =>\n `${JSON.stringify(moduleName)}: { ${exportNames.join(', ')} },`,\n )\n .join('\\n');\n}\n\n/**\n * Get the exports code for the WASM module. This code exports each of the\n * exports from the WASM module as a variable. This function assumes that the\n * exports are available in a variable named `exports`.\n *\n * @param descriptors - The export descriptors from the WASM module.\n * @returns The exports code for the WASM module.\n */\nexport function getExports(descriptors: WebAssembly.ModuleExportDescriptor[]) {\n return descriptors\n .map((descriptor) => {\n if (descriptor.name === 'default') {\n return `export default exports[${JSON.stringify(descriptor.name)}];`;\n }\n\n return `export const ${descriptor.name} = exports[${JSON.stringify(\n descriptor.name,\n )}];`;\n })\n .join('\\n');\n}\n\n/**\n * A Webpack loader that synchronously loads the WASM module. This makes it\n * possible to import the WASM module directly.\n *\n * @param source - The WASM module as `Uint8Array`.\n * @returns The WASM module as a JavaScript string.\n * @example\n * ```ts\n * import * as wasm from './program.wasm';\n *\n * // Do something with the WASM module...\n * ```\n */\n// Note: This function needs to be defined like this, so that Webpack can bind\n// `this` to the loader context, and TypeScript can infer the type of `this`.\nconst loader: LoaderDefinitionFunction = async function loader(\n source: unknown,\n) {\n assert(source instanceof Uint8Array, 'Expected source to be a Uint8Array.');\n\n const wasmModule = await WebAssembly.compile(source);\n\n const exports = WebAssembly.Module.exports(wasmModule);\n const imports = WebAssembly.Module.imports(wasmModule).reduce<\n Record<string, string[]>\n >((target, descriptor) => {\n target[descriptor.module] ??= [];\n target[descriptor.module].push(descriptor.name);\n\n return target;\n }, {});\n\n // Add the WASM import as a dependency so that Webpack will watch it for\n // changes.\n const path = dirname(this.resourcePath);\n for (const name of Object.keys(imports)) {\n this.addDependency(resolve(path, name));\n }\n\n return `\n ${getImports(imports)}\n\n const b64 = ${JSON.stringify(bytesToBase64(source))};\n\n function decode(encoded) {\n const str = atob(encoded);\n const bytes = new Uint8Array(str.length);\n for (let i = 0; i < str.length; i++) {\n bytes[i] = str.charCodeAt(i);\n }\n return bytes;\n }\n\n const bytes = decode(b64);\n const { instance } = await WebAssembly.instantiate(bytes, {\n ${getModuleImports(imports)}\n });\n\n const exports = instance.exports;\n ${getExports(exports)}\n `;\n};\n\nexport default loader;\n\n// By setting `raw` to `true`, we are telling Webpack to provide the source as a\n// `Uint8Array` instead of converting it to a string. This allows us to avoid\n// having to convert the source back to a `Uint8Array` in the loader.\nexport const raw = true;\n"]}
1
+ {"version":3,"file":"wasm.mjs","sourceRoot":"","sources":["../../../src/webpack/loaders/wasm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,wBAAwB;AACxD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa;AAGxC;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,SAAmC;IAC5D,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC;SAC7B,GAAG,CACF,CAAC,CAAC,UAAU,EAAE,WAAW,CAAC,EAAE,EAAE,CAC5B,YAAY,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,SAAS,CACzD,UAAU,CACX,GAAG,CACP;SACA,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAAmC;IAClE,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC;SAC7B,GAAG,CACF,CAAC,CAAC,UAAU,EAAE,WAAW,CAAC,EAAE,EAAE,CAC5B,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,OAAO,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAClE;SACA,IAAI,CAAC,IAA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I,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QACxC,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO;MACH,UAAU,CAAC,OAAO,CAAC;;kBAEP,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;;;;;;;;;;;;;QAa/C,gBAAgB,CAAC,OAAO,CAAC;;;;MAI3B,UAAU,CAAC,OAAO,CAAC;GACtB,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,MAAM,CAAC;AAEtB,gFAAgF;AAChF,6EAA6E;AAC7E,qEAAqE;AACrE,MAAM,CAAC,MAAM,GAAG,GAAG,IAAI,CAAC","sourcesContent":["import { assert, bytesToBase64 } from '@metamask/utils';\nimport { dirname, resolve } from 'path';\nimport type { LoaderDefinitionFunction } from 'webpack';\n\n/**\n * Get the imports code for the WASM module. This code imports each of the\n * imports from the WASM module.\n *\n * @param importMap - The import map for the WASM module.\n * @returns The imports code for the WASM module.\n */\nexport function getImports(importMap: Record<string, string[]>) {\n return Object.entries(importMap)\n .map(\n ([moduleName, exportNames]) =>\n `import { ${exportNames.join(', ')} } from ${JSON.stringify(\n moduleName,\n )};`,\n )\n .join('\\n');\n}\n\n/**\n * Get the imports code to use in `WebAssembly.Instance`. This code adds each of\n * the imports to the `imports` object.\n *\n * @param importMap - The import map for the WASM module.\n * @returns The imports code for the WASM module.\n */\nexport function getModuleImports(importMap: Record<string, string[]>) {\n return Object.entries(importMap)\n .map(\n ([moduleName, exportNames]) =>\n `${JSON.stringify(moduleName)}: { ${exportNames.join(', ')} },`,\n )\n .join('\\n');\n}\n\n/**\n * Get the exports code for the WASM module. This code exports each of the\n * exports from the WASM module as a variable. This function assumes that the\n * exports are available in a variable named `exports`.\n *\n * @param descriptors - The export descriptors from the WASM module.\n * @returns The exports code for the WASM module.\n */\nexport function getExports(descriptors: WebAssembly.ModuleExportDescriptor[]) {\n return descriptors\n .map((descriptor) => {\n if (descriptor.name === 'default') {\n return `export default exports[${JSON.stringify(descriptor.name)}];`;\n }\n\n return `export const ${descriptor.name} = exports[${JSON.stringify(\n descriptor.name,\n )}];`;\n })\n .join('\\n');\n}\n\n/**\n * A Webpack loader that synchronously loads the WASM module. This makes it\n * possible to import the WASM module directly.\n *\n * @param source - The WASM module as `Uint8Array`.\n * @returns The WASM module as a JavaScript string.\n * @example\n * ```ts\n * import * as wasm from './program.wasm';\n *\n * // Do something with the WASM module...\n * ```\n */\n// Note: This function needs to be defined like this, so that Webpack can bind\n// `this` to the loader context, and TypeScript can infer the type of `this`.\nconst loader: LoaderDefinitionFunction = async function loader(\n source: unknown,\n) {\n assert(source instanceof Uint8Array, 'Expected source to be a Uint8Array.');\n\n const wasmModule = await WebAssembly.compile(source);\n\n const exports = WebAssembly.Module.exports(wasmModule);\n const imports = WebAssembly.Module.imports(wasmModule).reduce<\n Record<string, string[]>\n >((target, descriptor) => {\n target[descriptor.module] ??= [];\n target[descriptor.module].push(descriptor.name);\n\n return target;\n }, {});\n\n // Add the WASM import as a dependency so that Webpack will watch it for\n // changes.\n const path = dirname(this.resourcePath);\n for (const name of Object.keys(imports)) {\n this.addDependency(resolve(path, name));\n }\n\n return `\n ${getImports(imports)}\n\n const b64 = ${JSON.stringify(bytesToBase64(source))};\n\n function decode(encoded) {\n const str = atob(encoded);\n const bytes = new Uint8Array(str.length);\n for (let i = 0; i < str.length; i++) {\n bytes[i] = str.charCodeAt(i);\n }\n return bytes;\n }\n\n const bytes = decode(b64);\n const { instance } = await WebAssembly.instantiate(bytes, {\n ${getModuleImports(imports)}\n });\n\n const exports = instance.exports;\n ${getExports(exports)}\n `;\n};\n\nexport default loader;\n\n// By setting `raw` to `true`, we are telling Webpack to provide the source as a\n// `Uint8Array` instead of converting it to a string. This allows us to avoid\n// having to convert the source back to a `Uint8Array` in the loader.\nexport const raw = true;\n"]}
@@ -1,16 +1,4 @@
1
1
  "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _SnapsStatsPlugin_instances, _SnapsStatsPlugin_spinner, _SnapsStatsPlugin_getStatsErrorMessage, _SnapsWatchPlugin_instances, _SnapsWatchPlugin_spinner, _SnapsWatchPlugin_safeEvaluate, _SnapsBuiltInResolver_source, _SnapsBundleWarningsPlugin_instances, _SnapsBundleWarningsPlugin_checkBuiltIns, _SnapsBundleWarningsPlugin_isProvidePlugin, _SnapsBundleWarningsPlugin_checkBuffer;
14
2
  Object.defineProperty(exports, "__esModule", { value: true });
15
3
  exports.SnapsBundleWarningsPlugin = exports.SnapsBuiltInResolver = exports.SnapsWatchPlugin = exports.SnapsStatsPlugin = void 0;
16
4
  const utils_1 = require("@metamask/utils");
@@ -25,16 +13,19 @@ const utils_3 = require("../utils/index.cjs");
25
13
  * the number of files compiled, and the time taken to compile them.
26
14
  */
27
15
  class SnapsStatsPlugin {
16
+ /**
17
+ * The options for the plugin.
18
+ */
19
+ options;
20
+ /**
21
+ * The spinner to use for logging.
22
+ */
23
+ #spinner;
28
24
  constructor(options = {
29
25
  verbose: false,
30
26
  }, spinner) {
31
- _SnapsStatsPlugin_instances.add(this);
32
- /**
33
- * The spinner to use for logging.
34
- */
35
- _SnapsStatsPlugin_spinner.set(this, void 0);
36
27
  this.options = options;
37
- __classPrivateFieldSet(this, _SnapsStatsPlugin_spinner, spinner, "f");
28
+ this.#spinner = spinner;
38
29
  }
39
30
  /**
40
31
  * Apply the plugin to the Webpack compiler.
@@ -51,58 +42,68 @@ class SnapsStatsPlugin {
51
42
  (0, utils_1.assert)(time, 'Time must be defined in stats.');
52
43
  if (errors?.length) {
53
44
  const formattedErrors = errors
54
- .map((statsError) => __classPrivateFieldGet(this, _SnapsStatsPlugin_instances, "m", _SnapsStatsPlugin_getStatsErrorMessage).call(this, statsError, chalk_1.red))
45
+ .map((statsError) => this.#getStatsErrorMessage(statsError, chalk_1.red))
55
46
  .join('\n\n');
56
- (0, utils_3.error)(`Compiled ${modules.length} ${(0, utils_2.pluralize)(modules.length, 'file')} in ${time}ms with ${errors.length} ${(0, utils_2.pluralize)(errors.length, 'error')}.\n\n${formattedErrors}\n`, __classPrivateFieldGet(this, _SnapsStatsPlugin_spinner, "f"));
57
- __classPrivateFieldGet(this, _SnapsStatsPlugin_spinner, "f")?.stop();
47
+ (0, utils_3.error)(`Compiled ${modules.length} ${(0, utils_2.pluralize)(modules.length, 'file')} in ${time}ms with ${errors.length} ${(0, utils_2.pluralize)(errors.length, 'error')}.\n\n${formattedErrors}\n`, this.#spinner);
48
+ this.#spinner?.stop();
58
49
  process.exitCode = 1;
59
50
  return;
60
51
  }
61
52
  if (warnings?.length) {
62
53
  const formattedWarnings = warnings
63
- .map((statsWarning) => __classPrivateFieldGet(this, _SnapsStatsPlugin_instances, "m", _SnapsStatsPlugin_getStatsErrorMessage).call(this, statsWarning, chalk_1.yellow))
54
+ .map((statsWarning) => this.#getStatsErrorMessage(statsWarning, chalk_1.yellow))
64
55
  .join('\n\n');
65
- (0, utils_3.warn)(`Compiled ${modules.length} ${(0, utils_2.pluralize)(modules.length, 'file')} in ${time}ms with ${warnings.length} ${(0, utils_2.pluralize)(warnings.length, 'warning')}.\n\n${formattedWarnings}\n`, __classPrivateFieldGet(this, _SnapsStatsPlugin_spinner, "f"));
56
+ (0, utils_3.warn)(`Compiled ${modules.length} ${(0, utils_2.pluralize)(modules.length, 'file')} in ${time}ms with ${warnings.length} ${(0, utils_2.pluralize)(warnings.length, 'warning')}.\n\n${formattedWarnings}\n`, this.#spinner);
66
57
  }
67
58
  else {
68
- (0, utils_3.info)(`Compiled ${modules.length} ${(0, utils_2.pluralize)(modules.length, 'file')} in ${time}ms.`, __classPrivateFieldGet(this, _SnapsStatsPlugin_spinner, "f"));
59
+ (0, utils_3.info)(`Compiled ${modules.length} ${(0, utils_2.pluralize)(modules.length, 'file')} in ${time}ms.`, this.#spinner);
69
60
  }
70
61
  if (compiler.watchMode) {
71
62
  // The spinner may be restarted by the watch plugin, outside of the
72
63
  // `executeSteps` flow, so we stop it here just in case.
73
- __classPrivateFieldGet(this, _SnapsStatsPlugin_spinner, "f")?.succeed('Done!');
64
+ this.#spinner?.succeed('Done!');
74
65
  }
75
66
  });
76
67
  }
68
+ /**
69
+ * Get the error message for the given stats error.
70
+ *
71
+ * @param statsError - The stats error.
72
+ * @param color - The color to use for the error message.
73
+ * @returns The error message.
74
+ */
75
+ #getStatsErrorMessage(statsError, color) {
76
+ const baseMessage = this.options.verbose
77
+ ? (0, utils_3.getErrorMessage)(statsError)
78
+ : statsError.message;
79
+ const [first, ...rest] = baseMessage.split('\n');
80
+ return [
81
+ color((0, utils_2.formatText)(`• ${first}`, 4, 2)),
82
+ ...rest.map((message) => (0, utils_2.formatText)(color(message), 4)),
83
+ statsError.details && `\n${(0, utils_2.formatText)((0, chalk_1.dim)(statsError.details), 6)}`,
84
+ ]
85
+ .filter(Boolean)
86
+ .join('\n');
87
+ }
77
88
  }
78
89
  exports.SnapsStatsPlugin = SnapsStatsPlugin;
79
- _SnapsStatsPlugin_spinner = new WeakMap(), _SnapsStatsPlugin_instances = new WeakSet(), _SnapsStatsPlugin_getStatsErrorMessage = function _SnapsStatsPlugin_getStatsErrorMessage(statsError, color) {
80
- const baseMessage = this.options.verbose
81
- ? (0, utils_3.getErrorMessage)(statsError)
82
- : statsError.message;
83
- const [first, ...rest] = baseMessage.split('\n');
84
- return [
85
- color((0, utils_2.formatText)(`• ${first}`, 4, 2)),
86
- ...rest.map((message) => (0, utils_2.formatText)(color(message), 4)),
87
- statsError.details && `\n${(0, utils_2.formatText)((0, chalk_1.dim)(statsError.details), 6)}`,
88
- ]
89
- .filter(Boolean)
90
- .join('\n');
91
- };
92
90
  /**
93
91
  * A plugin that adds extra files to watch. This is useful for watching files
94
92
  * that are not imported by the entry point, such as the `snap.manifest.json`
95
93
  * file.
96
94
  */
97
95
  class SnapsWatchPlugin {
96
+ /**
97
+ * The options for the plugin.
98
+ */
99
+ options;
100
+ /**
101
+ * The spinner to use for logging.
102
+ */
103
+ #spinner;
98
104
  constructor(options, spinner) {
99
- _SnapsWatchPlugin_instances.add(this);
100
- /**
101
- * The spinner to use for logging.
102
- */
103
- _SnapsWatchPlugin_spinner.set(this, void 0);
104
105
  this.options = options;
105
- __classPrivateFieldSet(this, _SnapsWatchPlugin_spinner, spinner, "f");
106
+ this.#spinner = spinner;
106
107
  }
107
108
  /**
108
109
  * Apply the plugin to the Webpack compiler.
@@ -111,36 +112,35 @@ class SnapsWatchPlugin {
111
112
  */
112
113
  apply(compiler) {
113
114
  compiler.hooks.invalid.tap(this.constructor.name, (file) => {
114
- __classPrivateFieldGet(this, _SnapsWatchPlugin_spinner, "f")?.start();
115
- (0, utils_3.info)(`Changes detected in ${(0, chalk_1.yellow)(file)}, recompiling.`, __classPrivateFieldGet(this, _SnapsWatchPlugin_spinner, "f"));
115
+ this.#spinner?.start();
116
+ (0, utils_3.info)(`Changes detected in ${(0, chalk_1.yellow)(file)}, recompiling.`, this.#spinner);
116
117
  });
117
118
  compiler.hooks.afterEmit.tapPromise(this.constructor.name, async ({ fileDependencies }) => {
118
119
  this.options.files?.forEach(fileDependencies.add.bind(fileDependencies));
119
120
  if (this.options.bundle && this.options.evaluate) {
120
- await __classPrivateFieldGet(this, _SnapsWatchPlugin_instances, "m", _SnapsWatchPlugin_safeEvaluate).call(this, this.options.bundle);
121
+ await this.#safeEvaluate(this.options.bundle);
121
122
  }
122
123
  });
123
124
  }
125
+ /**
126
+ * Safely evaluate the bundle at the given path. If an error occurs, it will
127
+ * be logged to the console, rather than throwing an error.
128
+ *
129
+ * This function should never throw an error.
130
+ *
131
+ * @param bundlePath - The path to the bundle.
132
+ */
133
+ async #safeEvaluate(bundlePath) {
134
+ try {
135
+ await (0, eval_1.evaluate)(bundlePath);
136
+ (0, utils_3.info)(`Snap bundle evaluated successfully.`, this.#spinner);
137
+ }
138
+ catch (evaluateError) {
139
+ (0, utils_3.error)(evaluateError.message, this.#spinner);
140
+ }
141
+ }
124
142
  }
125
143
  exports.SnapsWatchPlugin = SnapsWatchPlugin;
126
- _SnapsWatchPlugin_spinner = new WeakMap(), _SnapsWatchPlugin_instances = new WeakSet(), _SnapsWatchPlugin_safeEvaluate =
127
- /**
128
- * Safely evaluate the bundle at the given path. If an error occurs, it will
129
- * be logged to the console, rather than throwing an error.
130
- *
131
- * This function should never throw an error.
132
- *
133
- * @param bundlePath - The path to the bundle.
134
- */
135
- async function _SnapsWatchPlugin_safeEvaluate(bundlePath) {
136
- try {
137
- await (0, eval_1.evaluate)(bundlePath);
138
- (0, utils_3.info)(`Snap bundle evaluated successfully.`, __classPrivateFieldGet(this, _SnapsWatchPlugin_spinner, "f"));
139
- }
140
- catch (evaluateError) {
141
- (0, utils_3.error)(evaluateError.message, __classPrivateFieldGet(this, _SnapsWatchPlugin_spinner, "f"));
142
- }
143
- };
144
144
  /**
145
145
  * A plugin that logs a message when a built-in module is not resolved. The
146
146
  * MetaMask Snaps CLI does not support built-in modules by default, and this
@@ -148,17 +148,21 @@ async function _SnapsWatchPlugin_safeEvaluate(bundlePath) {
148
148
  * when no fallback is configured.
149
149
  */
150
150
  class SnapsBuiltInResolver {
151
+ /**
152
+ * The built-in modules that have been imported, but not resolved.
153
+ */
154
+ unresolvedModules = new Set();
155
+ /**
156
+ * The name of the resolver hook to tap into.
157
+ */
158
+ #source = 'described-resolve';
159
+ /**
160
+ * The options for the plugin.
161
+ */
162
+ options;
151
163
  constructor(options = {
152
164
  ignore: [],
153
165
  }) {
154
- /**
155
- * The built-in modules that have been imported, but not resolved.
156
- */
157
- this.unresolvedModules = new Set();
158
- /**
159
- * The name of the resolver hook to tap into.
160
- */
161
- _SnapsBuiltInResolver_source.set(this, 'described-resolve');
162
166
  this.options = options;
163
167
  }
164
168
  /**
@@ -168,7 +172,7 @@ class SnapsBuiltInResolver {
168
172
  */
169
173
  apply(resolver) {
170
174
  resolver
171
- .getHook(__classPrivateFieldGet(this, _SnapsBuiltInResolver_source, "f"))
175
+ .getHook(this.#source)
172
176
  .tapAsync(this.constructor.name, ({ module: isModule, request }, _, callback) => {
173
177
  if (!isModule || !request) {
174
178
  return callback();
@@ -186,7 +190,6 @@ class SnapsBuiltInResolver {
186
190
  }
187
191
  }
188
192
  exports.SnapsBuiltInResolver = SnapsBuiltInResolver;
189
- _SnapsBuiltInResolver_source = new WeakMap();
190
193
  /**
191
194
  * A plugin that logs a message when:
192
195
  *
@@ -204,11 +207,14 @@ _SnapsBuiltInResolver_source = new WeakMap();
204
207
  * plugin doesn't have access to the resolver.
205
208
  */
206
209
  class SnapsBundleWarningsPlugin {
210
+ /**
211
+ * The options for the plugin.
212
+ */
213
+ options;
207
214
  constructor(options = {
208
215
  buffer: true,
209
216
  builtIns: true,
210
217
  }) {
211
- _SnapsBundleWarningsPlugin_instances.add(this);
212
218
  this.options = options;
213
219
  }
214
220
  /**
@@ -218,55 +224,79 @@ class SnapsBundleWarningsPlugin {
218
224
  */
219
225
  apply(compiler) {
220
226
  if (this.options.builtIns) {
221
- __classPrivateFieldGet(this, _SnapsBundleWarningsPlugin_instances, "m", _SnapsBundleWarningsPlugin_checkBuiltIns).call(this, compiler);
227
+ this.#checkBuiltIns(compiler);
222
228
  }
223
229
  if (this.options.buffer) {
224
- __classPrivateFieldGet(this, _SnapsBundleWarningsPlugin_instances, "m", _SnapsBundleWarningsPlugin_checkBuffer).call(this, compiler);
230
+ this.#checkBuffer(compiler);
225
231
  }
226
232
  }
227
- }
228
- exports.SnapsBundleWarningsPlugin = SnapsBundleWarningsPlugin;
229
- _SnapsBundleWarningsPlugin_instances = new WeakSet(), _SnapsBundleWarningsPlugin_checkBuiltIns = function _SnapsBundleWarningsPlugin_checkBuiltIns(compiler) {
230
- compiler.hooks.afterCompile.tap(this.constructor.name, (compilation) => {
231
- if (!this.options.builtInResolver) {
232
- return;
233
- }
234
- const { unresolvedModules } = this.options.builtInResolver;
235
- if (unresolvedModules.size === 0) {
236
- return;
237
- }
238
- const formattedModules = new Array(...unresolvedModules)
239
- .map((name) => `• ${name}`)
240
- .join('\n');
241
- const webpackError = new webpack_1.WebpackError(`The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided. The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${(0, chalk_1.bold)('`polyfills`')} to ${(0, chalk_1.bold)('`true`')} or an object with the builtins to polyfill as the key and ${(0, chalk_1.bold)('`true`')} as the value. To disable this warning, set ${(0, chalk_1.bold)('`stats.builtIns`')} to ${(0, chalk_1.bold)('`false`')} in your snap config file, or add the module to the ${(0, chalk_1.bold)('`stats.builtIns.ignore`')} array.`);
242
- webpackError.details = formattedModules;
243
- compilation.warnings.push(webpackError);
244
- });
245
- }, _SnapsBundleWarningsPlugin_isProvidePlugin = function _SnapsBundleWarningsPlugin_isProvidePlugin(instance) {
246
- return ((0, utils_1.isObject)(instance) &&
247
- instance.constructor.name === 'ProvidePlugin' &&
248
- (0, utils_1.hasProperty)(instance, 'definitions'));
249
- }, _SnapsBundleWarningsPlugin_checkBuffer = function _SnapsBundleWarningsPlugin_checkBuffer(compiler) {
250
- const plugin = compiler.options.plugins?.find((instance) => __classPrivateFieldGet(this, _SnapsBundleWarningsPlugin_instances, "m", _SnapsBundleWarningsPlugin_isProvidePlugin).call(this, instance));
251
- // If the `ProvidePlugin` is configured to provide `Buffer`, then we don't
252
- // need to warn the user.
253
- if (plugin) {
254
- const { definitions } = plugin;
255
- if (definitions.Buffer) {
256
- return;
257
- }
233
+ /**
234
+ * Check if a built-in module is used, but not provided by Webpack's
235
+ * `fallback` configuration.
236
+ *
237
+ * @param compiler - The Webpack compiler.
238
+ */
239
+ #checkBuiltIns(compiler) {
240
+ compiler.hooks.afterCompile.tap(this.constructor.name, (compilation) => {
241
+ if (!this.options.builtInResolver) {
242
+ return;
243
+ }
244
+ const { unresolvedModules } = this.options.builtInResolver;
245
+ if (unresolvedModules.size === 0) {
246
+ return;
247
+ }
248
+ const formattedModules = new Array(...unresolvedModules)
249
+ .map((name) => `• ${name}`)
250
+ .join('\n');
251
+ const webpackError = new webpack_1.WebpackError(`The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided. The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${(0, chalk_1.bold)('`polyfills`')} to ${(0, chalk_1.bold)('`true`')} or an object with the builtins to polyfill as the key and ${(0, chalk_1.bold)('`true`')} as the value. To disable this warning, set ${(0, chalk_1.bold)('`stats.builtIns`')} to ${(0, chalk_1.bold)('`false`')} in your snap config file, or add the module to the ${(0, chalk_1.bold)('`stats.builtIns.ignore`')} array.`);
252
+ webpackError.details = formattedModules;
253
+ compilation.warnings.push(webpackError);
254
+ });
255
+ }
256
+ /**
257
+ * Check if the given instance is a `ProvidePlugin`. This is not guaranteed to
258
+ * be accurate, but it's good enough for our purposes. If we were to use
259
+ * `instanceof` instead, it might not work if multiple versions of Webpack are
260
+ * installed.
261
+ *
262
+ * @param instance - The instance to check.
263
+ * @returns Whether the instance is a `ProvidePlugin`, i.e., whether it's an
264
+ * object with the name `ProvidePlugin` and a `definitions` property.
265
+ */
266
+ #isProvidePlugin(instance) {
267
+ return ((0, utils_1.isObject)(instance) &&
268
+ instance.constructor.name === 'ProvidePlugin' &&
269
+ (0, utils_1.hasProperty)(instance, 'definitions'));
258
270
  }
259
- compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
260
- compilation.hooks.afterProcessAssets.tap(this.constructor.name, (assets) => {
261
- // Check if assets use `Buffer`.
262
- const bufferAssets = Object.entries(assets)
263
- .filter(([name]) => name.endsWith('.js'))
264
- .filter(([, asset]) => asset.source().includes('Buffer'));
265
- if (bufferAssets.length === 0) {
271
+ /**
272
+ * Check if the `Buffer` global is used, but not provided by Webpack's
273
+ * `DefinePlugin`.
274
+ *
275
+ * @param compiler - The Webpack compiler.
276
+ */
277
+ #checkBuffer(compiler) {
278
+ const plugin = compiler.options.plugins?.find((instance) => this.#isProvidePlugin(instance));
279
+ // If the `ProvidePlugin` is configured to provide `Buffer`, then we don't
280
+ // need to warn the user.
281
+ if (plugin) {
282
+ const { definitions } = plugin;
283
+ if (definitions.Buffer) {
266
284
  return;
267
285
  }
268
- compilation.warnings.push(new webpack_1.WebpackError(`The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default. To use the Buffer global, you must polyfill Buffer by setting ${(0, chalk_1.bold)('`buffer`')} to ${(0, chalk_1.bold)('`true`')} in the ${(0, chalk_1.bold)('`polyfills`')} config object in your snap config. To disable this warning, set ${(0, chalk_1.bold)('`stats.buffer`')} to ${(0, chalk_1.bold)('`false`')} in your snap config file.`));
286
+ }
287
+ compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
288
+ compilation.hooks.afterProcessAssets.tap(this.constructor.name, (assets) => {
289
+ // Check if assets use `Buffer`.
290
+ const bufferAssets = Object.entries(assets)
291
+ .filter(([name]) => name.endsWith('.js'))
292
+ .filter(([, asset]) => asset.source().includes('Buffer'));
293
+ if (bufferAssets.length === 0) {
294
+ return;
295
+ }
296
+ compilation.warnings.push(new webpack_1.WebpackError(`The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default. To use the Buffer global, you must polyfill Buffer by setting ${(0, chalk_1.bold)('`buffer`')} to ${(0, chalk_1.bold)('`true`')} in the ${(0, chalk_1.bold)('`polyfills`')} config object in your snap config. To disable this warning, set ${(0, chalk_1.bold)('`stats.buffer`')} to ${(0, chalk_1.bold)('`false`')} in your snap config file.`));
297
+ });
269
298
  });
270
- });
271
- };
299
+ }
300
+ }
301
+ exports.SnapsBundleWarningsPlugin = SnapsBundleWarningsPlugin;
272
302
  //# sourceMappingURL=plugins.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugins.cjs","sourceRoot":"","sources":["../../src/webpack/plugins.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAgE;AAChE,iCAA+C;AAC/C,mCAAmC;AASnC,qCAAuC;AAEvC,uCAAgD;AAChD,qDAA4C;AAC5C,8CAA8D;AAS9D;;;GAGG;AACH,MAAa,gBAAgB;IAW3B,YACE,UAAmC;QACjC,OAAO,EAAE,KAAK;KACf,EACD,OAAa;;QATf;;WAEG;QACM,4CAAe;QAQtB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,uBAAA,IAAI,6BAAY,OAAO,MAAA,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,QAAkB;QACtB,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;YAC5D,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,OAAO;YACT,CAAC;YAED,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;YAE3D,IAAA,cAAM,EAAC,OAAO,EAAE,mCAAmC,CAAC,CAAC;YACrD,IAAA,cAAM,EAAC,IAAI,EAAE,gCAAgC,CAAC,CAAC;YAE/C,IAAI,MAAM,EAAE,MAAM,EAAE,CAAC;gBACnB,MAAM,eAAe,GAAG,MAAM;qBAC3B,GAAG,CAAC,CAAC,UAAU,EAAE,EAA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AAC,IAAI,CACvB,IAAI,sBAAY,CACd,mLAAmL,IAAA,YAAI,EACrL,UAAU,CACX,OAAO,IAAA,YAAI,EAAC,QAAQ,CAAC,WAAW,IAAA,YAAI,EACnC,aAAa,CACd,oEAAoE,IAAA,YAAI,EACvE,gBAAgB,CACjB,OAAO,IAAA,YAAI,EAAC,SAAS,CAAC,4BAA4B,CACpD,CACF,CAAC;QACJ,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { assert, hasProperty, isObject } from '@metamask/utils';\nimport { bold, dim, red, yellow } from 'chalk';\nimport { isBuiltin } from 'module';\nimport type { Ora } from 'ora';\nimport type {\n Compiler,\n ProvidePlugin,\n Resolver,\n StatsError,\n WebpackPluginInstance,\n} from 'webpack';\nimport { WebpackError } from 'webpack';\n\nimport { formatText, pluralize } from './utils';\nimport { evaluate } from '../commands/eval';\nimport { error, getErrorMessage, info, warn } 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, warnings } = 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((statsError) => this.#getStatsErrorMessage(statsError, red))\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 if (warnings?.length) {\n const formattedWarnings = warnings\n .map((statsWarning) =>\n this.#getStatsErrorMessage(statsWarning, yellow),\n )\n .join('\\n\\n');\n\n warn(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms with ${warnings.length} ${pluralize(\n warnings.length,\n 'warning',\n )}.\\n\\n${formattedWarnings}\\n`,\n this.#spinner,\n );\n } else {\n info(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms.`,\n this.#spinner,\n );\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 * @param color - The color to use for the error message.\n * @returns The error message.\n */\n #getStatsErrorMessage(\n statsError: StatsError,\n color: typeof red | typeof yellow,\n ) {\n const baseMessage = this.options.verbose\n ? getErrorMessage(statsError)\n : statsError.message;\n\n const [first, ...rest] = baseMessage.split('\\n');\n\n return [\n color(formatText(`• ${first}`, 4, 2)),\n ...rest.map((message) => formatText(color(message), 4)),\n statsError.details && `\\n${formatText(dim(statsError.details), 6)}`,\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 Webpack resolve plugin.\n *\n * Copied from Webpack's own types, because the `ResolvePluginInstance` type is\n * a union, and can't be used as a type for a class.\n */\ntype ResolvePlugin = {\n apply: (resolver: Resolver) => void;\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 ResolvePlugin {\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 constructor(\n options: SnapsBuiltInResolverOptions = {\n ignore: [],\n },\n ) {\n this.options = options;\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 options for the plugin.\n */\n readonly options: SnapsBundleWarningsPluginOptions;\n\n constructor(\n options: SnapsBundleWarningsPluginOptions = {\n buffer: true,\n builtIns: true,\n },\n ) {\n this.options = options;\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, (compilation) => {\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) => `• ${name}`)\n .join('\\n');\n\n const webpackError = new WebpackError(\n `The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided. The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${bold(\n '`polyfills`',\n )} to ${bold(\n '`true`',\n )} or an object with the builtins to polyfill as the key and ${bold(\n '`true`',\n )} as the value. To disable this warning, set ${bold(\n '`stats.builtIns`',\n )} to ${bold(\n '`false`',\n )} in your snap config file, or add the module to the ${bold(\n '`stats.builtIns.ignore`',\n )} array.`,\n );\n\n webpackError.details = formattedModules;\n compilation.warnings.push(webpackError);\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 compilation.warnings.push(\n new WebpackError(\n `The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default. To use the Buffer global, you must polyfill Buffer by setting ${bold(\n '`buffer`',\n )} to ${bold('`true`')} in the ${bold(\n '`polyfills`',\n )} config object in your snap config. To disable this warning, set ${bold(\n '`stats.buffer`',\n )} to ${bold('`false`')} in your snap config file.`,\n ),\n );\n },\n );\n });\n }\n}\n"]}
1
+ {"version":3,"file":"plugins.cjs","sourceRoot":"","sources":["../../src/webpack/plugins.ts"],"names":[],"mappings":";;;AAAA,2CAAgE;AAChE,iCAA+C;AAC/C,mCAAmC;AASnC,qCAAuC;AAEvC,uCAAgD;AAChD,qDAA4C;AAC5C,8CAA8D;AAS9D;;;GAGG;AACH,MAAa,gBAAgB;IAC3B;;OAEG;IACM,OAAO,CAA0B;IAE1C;;OAEG;IACM,QAAQ,CAAO;IAExB,YACE,UAAmC;QACjC,OAAO,E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AAE,GAAG,MAAM,CAAC;YAC/B,IAAI,WAAW,CAAC,MAAM,EAAE,CAAC;gBACvB,OAAO;YACT,CAAC;QACH,CAAC;QAED,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE;YACpE,WAAW,CAAC,KAAK,CAAC,kBAAkB,CAAC,GAAG,CACtC,IAAI,CAAC,WAAW,CAAC,IAAI,EACrB,CAAC,MAAM,EAAE,EAAE;gBACT,gCAAgC;gBAChC,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;qBACxC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;qBACxC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;gBAE5D,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;oBAC9B,OAAO;gBACT,CAAC;gBAED,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,IAAI,sBAAY,CACd,mLAAmL,IAAA,YAAI,EACrL,UAAU,CACX,OAAO,IAAA,YAAI,EAAC,QAAQ,CAAC,WAAW,IAAA,YAAI,EACnC,aAAa,CACd,oEAAoE,IAAA,YAAI,EACvE,gBAAgB,CACjB,OAAO,IAAA,YAAI,EAAC,SAAS,CAAC,4BAA4B,CACpD,CACF,CAAC;YACJ,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA1ID,8DA0IC","sourcesContent":["import { assert, hasProperty, isObject } from '@metamask/utils';\nimport { bold, dim, red, yellow } from 'chalk';\nimport { isBuiltin } from 'module';\nimport type { Ora } from 'ora';\nimport type {\n Compiler,\n ProvidePlugin,\n Resolver,\n StatsError,\n WebpackPluginInstance,\n} from 'webpack';\nimport { WebpackError } from 'webpack';\n\nimport { formatText, pluralize } from './utils';\nimport { evaluate } from '../commands/eval';\nimport { error, getErrorMessage, info, warn } 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, warnings } = 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((statsError) => this.#getStatsErrorMessage(statsError, red))\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 if (warnings?.length) {\n const formattedWarnings = warnings\n .map((statsWarning) =>\n this.#getStatsErrorMessage(statsWarning, yellow),\n )\n .join('\\n\\n');\n\n warn(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms with ${warnings.length} ${pluralize(\n warnings.length,\n 'warning',\n )}.\\n\\n${formattedWarnings}\\n`,\n this.#spinner,\n );\n } else {\n info(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms.`,\n this.#spinner,\n );\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 * @param color - The color to use for the error message.\n * @returns The error message.\n */\n #getStatsErrorMessage(\n statsError: StatsError,\n color: typeof red | typeof yellow,\n ) {\n const baseMessage = this.options.verbose\n ? getErrorMessage(statsError)\n : statsError.message;\n\n const [first, ...rest] = baseMessage.split('\\n');\n\n return [\n color(formatText(`• ${first}`, 4, 2)),\n ...rest.map((message) => formatText(color(message), 4)),\n statsError.details && `\\n${formatText(dim(statsError.details), 6)}`,\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 Webpack resolve plugin.\n *\n * Copied from Webpack's own types, because the `ResolvePluginInstance` type is\n * a union, and can't be used as a type for a class.\n */\ntype ResolvePlugin = {\n apply: (resolver: Resolver) => void;\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 ResolvePlugin {\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 constructor(\n options: SnapsBuiltInResolverOptions = {\n ignore: [],\n },\n ) {\n this.options = options;\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 options for the plugin.\n */\n readonly options: SnapsBundleWarningsPluginOptions;\n\n constructor(\n options: SnapsBundleWarningsPluginOptions = {\n buffer: true,\n builtIns: true,\n },\n ) {\n this.options = options;\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, (compilation) => {\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) => `• ${name}`)\n .join('\\n');\n\n const webpackError = new WebpackError(\n `The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided. The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${bold(\n '`polyfills`',\n )} to ${bold(\n '`true`',\n )} or an object with the builtins to polyfill as the key and ${bold(\n '`true`',\n )} as the value. To disable this warning, set ${bold(\n '`stats.builtIns`',\n )} to ${bold(\n '`false`',\n )} in your snap config file, or add the module to the ${bold(\n '`stats.builtIns.ignore`',\n )} array.`,\n );\n\n webpackError.details = formattedModules;\n compilation.warnings.push(webpackError);\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 compilation.warnings.push(\n new WebpackError(\n `The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default. To use the Buffer global, you must polyfill Buffer by setting ${bold(\n '`buffer`',\n )} to ${bold('`true`')} in the ${bold(\n '`polyfills`',\n )} config object in your snap config. To disable this warning, set ${bold(\n '`stats.buffer`',\n )} to ${bold('`false`')} in your snap config file.`,\n ),\n );\n },\n );\n });\n }\n}\n"]}
@@ -1,15 +1,3 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _SnapsStatsPlugin_instances, _SnapsStatsPlugin_spinner, _SnapsStatsPlugin_getStatsErrorMessage, _SnapsWatchPlugin_instances, _SnapsWatchPlugin_spinner, _SnapsWatchPlugin_safeEvaluate, _SnapsBuiltInResolver_source, _SnapsBundleWarningsPlugin_instances, _SnapsBundleWarningsPlugin_checkBuiltIns, _SnapsBundleWarningsPlugin_isProvidePlugin, _SnapsBundleWarningsPlugin_checkBuffer;
13
1
  import { assert, hasProperty, isObject } from "@metamask/utils";
14
2
  import $chalk from "chalk";
15
3
  const { bold, dim, red, yellow } = $chalk;
@@ -24,16 +12,19 @@ import { error, getErrorMessage, info, warn } from "../utils/index.mjs";
24
12
  * the number of files compiled, and the time taken to compile them.
25
13
  */
26
14
  export class SnapsStatsPlugin {
15
+ /**
16
+ * The options for the plugin.
17
+ */
18
+ options;
19
+ /**
20
+ * The spinner to use for logging.
21
+ */
22
+ #spinner;
27
23
  constructor(options = {
28
24
  verbose: false,
29
25
  }, spinner) {
30
- _SnapsStatsPlugin_instances.add(this);
31
- /**
32
- * The spinner to use for logging.
33
- */
34
- _SnapsStatsPlugin_spinner.set(this, void 0);
35
26
  this.options = options;
36
- __classPrivateFieldSet(this, _SnapsStatsPlugin_spinner, spinner, "f");
27
+ this.#spinner = spinner;
37
28
  }
38
29
  /**
39
30
  * Apply the plugin to the Webpack compiler.
@@ -50,57 +41,67 @@ export class SnapsStatsPlugin {
50
41
  assert(time, 'Time must be defined in stats.');
51
42
  if (errors?.length) {
52
43
  const formattedErrors = errors
53
- .map((statsError) => __classPrivateFieldGet(this, _SnapsStatsPlugin_instances, "m", _SnapsStatsPlugin_getStatsErrorMessage).call(this, statsError, red))
44
+ .map((statsError) => this.#getStatsErrorMessage(statsError, red))
54
45
  .join('\n\n');
55
- error(`Compiled ${modules.length} ${pluralize(modules.length, 'file')} in ${time}ms with ${errors.length} ${pluralize(errors.length, 'error')}.\n\n${formattedErrors}\n`, __classPrivateFieldGet(this, _SnapsStatsPlugin_spinner, "f"));
56
- __classPrivateFieldGet(this, _SnapsStatsPlugin_spinner, "f")?.stop();
46
+ error(`Compiled ${modules.length} ${pluralize(modules.length, 'file')} in ${time}ms with ${errors.length} ${pluralize(errors.length, 'error')}.\n\n${formattedErrors}\n`, this.#spinner);
47
+ this.#spinner?.stop();
57
48
  process.exitCode = 1;
58
49
  return;
59
50
  }
60
51
  if (warnings?.length) {
61
52
  const formattedWarnings = warnings
62
- .map((statsWarning) => __classPrivateFieldGet(this, _SnapsStatsPlugin_instances, "m", _SnapsStatsPlugin_getStatsErrorMessage).call(this, statsWarning, yellow))
53
+ .map((statsWarning) => this.#getStatsErrorMessage(statsWarning, yellow))
63
54
  .join('\n\n');
64
- warn(`Compiled ${modules.length} ${pluralize(modules.length, 'file')} in ${time}ms with ${warnings.length} ${pluralize(warnings.length, 'warning')}.\n\n${formattedWarnings}\n`, __classPrivateFieldGet(this, _SnapsStatsPlugin_spinner, "f"));
55
+ warn(`Compiled ${modules.length} ${pluralize(modules.length, 'file')} in ${time}ms with ${warnings.length} ${pluralize(warnings.length, 'warning')}.\n\n${formattedWarnings}\n`, this.#spinner);
65
56
  }
66
57
  else {
67
- info(`Compiled ${modules.length} ${pluralize(modules.length, 'file')} in ${time}ms.`, __classPrivateFieldGet(this, _SnapsStatsPlugin_spinner, "f"));
58
+ info(`Compiled ${modules.length} ${pluralize(modules.length, 'file')} in ${time}ms.`, this.#spinner);
68
59
  }
69
60
  if (compiler.watchMode) {
70
61
  // The spinner may be restarted by the watch plugin, outside of the
71
62
  // `executeSteps` flow, so we stop it here just in case.
72
- __classPrivateFieldGet(this, _SnapsStatsPlugin_spinner, "f")?.succeed('Done!');
63
+ this.#spinner?.succeed('Done!');
73
64
  }
74
65
  });
75
66
  }
67
+ /**
68
+ * Get the error message for the given stats error.
69
+ *
70
+ * @param statsError - The stats error.
71
+ * @param color - The color to use for the error message.
72
+ * @returns The error message.
73
+ */
74
+ #getStatsErrorMessage(statsError, color) {
75
+ const baseMessage = this.options.verbose
76
+ ? getErrorMessage(statsError)
77
+ : statsError.message;
78
+ const [first, ...rest] = baseMessage.split('\n');
79
+ return [
80
+ color(formatText(`• ${first}`, 4, 2)),
81
+ ...rest.map((message) => formatText(color(message), 4)),
82
+ statsError.details && `\n${formatText(dim(statsError.details), 6)}`,
83
+ ]
84
+ .filter(Boolean)
85
+ .join('\n');
86
+ }
76
87
  }
77
- _SnapsStatsPlugin_spinner = new WeakMap(), _SnapsStatsPlugin_instances = new WeakSet(), _SnapsStatsPlugin_getStatsErrorMessage = function _SnapsStatsPlugin_getStatsErrorMessage(statsError, color) {
78
- const baseMessage = this.options.verbose
79
- ? getErrorMessage(statsError)
80
- : statsError.message;
81
- const [first, ...rest] = baseMessage.split('\n');
82
- return [
83
- color(formatText(`• ${first}`, 4, 2)),
84
- ...rest.map((message) => formatText(color(message), 4)),
85
- statsError.details && `\n${formatText(dim(statsError.details), 6)}`,
86
- ]
87
- .filter(Boolean)
88
- .join('\n');
89
- };
90
88
  /**
91
89
  * A plugin that adds extra files to watch. This is useful for watching files
92
90
  * that are not imported by the entry point, such as the `snap.manifest.json`
93
91
  * file.
94
92
  */
95
93
  export class SnapsWatchPlugin {
94
+ /**
95
+ * The options for the plugin.
96
+ */
97
+ options;
98
+ /**
99
+ * The spinner to use for logging.
100
+ */
101
+ #spinner;
96
102
  constructor(options, spinner) {
97
- _SnapsWatchPlugin_instances.add(this);
98
- /**
99
- * The spinner to use for logging.
100
- */
101
- _SnapsWatchPlugin_spinner.set(this, void 0);
102
103
  this.options = options;
103
- __classPrivateFieldSet(this, _SnapsWatchPlugin_spinner, spinner, "f");
104
+ this.#spinner = spinner;
104
105
  }
105
106
  /**
106
107
  * Apply the plugin to the Webpack compiler.
@@ -109,35 +110,34 @@ export class SnapsWatchPlugin {
109
110
  */
110
111
  apply(compiler) {
111
112
  compiler.hooks.invalid.tap(this.constructor.name, (file) => {
112
- __classPrivateFieldGet(this, _SnapsWatchPlugin_spinner, "f")?.start();
113
- info(`Changes detected in ${yellow(file)}, recompiling.`, __classPrivateFieldGet(this, _SnapsWatchPlugin_spinner, "f"));
113
+ this.#spinner?.start();
114
+ info(`Changes detected in ${yellow(file)}, recompiling.`, this.#spinner);
114
115
  });
115
116
  compiler.hooks.afterEmit.tapPromise(this.constructor.name, async ({ fileDependencies }) => {
116
117
  this.options.files?.forEach(fileDependencies.add.bind(fileDependencies));
117
118
  if (this.options.bundle && this.options.evaluate) {
118
- await __classPrivateFieldGet(this, _SnapsWatchPlugin_instances, "m", _SnapsWatchPlugin_safeEvaluate).call(this, this.options.bundle);
119
+ await this.#safeEvaluate(this.options.bundle);
119
120
  }
120
121
  });
121
122
  }
122
- }
123
- _SnapsWatchPlugin_spinner = new WeakMap(), _SnapsWatchPlugin_instances = new WeakSet(), _SnapsWatchPlugin_safeEvaluate =
124
- /**
125
- * Safely evaluate the bundle at the given path. If an error occurs, it will
126
- * be logged to the console, rather than throwing an error.
127
- *
128
- * This function should never throw an error.
129
- *
130
- * @param bundlePath - The path to the bundle.
131
- */
132
- async function _SnapsWatchPlugin_safeEvaluate(bundlePath) {
133
- try {
134
- await evaluate(bundlePath);
135
- info(`Snap bundle evaluated successfully.`, __classPrivateFieldGet(this, _SnapsWatchPlugin_spinner, "f"));
136
- }
137
- catch (evaluateError) {
138
- error(evaluateError.message, __classPrivateFieldGet(this, _SnapsWatchPlugin_spinner, "f"));
123
+ /**
124
+ * Safely evaluate the bundle at the given path. If an error occurs, it will
125
+ * be logged to the console, rather than throwing an error.
126
+ *
127
+ * This function should never throw an error.
128
+ *
129
+ * @param bundlePath - The path to the bundle.
130
+ */
131
+ async #safeEvaluate(bundlePath) {
132
+ try {
133
+ await evaluate(bundlePath);
134
+ info(`Snap bundle evaluated successfully.`, this.#spinner);
135
+ }
136
+ catch (evaluateError) {
137
+ error(evaluateError.message, this.#spinner);
138
+ }
139
139
  }
140
- };
140
+ }
141
141
  /**
142
142
  * A plugin that logs a message when a built-in module is not resolved. The
143
143
  * MetaMask Snaps CLI does not support built-in modules by default, and this
@@ -145,17 +145,21 @@ async function _SnapsWatchPlugin_safeEvaluate(bundlePath) {
145
145
  * when no fallback is configured.
146
146
  */
147
147
  export class SnapsBuiltInResolver {
148
+ /**
149
+ * The built-in modules that have been imported, but not resolved.
150
+ */
151
+ unresolvedModules = new Set();
152
+ /**
153
+ * The name of the resolver hook to tap into.
154
+ */
155
+ #source = 'described-resolve';
156
+ /**
157
+ * The options for the plugin.
158
+ */
159
+ options;
148
160
  constructor(options = {
149
161
  ignore: [],
150
162
  }) {
151
- /**
152
- * The built-in modules that have been imported, but not resolved.
153
- */
154
- this.unresolvedModules = new Set();
155
- /**
156
- * The name of the resolver hook to tap into.
157
- */
158
- _SnapsBuiltInResolver_source.set(this, 'described-resolve');
159
163
  this.options = options;
160
164
  }
161
165
  /**
@@ -165,7 +169,7 @@ export class SnapsBuiltInResolver {
165
169
  */
166
170
  apply(resolver) {
167
171
  resolver
168
- .getHook(__classPrivateFieldGet(this, _SnapsBuiltInResolver_source, "f"))
172
+ .getHook(this.#source)
169
173
  .tapAsync(this.constructor.name, ({ module: isModule, request }, _, callback) => {
170
174
  if (!isModule || !request) {
171
175
  return callback();
@@ -182,7 +186,6 @@ export class SnapsBuiltInResolver {
182
186
  });
183
187
  }
184
188
  }
185
- _SnapsBuiltInResolver_source = new WeakMap();
186
189
  /**
187
190
  * A plugin that logs a message when:
188
191
  *
@@ -200,11 +203,14 @@ _SnapsBuiltInResolver_source = new WeakMap();
200
203
  * plugin doesn't have access to the resolver.
201
204
  */
202
205
  export class SnapsBundleWarningsPlugin {
206
+ /**
207
+ * The options for the plugin.
208
+ */
209
+ options;
203
210
  constructor(options = {
204
211
  buffer: true,
205
212
  builtIns: true,
206
213
  }) {
207
- _SnapsBundleWarningsPlugin_instances.add(this);
208
214
  this.options = options;
209
215
  }
210
216
  /**
@@ -214,54 +220,78 @@ export class SnapsBundleWarningsPlugin {
214
220
  */
215
221
  apply(compiler) {
216
222
  if (this.options.builtIns) {
217
- __classPrivateFieldGet(this, _SnapsBundleWarningsPlugin_instances, "m", _SnapsBundleWarningsPlugin_checkBuiltIns).call(this, compiler);
223
+ this.#checkBuiltIns(compiler);
218
224
  }
219
225
  if (this.options.buffer) {
220
- __classPrivateFieldGet(this, _SnapsBundleWarningsPlugin_instances, "m", _SnapsBundleWarningsPlugin_checkBuffer).call(this, compiler);
226
+ this.#checkBuffer(compiler);
221
227
  }
222
228
  }
223
- }
224
- _SnapsBundleWarningsPlugin_instances = new WeakSet(), _SnapsBundleWarningsPlugin_checkBuiltIns = function _SnapsBundleWarningsPlugin_checkBuiltIns(compiler) {
225
- compiler.hooks.afterCompile.tap(this.constructor.name, (compilation) => {
226
- if (!this.options.builtInResolver) {
227
- return;
228
- }
229
- const { unresolvedModules } = this.options.builtInResolver;
230
- if (unresolvedModules.size === 0) {
231
- return;
232
- }
233
- const formattedModules = new Array(...unresolvedModules)
234
- .map((name) => `• ${name}`)
235
- .join('\n');
236
- const webpackError = new WebpackError(`The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided. The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${bold('`polyfills`')} to ${bold('`true`')} or an object with the builtins to polyfill as the key and ${bold('`true`')} as the value. To disable this warning, set ${bold('`stats.builtIns`')} to ${bold('`false`')} in your snap config file, or add the module to the ${bold('`stats.builtIns.ignore`')} array.`);
237
- webpackError.details = formattedModules;
238
- compilation.warnings.push(webpackError);
239
- });
240
- }, _SnapsBundleWarningsPlugin_isProvidePlugin = function _SnapsBundleWarningsPlugin_isProvidePlugin(instance) {
241
- return (isObject(instance) &&
242
- instance.constructor.name === 'ProvidePlugin' &&
243
- hasProperty(instance, 'definitions'));
244
- }, _SnapsBundleWarningsPlugin_checkBuffer = function _SnapsBundleWarningsPlugin_checkBuffer(compiler) {
245
- const plugin = compiler.options.plugins?.find((instance) => __classPrivateFieldGet(this, _SnapsBundleWarningsPlugin_instances, "m", _SnapsBundleWarningsPlugin_isProvidePlugin).call(this, instance));
246
- // If the `ProvidePlugin` is configured to provide `Buffer`, then we don't
247
- // need to warn the user.
248
- if (plugin) {
249
- const { definitions } = plugin;
250
- if (definitions.Buffer) {
251
- return;
252
- }
229
+ /**
230
+ * Check if a built-in module is used, but not provided by Webpack's
231
+ * `fallback` configuration.
232
+ *
233
+ * @param compiler - The Webpack compiler.
234
+ */
235
+ #checkBuiltIns(compiler) {
236
+ compiler.hooks.afterCompile.tap(this.constructor.name, (compilation) => {
237
+ if (!this.options.builtInResolver) {
238
+ return;
239
+ }
240
+ const { unresolvedModules } = this.options.builtInResolver;
241
+ if (unresolvedModules.size === 0) {
242
+ return;
243
+ }
244
+ const formattedModules = new Array(...unresolvedModules)
245
+ .map((name) => `• ${name}`)
246
+ .join('\n');
247
+ const webpackError = new WebpackError(`The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided. The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${bold('`polyfills`')} to ${bold('`true`')} or an object with the builtins to polyfill as the key and ${bold('`true`')} as the value. To disable this warning, set ${bold('`stats.builtIns`')} to ${bold('`false`')} in your snap config file, or add the module to the ${bold('`stats.builtIns.ignore`')} array.`);
248
+ webpackError.details = formattedModules;
249
+ compilation.warnings.push(webpackError);
250
+ });
251
+ }
252
+ /**
253
+ * Check if the given instance is a `ProvidePlugin`. This is not guaranteed to
254
+ * be accurate, but it's good enough for our purposes. If we were to use
255
+ * `instanceof` instead, it might not work if multiple versions of Webpack are
256
+ * installed.
257
+ *
258
+ * @param instance - The instance to check.
259
+ * @returns Whether the instance is a `ProvidePlugin`, i.e., whether it's an
260
+ * object with the name `ProvidePlugin` and a `definitions` property.
261
+ */
262
+ #isProvidePlugin(instance) {
263
+ return (isObject(instance) &&
264
+ instance.constructor.name === 'ProvidePlugin' &&
265
+ hasProperty(instance, 'definitions'));
253
266
  }
254
- compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
255
- compilation.hooks.afterProcessAssets.tap(this.constructor.name, (assets) => {
256
- // Check if assets use `Buffer`.
257
- const bufferAssets = Object.entries(assets)
258
- .filter(([name]) => name.endsWith('.js'))
259
- .filter(([, asset]) => asset.source().includes('Buffer'));
260
- if (bufferAssets.length === 0) {
267
+ /**
268
+ * Check if the `Buffer` global is used, but not provided by Webpack's
269
+ * `DefinePlugin`.
270
+ *
271
+ * @param compiler - The Webpack compiler.
272
+ */
273
+ #checkBuffer(compiler) {
274
+ const plugin = compiler.options.plugins?.find((instance) => this.#isProvidePlugin(instance));
275
+ // If the `ProvidePlugin` is configured to provide `Buffer`, then we don't
276
+ // need to warn the user.
277
+ if (plugin) {
278
+ const { definitions } = plugin;
279
+ if (definitions.Buffer) {
261
280
  return;
262
281
  }
263
- compilation.warnings.push(new WebpackError(`The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default. To use the Buffer global, you must polyfill Buffer by setting ${bold('`buffer`')} to ${bold('`true`')} in the ${bold('`polyfills`')} config object in your snap config. To disable this warning, set ${bold('`stats.buffer`')} to ${bold('`false`')} in your snap config file.`));
282
+ }
283
+ compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
284
+ compilation.hooks.afterProcessAssets.tap(this.constructor.name, (assets) => {
285
+ // Check if assets use `Buffer`.
286
+ const bufferAssets = Object.entries(assets)
287
+ .filter(([name]) => name.endsWith('.js'))
288
+ .filter(([, asset]) => asset.source().includes('Buffer'));
289
+ if (bufferAssets.length === 0) {
290
+ return;
291
+ }
292
+ compilation.warnings.push(new WebpackError(`The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default. To use the Buffer global, you must polyfill Buffer by setting ${bold('`buffer`')} to ${bold('`true`')} in the ${bold('`polyfills`')} config object in your snap config. To disable this warning, set ${bold('`stats.buffer`')} to ${bold('`false`')} in your snap config file.`));
293
+ });
264
294
  });
265
- });
266
- };
295
+ }
296
+ }
267
297
  //# sourceMappingURL=plugins.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugins.mjs","sourceRoot":"","sources":["../../src/webpack/plugins.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,wBAAwB;;;AAEhE,OAAO,EAAE,SAAS,EAAE,eAAe;;;AAWnC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,oBAAgB;AAChD,OAAO,EAAE,QA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,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE;QACpE,WAAW,CAAC,KAAK,CAAC,kBAAkB,CAAC,GAAG,CACtC,IAAI,CAAC,WAAW,CAAC,IAAI,EACrB,CAAC,MAAM,EAAE,EAAE;YACT,gCAAgC;YAChC,MAAM,YAAY,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;iBACxC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;iBACxC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;YAE5D,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC9B,OAAO;YACT,CAAC;YAED,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,IAAI,YAAY,CACd,mLAAmL,IAAI,CACrL,UAAU,CACX,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,IAAI,CACnC,aAAa,CACd,oEAAoE,IAAI,CACvE,gBAAgB,CACjB,OAAO,IAAI,CAAC,SAAS,CAAC,4BAA4B,CACpD,CACF,CAAC;QACJ,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { assert, hasProperty, isObject } from '@metamask/utils';\nimport { bold, dim, red, yellow } from 'chalk';\nimport { isBuiltin } from 'module';\nimport type { Ora } from 'ora';\nimport type {\n Compiler,\n ProvidePlugin,\n Resolver,\n StatsError,\n WebpackPluginInstance,\n} from 'webpack';\nimport { WebpackError } from 'webpack';\n\nimport { formatText, pluralize } from './utils';\nimport { evaluate } from '../commands/eval';\nimport { error, getErrorMessage, info, warn } 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, warnings } = 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((statsError) => this.#getStatsErrorMessage(statsError, red))\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 if (warnings?.length) {\n const formattedWarnings = warnings\n .map((statsWarning) =>\n this.#getStatsErrorMessage(statsWarning, yellow),\n )\n .join('\\n\\n');\n\n warn(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms with ${warnings.length} ${pluralize(\n warnings.length,\n 'warning',\n )}.\\n\\n${formattedWarnings}\\n`,\n this.#spinner,\n );\n } else {\n info(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms.`,\n this.#spinner,\n );\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 * @param color - The color to use for the error message.\n * @returns The error message.\n */\n #getStatsErrorMessage(\n statsError: StatsError,\n color: typeof red | typeof yellow,\n ) {\n const baseMessage = this.options.verbose\n ? getErrorMessage(statsError)\n : statsError.message;\n\n const [first, ...rest] = baseMessage.split('\\n');\n\n return [\n color(formatText(`• ${first}`, 4, 2)),\n ...rest.map((message) => formatText(color(message), 4)),\n statsError.details && `\\n${formatText(dim(statsError.details), 6)}`,\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 Webpack resolve plugin.\n *\n * Copied from Webpack's own types, because the `ResolvePluginInstance` type is\n * a union, and can't be used as a type for a class.\n */\ntype ResolvePlugin = {\n apply: (resolver: Resolver) => void;\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 ResolvePlugin {\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 constructor(\n options: SnapsBuiltInResolverOptions = {\n ignore: [],\n },\n ) {\n this.options = options;\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 options for the plugin.\n */\n readonly options: SnapsBundleWarningsPluginOptions;\n\n constructor(\n options: SnapsBundleWarningsPluginOptions = {\n buffer: true,\n builtIns: true,\n },\n ) {\n this.options = options;\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, (compilation) => {\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) => `• ${name}`)\n .join('\\n');\n\n const webpackError = new WebpackError(\n `The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided. The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${bold(\n '`polyfills`',\n )} to ${bold(\n '`true`',\n )} or an object with the builtins to polyfill as the key and ${bold(\n '`true`',\n )} as the value. To disable this warning, set ${bold(\n '`stats.builtIns`',\n )} to ${bold(\n '`false`',\n )} in your snap config file, or add the module to the ${bold(\n '`stats.builtIns.ignore`',\n )} array.`,\n );\n\n webpackError.details = formattedModules;\n compilation.warnings.push(webpackError);\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 compilation.warnings.push(\n new WebpackError(\n `The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default. To use the Buffer global, you must polyfill Buffer by setting ${bold(\n '`buffer`',\n )} to ${bold('`true`')} in the ${bold(\n '`polyfills`',\n )} config object in your snap config. To disable this warning, set ${bold(\n '`stats.buffer`',\n )} to ${bold('`false`')} in your snap config file.`,\n ),\n );\n },\n );\n });\n }\n}\n"]}
1
+ {"version":3,"file":"plugins.mjs","sourceRoot":"","sources":["../../src/webpack/plugins.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,wBAAwB;;;AAEhE,OAAO,EAAE,SAAS,EAAE,eAAe;;;AAWnC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,oBAAgB;AAChD,OAAO,EAAE,QAAQ,EAAE,mCAAyB;AAC5C,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE,IAAI,EAAE,2BAAiB;AAS9D;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IAC3B;;OAEG;IACM,OAAO,CAA0B;IAE1C;;OAEG;IACM,QAAQ,CAAO;IAExB,YACE,UAAmC;QACjC,OAAO,EAAE,KAAK;KACf,EACD,OAAa;QAEb,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,QAAkB;QACtB,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;YAC5D,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,OAAO;YACT,CAAC;YAED,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;YAE3D,MAAM,CA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AAM,KAAK,CAAC,EAAE,CAAC;oBAC9B,OAAO;gBACT,CAAC;gBAED,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,IAAI,YAAY,CACd,mLAAmL,IAAI,CACrL,UAAU,CACX,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,IAAI,CACnC,aAAa,CACd,oEAAoE,IAAI,CACvE,gBAAgB,CACjB,OAAO,IAAI,CAAC,SAAS,CAAC,4BAA4B,CACpD,CACF,CAAC;YACJ,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { assert, hasProperty, isObject } from '@metamask/utils';\nimport { bold, dim, red, yellow } from 'chalk';\nimport { isBuiltin } from 'module';\nimport type { Ora } from 'ora';\nimport type {\n Compiler,\n ProvidePlugin,\n Resolver,\n StatsError,\n WebpackPluginInstance,\n} from 'webpack';\nimport { WebpackError } from 'webpack';\n\nimport { formatText, pluralize } from './utils';\nimport { evaluate } from '../commands/eval';\nimport { error, getErrorMessage, info, warn } 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, warnings } = 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((statsError) => this.#getStatsErrorMessage(statsError, red))\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 if (warnings?.length) {\n const formattedWarnings = warnings\n .map((statsWarning) =>\n this.#getStatsErrorMessage(statsWarning, yellow),\n )\n .join('\\n\\n');\n\n warn(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms with ${warnings.length} ${pluralize(\n warnings.length,\n 'warning',\n )}.\\n\\n${formattedWarnings}\\n`,\n this.#spinner,\n );\n } else {\n info(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms.`,\n this.#spinner,\n );\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 * @param color - The color to use for the error message.\n * @returns The error message.\n */\n #getStatsErrorMessage(\n statsError: StatsError,\n color: typeof red | typeof yellow,\n ) {\n const baseMessage = this.options.verbose\n ? getErrorMessage(statsError)\n : statsError.message;\n\n const [first, ...rest] = baseMessage.split('\\n');\n\n return [\n color(formatText(`• ${first}`, 4, 2)),\n ...rest.map((message) => formatText(color(message), 4)),\n statsError.details && `\\n${formatText(dim(statsError.details), 6)}`,\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 Webpack resolve plugin.\n *\n * Copied from Webpack's own types, because the `ResolvePluginInstance` type is\n * a union, and can't be used as a type for a class.\n */\ntype ResolvePlugin = {\n apply: (resolver: Resolver) => void;\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 ResolvePlugin {\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 constructor(\n options: SnapsBuiltInResolverOptions = {\n ignore: [],\n },\n ) {\n this.options = options;\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 options for the plugin.\n */\n readonly options: SnapsBundleWarningsPluginOptions;\n\n constructor(\n options: SnapsBundleWarningsPluginOptions = {\n buffer: true,\n builtIns: true,\n },\n ) {\n this.options = options;\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, (compilation) => {\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) => `• ${name}`)\n .join('\\n');\n\n const webpackError = new WebpackError(\n `The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided. The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${bold(\n '`polyfills`',\n )} to ${bold(\n '`true`',\n )} or an object with the builtins to polyfill as the key and ${bold(\n '`true`',\n )} as the value. To disable this warning, set ${bold(\n '`stats.builtIns`',\n )} to ${bold(\n '`false`',\n )} in your snap config file, or add the module to the ${bold(\n '`stats.builtIns.ignore`',\n )} array.`,\n );\n\n webpackError.details = formattedModules;\n compilation.warnings.push(webpackError);\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 compilation.warnings.push(\n new WebpackError(\n `The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default. To use the Buffer global, you must polyfill Buffer by setting ${bold(\n '`buffer`',\n )} to ${bold('`true`')} in the ${bold(\n '`polyfills`',\n )} config object in your snap config. To disable this warning, set ${bold(\n '`stats.buffer`',\n )} to ${bold('`false`')} in your snap config file.`,\n ),\n );\n },\n );\n });\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-cli",
3
- "version": "7.2.0",
3
+ "version": "8.0.0",
4
4
  "description": "A CLI for developing MetaMask Snaps",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -64,14 +64,14 @@
64
64
  "test:watch": "jest --watch"
65
65
  },
66
66
  "dependencies": {
67
- "@metamask/snaps-rpc-methods": "^12.4.0",
67
+ "@metamask/snaps-rpc-methods": "^13.0.0",
68
68
  "@metamask/snaps-sandbox": "^1.0.0",
69
- "@metamask/snaps-sdk": "^7.1.0",
70
- "@metamask/snaps-utils": "^9.4.0",
69
+ "@metamask/snaps-sdk": "^8.0.0",
70
+ "@metamask/snaps-utils": "^10.0.0",
71
71
  "@metamask/snaps-webpack-plugin": "^4.3.0",
72
72
  "@metamask/superstruct": "^3.2.1",
73
73
  "@metamask/utils": "^11.4.0",
74
- "@swc/core": "1.3.78",
74
+ "@swc/core": "1.11.31",
75
75
  "assert": "^2.0.0",
76
76
  "browserify-zlib": "^0.2.0",
77
77
  "buffer": "^6.0.3",
@@ -96,7 +96,7 @@
96
96
  "stream-http": "^3.2.0",
97
97
  "string_decoder": "^1.3.0",
98
98
  "strip-ansi": "^6.0.1",
99
- "swc-loader": "^0.2.3",
99
+ "swc-loader": "^0.2.6",
100
100
  "terser-webpack-plugin": "^5.3.9",
101
101
  "timers-browserify": "^2.0.12",
102
102
  "tty-browserify": "^0.0.1",
@@ -111,7 +111,7 @@
111
111
  "devDependencies": {
112
112
  "@lavamoat/allow-scripts": "^3.3.3",
113
113
  "@metamask/auto-changelog": "^5.0.2",
114
- "@swc/jest": "^0.2.26",
114
+ "@swc/jest": "^0.2.38",
115
115
  "@ts-bridge/cli": "^0.6.1",
116
116
  "@types/express": "^5.0.1",
117
117
  "@types/jest": "^27.5.1",
@@ -132,7 +132,7 @@
132
132
  "typescript": "~5.3.3"
133
133
  },
134
134
  "engines": {
135
- "node": "^18.16 || >=20"
135
+ "node": "^20 || >=22"
136
136
  },
137
137
  "publishConfig": {
138
138
  "access": "public",