@metamask/snaps-cli 5.0.0 → 5.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -1
- package/dist/cjs/config.js +1 -5
- package/dist/cjs/config.js.map +1 -1
- package/dist/cjs/webpack/config.js +23 -7
- package/dist/cjs/webpack/config.js.map +1 -1
- package/dist/cjs/webpack/loaders/wasm.js +3 -1
- package/dist/cjs/webpack/loaders/wasm.js.map +1 -1
- package/dist/cjs/webpack/plugins.js +22 -21
- package/dist/cjs/webpack/plugins.js.map +1 -1
- package/dist/cjs/webpack/utils.js +77 -4
- package/dist/cjs/webpack/utils.js.map +1 -1
- package/dist/esm/config.js +1 -5
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/webpack/config.js +25 -9
- package/dist/esm/webpack/config.js.map +1 -1
- package/dist/esm/webpack/loaders/wasm.js +3 -1
- package/dist/esm/webpack/loaders/wasm.js.map +1 -1
- package/dist/esm/webpack/plugins.js +24 -23
- package/dist/esm/webpack/plugins.js.map +1 -1
- package/dist/esm/webpack/utils.js +81 -4
- package/dist/esm/webpack/utils.js.map +1 -1
- package/dist/types/webpack/plugins.d.ts +1 -1
- package/dist/types/webpack/utils.d.ts +30 -3
- package/package.json +6 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/webpack/config.ts"],"sourcesContent":["import SnapsWebpackPlugin from '@metamask/snaps-webpack-plugin';\nimport type { Ora } from 'ora';\nimport { resolve } from 'path';\nimport TerserPlugin from 'terser-webpack-plugin';\nimport type { Configuration } from 'webpack';\nimport { EnvironmentPlugin, ProgressPlugin, ProvidePlugin } from 'webpack';\n\nimport type { ProcessedWebpackConfig } from '../config';\nimport {\n SnapsBuiltInResolver,\n SnapsBundleWarningsPlugin,\n SnapsStatsPlugin,\n SnapsWatchPlugin,\n} from './plugins';\nimport {\n BROWSERSLIST_FILE,\n getDefaultLoader,\n getDevTool,\n getFallbacks,\n getProgressHandler,\n} from './utils';\n\nexport type WebpackOptions = {\n /**\n * Whether to watch for changes.\n */\n watch?: boolean;\n\n /**\n * Whether to evaluate the bundle. If this is set, it will override the\n * `evaluate` option in the config object.\n */\n evaluate?: boolean;\n\n /**\n * The spinner to use for logging.\n */\n spinner?: Ora;\n};\n\n/**\n * Get the default Webpack configuration. This is the configuration that will\n * be used if the user doesn't provide a custom Webpack configuration. The\n * configuration is based on the snap config.\n *\n * The default configuration includes:\n *\n * - `SWC` to transpile TypeScript and JavaScript files.\n * - `TerserPlugin` to minify the bundle.\n * - `SnapsWebpackPlugin` to validate the bundle and update the manifest.\n *\n * It can be customized through the `customizeWebpackConfig` function in the\n * snap config, but in most cases, you shouldn't need to do that.\n *\n * @param config - The processed snap Webpack config.\n * @param options - The Webpack options.\n * @returns The default Webpack configuration.\n */\nexport async function getDefaultConfiguration(\n config: ProcessedWebpackConfig,\n options: WebpackOptions = {\n evaluate: config.evaluate,\n },\n): Promise<Configuration> {\n const spinnerText = options.spinner?.text;\n const builtInResolver =\n config.stats.builtIns &&\n new SnapsBuiltInResolver(config.stats.builtIns, options.spinner);\n\n return {\n /**\n * The target is set to `browserslist` so that Webpack will compile the\n * bundle to support the browsers specified in the `.browserslistrc` file.\n * This Browserslist file contains the browsers that are supported by\n * MetaMask Snaps.\n *\n * @see https://webpack.js.org/configuration/target/\n */\n target: `browserslist:${BROWSERSLIST_FILE}`,\n\n /**\n * The mode is set to `production` by default, so that Webpack will minify\n * and optimize the bundle.\n *\n * @see https://webpack.js.org/configuration/mode/\n */\n mode: 'production',\n\n /**\n * The entry point is set to the `input` value from the config object.\n *\n * @see https://webpack.js.org/configuration/entry-context/\n */\n entry: config.input,\n\n /**\n * The devtool option controls how source maps are generated. We set it to\n * the `sourceMap` value from the config object.\n *\n * @see https://webpack.js.org/configuration/devtool/\n */\n devtool: getDevTool(config.sourceMap),\n\n /**\n * The stats option controls how much information is printed to the console\n * when Webpack is running. We set it to `none` so that we can control the\n * output ourselves.\n *\n * @see https://webpack.js.org/configuration/stats/\n */\n stats: 'none',\n\n /**\n * The output options.\n *\n * @see https://webpack.js.org/configuration/output/\n */\n output: {\n /**\n * This indicates whether Webpack should clear the output directory\n * before building. We set it to the `clean` value from the config\n * object.\n *\n * @see https://webpack.js.org/configuration/output/#outputclean\n */\n clean: config.output.clean,\n\n /**\n * The filename of the bundle. We set it to the `filename` value from\n * the config object.\n *\n * @see https://webpack.js.org/configuration/output/#outputfilename\n */\n filename: config.output.filename,\n\n /**\n * The path to the output directory. We set it to the `path` value from\n * the config object.\n *\n * @see https://webpack.js.org/configuration/output/#outputpath\n */\n path: config.output.path,\n\n /**\n * The public path of the bundle. We set it to `/` by default, so that\n * the bundle can be loaded from the root of the server.\n *\n * @see https://webpack.js.org/configuration/output/#outputpublicpath\n */\n publicPath: '/',\n\n /**\n * The library configuration. This tells Webpack how to export the\n * bundle.\n *\n * @see https://webpack.js.org/configuration/output/#outputlibrary\n */\n library: {\n /**\n * This tells Webpack to export the bundle as a CommonJS module. This\n * is necessary for MetaMask Snaps.\n *\n * @see https://webpack.js.org/configuration/output/#outputlibrarytarget\n */\n type: 'commonjs',\n },\n\n /**\n * The chunk format. This tells Webpack how to export chunks. This is\n * required because we use browserslist to target browsers, but Snaps are\n * not fully compatible with browser APIs (such as `window` and\n * `document`).\n *\n * @see https://webpack.js.org/configuration/output/#outputchunkformat\n */\n chunkFormat: 'commonjs',\n },\n\n /**\n * The module configuration. This is where we tell Webpack how to handle\n * different types of files.\n *\n * @see https://webpack.js.org/configuration/module/\n */\n module: {\n rules: [\n {\n test: /\\.[tj]sx?$/u,\n exclude: /node_modules/u,\n use: await getDefaultLoader(config),\n },\n\n /**\n * This allows importing modules that uses `.js` and not `.mjs` for the\n * ES build.\n *\n * @see https://webpack.js.org/configuration/module/#resolvefullyspecified\n */\n {\n test: /\\.m?js/u,\n resolve: {\n fullySpecified: false,\n },\n },\n\n config.experimental.wasm && {\n test: /\\.wasm$/u,\n use: {\n loader: resolve(__dirname, 'loaders', 'wasm'),\n },\n },\n ],\n },\n\n /**\n * The resolve configuration. This tells Webpack how to resolve imports.\n * We set it to resolve `.js` and `.ts` files.\n *\n * @see https://webpack.js.org/configuration/resolve/\n */\n resolve: {\n /**\n * The extensions to resolve. We set it to resolve `.js` and `.ts`\n * files.\n */\n extensions: ['.js', '.ts'],\n\n /**\n * The fallback options. This tells Webpack how to handle imports that\n * aren't resolved. By default, we set Node.js built-ins to `false`, so\n * that they are ignored.\n */\n fallback: getFallbacks(config.polyfills),\n\n /**\n * The plugins to use. We use the {@link SnapsBuiltInResolver} to show\n * warnings about using Node.js built-ins, when no fallback is specified.\n */\n plugins: [builtInResolver],\n },\n\n /**\n * The plugins to use.\n *\n * @see https://webpack.js.org/configuration/plugins/\n */\n plugins: [\n /**\n * The `SnapsWebpackPlugin` is a Webpack plugin that checks and updates\n * the manifest file, and evaluates the bundle in SES. While not strictly\n * required, it's highly recommended to use this plugin.\n */\n new SnapsWebpackPlugin({\n manifestPath: config.manifest.path,\n writeManifest: config.manifest.update,\n eval: !options.watch && options.evaluate,\n }),\n\n /**\n * The `SnapsStatsPlugin` is a Webpack plugin that handles the stats\n * output. It's used to show the stats in the terminal, in a format that\n * is easy to read.\n */\n new SnapsStatsPlugin({ verbose: config.stats.verbose }, options.spinner),\n\n /**\n * The `EnvironmentPlugin` is a Webpack plugin that adds environment\n * variables to the bundle. We use it to add the `NODE_ENV` and `DEBUG`\n * environment variables.\n */\n new EnvironmentPlugin(config.environment),\n\n /**\n * The `ProgressPlugin` is a Webpack plugin that logs the progress of\n * the build. We set it to log the progress to the spinner.\n */\n new ProgressPlugin({\n handler: getProgressHandler(options.spinner, spinnerText),\n }),\n\n /**\n * The `SnapsBundleWarningPlugin` is a Webpack plugin that shows a\n * warning when the bundle is potentially incompatible with MetaMask\n * Snaps.\n */\n new SnapsBundleWarningsPlugin(\n {\n builtInResolver,\n builtIns: Boolean(config.stats.builtIns),\n buffer: config.stats.buffer,\n },\n options.spinner,\n ),\n\n /**\n * The `WatchPlugin` is a Webpack plugin that adds extra files to watch\n * for changes. This is useful for rebuilding the bundle when the\n * manifest file changes.\n */\n options.watch &&\n new SnapsWatchPlugin(\n {\n bundle: resolve(config.output.path, config.output.filename),\n evaluate: options.evaluate,\n files: [config.manifest.path],\n },\n options.spinner,\n ),\n\n /**\n * The `ProviderPlugin` is a Webpack plugin that automatically load\n * modules instead of having to import or require them everywhere.\n */\n (config.polyfills === true ||\n (config.polyfills !== false && config.polyfills.buffer)) &&\n new ProvidePlugin({\n Buffer: ['buffer', 'Buffer'],\n }),\n ].filter(Boolean),\n\n /**\n * The optimization configuration. This tells Webpack how to optimize the\n * bundle. Most of the time, you won't need to change this, as the default\n * options set by the `mode` option are sufficient.\n */\n optimization: {\n minimize: config.output.minimize,\n\n /**\n * The minimizer to use. We set it to use the `TerserPlugin`.\n */\n minimizer: [\n new TerserPlugin({\n parallel: true,\n }),\n ],\n },\n\n /**\n * The infrastructure logging configuration. This tells Webpack how to\n * log messages.\n *\n * @see https://webpack.js.org/configuration/infrastructure-logging\n */\n infrastructureLogging: {\n /**\n * The level of logging to use. We set it to `none`, so that we can\n * control the output ourselves.\n */\n level: 'none',\n },\n };\n}\n"],"names":["SnapsWebpackPlugin","resolve","TerserPlugin","EnvironmentPlugin","ProgressPlugin","ProvidePlugin","SnapsBuiltInResolver","SnapsBundleWarningsPlugin","SnapsStatsPlugin","SnapsWatchPlugin","BROWSERSLIST_FILE","getDefaultLoader","getDevTool","getFallbacks","getProgressHandler","getDefaultConfiguration","config","options","evaluate","spinnerText","spinner","text","builtInResolver","stats","builtIns","target","mode","entry","input","devtool","sourceMap","output","clean","filename","path","publicPath","library","type","chunkFormat","module","rules","test","exclude","use","fullySpecified","experimental","wasm","loader","__dirname","extensions","fallback","polyfills","plugins","manifestPath","manifest","writeManifest","update","eval","watch","verbose","environment","handler","Boolean","buffer","bundle","files","Buffer","filter","optimization","minimize","minimizer","parallel","infrastructureLogging","level"],"mappings":"AAA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Df,UAAUD,QAAQC,QAAQ;gBAC1B+C,OAAO;oBAACjD,OAAOsC,QAAQ,CAACpB,IAAI;iBAAC;YAC/B,GACAjB,QAAQG,OAAO;YAGnB;;;OAGC,GACAJ,CAAAA,OAAOmC,SAAS,KAAK,QACnBnC,OAAOmC,SAAS,KAAK,SAASnC,OAAOmC,SAAS,CAACY,MAAM,KACtD,IAAI1D,cAAc;gBAChB6D,QAAQ;oBAAC;oBAAU;iBAAS;YAC9B;SACH,CAACC,MAAM,CAACL;QAET;;;;KAIC,GACDM,cAAc;YACZC,UAAUrD,OAAOe,MAAM,CAACsC,QAAQ;YAEhC;;OAEC,GACDC,WAAW;gBACT,IAAIpE,aAAa;oBACfqE,UAAU;gBACZ;aACD;QACH;QAEA;;;;;KAKC,GACDC,uBAAuB;YACrB;;;OAGC,GACDC,OAAO;QACT;IACF;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../src/webpack/config.ts"],"sourcesContent":["import SnapsWebpackPlugin from '@metamask/snaps-webpack-plugin';\nimport type { Ora } from 'ora';\nimport { resolve } from 'path';\nimport TerserPlugin from 'terser-webpack-plugin';\nimport type { Configuration } from 'webpack';\nimport { DefinePlugin, ProgressPlugin, ProvidePlugin } from 'webpack';\n\nimport type { ProcessedWebpackConfig } from '../config';\nimport {\n SnapsBuiltInResolver,\n SnapsBundleWarningsPlugin,\n SnapsStatsPlugin,\n SnapsWatchPlugin,\n} from './plugins';\nimport {\n BROWSERSLIST_FILE,\n getDefaultLoader,\n getDevTool,\n getEnvironmentVariables,\n getFallbacks,\n getProgressHandler,\n} from './utils';\n\nexport type WebpackOptions = {\n /**\n * Whether to watch for changes.\n */\n watch?: boolean;\n\n /**\n * Whether to evaluate the bundle. If this is set, it will override the\n * `evaluate` option in the config object.\n */\n evaluate?: boolean;\n\n /**\n * The spinner to use for logging.\n */\n spinner?: Ora;\n};\n\n/**\n * Get the default Webpack configuration. This is the configuration that will\n * be used if the user doesn't provide a custom Webpack configuration. The\n * configuration is based on the snap config.\n *\n * The default configuration includes:\n *\n * - `SWC` to transpile TypeScript and JavaScript files.\n * - `TerserPlugin` to minify the bundle.\n * - `SnapsWebpackPlugin` to validate the bundle and update the manifest.\n *\n * It can be customized through the `customizeWebpackConfig` function in the\n * snap config, but in most cases, you shouldn't need to do that.\n *\n * @param config - The processed snap Webpack config.\n * @param options - The Webpack options.\n * @returns The default Webpack configuration.\n */\nexport async function getDefaultConfiguration(\n config: ProcessedWebpackConfig,\n options: WebpackOptions = {\n evaluate: config.evaluate,\n },\n): Promise<Configuration> {\n const spinnerText = options.spinner?.text;\n const builtInResolver =\n config.stats.builtIns &&\n new SnapsBuiltInResolver(config.stats.builtIns, options.spinner);\n\n return {\n /**\n * The target is set to `browserslist` so that Webpack will compile the\n * bundle to support the browsers specified in the `.browserslistrc` file.\n * This Browserslist file contains the browsers that are supported by\n * MetaMask Snaps.\n *\n * @see https://webpack.js.org/configuration/target/\n */\n target: `browserslist:${BROWSERSLIST_FILE}`,\n\n /**\n * The mode is set to `production` by default, so that Webpack will minify\n * and optimize the bundle.\n *\n * @see https://webpack.js.org/configuration/mode/\n */\n mode: 'production',\n\n /**\n * The entry point is set to the `input` value from the config object.\n *\n * @see https://webpack.js.org/configuration/entry-context/\n */\n entry: config.input,\n\n /**\n * The devtool option controls how source maps are generated. We set it to\n * the `sourceMap` value from the config object.\n *\n * @see https://webpack.js.org/configuration/devtool/\n */\n devtool: getDevTool(config.sourceMap),\n\n /**\n * The stats option controls how much information is printed to the console\n * when Webpack is running. We set it to `none` so that we can control the\n * output ourselves.\n *\n * @see https://webpack.js.org/configuration/stats/\n */\n stats: 'none',\n\n /**\n * The output options.\n *\n * @see https://webpack.js.org/configuration/output/\n */\n output: {\n /**\n * This indicates whether Webpack should clear the output directory\n * before building. We set it to the `clean` value from the config\n * object.\n *\n * @see https://webpack.js.org/configuration/output/#outputclean\n */\n clean: config.output.clean,\n\n /**\n * The filename of the bundle. We set it to the `filename` value from\n * the config object.\n *\n * @see https://webpack.js.org/configuration/output/#outputfilename\n */\n filename: config.output.filename,\n\n /**\n * The path to the output directory. We set it to the `path` value from\n * the config object.\n *\n * @see https://webpack.js.org/configuration/output/#outputpath\n */\n path: config.output.path,\n\n /**\n * The public path of the bundle. We set it to `/` by default, so that\n * the bundle can be loaded from the root of the server.\n *\n * @see https://webpack.js.org/configuration/output/#outputpublicpath\n */\n publicPath: '/',\n\n /**\n * The library configuration. This tells Webpack how to export the\n * bundle.\n *\n * @see https://webpack.js.org/configuration/output/#outputlibrary\n */\n library: {\n /**\n * This tells Webpack to export the bundle as a CommonJS module. This\n * is necessary for MetaMask Snaps.\n *\n * @see https://webpack.js.org/configuration/output/#outputlibrarytarget\n */\n type: 'commonjs',\n },\n\n /**\n * The chunk format. This tells Webpack how to export chunks. This is\n * required because we use browserslist to target browsers, but Snaps are\n * not fully compatible with browser APIs (such as `window` and\n * `document`).\n *\n * @see https://webpack.js.org/configuration/output/#outputchunkformat\n */\n chunkFormat: 'commonjs',\n },\n\n /**\n * The module configuration. This is where we tell Webpack how to handle\n * different types of files.\n *\n * @see https://webpack.js.org/configuration/module/\n */\n module: {\n rules: [\n {\n test: /\\.(js|mjs|cjs|ts)$/u,\n exclude: /node_modules/u,\n use: await getDefaultLoader(config),\n },\n\n /**\n * This allows importing modules that uses `.js` and not `.mjs` for the\n * ES build.\n *\n * @see https://webpack.js.org/configuration/module/#resolvefullyspecified\n */\n {\n test: /\\.m?js/u,\n resolve: {\n fullySpecified: false,\n },\n },\n\n config.experimental.wasm && {\n test: /\\.wasm$/u,\n use: {\n loader: resolve(__dirname, 'loaders', 'wasm'),\n },\n },\n ],\n },\n\n /**\n * The resolve configuration. This tells Webpack how to resolve imports.\n * We set it to resolve `.js` and `.ts` files.\n *\n * @see https://webpack.js.org/configuration/resolve/\n */\n resolve: {\n /**\n * The extensions to resolve. We set it to resolve `.(c|m)?js` and `.ts`\n * files.\n */\n extensions: ['.js', '.mjs', '.cjs', '.ts'],\n\n /**\n * The fallback options. This tells Webpack how to handle imports that\n * aren't resolved. By default, we set Node.js built-ins to `false`, so\n * that they are ignored.\n */\n fallback: getFallbacks(config.polyfills),\n\n /**\n * The plugins to use. We use the {@link SnapsBuiltInResolver} to show\n * warnings about using Node.js built-ins, when no fallback is specified.\n */\n plugins: [builtInResolver],\n },\n\n /**\n * The plugins to use.\n *\n * @see https://webpack.js.org/configuration/plugins/\n */\n plugins: [\n /**\n * The `SnapsWebpackPlugin` is a Webpack plugin that checks and updates\n * the manifest file, and evaluates the bundle in SES. While not strictly\n * required, it's highly recommended to use this plugin.\n */\n new SnapsWebpackPlugin({\n manifestPath: config.manifest.path,\n writeManifest: config.manifest.update,\n eval: !options.watch && options.evaluate,\n }),\n\n /**\n * The `SnapsStatsPlugin` is a Webpack plugin that handles the stats\n * output. It's used to show the stats in the terminal, in a format that\n * is easy to read.\n */\n new SnapsStatsPlugin({ verbose: config.stats.verbose }, options.spinner),\n\n /**\n * The `DefinePlugin` is a Webpack plugin that adds static values to the\n * bundle. We use it to add the `NODE_DEBUG`, `NODE_ENV`, and `DEBUG`\n * environment variables, as well as any custom environment\n * variables (as `process.env`).\n */\n new DefinePlugin(getEnvironmentVariables(config.environment)),\n\n /**\n * The `ProgressPlugin` is a Webpack plugin that logs the progress of\n * the build. We set it to log the progress to the spinner.\n */\n new ProgressPlugin({\n handler: getProgressHandler(options.spinner, spinnerText),\n }),\n\n /**\n * The `SnapsBundleWarningPlugin` is a Webpack plugin that shows a\n * warning when the bundle is potentially incompatible with MetaMask\n * Snaps.\n */\n new SnapsBundleWarningsPlugin({\n builtInResolver,\n builtIns: Boolean(config.stats.builtIns),\n buffer: config.stats.buffer,\n }),\n\n /**\n * The `WatchPlugin` is a Webpack plugin that adds extra files to watch\n * for changes. This is useful for rebuilding the bundle when the\n * manifest file changes.\n */\n options.watch &&\n new SnapsWatchPlugin(\n {\n bundle: resolve(config.output.path, config.output.filename),\n evaluate: options.evaluate,\n files: [config.manifest.path],\n },\n options.spinner,\n ),\n\n /**\n * The `ProviderPlugin` is a Webpack plugin that automatically load\n * modules instead of having to import or require them everywhere.\n */\n (config.polyfills === true ||\n (config.polyfills !== false && config.polyfills.buffer)) &&\n new ProvidePlugin({\n Buffer: ['buffer', 'Buffer'],\n }),\n ].filter(Boolean),\n\n /**\n * The optimization configuration. This tells Webpack how to optimize the\n * bundle. Most of the time, you won't need to change this, as the default\n * options set by the `mode` option are sufficient.\n */\n optimization: {\n minimize: config.output.minimize,\n\n /**\n * The minimizer to use. We set it to use the `TerserPlugin`.\n */\n minimizer: [\n new TerserPlugin({\n parallel: true,\n }),\n ],\n },\n\n /**\n * The performance configuration. This tells Webpack how to handle\n * performance hints.\n *\n * @see https://webpack.js.org/configuration/performance/\n */\n performance: {\n /**\n * The hints to show. We set it to `false`, so that we don't get\n * performance hints, as they are not relevant for Snaps.\n *\n * @see https://webpack.js.org/configuration/performance/#performancehints\n */\n hints: false,\n },\n\n /**\n * The infrastructure logging configuration. This tells Webpack how to\n * log messages.\n *\n * @see https://webpack.js.org/configuration/infrastructure-logging\n */\n infrastructureLogging: {\n /**\n * The level of logging to use. We set it to `none`, so that we can\n * control the output ourselves.\n */\n level: 'none',\n },\n };\n}\n"],"names":["SnapsWebpackPlugin","resolve","TerserPlugin","DefinePlugin","ProgressPlugin","ProvidePlugin","SnapsBuiltInResolver","SnapsBundleWarningsPlugin","SnapsStatsPlugin","SnapsWatchPlugin","BROWSERSLIST_FILE","getDefaultLoader","getDevTool","getEnvironmentVariables","getFallbacks","getProgressHandler","getDefaultConfiguration","config","options","evaluate","spinnerText","spinner","text","builtInResolver","stats","builtIns","target","mode","entry","input","devtool","sourceMap","output","clean","filename","path","publicPath","library","type","chunkFormat","module","rules","test","exclude","use","fullySpecified","experimental","wasm","loader","__dirname","extensions","fallback","polyfills","plugins","manifestPath","manifest","writeManifest","update","eval","watch","verbose","environment","handler","Boolean","buffer","bundle","files","Buffer","filter","optimization","minimize","minimizer","parallel","performance","hints","infrastructureLogging","level"],"mappings":"AAAA,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M,CAACE,QAAQ;gBAC1Df,UAAUD,QAAQC,QAAQ;gBAC1B+C,OAAO;oBAACjD,OAAOsC,QAAQ,CAACpB,IAAI;iBAAC;YAC/B,GACAjB,QAAQG,OAAO;YAGnB;;;OAGC,GACAJ,CAAAA,OAAOmC,SAAS,KAAK,QACnBnC,OAAOmC,SAAS,KAAK,SAASnC,OAAOmC,SAAS,CAACY,MAAM,KACtD,IAAI3D,cAAc;gBAChB8D,QAAQ;oBAAC;oBAAU;iBAAS;YAC9B;SACH,CAACC,MAAM,CAACL;QAET;;;;KAIC,GACDM,cAAc;YACZC,UAAUrD,OAAOe,MAAM,CAACsC,QAAQ;YAEhC;;OAEC,GACDC,WAAW;gBACT,IAAIrE,aAAa;oBACfsE,UAAU;gBACZ;aACD;QACH;QAEA;;;;;KAKC,GACDC,aAAa;YACX;;;;;OAKC,GACDC,OAAO;QACT;QAEA;;;;;KAKC,GACDC,uBAAuB;YACrB;;;OAGC,GACDC,OAAO;QACT;IACF;AACF"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-restricted-globals */ import { assert } from '@metamask/utils';
|
|
2
|
+
import { dirname, resolve } from 'path';
|
|
2
3
|
/**
|
|
3
4
|
* Get the imports code for the WASM module. This code imports each of the
|
|
4
5
|
* imports from the WASM module.
|
|
@@ -60,8 +61,9 @@ const loader = async function loader(source) {
|
|
|
60
61
|
}, {});
|
|
61
62
|
// Add the WASM import as a dependency so that Webpack will watch it for
|
|
62
63
|
// changes.
|
|
64
|
+
const path = dirname(this.resourcePath);
|
|
63
65
|
for (const name of Object.keys(imports)){
|
|
64
|
-
this.addDependency(name);
|
|
66
|
+
this.addDependency(resolve(path, name));
|
|
65
67
|
}
|
|
66
68
|
return `
|
|
67
69
|
${getImports(imports)}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/webpack/loaders/wasm.ts"],"sourcesContent":["/* eslint-disable no-restricted-globals */\n\nimport { assert } from '@metamask/utils';\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 bytes = new Uint8Array(source);\n const wasmModule = await WebAssembly.compile(bytes);\n\n // eslint-disable-next-line @typescript-eslint/no-shadow\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 for (const name of Object.keys(imports)) {\n this.addDependency(name);\n }\n\n return `\n ${getImports(imports)}\n\n const bytes = new Uint8Array(${JSON.stringify(Array.from(source))});\n const module = new WebAssembly.Module(bytes);\n const instance = new WebAssembly.Instance(module, {\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"],"names":["assert","getImports","importMap","Object","entries","map","moduleName","exportNames","join","JSON","stringify","getModuleImports","getExports","descriptors","descriptor","name","loader","source","Uint8Array","bytes","wasmModule","WebAssembly","compile","exports","Module","imports","reduce","target","module","push","keys","addDependency","Array","from","raw"],"mappings":"AAAA,wCAAwC,GAExC,SAASA,MAAM,QAAQ,kBAAkB;
|
|
1
|
+
{"version":3,"sources":["../../../../src/webpack/loaders/wasm.ts"],"sourcesContent":["/* eslint-disable no-restricted-globals */\n\nimport { assert } 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 bytes = new Uint8Array(source);\n const wasmModule = await WebAssembly.compile(bytes);\n\n // eslint-disable-next-line @typescript-eslint/no-shadow\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 bytes = new Uint8Array(${JSON.stringify(Array.from(source))});\n const module = new WebAssembly.Module(bytes);\n const instance = new WebAssembly.Instance(module, {\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"],"names":["assert","dirname","resolve","getImports","importMap","Object","entries","map","moduleName","exportNames","join","JSON","stringify","getModuleImports","getExports","descriptors","descriptor","name","loader","source","Uint8Array","bytes","wasmModule","WebAssembly","compile","exports","Module","imports","reduce","target","module","push","path","resourcePath","keys","addDependency","Array","from","raw"],"mappings":"AAAA,wCAAwC,GAExC,SAASA,MAAM,QAAQ,kBAAkB;AACzC,SAASC,OAAO,EAAEC,OAAO,QAAQ,OAAO;AAGxC;;;;;;CAMC,GACD,OAAO,SAASC,WAAWC,SAAmC;IAC5D,OAAOC,OAAOC,OAAO,CAACF,WACnBG,GAAG,CACF,CAAC,CAACC,YAAYC,YAAY,GACxB,CAAC,SAAS,EAAEA,YAAYC,IAAI,CAAC,MAAM,QAAQ,EAAEC,KAAKC,SAAS,CACzDJ,YACA,CAAC,CAAC,EAEPE,IAAI,CAAC;AACV;AAEA;;;;;;CAMC,GACD,OAAO,SAASG,iBAAiBT,SAAmC;IAClE,OAAOC,OAAOC,OAAO,CAACF,WACnBG,GAAG,CACF,CAAC,CAACC,YAAYC,YAAY,GACxB,CAAC,EAAEE,KAAKC,SAAS,CAACJ,YAAY,IAAI,EAAEC,YAAYC,IAAI,CAAC,MAAM,GAAG,CAAC,EAElEA,IAAI,CAAC;AACV;AAEA;;;;;;;CAOC,GACD,OAAO,SAASI,WAAWC,WAAiD;IAC1E,OAAOA,YACJR,GAAG,CAAC,CAACS;QACJ,IAAIA,WAAWC,IAAI,KAAK,WAAW;YACjC,OAAO,CAAC,uBAAuB,EA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AO,MAAMoB,MAAM,KAAK"}
|
|
@@ -61,17 +61,18 @@ function _define_property(obj, key, value) {
|
|
|
61
61
|
}
|
|
62
62
|
return obj;
|
|
63
63
|
}
|
|
64
|
-
import { indent } from '@metamask/snaps-utils';
|
|
65
64
|
import { assert, hasProperty, isObject } from '@metamask/utils';
|
|
66
|
-
import { dim, red, yellow } from 'chalk';
|
|
65
|
+
import { bold, dim, red, yellow } from 'chalk';
|
|
67
66
|
import { isBuiltin } from 'module';
|
|
67
|
+
import { WebpackError } from 'webpack';
|
|
68
68
|
import { evaluate } from '../commands/eval';
|
|
69
69
|
import { error, getErrorMessage, info, warn } from '../utils';
|
|
70
|
-
import { pluralize } from './utils';
|
|
70
|
+
import { formatText, pluralize } from './utils';
|
|
71
71
|
var _spinner = /*#__PURE__*/ new WeakMap(), /**
|
|
72
72
|
* Get the error message for the given stats error.
|
|
73
73
|
*
|
|
74
74
|
* @param statsError - The stats error.
|
|
75
|
+
* @param color - The color to use for the error message.
|
|
75
76
|
* @returns The error message.
|
|
76
77
|
*/ _getStatsErrorMessage = /*#__PURE__*/ new WeakSet();
|
|
77
78
|
/**
|
|
@@ -87,17 +88,22 @@ var _spinner = /*#__PURE__*/ new WeakMap(), /**
|
|
|
87
88
|
if (!stats) {
|
|
88
89
|
return;
|
|
89
90
|
}
|
|
90
|
-
const { modules, time, errors } = stats.toJson();
|
|
91
|
+
const { modules, time, errors, warnings } = stats.toJson();
|
|
91
92
|
assert(modules, 'Modules must be defined in stats.');
|
|
92
93
|
assert(time, 'Time must be defined in stats.');
|
|
93
94
|
if (errors?.length) {
|
|
94
|
-
const formattedErrors = errors.map(_class_private_method_get(this, _getStatsErrorMessage, getStatsErrorMessage).
|
|
95
|
+
const formattedErrors = errors.map((statsError)=>_class_private_method_get(this, _getStatsErrorMessage, getStatsErrorMessage).call(this, statsError)).join('\n\n');
|
|
95
96
|
error(`Compiled ${modules.length} ${pluralize(modules.length, 'file')} in ${time}ms with ${errors.length} ${pluralize(errors.length, 'error')}.\n\n${formattedErrors}\n`, _class_private_field_get(this, _spinner));
|
|
96
97
|
_class_private_field_get(this, _spinner)?.stop();
|
|
97
98
|
process.exitCode = 1;
|
|
98
99
|
return;
|
|
99
100
|
}
|
|
100
|
-
|
|
101
|
+
if (warnings?.length) {
|
|
102
|
+
const formattedWarnings = warnings.map((statsWarning)=>_class_private_method_get(this, _getStatsErrorMessage, getStatsErrorMessage).call(this, statsWarning, yellow)).join('\n\n');
|
|
103
|
+
warn(`Compiled ${modules.length} ${pluralize(modules.length, 'file')} in ${time}ms with ${warnings.length} ${pluralize(warnings.length, 'warning')}.\n\n${formattedWarnings}\n`, _class_private_field_get(this, _spinner));
|
|
104
|
+
} else {
|
|
105
|
+
info(`Compiled ${modules.length} ${pluralize(modules.length, 'file')} in ${time}ms.`, _class_private_field_get(this, _spinner));
|
|
106
|
+
}
|
|
101
107
|
if (compiler.watchMode) {
|
|
102
108
|
// The spinner may be restarted by the watch plugin, outside of the
|
|
103
109
|
// `executeSteps` flow, so we stop it here just in case.
|
|
@@ -122,13 +128,13 @@ var _spinner = /*#__PURE__*/ new WeakMap(), /**
|
|
|
122
128
|
_class_private_field_set(this, _spinner, spinner);
|
|
123
129
|
}
|
|
124
130
|
}
|
|
125
|
-
function getStatsErrorMessage(statsError) {
|
|
131
|
+
function getStatsErrorMessage(statsError, color = red) {
|
|
126
132
|
const baseMessage = this.options.verbose ? getErrorMessage(statsError) : statsError.message;
|
|
127
133
|
const [first, ...rest] = baseMessage.split('\n');
|
|
128
134
|
return [
|
|
129
|
-
|
|
130
|
-
...rest.map((message)=>
|
|
131
|
-
statsError.details &&
|
|
135
|
+
color(formatText(`• ${first}`, 4, 2)),
|
|
136
|
+
...rest.map((message)=>formatText(color(message), 4)),
|
|
137
|
+
statsError.details && `\n${formatText(dim(statsError.details), 6)}`
|
|
132
138
|
].filter(Boolean).join('\n');
|
|
133
139
|
}
|
|
134
140
|
var _spinner1 = /*#__PURE__*/ new WeakMap(), _safeEvaluate = /*#__PURE__*/ new WeakSet();
|
|
@@ -234,7 +240,7 @@ var _source = /*#__PURE__*/ new WeakMap(), _spinner2 = /*#__PURE__*/ new WeakMap
|
|
|
234
240
|
_class_private_field_set(this, _spinner2, spinner);
|
|
235
241
|
}
|
|
236
242
|
}
|
|
237
|
-
var
|
|
243
|
+
var /**
|
|
238
244
|
* Check if a built-in module is used, but not provided by Webpack's
|
|
239
245
|
* `fallback` configuration.
|
|
240
246
|
*
|
|
@@ -285,25 +291,18 @@ var _spinner3 = /*#__PURE__*/ new WeakMap(), /**
|
|
|
285
291
|
constructor(options = {
|
|
286
292
|
buffer: true,
|
|
287
293
|
builtIns: true
|
|
288
|
-
}
|
|
294
|
+
}){
|
|
289
295
|
_class_private_method_init(this, _checkBuiltIns);
|
|
290
296
|
_class_private_method_init(this, _isProvidePlugin);
|
|
291
297
|
_class_private_method_init(this, _checkBuffer);
|
|
292
298
|
/**
|
|
293
|
-
* The spinner to use for logging.
|
|
294
|
-
*/ _class_private_field_init(this, _spinner3, {
|
|
295
|
-
writable: true,
|
|
296
|
-
value: void 0
|
|
297
|
-
});
|
|
298
|
-
/**
|
|
299
299
|
* The options for the plugin.
|
|
300
300
|
*/ _define_property(this, "options", void 0);
|
|
301
301
|
this.options = options;
|
|
302
|
-
_class_private_field_set(this, _spinner3, spinner);
|
|
303
302
|
}
|
|
304
303
|
}
|
|
305
304
|
function checkBuiltIns(compiler) {
|
|
306
|
-
compiler.hooks.afterCompile.tap(this.constructor.name, ()=>{
|
|
305
|
+
compiler.hooks.afterCompile.tap(this.constructor.name, (compilation)=>{
|
|
307
306
|
if (!this.options.builtInResolver) {
|
|
308
307
|
return;
|
|
309
308
|
}
|
|
@@ -311,8 +310,10 @@ function checkBuiltIns(compiler) {
|
|
|
311
310
|
if (unresolvedModules.size === 0) {
|
|
312
311
|
return;
|
|
313
312
|
}
|
|
314
|
-
const formattedModules = new Array(...unresolvedModules).map((name)
|
|
315
|
-
|
|
313
|
+
const formattedModules = new Array(...unresolvedModules).map((name)=>`• ${name}`).join('\n');
|
|
314
|
+
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.`);
|
|
315
|
+
webpackError.details = formattedModules;
|
|
316
|
+
compilation.warnings.push(webpackError);
|
|
316
317
|
});
|
|
317
318
|
}
|
|
318
319
|
function isProvidePlugin(instance) {
|
|
@@ -335,7 +336,7 @@ function checkBuffer(compiler) {
|
|
|
335
336
|
if (bufferAssets.length === 0) {
|
|
336
337
|
return;
|
|
337
338
|
}
|
|
338
|
-
|
|
339
|
+
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.`));
|
|
339
340
|
});
|
|
340
341
|
});
|
|
341
342
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/webpack/plugins.ts"],"sourcesContent":["import { indent } from '@metamask/snaps-utils';\nimport { assert, hasProperty, isObject } from '@metamask/utils';\nimport { dim, red, yellow } from 'chalk';\nimport { isBuiltin } from 'module';\nimport type { Ora } from 'ora';\nimport type {\n Compiler,\n ProvidePlugin,\n ResolvePluginInstance,\n Resolver,\n StatsError,\n WebpackPluginInstance,\n} from 'webpack';\n\nimport { evaluate } from '../commands/eval';\nimport { error, getErrorMessage, info, warn } from '../utils';\nimport { pluralize } from './utils';\n\nexport type SnapsStatsPluginOptions = {\n /**\n * Whether to log the verbose stats.\n */\n verbose?: boolean;\n};\n\n/**\n * A plugin that logs the stats after compilation. This is useful for logging\n * the number of files compiled, and the time taken to compile them.\n */\nexport class SnapsStatsPlugin implements WebpackPluginInstance {\n /**\n * The options for the plugin.\n */\n readonly options: SnapsStatsPluginOptions;\n\n /**\n * The spinner to use for logging.\n */\n readonly #spinner?: Ora;\n\n constructor(\n options: SnapsStatsPluginOptions = {\n verbose: false,\n },\n spinner?: Ora,\n ) {\n this.options = options;\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n compiler.hooks.afterDone.tap(this.constructor.name, (stats) => {\n if (!stats) {\n return;\n }\n\n const { modules, time, errors } = stats.toJson();\n\n assert(modules, 'Modules must be defined in stats.');\n assert(time, 'Time must be defined in stats.');\n\n if (errors?.length) {\n const formattedErrors = errors\n .map(this.#getStatsErrorMessage.bind(this))\n .join('\\n\\n');\n\n error(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms with ${errors.length} ${pluralize(\n errors.length,\n 'error',\n )}.\\n\\n${formattedErrors}\\n`,\n this.#spinner,\n );\n\n this.#spinner?.stop();\n\n process.exitCode = 1;\n return;\n }\n\n info(\n `Compiled ${modules.length} ${pluralize(\n modules.length,\n 'file',\n )} in ${time}ms.`,\n this.#spinner,\n );\n\n if (compiler.watchMode) {\n // The spinner may be restarted by the watch plugin, outside of the\n // `executeSteps` flow, so we stop it here just in case.\n this.#spinner?.succeed('Done!');\n }\n });\n }\n\n /**\n * Get the error message for the given stats error.\n *\n * @param statsError - The stats error.\n * @returns The error message.\n */\n #getStatsErrorMessage(statsError: StatsError) {\n const baseMessage = this.options.verbose\n ? getErrorMessage(statsError)\n : statsError.message;\n\n const [first, ...rest] = baseMessage.split('\\n');\n\n return [\n indent(red(`• ${first}`), 2),\n ...rest.map((message) => indent(red(message), 4)),\n statsError.details && indent(dim(statsError.details), 4),\n ]\n .filter(Boolean)\n .join('\\n');\n }\n}\n\n/**\n * The options for the {@link SnapsWatchPlugin}.\n */\nexport type SnapsWatchPluginOptions = {\n /**\n * The bundle path. This is the file that will be evaluated, if the `evaluate`\n * option is set.\n */\n bundle?: string;\n\n /**\n * Whether to evaluate the bundle. This only applies if the `bundle` option is\n * set.\n */\n evaluate?: boolean;\n\n /**\n * The extra files to watch.\n */\n files?: string[];\n};\n\n/**\n * A plugin that adds extra files to watch. This is useful for watching files\n * that are not imported by the entry point, such as the `snap.manifest.json`\n * file.\n */\nexport class SnapsWatchPlugin implements WebpackPluginInstance {\n /**\n * The options for the plugin.\n */\n readonly options: SnapsWatchPluginOptions;\n\n /**\n * The spinner to use for logging.\n */\n readonly #spinner?: Ora;\n\n constructor(options: SnapsWatchPluginOptions, spinner?: Ora) {\n this.options = options;\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n compiler.hooks.invalid.tap(this.constructor.name, (file) => {\n this.#spinner?.start();\n info(`Changes detected in ${yellow(file)}, recompiling.`, this.#spinner);\n });\n\n compiler.hooks.afterEmit.tapPromise(\n this.constructor.name,\n async ({ fileDependencies }) => {\n this.options.files?.forEach(\n fileDependencies.add.bind(fileDependencies),\n );\n\n if (this.options.bundle && this.options.evaluate) {\n await this.#safeEvaluate(this.options.bundle);\n }\n },\n );\n }\n\n /**\n * Safely evaluate the bundle at the given path. If an error occurs, it will\n * be logged to the console, rather than throwing an error.\n *\n * This function should never throw an error.\n *\n * @param bundlePath - The path to the bundle.\n */\n async #safeEvaluate(bundlePath: string) {\n try {\n await evaluate(bundlePath);\n info(`Snap bundle evaluated successfully.`, this.#spinner);\n } catch (evaluateError) {\n error(evaluateError.message, this.#spinner);\n }\n }\n}\n\n/**\n * The options for the {@link SnapsBuiltInResolver}.\n */\nexport type SnapsBuiltInResolverOptions = {\n /**\n * The built-in modules to ignore.\n */\n ignore?: string[];\n};\n\n/**\n * A plugin that logs a message when a built-in module is not resolved. The\n * MetaMask Snaps CLI does not support built-in modules by default, and this\n * plugin is used to warn the user when they try to import a built-in module,\n * when no fallback is configured.\n */\nexport class SnapsBuiltInResolver implements ResolvePluginInstance {\n /**\n * The built-in modules that have been imported, but not resolved.\n */\n readonly unresolvedModules = new Set<string>();\n\n /**\n * The name of the resolver hook to tap into.\n */\n readonly #source = 'described-resolve';\n\n /**\n * The options for the plugin.\n */\n readonly options: SnapsBuiltInResolverOptions;\n\n /**\n * The spinner to use for logging.\n */\n readonly #spinner?: Ora;\n\n constructor(\n options: SnapsBuiltInResolverOptions = {\n ignore: [],\n },\n spinner?: Ora,\n ) {\n this.options = options;\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack resolver.\n *\n * @param resolver - The Webpack resolver.\n */\n apply(resolver: Resolver) {\n resolver\n .getHook(this.#source)\n .tapAsync(\n this.constructor.name,\n ({ module: isModule, request }, _, callback) => {\n if (!isModule || !request) {\n return callback();\n }\n\n const baseRequest = request.split('/')[0];\n if (\n isBuiltin(baseRequest) &&\n !this.options.ignore?.includes(baseRequest)\n ) {\n const fallback = resolver.options.fallback.find(\n ({ name }) => name === baseRequest,\n );\n\n if (fallback && !fallback.alias) {\n this.unresolvedModules.add(baseRequest);\n }\n }\n\n return callback();\n },\n );\n }\n}\n\n/**\n * The options for the {@link SnapsBundleWarningsPlugin}.\n */\nexport type SnapsBundleWarningsPluginOptions = {\n /**\n * The {@link SnapsBuiltInResolver} instance to use for detecting built-in\n * modules.\n */\n builtInResolver?: SnapsBuiltInResolver | false;\n\n /**\n * Whether to show warnings if built-in modules are used, but not provided by\n * Webpack's `fallback` configuration.\n */\n builtIns?: boolean;\n\n /**\n * Whether to show warnings if the `Buffer` global is used, but not provided\n * by Webpack's `DefinePlugin`.\n */\n buffer?: boolean;\n};\n\n/**\n * A plugin that logs a message when:\n *\n * - A built-in module is not resolved. The MetaMask Snaps CLI does not support\n * built-in modules by default, and this plugin is used to warn the user when\n * they try to import a built-in module, when no fallback is configured.\n * - A snap uses the `Buffer` global. The MetaMask Snaps CLI does not support\n * the `Buffer` global by default, and this plugin is used to warn the user when\n * they try to use the `Buffer` global.\n *\n * We use both a resolver and a plugin, because the resolver is used to detect\n * when a built-in module is imported, and the plugin is used to log a single\n * message when the compilation is complete. We can't do everything in a single\n * plugin, because the resolver doesn't have access to the compilation, and the\n * plugin doesn't have access to the resolver.\n */\n\nexport class SnapsBundleWarningsPlugin implements WebpackPluginInstance {\n /**\n * The spinner to use for logging.\n */\n readonly #spinner?: Ora;\n\n /**\n * The options for the plugin.\n */\n readonly options: SnapsBundleWarningsPluginOptions;\n\n constructor(\n options: SnapsBundleWarningsPluginOptions = {\n buffer: true,\n builtIns: true,\n },\n spinner?: Ora,\n ) {\n this.options = options;\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n if (this.options.builtIns) {\n this.#checkBuiltIns(compiler);\n }\n\n if (this.options.buffer) {\n this.#checkBuffer(compiler);\n }\n }\n\n /**\n * Check if a built-in module is used, but not provided by Webpack's\n * `fallback` configuration.\n *\n * @param compiler - The Webpack compiler.\n */\n #checkBuiltIns(compiler: Compiler) {\n compiler.hooks.afterCompile.tap(this.constructor.name, () => {\n if (!this.options.builtInResolver) {\n return;\n }\n\n const { unresolvedModules } = this.options.builtInResolver;\n if (unresolvedModules.size === 0) {\n return;\n }\n\n const formattedModules = new Array(...unresolvedModules)\n .map((name) => indent(`• ${name}`, 2))\n .join('\\n');\n\n warn(\n `The snap attempted to use one or more Node.js builtins, but no browser fallback has been provided.\\n` +\n `The MetaMask Snaps CLI does not support Node.js builtins by default. If you want to use this module, you must set ${yellow(\n `polyfills`,\n )} to ${yellow(\n `true`,\n )} or an object with the builtins to polyfill as the key and ${yellow(\n `true`,\n )} as the value.\\n` +\n `To disable this warning, set ${yellow(\n '`stats.builtIns`',\n )} to ${yellow(\n '`false`',\n )} in your snap config file, or add the module to the ${yellow(\n '`stats.builtIns.ignore`',\n )} array.\\n\\n${formattedModules}\\n`,\n this.#spinner,\n );\n });\n }\n\n /**\n * Check if the given instance is a `ProvidePlugin`. This is not guaranteed to\n * be accurate, but it's good enough for our purposes. If we were to use\n * `instanceof` instead, it might not work if multiple versions of Webpack are\n * installed.\n *\n * @param instance - The instance to check.\n * @returns Whether the instance is a `ProvidePlugin`, i.e., whether it's an\n * object with the name `ProvidePlugin` and a `definitions` property.\n */\n #isProvidePlugin(instance: unknown): instance is ProvidePlugin {\n return (\n isObject(instance) &&\n instance.constructor.name === 'ProvidePlugin' &&\n hasProperty(instance, 'definitions')\n );\n }\n\n /**\n * Check if the `Buffer` global is used, but not provided by Webpack's\n * `DefinePlugin`.\n *\n * @param compiler - The Webpack compiler.\n */\n #checkBuffer(compiler: Compiler) {\n const plugin = compiler.options.plugins?.find((instance) =>\n this.#isProvidePlugin(instance),\n ) as ProvidePlugin | undefined;\n\n // If the `ProvidePlugin` is configured to provide `Buffer`, then we don't\n // need to warn the user.\n if (plugin) {\n const { definitions } = plugin;\n if (definitions.Buffer) {\n return;\n }\n }\n\n compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {\n compilation.hooks.afterProcessAssets.tap(\n this.constructor.name,\n (assets) => {\n // Check if assets use `Buffer`.\n const bufferAssets = Object.entries(assets)\n .filter(([name]) => name.endsWith('.js'))\n .filter(([, asset]) => asset.source().includes('Buffer'));\n\n if (bufferAssets.length === 0) {\n return;\n }\n\n warn(\n `The snap attempted to use the Node.js Buffer global, which is not supported in the MetaMask Snaps CLI by default.\\n` +\n `To use the Buffer global, you must polyfill Buffer by setting ${yellow(\n `buffer`,\n )} to ${yellow(`true`)} in the ${yellow(\n `polyfills`,\n )} config object in your snap config.\\n` +\n `To disable this warning, set ${yellow(\n '`stats.buffer`',\n )} to ${yellow('`false`')} in your snap config file.`,\n this.#spinner,\n );\n },\n );\n });\n }\n}\n"],"names":["indent","assert","hasProperty","isObject","dim","red","yellow","isBuiltin","evaluate","error","getErrorMessage","info","warn","pluralize","SnapsStatsPlugin","apply","compiler","hooks","afterDone","tap","constructor","name","stats","modules","time","errors","toJson","length","formattedErrors","map","getStatsErrorMessage","bind","join","spinner","stop","process","exitCode","watchMode","succeed","options","verbose","statsError","baseMessage","message","first","rest","split","details","filter","Boolean","SnapsWatchPlugin","invalid","file","start","afterEmit","tapPromise","fileDependencies","files","forEach","add","bundle","safeEvaluate","bundlePath","evaluateError","SnapsBuiltInResolver","resolver","getHook","source","tapAsync","module","isModule","request","_","callback","baseRequest","ignore","includes","fallback","find","alias","unresolvedModules","Set","SnapsBundleWarningsPlugin","builtIns","checkBuiltIns","buffer","checkBuffer","afterCompile","builtInResolver","size","formattedModules","Array","instance","plugin","plugins","isProvidePlugin","definitions","Buffer","compilation","afterProcessAssets","assets","bufferAssets","Object","entries","endsWith","asset"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAASA,MAAM,QAAQ,wBAAwB;AAC/C,SAASC,MAAM,EAAEC,WAAW,EAAEC,QAAQ,QAAQ,kBAAkB;AAChE,SAASC,GAAG,EAAEC,GAAG,EAAEC,MAAM,QAAQ,QAAQ;AACzC,SAASC,SAAS,QAAQ,SAAS;AAWnC,SAASC,QAAQ,QAAQ,mBAAmB;AAC5C,SAASC,KAAK,EAAEC,eAAe,EAAEC,IAAI,EAAEC,IAAI,QAAQ,WAAW;AAC9D,SAASC,SAAS,QAAQ,UAAU;IAsBzB,wCAkET;;;;;GAKC,GACD;AArFF;;;CAGC,GACD,OAAO,MAAMC;IAqBX;;;;GAIC,GACDC,MAAMC,QAAkB,EAAE;QACxBA,SAASC,KAAK,CAACC,SAAS,CAACC,GAAG,CAAC,IAAI,CAACC,WAAW,CAACC,IAAI,EAAE,CAACC;YACnD,IAAI,CAACA,OAAO;gBACV;YACF;YAEA,MAAM,EAAEC,OAAO,EAAEC,IAAI,EAAEC,MAAM,EAAE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AEA,OAAO,WAAW,0BAA0B,CAAC,2BACvD,IAAI,EAAE2B;QAEV;IAEJ;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../src/webpack/plugins.ts"],"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 ResolvePluginInstance,\n Resolver,\n StatsError,\n WebpackPluginInstance,\n} from 'webpack';\nimport { WebpackError } from 'webpack';\n\nimport { evaluate } from '../commands/eval';\nimport { error, getErrorMessage, info, warn } from '../utils';\nimport { formatText, pluralize } from './utils';\n\nexport type SnapsStatsPluginOptions = {\n /**\n * Whether to log the verbose stats.\n */\n verbose?: boolean;\n};\n\n/**\n * A plugin that logs the stats after compilation. This is useful for logging\n * the number of files compiled, and the time taken to compile them.\n */\nexport class SnapsStatsPlugin implements WebpackPluginInstance {\n /**\n * The options for the plugin.\n */\n readonly options: SnapsStatsPluginOptions;\n\n /**\n * The spinner to use for logging.\n */\n readonly #spinner?: Ora;\n\n constructor(\n options: SnapsStatsPluginOptions = {\n verbose: false,\n },\n spinner?: Ora,\n ) {\n this.options = options;\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n compiler.hooks.afterDone.tap(this.constructor.name, (stats) => {\n if (!stats) {\n return;\n }\n\n const { modules, time, errors, 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))\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(statsError: StatsError, color = red) {\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 plugin that logs a message when a built-in module is not resolved. The\n * MetaMask Snaps CLI does not support built-in modules by default, and this\n * plugin is used to warn the user when they try to import a built-in module,\n * when no fallback is configured.\n */\nexport class SnapsBuiltInResolver implements ResolvePluginInstance {\n /**\n * The built-in modules that have been imported, but not resolved.\n */\n readonly unresolvedModules = new Set<string>();\n\n /**\n * The name of the resolver hook to tap into.\n */\n readonly #source = 'described-resolve';\n\n /**\n * The options for the plugin.\n */\n readonly options: SnapsBuiltInResolverOptions;\n\n /**\n * The spinner to use for logging.\n */\n readonly #spinner?: Ora;\n\n constructor(\n options: SnapsBuiltInResolverOptions = {\n ignore: [],\n },\n spinner?: Ora,\n ) {\n this.options = options;\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack resolver.\n *\n * @param resolver - The Webpack resolver.\n */\n apply(resolver: Resolver) {\n resolver\n .getHook(this.#source)\n .tapAsync(\n this.constructor.name,\n ({ module: isModule, request }, _, callback) => {\n if (!isModule || !request) {\n return callback();\n }\n\n const baseRequest = request.split('/')[0];\n if (\n isBuiltin(baseRequest) &&\n !this.options.ignore?.includes(baseRequest)\n ) {\n const fallback = resolver.options.fallback.find(\n ({ name }) => name === baseRequest,\n );\n\n if (fallback && !fallback.alias) {\n this.unresolvedModules.add(baseRequest);\n }\n }\n\n return callback();\n },\n );\n }\n}\n\n/**\n * The options for the {@link SnapsBundleWarningsPlugin}.\n */\nexport type SnapsBundleWarningsPluginOptions = {\n /**\n * The {@link SnapsBuiltInResolver} instance to use for detecting built-in\n * modules.\n */\n builtInResolver?: SnapsBuiltInResolver | false;\n\n /**\n * Whether to show warnings if built-in modules are used, but not provided by\n * Webpack's `fallback` configuration.\n */\n builtIns?: boolean;\n\n /**\n * Whether to show warnings if the `Buffer` global is used, but not provided\n * by Webpack's `DefinePlugin`.\n */\n buffer?: boolean;\n};\n\n/**\n * A plugin that logs a message when:\n *\n * - A built-in module is not resolved. The MetaMask Snaps CLI does not support\n * built-in modules by default, and this plugin is used to warn the user when\n * they try to import a built-in module, when no fallback is configured.\n * - A snap uses the `Buffer` global. The MetaMask Snaps CLI does not support\n * the `Buffer` global by default, and this plugin is used to warn the user when\n * they try to use the `Buffer` global.\n *\n * We use both a resolver and a plugin, because the resolver is used to detect\n * when a built-in module is imported, and the plugin is used to log a single\n * message when the compilation is complete. We can't do everything in a single\n * plugin, because the resolver doesn't have access to the compilation, and the\n * plugin doesn't have access to the resolver.\n */\n\nexport class SnapsBundleWarningsPlugin implements WebpackPluginInstance {\n /**\n * The 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"],"names":["assert","hasProperty","isObject","bold","dim","red","yellow","isBuiltin","WebpackError","evaluate","error","getErrorMessage","info","warn","formatText","pluralize","SnapsStatsPlugin","apply","compiler","hooks","afterDone","tap","constructor","name","stats","modules","time","errors","warnings","toJson","length","formattedErrors","map","statsError","getStatsErrorMessage","join","spinner","stop","process","exitCode","formattedWarnings","statsWarning","watchMode","succeed","options","verbose","color","baseMessage","message","first","rest","split","details","filter","Boolean","SnapsWatchPlugin","invalid","file","start","afterEmit","tapPromise","fileDependencies","files","forEach","add","bind","bundle","safeEvaluate","bundlePath","evaluateError","SnapsBuiltInResolver","resolver","getHook","source","tapAsync","module","isModule","request","_","callback","baseRequest","ignore","includes","fallback","find","alias","unresolvedModules","Set","SnapsBundleWarningsPlugin","builtIns","checkBuiltIns","buffer","checkBuffer","afterCompile","compilation","builtInResolver","size","formattedModules","Array","webpackError","push","instance","plugin","plugins","isProvidePlugin","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
import { builtinModules } from 'module';
|
|
4
4
|
import { dirname, resolve } from 'path';
|
|
5
|
+
import stripAnsi from 'strip-ansi';
|
|
5
6
|
export const BROWSERSLIST_FILE = resolve(dirname(// eslint-disable-next-line n/no-extraneous-require
|
|
6
7
|
require.resolve('@metamask/snaps-cli/package.json')), '.browserslistrc');
|
|
7
8
|
export const WEBPACK_FALLBACKS = {
|
|
@@ -103,11 +104,13 @@ export const WEBPACK_FALLBACKS = {
|
|
|
103
104
|
* @see https://swc.rs/docs/configuration/modules
|
|
104
105
|
*/ module: {
|
|
105
106
|
/**
|
|
106
|
-
* This tells SWC to output
|
|
107
|
-
*
|
|
107
|
+
* This tells SWC to output ES6 modules. This will allow Webpack to
|
|
108
|
+
* optimize the output code better. Snaps don't support ES6 however, so
|
|
109
|
+
* the output code will be transpiled to CommonJS by Webpack later in
|
|
110
|
+
* the build process.
|
|
108
111
|
*
|
|
109
|
-
* @see https://swc.rs/docs/configuration/modules#
|
|
110
|
-
*/ type: '
|
|
112
|
+
* @see https://swc.rs/docs/configuration/modules#es6
|
|
113
|
+
*/ type: 'es6'
|
|
111
114
|
},
|
|
112
115
|
env: {
|
|
113
116
|
targets: targets.join(', ')
|
|
@@ -204,5 +207,79 @@ export function getProgressHandler(spinner, spinnerText) {
|
|
|
204
207
|
polyfills[name] ? WEBPACK_FALLBACKS[name] : false
|
|
205
208
|
]));
|
|
206
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Get an object that can be used as environment variables for Webpack's
|
|
212
|
+
* `DefinePlugin`.
|
|
213
|
+
*
|
|
214
|
+
* @param environment - The environment object from the Snap config.
|
|
215
|
+
* @param defaults - The default environment variables.
|
|
216
|
+
* @returns The Webpack environment variables.
|
|
217
|
+
*/ export function getEnvironmentVariables(environment, defaults = {
|
|
218
|
+
NODE_DEBUG: 'false',
|
|
219
|
+
NODE_ENV: 'production',
|
|
220
|
+
DEBUG: 'false'
|
|
221
|
+
}) {
|
|
222
|
+
return Object.fromEntries(Object.entries({
|
|
223
|
+
...defaults,
|
|
224
|
+
...environment
|
|
225
|
+
}).map(([key, value])=>[
|
|
226
|
+
`process.env.${key}`,
|
|
227
|
+
JSON.stringify(value)
|
|
228
|
+
]));
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Format the given line to fit within the terminal width.
|
|
232
|
+
*
|
|
233
|
+
* @param line - The line to format.
|
|
234
|
+
* @param indent - The indentation to use.
|
|
235
|
+
* @param initialIndent - The initial indentation to use, i.e., the indentation
|
|
236
|
+
* for the first line.
|
|
237
|
+
* @returns The formatted line.
|
|
238
|
+
*/ function formatLine(line, indent, initialIndent) {
|
|
239
|
+
const terminalWidth = process.stdout.columns;
|
|
240
|
+
if (!terminalWidth) {
|
|
241
|
+
return `${' '.repeat(initialIndent)}${line}`;
|
|
242
|
+
}
|
|
243
|
+
return line.split(' ').reduce(({ formattedText, currentLineLength }, word, index)=>{
|
|
244
|
+
// `chalk` adds ANSI escape codes to the text, which are not visible
|
|
245
|
+
// characters. We need to strip them to get the visible length of the
|
|
246
|
+
// text.
|
|
247
|
+
const visibleWord = stripAnsi(word);
|
|
248
|
+
// Determine if a space should be added before the word.
|
|
249
|
+
const spaceBeforeWord = index > 0 ? ' ' : '';
|
|
250
|
+
const wordLengthWithSpace = visibleWord.length + spaceBeforeWord.length;
|
|
251
|
+
// If the word would exceed the terminal width, start a new line.
|
|
252
|
+
if (currentLineLength + wordLengthWithSpace > terminalWidth) {
|
|
253
|
+
return {
|
|
254
|
+
formattedText: `${formattedText}\n${' '.repeat(indent)}${word}`,
|
|
255
|
+
currentLineLength: indent + visibleWord.length
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
// Otherwise, add the word to the current line.
|
|
259
|
+
return {
|
|
260
|
+
formattedText: formattedText + spaceBeforeWord + word,
|
|
261
|
+
currentLineLength: currentLineLength + wordLengthWithSpace
|
|
262
|
+
};
|
|
263
|
+
}, {
|
|
264
|
+
formattedText: ' '.repeat(initialIndent),
|
|
265
|
+
currentLineLength: initialIndent
|
|
266
|
+
}).formattedText;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Format the given text to fit within the terminal width.
|
|
270
|
+
*
|
|
271
|
+
* @param text - The text to format.
|
|
272
|
+
* @param indent - The indentation to use.
|
|
273
|
+
* @param initialIndent - The initial indentation to use, i.e., the indentation
|
|
274
|
+
* for the first line.
|
|
275
|
+
* @returns The formatted text.
|
|
276
|
+
*/ export function formatText(text, indent, initialIndent = indent) {
|
|
277
|
+
const lines = text.split('\n');
|
|
278
|
+
// Apply formatting to each line separately and then join them.
|
|
279
|
+
return lines.map((line, index)=>{
|
|
280
|
+
const lineIndent = index === 0 ? initialIndent : indent;
|
|
281
|
+
return formatLine(line, indent, lineIndent);
|
|
282
|
+
}).join('\n');
|
|
283
|
+
}
|
|
207
284
|
|
|
208
285
|
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/webpack/utils.ts"],"sourcesContent":["import { dim } from 'chalk';\nimport { promises as fs } from 'fs';\nimport { builtinModules } from 'module';\nimport type { Ora } from 'ora';\nimport { dirname, resolve } from 'path';\nimport type { Configuration } from 'webpack';\n\nimport type { ProcessedWebpackConfig } from '../config';\n\nexport const BROWSERSLIST_FILE = resolve(\n dirname(\n // eslint-disable-next-line n/no-extraneous-require\n require.resolve('@metamask/snaps-cli/package.json'),\n ),\n '.browserslistrc',\n);\n\nexport const WEBPACK_FALLBACKS = {\n assert: require.resolve('assert/'),\n buffer: require.resolve('buffer/'),\n console: require.resolve('console-browserify'),\n constants: require.resolve('constants-browserify'),\n crypto: require.resolve('crypto-browserify'),\n domain: require.resolve('domain-browser'),\n events: require.resolve('events/'),\n http: require.resolve('stream-http'),\n https: require.resolve('https-browserify'),\n os: require.resolve('os-browserify/browser'),\n path: require.resolve('path-browserify'),\n punycode: require.resolve('punycode/'),\n process: require.resolve('process/browser'),\n querystring: require.resolve('querystring-es3'),\n stream: require.resolve('stream-browserify'),\n /* eslint-disable @typescript-eslint/naming-convention */\n _stream_duplex: require.resolve('readable-stream/lib/_stream_duplex'),\n _stream_passthrough: require.resolve(\n 'readable-stream/lib/_stream_passthrough',\n ),\n _stream_readable: require.resolve('readable-stream/lib/_stream_readable'),\n _stream_transform: require.resolve('readable-stream/lib/_stream_transform'),\n _stream_writable: require.resolve('readable-stream/lib/_stream_writable'),\n string_decoder: require.resolve('string_decoder/'),\n /* eslint-enable @typescript-eslint/naming-convention */\n sys: require.resolve('util/'),\n timers: require.resolve('timers-browserify'),\n tty: require.resolve('tty-browserify'),\n url: require.resolve('url/'),\n util: require.resolve('util/'),\n vm: require.resolve('vm-browserify'),\n zlib: require.resolve('browserify-zlib'),\n};\n\n/**\n * Get the default loader for JavaScript and TypeScript files, based on the\n * config object.\n *\n * - If the `legacy` option is set, we use the custom `browserify` loader. This\n * uses the legacy Browserify config to transpile the code.\n * - Otherwise, we use the `swc-loader`. This is a Webpack loader that uses the\n * `SWC` compiler, which is a much faster alternative to Babel and TypeScript's\n * own compiler.\n *\n * @param config - The processed snap Webpack config.\n * @param config.legacy - The legacy config object, if any.\n * @param config.sourceMap - Whether to generate source maps.\n * @see https://swc.rs/docs/usage/swc-loader\n * @returns The default loader.\n */\nexport async function getDefaultLoader({\n legacy,\n sourceMap,\n}: ProcessedWebpackConfig) {\n if (legacy) {\n return {\n /**\n * If the snap uses the legacy config, we use the custom `browserify`\n * loader. This uses the legacy Browserify config to transpile the code.\n * This is necessary for backwards compatibility with the\n * `bundlerCustomizer` function.\n */\n loader: resolve(__dirname, 'loaders', 'browserify'),\n\n /**\n * The options for the `browserify` loader. These can be overridden in the\n * snap config.\n */\n options: legacy,\n };\n }\n\n const targets = await getBrowserslistTargets();\n return {\n /**\n * We use the `swc-loader` to transpile TypeScript and JavaScript files.\n * This is a Webpack loader that uses the `SWC` compiler, which is a much\n * faster alternative to Babel and TypeScript's own compiler.\n */\n loader: require.resolve('swc-loader'),\n\n /**\n * The options for the `swc-loader`. These can be overridden in the\n * `.swcrc` file.\n *\n * @see https://swc.rs/docs/configuration/swcrc\n */\n options: {\n sync: false,\n\n /**\n * This tells SWC to generate source maps. We set it to the\n * `sourceMap` value from the config object.\n *\n * This must be enabled if source maps are enabled in the config.\n */\n sourceMaps: Boolean(getDevTool(sourceMap)),\n\n jsc: {\n parser: {\n /**\n * This tells the parser to parse TypeScript files. If you\n * don't need to support TypeScript, you can set this to\n * `ecmascript` instead, but there's no harm in leaving it\n * as `typescript`.\n *\n * @see https://swc.rs/docs/configuration/compilation#jscparser\n */\n syntax: 'typescript',\n },\n },\n\n /**\n * The module configuration. This tells SWC how to output the\n * transpiled code.\n *\n * @see https://swc.rs/docs/configuration/modules\n */\n module: {\n /**\n * This tells SWC to output CommonJS modules. MetaMask Snaps\n * doesn't support ES modules yet, so this is necessary.\n *\n * @see https://swc.rs/docs/configuration/modules#commonjs\n */\n type: 'commonjs',\n },\n\n env: {\n targets: targets.join(', '),\n },\n },\n };\n}\n\n/**\n * Get the Webpack devtool configuration based on the given snap config.\n *\n * - If `sourceMap` is `inline`, return `inline-source-map`.\n * - If `sourceMap` is `true`, return `source-map`.\n * - Otherwise, return `false`.\n *\n * @param sourceMap - The `sourceMap` value from the snap config.\n * @returns The Webpack devtool configuration.\n */\nexport function getDevTool(\n sourceMap: ProcessedWebpackConfig['sourceMap'],\n): Configuration['devtool'] {\n if (sourceMap === 'inline') {\n return 'inline-source-map';\n }\n\n if (sourceMap === true) {\n return 'source-map';\n }\n\n return false;\n}\n\n/**\n * Get a function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n *\n * @param spinner - The spinner to update.\n * @param spinnerText - The initial spinner text. This will be prepended to the\n * percentage.\n * @returns A function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n */\n// Note: This is extracted for testing purposes.\nexport function getProgressHandler(spinner?: Ora, spinnerText?: string) {\n return (percentage: number) => {\n if (spinner && spinnerText) {\n spinner.text = `${spinnerText} ${dim(\n `(${Math.round(percentage * 100)}%)`,\n )}`;\n }\n };\n}\n\n/**\n * Get the targets from the `.browserslistrc` file.\n *\n * @returns The browser targets as an array of strings.\n */\nexport async function getBrowserslistTargets() {\n const contents = await fs.readFile(BROWSERSLIST_FILE, 'utf8');\n return contents\n .split('\\n')\n .map((line) => line.trim())\n .filter((line) => line && !line.startsWith('#'));\n}\n\n/**\n * Get a singular or plural string based on the given count. This is useful for\n * generating messages like \"1 error\" or \"2 errors\". By default, the plural\n * string is the singular string with an \"s\" appended to it.\n *\n * This assumes that the text is in English, and likely won't work for some\n * other languages.\n *\n * @param count - The count.\n * @param singular - The singular string.\n * @param plural - The plural string.\n * @returns The singular or plural string.\n * @example\n * ```typescript\n * pluralize(1, 'error'); // => 'error'\n * pluralize(2, 'error'); // => 'errors'\n * pluralize(1, 'error', 'problem'); // => 'error'\n * pluralize(2, 'error', 'problems'); // => 'problems'\n * ```\n */\nexport function pluralize(\n count: number,\n singular: string,\n plural = `${singular}s`,\n) {\n return count === 1 ? singular : plural;\n}\n\n/**\n * Get an object that can be used as fallback config for Webpack's\n * `fallback` config.\n *\n * @param polyfills - The polyfill object from the snap config.\n * @returns The webpack fallback config.\n */\nexport function getFallbacks(polyfills: ProcessedWebpackConfig['polyfills']): {\n [index: string]: string | false;\n} {\n if (polyfills === true) {\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS] ?? false,\n ]),\n );\n }\n\n if (polyfills === false) {\n return Object.fromEntries(builtinModules.map((name) => [name, false]));\n }\n\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n polyfills[name as keyof ProcessedWebpackConfig['polyfills']]\n ? WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS]\n : false,\n ]),\n );\n}\n"],"names":["dim","promises","fs","builtinModules","dirname","resolve","BROWSERSLIST_FILE","require","WEBPACK_FALLBACKS","assert","buffer","console","constants","crypto","domain","events","http","https","os","path","punycode","process","querystring","stream","_stream_duplex","_stream_passthrough","_stream_readable","_stream_transform","_stream_writable","string_decoder","sys","timers","tty","url","util","vm","zlib","getDefaultLoader","legacy","sourceMap","loader","__dirname","options","targets","getBrowserslistTargets","sync","sourceMaps","Boolean","getDevTool","jsc","parser","syntax","module","type","env","join","getProgressHandler","spinner","spinnerText","percentage","text","Math","round","contents","readFile","split","map","line","trim","filter","startsWith","pluralize","count","singular","plural","getFallbacks","polyfills","Object","fromEntries","name"],"mappings":"AAAA,SAASA,GAAG,QAAQ,QAAQ;AAC5B,SAASC,YAAYC,EAAE,QAAQ,KAAK;AACpC,SAASC,cAAc,QAAQ,SAAS;AAExC,SAASC,OAAO,EAAEC,OAAO,QAAQ,OAAO;AAKxC,OAAO,MAAMC,oBAAoBD,QAC/BD,QACE,mDAAmD;AACnDG,QAAQF,OAAO,CAAC,sCAElB,mBACA;AAEF,OAAO,MAAMG,oBAAoB;IAC/BC,QAAQF,QAAQF,OAAO,CAAC;IACxBK,QAAQH,QAAQF,OAAO,CAAC;IACxBM,SAASJ,QAAQF,OAAO,CAAC;IACzBO,WAAWL,QAAQF,OAAO,CAAC;IAC3BQ,QAAQN,QAAQF,OAAO,CAAC;IACxBS,QAAQP,QAAQF,OAAO,CAAC;IACxBU,QAAQR,QAAQF,OAAO,CAAC;IACxBW,MAAMT,QAAQF,OAAO,CAAC;IACtBY,OAAOV,QAAQF,OAAO,CAAC;IACvBa,IAAIX,QAAQF,OAAO,CAAC;IACpBc,MAAMZ,QAAQF,OAAO,CAAC;IACtBe,UAAUb,QAAQF,OAAO,CAAC;IAC1BgB,SAASd,QAAQF,OAAO,CAAC;IACzBiB,aAAaf,QAAQF,OAAO,CAAC;IAC7BkB,QAAQhB,QAAQF,OAAO,CAAC;IACxB,wDAAwD,GACxDmB,gBAAgBjB,QAAQF,OAAO,CAAC;IAChCoB,qBAAqBlB,QAAQF,OAAO,CAClC;IAEFqB,kBAAkBnB,QAAQF,OAAO,CAAC;IAClCsB,mBAAmBpB,QAAQF,OAAO,CA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AQG,IAAI,GAAG,CAAC,EAAEF,YAAY,CAAC,EAAE1D,IAC/B,CAAC,CAAC,EAAE6D,KAAKC,KAAK,CAACH,aAAa,KAAK,EAAE,CAAC,EACpC,CAAC;QACL;IACF;AACF;AAEA;;;;CAIC,GACD,OAAO,eAAef;IACpB,MAAMmB,WAAW,MAAM7D,GAAG8D,QAAQ,CAAC1D,mBAAmB;IACtD,OAAOyD,SACJE,KAAK,CAAC,MACNC,GAAG,CAAC,CAACC,OAASA,KAAKC,IAAI,IACvBC,MAAM,CAAC,CAACF,OAASA,QAAQ,CAACA,KAAKG,UAAU,CAAC;AAC/C;AAEA;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,SAASC,UACdC,KAAa,EACbC,QAAgB,EAChBC,SAAS,CAAC,EAAED,SAAS,CAAC,CAAC;IAEvB,OAAOD,UAAU,IAAIC,WAAWC;AAClC;AAEA;;;;;;CAMC,GACD,OAAO,SAASC,aAAaC,SAA8C;IAGzE,IAAIA,cAAc,MAAM;QACtB,OAAOC,OAAOC,WAAW,CACvB3E,eAAe+D,GAAG,CAAC,CAACa,OAAS;gBAC3BA;gBACAvE,iBAAiB,CAACuE,KAAuC,IAAI;aAC9D;IAEL;IAEA,IAAIH,cAAc,OAAO;QACvB,OAAOC,OAAOC,WAAW,CAAC3E,eAAe+D,GAAG,CAAC,CAACa,OAAS;gBAACA;gBAAM;aAAM;IACtE;IAEA,OAAOF,OAAOC,WAAW,CACvB3E,eAAe+D,GAAG,CAAC,CAACa,OAAS;YAC3BA;YACAH,SAAS,CAACG,KAAkD,GACxDvE,iBAAiB,CAACuE,KAAuC,GACzD;SACL;AAEL"}
|
|
1
|
+
{"version":3,"sources":["../../../src/webpack/utils.ts"],"sourcesContent":["import { dim } from 'chalk';\nimport { promises as fs } from 'fs';\nimport { builtinModules } from 'module';\nimport type { Ora } from 'ora';\nimport { dirname, resolve } from 'path';\nimport stripAnsi from 'strip-ansi';\nimport type { Configuration } from 'webpack';\n\nimport type { ProcessedWebpackConfig } from '../config';\n\nexport const BROWSERSLIST_FILE = resolve(\n dirname(\n // eslint-disable-next-line n/no-extraneous-require\n require.resolve('@metamask/snaps-cli/package.json'),\n ),\n '.browserslistrc',\n);\n\nexport const WEBPACK_FALLBACKS = {\n assert: require.resolve('assert/'),\n buffer: require.resolve('buffer/'),\n console: require.resolve('console-browserify'),\n constants: require.resolve('constants-browserify'),\n crypto: require.resolve('crypto-browserify'),\n domain: require.resolve('domain-browser'),\n events: require.resolve('events/'),\n http: require.resolve('stream-http'),\n https: require.resolve('https-browserify'),\n os: require.resolve('os-browserify/browser'),\n path: require.resolve('path-browserify'),\n punycode: require.resolve('punycode/'),\n process: require.resolve('process/browser'),\n querystring: require.resolve('querystring-es3'),\n stream: require.resolve('stream-browserify'),\n /* eslint-disable @typescript-eslint/naming-convention */\n _stream_duplex: require.resolve('readable-stream/lib/_stream_duplex'),\n _stream_passthrough: require.resolve(\n 'readable-stream/lib/_stream_passthrough',\n ),\n _stream_readable: require.resolve('readable-stream/lib/_stream_readable'),\n _stream_transform: require.resolve('readable-stream/lib/_stream_transform'),\n _stream_writable: require.resolve('readable-stream/lib/_stream_writable'),\n string_decoder: require.resolve('string_decoder/'),\n /* eslint-enable @typescript-eslint/naming-convention */\n sys: require.resolve('util/'),\n timers: require.resolve('timers-browserify'),\n tty: require.resolve('tty-browserify'),\n url: require.resolve('url/'),\n util: require.resolve('util/'),\n vm: require.resolve('vm-browserify'),\n zlib: require.resolve('browserify-zlib'),\n};\n\n/**\n * Get the default loader for JavaScript and TypeScript files, based on the\n * config object.\n *\n * - If the `legacy` option is set, we use the custom `browserify` loader. This\n * uses the legacy Browserify config to transpile the code.\n * - Otherwise, we use the `swc-loader`. This is a Webpack loader that uses the\n * `SWC` compiler, which is a much faster alternative to Babel and TypeScript's\n * own compiler.\n *\n * @param config - The processed snap Webpack config.\n * @param config.legacy - The legacy config object, if any.\n * @param config.sourceMap - Whether to generate source maps.\n * @see https://swc.rs/docs/usage/swc-loader\n * @returns The default loader.\n */\nexport async function getDefaultLoader({\n legacy,\n sourceMap,\n}: ProcessedWebpackConfig) {\n if (legacy) {\n return {\n /**\n * If the snap uses the legacy config, we use the custom `browserify`\n * loader. This uses the legacy Browserify config to transpile the code.\n * This is necessary for backwards compatibility with the\n * `bundlerCustomizer` function.\n */\n loader: resolve(__dirname, 'loaders', 'browserify'),\n\n /**\n * The options for the `browserify` loader. These can be overridden in the\n * snap config.\n */\n options: legacy,\n };\n }\n\n const targets = await getBrowserslistTargets();\n return {\n /**\n * We use the `swc-loader` to transpile TypeScript and JavaScript files.\n * This is a Webpack loader that uses the `SWC` compiler, which is a much\n * faster alternative to Babel and TypeScript's own compiler.\n */\n loader: require.resolve('swc-loader'),\n\n /**\n * The options for the `swc-loader`. These can be overridden in the\n * `.swcrc` file.\n *\n * @see https://swc.rs/docs/configuration/swcrc\n */\n options: {\n sync: false,\n\n /**\n * This tells SWC to generate source maps. We set it to the\n * `sourceMap` value from the config object.\n *\n * This must be enabled if source maps are enabled in the config.\n */\n sourceMaps: Boolean(getDevTool(sourceMap)),\n\n jsc: {\n parser: {\n /**\n * This tells the parser to parse TypeScript files. If you\n * don't need to support TypeScript, you can set this to\n * `ecmascript` instead, but there's no harm in leaving it\n * as `typescript`.\n *\n * @see https://swc.rs/docs/configuration/compilation#jscparser\n */\n syntax: 'typescript',\n },\n },\n\n /**\n * The module configuration. This tells SWC how to output the\n * transpiled code.\n *\n * @see https://swc.rs/docs/configuration/modules\n */\n module: {\n /**\n * This tells SWC to output ES6 modules. This will allow Webpack to\n * optimize the output code better. Snaps don't support ES6 however, so\n * the output code will be transpiled to CommonJS by Webpack later in\n * the build process.\n *\n * @see https://swc.rs/docs/configuration/modules#es6\n */\n type: 'es6',\n },\n\n env: {\n targets: targets.join(', '),\n },\n },\n };\n}\n\n/**\n * Get the Webpack devtool configuration based on the given snap config.\n *\n * - If `sourceMap` is `inline`, return `inline-source-map`.\n * - If `sourceMap` is `true`, return `source-map`.\n * - Otherwise, return `false`.\n *\n * @param sourceMap - The `sourceMap` value from the snap config.\n * @returns The Webpack devtool configuration.\n */\nexport function getDevTool(\n sourceMap: ProcessedWebpackConfig['sourceMap'],\n): Configuration['devtool'] {\n if (sourceMap === 'inline') {\n return 'inline-source-map';\n }\n\n if (sourceMap === true) {\n return 'source-map';\n }\n\n return false;\n}\n\n/**\n * Get a function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n *\n * @param spinner - The spinner to update.\n * @param spinnerText - The initial spinner text. This will be prepended to the\n * percentage.\n * @returns A function that can be used as handler function for Webpack's\n * `ProgressPlugin`.\n */\n// Note: This is extracted for testing purposes.\nexport function getProgressHandler(spinner?: Ora, spinnerText?: string) {\n return (percentage: number) => {\n if (spinner && spinnerText) {\n spinner.text = `${spinnerText} ${dim(\n `(${Math.round(percentage * 100)}%)`,\n )}`;\n }\n };\n}\n\n/**\n * Get the targets from the `.browserslistrc` file.\n *\n * @returns The browser targets as an array of strings.\n */\nexport async function getBrowserslistTargets() {\n const contents = await fs.readFile(BROWSERSLIST_FILE, 'utf8');\n return contents\n .split('\\n')\n .map((line) => line.trim())\n .filter((line) => line && !line.startsWith('#'));\n}\n\n/**\n * Get a singular or plural string based on the given count. This is useful for\n * generating messages like \"1 error\" or \"2 errors\". By default, the plural\n * string is the singular string with an \"s\" appended to it.\n *\n * This assumes that the text is in English, and likely won't work for some\n * other languages.\n *\n * @param count - The count.\n * @param singular - The singular string.\n * @param plural - The plural string.\n * @returns The singular or plural string.\n * @example\n * ```typescript\n * pluralize(1, 'error'); // => 'error'\n * pluralize(2, 'error'); // => 'errors'\n * pluralize(1, 'error', 'problem'); // => 'error'\n * pluralize(2, 'error', 'problems'); // => 'problems'\n * ```\n */\nexport function pluralize(\n count: number,\n singular: string,\n plural = `${singular}s`,\n) {\n return count === 1 ? singular : plural;\n}\n\n/**\n * Get an object that can be used as fallback config for Webpack's\n * `fallback` config.\n *\n * @param polyfills - The polyfill object from the snap config.\n * @returns The webpack fallback config.\n */\nexport function getFallbacks(polyfills: ProcessedWebpackConfig['polyfills']): {\n [index: string]: string | false;\n} {\n if (polyfills === true) {\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS] ?? false,\n ]),\n );\n }\n\n if (polyfills === false) {\n return Object.fromEntries(builtinModules.map((name) => [name, false]));\n }\n\n return Object.fromEntries(\n builtinModules.map((name) => [\n name,\n polyfills[name as keyof ProcessedWebpackConfig['polyfills']]\n ? WEBPACK_FALLBACKS[name as keyof typeof WEBPACK_FALLBACKS]\n : false,\n ]),\n );\n}\n\n/**\n * Get an object that can be used as environment variables for Webpack's\n * `DefinePlugin`.\n *\n * @param environment - The environment object from the Snap config.\n * @param defaults - The default environment variables.\n * @returns The Webpack environment variables.\n */\nexport function getEnvironmentVariables(\n environment: Record<string, unknown>,\n defaults = {\n NODE_DEBUG: 'false',\n NODE_ENV: 'production',\n DEBUG: 'false',\n },\n) {\n return Object.fromEntries(\n Object.entries({\n ...defaults,\n ...environment,\n }).map(([key, value]) => [`process.env.${key}`, JSON.stringify(value)]),\n );\n}\n\n/**\n * Format the given line to fit within the terminal width.\n *\n * @param line - The line to format.\n * @param indent - The indentation to use.\n * @param initialIndent - The initial indentation to use, i.e., the indentation\n * for the first line.\n * @returns The formatted line.\n */\nfunction formatLine(line: string, indent: number, initialIndent: number) {\n const terminalWidth = process.stdout.columns;\n if (!terminalWidth) {\n return `${' '.repeat(initialIndent)}${line}`;\n }\n\n return line.split(' ').reduce(\n ({ formattedText, currentLineLength }, word, index) => {\n // `chalk` adds ANSI escape codes to the text, which are not visible\n // characters. We need to strip them to get the visible length of the\n // text.\n const visibleWord = stripAnsi(word);\n\n // Determine if a space should be added before the word.\n const spaceBeforeWord = index > 0 ? ' ' : '';\n const wordLengthWithSpace = visibleWord.length + spaceBeforeWord.length;\n\n // If the word would exceed the terminal width, start a new line.\n if (currentLineLength + wordLengthWithSpace > terminalWidth) {\n return {\n formattedText: `${formattedText}\\n${' '.repeat(indent)}${word}`,\n currentLineLength: indent + visibleWord.length,\n };\n }\n\n // Otherwise, add the word to the current line.\n return {\n formattedText: formattedText + spaceBeforeWord + word,\n currentLineLength: currentLineLength + wordLengthWithSpace,\n };\n },\n {\n formattedText: ' '.repeat(initialIndent),\n currentLineLength: initialIndent,\n },\n ).formattedText;\n}\n\n/**\n * Format the given text to fit within the terminal width.\n *\n * @param text - The text to format.\n * @param indent - The indentation to use.\n * @param initialIndent - The initial indentation to use, i.e., the indentation\n * for the first line.\n * @returns The formatted text.\n */\nexport function formatText(\n text: string,\n indent: number,\n initialIndent = indent,\n) {\n const lines = text.split('\\n');\n\n // Apply formatting to each line separately and then join them.\n return lines\n .map((line, index) => {\n const lineIndent = index === 0 ? initialIndent : indent;\n return formatLine(line, indent, lineIndent);\n })\n .join('\\n');\n}\n"],"names":["dim","promises","fs","builtinModules","dirname","resolve","stripAnsi","BROWSERSLIST_FILE","require","WEBPACK_FALLBACKS","assert","buffer","console","constants","crypto","domain","events","http","https","os","path","punycode","process","querystring","stream","_stream_duplex","_stream_passthrough","_stream_readable","_stream_transform","_stream_writable","string_decoder","sys","timers","tty","url","util","vm","zlib","getDefaultLoader","legacy","sourceMap","loader","__dirname","options","targets","getBrowserslistTargets","sync","sourceMaps","Boolean","getDevTool","jsc","parser","syntax","module","type","env","join","getProgressHandler","spinner","spinnerText","percentage","text","Math","round","contents","readFile","split","map","line","trim","filter","startsWith","pluralize","count","singular","plural","getFallbacks","polyfills","Object","fromEntries","name","getEnvironmentVariables","environment","defaults","NODE_DEBUG","NODE_ENV","DEBUG","entries","key","value","JSON","stringify","formatLine","indent","initialIndent","terminalWidth","stdout","columns","repeat","reduce","formattedText","currentLineLength","word","index","visibleWord","spaceBeforeWord","wordLengthWithSpace","length","formatText","lines","lineIndent"],"mappings":"AAAA,SAASA,GAAG,QAAQ,QAAQ;AAC5B,SAASC,YAAYC,EAAE,QAAQ,KAAK;AACpC,SAASC,cAAc,QAA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C;IAEA,OAAOA,KAAKF,KAAK,CAAC,KAAKiC,MAAM,CAC3B,CAAC,EAAEC,aAAa,EAAEC,iBAAiB,EAAE,EAAEC,MAAMC;QAC3C,oEAAoE;QACpE,qEAAqE;QACrE,QAAQ;QACR,MAAMC,cAAclG,UAAUgG;QAE9B,wDAAwD;QACxD,MAAMG,kBAAkBF,QAAQ,IAAI,MAAM;QAC1C,MAAMG,sBAAsBF,YAAYG,MAAM,GAAGF,gBAAgBE,MAAM;QAEvE,iEAAiE;QACjE,IAAIN,oBAAoBK,sBAAsBX,eAAe;YAC3D,OAAO;gBACLK,eAAe,CAAC,EAAEA,cAAc,EAAE,EAAE,IAAIF,MAAM,CAACL,QAAQ,EAAES,KAAK,CAAC;gBAC/DD,mBAAmBR,SAASW,YAAYG,MAAM;YAChD;QACF;QAEA,+CAA+C;QAC/C,OAAO;YACLP,eAAeA,gBAAgBK,kBAAkBH;YACjDD,mBAAmBA,oBAAoBK;QACzC;IACF,GACA;QACEN,eAAe,IAAIF,MAAM,CAACJ;QAC1BO,mBAAmBP;IACrB,GACAM,aAAa;AACjB;AAEA;;;;;;;;CAQC,GACD,OAAO,SAASQ,WACd/C,IAAY,EACZgC,MAAc,EACdC,gBAAgBD,MAAM;IAEtB,MAAMgB,QAAQhD,KAAKK,KAAK,CAAC;IAEzB,+DAA+D;IAC/D,OAAO2C,MACJ1C,GAAG,CAAC,CAACC,MAAMmC;QACV,MAAMO,aAAaP,UAAU,IAAIT,gBAAgBD;QACjD,OAAOD,WAAWxB,MAAMyB,QAAQiB;IAClC,GACCtD,IAAI,CAAC;AACV"}
|
|
@@ -137,7 +137,7 @@ export declare class SnapsBundleWarningsPlugin implements WebpackPluginInstance
|
|
|
137
137
|
* The options for the plugin.
|
|
138
138
|
*/
|
|
139
139
|
readonly options: SnapsBundleWarningsPluginOptions;
|
|
140
|
-
constructor(options?: SnapsBundleWarningsPluginOptions
|
|
140
|
+
constructor(options?: SnapsBundleWarningsPluginOptions);
|
|
141
141
|
/**
|
|
142
142
|
* Apply the plugin to the Webpack compiler.
|
|
143
143
|
*
|
|
@@ -115,10 +115,12 @@ export declare function getDefaultLoader({ legacy, sourceMap, }: ProcessedWebpac
|
|
|
115
115
|
*/
|
|
116
116
|
module: {
|
|
117
117
|
/**
|
|
118
|
-
* This tells SWC to output
|
|
119
|
-
*
|
|
118
|
+
* This tells SWC to output ES6 modules. This will allow Webpack to
|
|
119
|
+
* optimize the output code better. Snaps don't support ES6 however, so
|
|
120
|
+
* the output code will be transpiled to CommonJS by Webpack later in
|
|
121
|
+
* the build process.
|
|
120
122
|
*
|
|
121
|
-
* @see https://swc.rs/docs/configuration/modules#
|
|
123
|
+
* @see https://swc.rs/docs/configuration/modules#es6
|
|
122
124
|
*/
|
|
123
125
|
type: string;
|
|
124
126
|
};
|
|
@@ -186,3 +188,28 @@ export declare function pluralize(count: number, singular: string, plural?: stri
|
|
|
186
188
|
export declare function getFallbacks(polyfills: ProcessedWebpackConfig['polyfills']): {
|
|
187
189
|
[index: string]: string | false;
|
|
188
190
|
};
|
|
191
|
+
/**
|
|
192
|
+
* Get an object that can be used as environment variables for Webpack's
|
|
193
|
+
* `DefinePlugin`.
|
|
194
|
+
*
|
|
195
|
+
* @param environment - The environment object from the Snap config.
|
|
196
|
+
* @param defaults - The default environment variables.
|
|
197
|
+
* @returns The Webpack environment variables.
|
|
198
|
+
*/
|
|
199
|
+
export declare function getEnvironmentVariables(environment: Record<string, unknown>, defaults?: {
|
|
200
|
+
NODE_DEBUG: string;
|
|
201
|
+
NODE_ENV: string;
|
|
202
|
+
DEBUG: string;
|
|
203
|
+
}): {
|
|
204
|
+
[k: string]: string;
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Format the given text to fit within the terminal width.
|
|
208
|
+
*
|
|
209
|
+
* @param text - The text to format.
|
|
210
|
+
* @param indent - The indentation to use.
|
|
211
|
+
* @param initialIndent - The initial indentation to use, i.e., the indentation
|
|
212
|
+
* for the first line.
|
|
213
|
+
* @returns The formatted text.
|
|
214
|
+
*/
|
|
215
|
+
export declare function formatText(text: string, indent: number, initialIndent?: number): string;
|