@metamask/snaps-webpack-plugin 4.2.1 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -1
- package/dist/manifest.cjs +31 -9
- package/dist/manifest.cjs.map +1 -1
- package/dist/manifest.d.cts +0 -3
- package/dist/manifest.d.cts.map +1 -1
- package/dist/manifest.d.mts +0 -3
- package/dist/manifest.d.mts.map +1 -1
- package/dist/manifest.mjs +8 -11
- package/dist/manifest.mjs.map +1 -1
- package/dist/plugin.cjs +28 -2
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +7 -1
- package/dist/plugin.d.cts.map +1 -1
- package/dist/plugin.d.mts +7 -1
- package/dist/plugin.d.mts.map +1 -1
- package/dist/plugin.mjs +30 -3
- package/dist/plugin.mjs.map +1 -1
- package/package.json +16 -28
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [5.0.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Support scheduling cronjobs with an ISO 8601 duration ([#3421](https://github.com/MetaMask/snaps/pull/3421))
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- **BREAKING:** Drop support for Node.js 18 and 21 ([#3447](https://github.com/MetaMask/snaps/pull/3447))
|
|
19
|
+
|
|
20
|
+
## [4.3.0]
|
|
21
|
+
|
|
22
|
+
### Changed
|
|
23
|
+
|
|
24
|
+
- Improve readability of eval errors ([#3335](https://github.com/MetaMask/snaps/pull/3335))
|
|
25
|
+
- Bump `@metamask/utils` from `11.0.1` to `11.3.0` ([#3050](https://github.com/MetaMask/snaps/pull/3050), [#3091](https://github.com/MetaMask/snaps/pull/3091),[#3232](https://github.com/MetaMask/snaps/pull/3232))
|
|
26
|
+
|
|
10
27
|
## [4.2.1]
|
|
11
28
|
|
|
12
29
|
### Changed
|
|
@@ -121,7 +138,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
121
138
|
- The version of the package no longer needs to match the version of all other
|
|
122
139
|
MetaMask Snaps packages.
|
|
123
140
|
|
|
124
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@
|
|
141
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@5.0.0...HEAD
|
|
142
|
+
[5.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.3.0...@metamask/snaps-webpack-plugin@5.0.0
|
|
143
|
+
[4.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.2.1...@metamask/snaps-webpack-plugin@4.3.0
|
|
125
144
|
[4.2.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.2.0...@metamask/snaps-webpack-plugin@4.2.1
|
|
126
145
|
[4.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.1.2...@metamask/snaps-webpack-plugin@4.2.0
|
|
127
146
|
[4.1.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-webpack-plugin@4.1.1...@metamask/snaps-webpack-plugin@4.1.2
|
package/dist/manifest.cjs
CHANGED
|
@@ -1,27 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
exports.writeManifest = void 0;
|
|
4
27
|
const fs_1 = require("fs");
|
|
5
|
-
const
|
|
28
|
+
const babel = __importStar(require("prettier/plugins/babel"));
|
|
29
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
30
|
+
// eslint-disable-next-line import-x/namespace
|
|
31
|
+
const estree = __importStar(require("prettier/plugins/estree"));
|
|
32
|
+
const standalone_1 = require("prettier/standalone");
|
|
6
33
|
/**
|
|
7
34
|
* Format the manifest data with Prettier and write it to disk.
|
|
8
35
|
*
|
|
9
|
-
* It uses the Prettier configuration found in the project directory (if any),
|
|
10
|
-
* or the default Prettier configuration if none is found.
|
|
11
|
-
*
|
|
12
36
|
* @param path - The path to write the manifest to.
|
|
13
37
|
* @param data - The manifest data.
|
|
14
38
|
* @param writeFileFn - The function to use to write the manifest.
|
|
15
39
|
* @returns A promise that resolves when the manifest has been written.
|
|
16
40
|
*/
|
|
17
41
|
async function writeManifest(path, data, writeFileFn = fs_1.promises.writeFile) {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
});
|
|
21
|
-
const formattedManifest = (0, prettier_1.format)(data, {
|
|
22
|
-
...config,
|
|
42
|
+
const formattedManifest = await (0, standalone_1.format)(data, {
|
|
43
|
+
tabWidth: 2,
|
|
23
44
|
parser: 'json',
|
|
24
45
|
filepath: path,
|
|
46
|
+
plugins: [babel, estree],
|
|
25
47
|
});
|
|
26
48
|
await writeFileFn(path, formattedManifest);
|
|
27
49
|
}
|
package/dist/manifest.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.cjs","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"manifest.cjs","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,2BAAoC;AACpC,8DAAgD;AAChD,gFAAgF;AAChF,8CAA8C;AAC9C,gEAAkD;AAClD,oDAA6C;AAE7C;;;;;;;GAOG;AACI,KAAK,UAAU,aAAa,CACjC,IAAY,EACZ,IAAY,EACZ,cAAiC,aAAE,CAAC,SAAS;IAE7C,MAAM,iBAAiB,GAAG,MAAM,IAAA,mBAAM,EAAC,IAAI,EAAE;QAC3C,QAAQ,EAAE,CAAC;QACX,MAAM,EAAE,MAAM;QACd,QAAQ,EAAE,IAAI;QACd,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC;KACzB,CAAC,CAAC;IAEH,MAAM,WAAW,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAC7C,CAAC;AAbD,sCAaC","sourcesContent":["import type { WriteFileFunction } from '@metamask/snaps-utils/node';\nimport { promises as fs } from 'fs';\nimport * as babel from 'prettier/plugins/babel';\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line import-x/namespace\nimport * as estree from 'prettier/plugins/estree';\nimport { format } from 'prettier/standalone';\n\n/**\n * Format the manifest data with Prettier and write it to disk.\n *\n * @param path - The path to write the manifest to.\n * @param data - The manifest data.\n * @param writeFileFn - The function to use to write the manifest.\n * @returns A promise that resolves when the manifest has been written.\n */\nexport async function writeManifest(\n path: string,\n data: string,\n writeFileFn: WriteFileFunction = fs.writeFile,\n) {\n const formattedManifest = await format(data, {\n tabWidth: 2,\n parser: 'json',\n filepath: path,\n plugins: [babel, estree],\n });\n\n await writeFileFn(path, formattedManifest);\n}\n"]}
|
package/dist/manifest.d.cts
CHANGED
|
@@ -2,9 +2,6 @@ import type { WriteFileFunction } from "@metamask/snaps-utils/node";
|
|
|
2
2
|
/**
|
|
3
3
|
* Format the manifest data with Prettier and write it to disk.
|
|
4
4
|
*
|
|
5
|
-
* It uses the Prettier configuration found in the project directory (if any),
|
|
6
|
-
* or the default Prettier configuration if none is found.
|
|
7
|
-
*
|
|
8
5
|
* @param path - The path to write the manifest to.
|
|
9
6
|
* @param data - The manifest data.
|
|
10
7
|
* @param writeFileFn - The function to use to write the manifest.
|
package/dist/manifest.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.d.cts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mCAAmC;
|
|
1
|
+
{"version":3,"file":"manifest.d.cts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mCAAmC;AAQpE;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,WAAW,GAAE,iBAAgC,iBAU9C"}
|
package/dist/manifest.d.mts
CHANGED
|
@@ -2,9 +2,6 @@ import type { WriteFileFunction } from "@metamask/snaps-utils/node";
|
|
|
2
2
|
/**
|
|
3
3
|
* Format the manifest data with Prettier and write it to disk.
|
|
4
4
|
*
|
|
5
|
-
* It uses the Prettier configuration found in the project directory (if any),
|
|
6
|
-
* or the default Prettier configuration if none is found.
|
|
7
|
-
*
|
|
8
5
|
* @param path - The path to write the manifest to.
|
|
9
6
|
* @param data - The manifest data.
|
|
10
7
|
* @param writeFileFn - The function to use to write the manifest.
|
package/dist/manifest.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.d.mts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mCAAmC;
|
|
1
|
+
{"version":3,"file":"manifest.d.mts","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mCAAmC;AAQpE;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,WAAW,GAAE,iBAAgC,iBAU9C"}
|
package/dist/manifest.mjs
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
import { promises as fs } from "fs";
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import * as babel from "prettier/plugins/babel";
|
|
3
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
4
|
+
// eslint-disable-next-line import-x/namespace
|
|
5
|
+
import * as estree from "prettier/plugins/estree";
|
|
6
|
+
import { format } from "prettier/standalone";
|
|
5
7
|
/**
|
|
6
8
|
* Format the manifest data with Prettier and write it to disk.
|
|
7
9
|
*
|
|
8
|
-
* It uses the Prettier configuration found in the project directory (if any),
|
|
9
|
-
* or the default Prettier configuration if none is found.
|
|
10
|
-
*
|
|
11
10
|
* @param path - The path to write the manifest to.
|
|
12
11
|
* @param data - The manifest data.
|
|
13
12
|
* @param writeFileFn - The function to use to write the manifest.
|
|
14
13
|
* @returns A promise that resolves when the manifest has been written.
|
|
15
14
|
*/
|
|
16
15
|
export async function writeManifest(path, data, writeFileFn = fs.writeFile) {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
const formattedManifest = format(data, {
|
|
21
|
-
...config,
|
|
16
|
+
const formattedManifest = await format(data, {
|
|
17
|
+
tabWidth: 2,
|
|
22
18
|
parser: 'json',
|
|
23
19
|
filepath: path,
|
|
20
|
+
plugins: [babel, estree],
|
|
24
21
|
});
|
|
25
22
|
await writeFileFn(path, formattedManifest);
|
|
26
23
|
}
|
package/dist/manifest.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.mjs","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,WAAW
|
|
1
|
+
{"version":3,"file":"manifest.mjs","sourceRoot":"","sources":["../src/manifest.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,WAAW;AACpC,OAAO,KAAK,KAAK,+BAA+B;AAChD,gFAAgF;AAChF,8CAA8C;AAC9C,OAAO,KAAK,MAAM,gCAAgC;AAClD,OAAO,EAAE,MAAM,EAAE,4BAA4B;AAE7C;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,IAAY,EACZ,IAAY,EACZ,cAAiC,EAAE,CAAC,SAAS;IAE7C,MAAM,iBAAiB,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE;QAC3C,QAAQ,EAAE,CAAC;QACX,MAAM,EAAE,MAAM;QACd,QAAQ,EAAE,IAAI;QACd,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC;KACzB,CAAC,CAAC;IAEH,MAAM,WAAW,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAC7C,CAAC","sourcesContent":["import type { WriteFileFunction } from '@metamask/snaps-utils/node';\nimport { promises as fs } from 'fs';\nimport * as babel from 'prettier/plugins/babel';\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line import-x/namespace\nimport * as estree from 'prettier/plugins/estree';\nimport { format } from 'prettier/standalone';\n\n/**\n * Format the manifest data with Prettier and write it to disk.\n *\n * @param path - The path to write the manifest to.\n * @param data - The manifest data.\n * @param writeFileFn - The function to use to write the manifest.\n * @returns A promise that resolves when the manifest has been written.\n */\nexport async function writeManifest(\n path: string,\n data: string,\n writeFileFn: WriteFileFunction = fs.writeFile,\n) {\n const formattedManifest = await format(data, {\n tabWidth: 2,\n parser: 'json',\n filepath: path,\n plugins: [babel, estree],\n });\n\n await writeFileFn(path, formattedManifest);\n}\n"]}
|
package/dist/plugin.cjs
CHANGED
|
@@ -3,9 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const snaps_rpc_methods_1 = require("@metamask/snaps-rpc-methods");
|
|
6
7
|
const snaps_sdk_1 = require("@metamask/snaps-sdk");
|
|
7
8
|
const node_1 = require("@metamask/snaps-utils/node");
|
|
8
9
|
const utils_1 = require("@metamask/utils");
|
|
10
|
+
const chalk_1 = require("chalk");
|
|
9
11
|
const path_1 = __importDefault(require("path"));
|
|
10
12
|
const util_1 = require("util");
|
|
11
13
|
const webpack_1 = require("webpack");
|
|
@@ -13,6 +15,8 @@ const webpack_sources_1 = require("webpack-sources");
|
|
|
13
15
|
const manifest_1 = require("./manifest.cjs");
|
|
14
16
|
const PLUGIN_NAME = 'SnapsWebpackPlugin';
|
|
15
17
|
class SnapsWebpackPlugin {
|
|
18
|
+
options;
|
|
19
|
+
#spinner;
|
|
16
20
|
/**
|
|
17
21
|
* Construct an instance of the plugin.
|
|
18
22
|
*
|
|
@@ -26,14 +30,16 @@ class SnapsWebpackPlugin {
|
|
|
26
30
|
* `process.cwd() + '/snap.manifest.json'`.
|
|
27
31
|
* @param options.writeManifest - Whether to fix the manifest.
|
|
28
32
|
* Defaults to `true`.
|
|
33
|
+
* @param spinner - The spinner to use for logging. For internal use only.
|
|
29
34
|
*/
|
|
30
|
-
constructor(options) {
|
|
35
|
+
constructor(options, spinner) {
|
|
31
36
|
this.options = {
|
|
32
37
|
eval: true,
|
|
33
38
|
manifestPath: path_1.default.join(process.cwd(), 'snap.manifest.json'),
|
|
34
39
|
writeManifest: true,
|
|
35
40
|
...options,
|
|
36
41
|
};
|
|
42
|
+
this.#spinner = spinner;
|
|
37
43
|
}
|
|
38
44
|
/**
|
|
39
45
|
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
@@ -88,17 +94,37 @@ class SnapsWebpackPlugin {
|
|
|
88
94
|
(0, utils_1.assert)(compilation.outputOptions.path);
|
|
89
95
|
const outputPath = compilation.outputOptions.path;
|
|
90
96
|
const filePath = path_1.default.join(outputPath, file.name);
|
|
97
|
+
(0, utils_1.assert)(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
|
|
91
98
|
const bundleFile = await (0, util_1.promisify)(compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem))(filePath);
|
|
92
99
|
(0, utils_1.assert)(bundleFile);
|
|
93
100
|
const bundleContent = bundleFile.toString();
|
|
101
|
+
let exports;
|
|
94
102
|
if (this.options.eval) {
|
|
95
|
-
|
|
103
|
+
try {
|
|
104
|
+
const output = await (0, node_1.useTemporaryFile)('snaps-bundle.js', bundleContent, async (path) => (0, node_1.evalBundle)(path));
|
|
105
|
+
this.#spinner?.clear();
|
|
106
|
+
this.#spinner?.frame();
|
|
107
|
+
(0, node_1.logInfo)(`${(0, chalk_1.blue)('ℹ')} ${(0, chalk_1.dim)('Snap bundle evaluated successfully.')}`);
|
|
108
|
+
exports = output.exports;
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
const webpackError = new webpack_1.WebpackError(`Failed to evaluate Snap bundle in SES. This is likely due to an incompatibility with the SES environment in your Snap: ${(0, snaps_sdk_1.getErrorMessage)(error)}`);
|
|
112
|
+
if (error instanceof node_1.SnapEvalError) {
|
|
113
|
+
// The constructor for `WebpackError` doesn't accept the details
|
|
114
|
+
// property, so we need to set it manually.
|
|
115
|
+
webpackError.details = error.output.stderr;
|
|
116
|
+
}
|
|
117
|
+
compilation.errors.push(webpackError);
|
|
118
|
+
}
|
|
96
119
|
}
|
|
97
120
|
if (this.options.manifestPath) {
|
|
98
121
|
const { reports } = await (0, node_1.checkManifest)(path_1.default.dirname(this.options.manifestPath), {
|
|
99
122
|
updateAndWriteManifest: this.options.writeManifest,
|
|
100
123
|
sourceCode: bundleContent,
|
|
124
|
+
exports,
|
|
125
|
+
handlerEndowments: snaps_rpc_methods_1.handlerEndowments,
|
|
101
126
|
writeFileFn: async (path, data) => {
|
|
127
|
+
(0, utils_1.assert)(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
|
|
102
128
|
return (0, manifest_1.writeManifest)(path, data, (0, util_1.promisify)(compiler.outputFileSystem.writeFile));
|
|
103
129
|
},
|
|
104
130
|
});
|
package/dist/plugin.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.cjs","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;;;AAAA,mDAAsD;AACtD,qDAKoC;AAEpC,2CAAyC;AACzC,gDAA6B;AAC7B,+BAAiC;AAEjC,qCAAoD;AACpD,qDAA6D;AAE7D,6CAA2C;AAE3C,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAWzC,MAAqB,kBAAkB;IAGrC;;;;;;;;;;;;;OAaG;IACH,YAAY,OAA0B;QACpC,IAAI,CAAC,OAAO,GAAG;YACb,IAAI,EAAE,IAAI;YACV,YAAY,EAAE,cAAS,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,oBAAoB,CAAC;YACjE,aAAa,EAAE,IAAI;YACnB,GAAG,OAAO;SACX,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,QAAkB;QACtB,MAAM,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC,OAAO,CAAC;QAErC,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,WAAW,EAAE,EAA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,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBAEnC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACtB,WAAW,CAAC,MAAM,CAAC,IAAI,CACrB,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,sBAAY,CAAC,KAAK,CAAC,CAAC,CAClD,CAAC;gBACJ,CAAC;gBAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACxB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,sBAAY,CAAC,OAAO,CAAC,CAAC,CACxD,CAAC;gBACJ,CAAC;gBAED,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACrB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,sBAAY,CAAC,GAAG,OAAO,UAAU,CAAC,CAAC,CAClE,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAjKD,qCAiKC","sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport {\n checkManifest,\n evalBundle,\n postProcessBundle,\n useTemporaryFile,\n} from '@metamask/snaps-utils/node';\nimport type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils/node';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nimport { writeManifest } from './manifest';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n */\n constructor(options?: Partial<Options>) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n try {\n const processed = postProcessBundle(source, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool\n ? (sourceMap as SourceMap)\n : undefined,\n });\n\n if (processed.warnings.length > 0) {\n const webpackErrors = processed.warnings.map(\n (warning) => new WebpackError(warning),\n );\n\n compilation.warnings.push(...webpackErrors);\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n source,\n sourceMap as SourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with\n // Webpack's own `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\n } catch (error) {\n compilation.errors.push(\n new WebpackError(getErrorMessage(error)),\n );\n }\n });\n },\n );\n });\n\n compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file);\n\n assert(compilation.outputOptions.path);\n const outputPath = compilation.outputOptions.path;\n\n const filePath = pathUtils.join(outputPath, file.name);\n\n const bundleFile = await promisify(\n compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem),\n )(filePath);\n assert(bundleFile);\n\n const bundleContent = bundleFile.toString();\n\n if (this.options.eval) {\n await useTemporaryFile('snaps-bundle.js', bundleContent, (path) =>\n evalBundle(path),\n );\n }\n\n if (this.options.manifestPath) {\n const { reports } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n {\n updateAndWriteManifest: this.options.writeManifest,\n sourceCode: bundleContent,\n writeFileFn: async (path, data) => {\n return writeManifest(\n path,\n data,\n promisify(compiler.outputFileSystem.writeFile),\n );\n },\n },\n );\n\n const errors = reports\n .filter((report) => report.severity === 'error' && !report.wasFixed)\n .map((report) => report.message);\n const warnings = reports\n .filter((report) => report.severity === 'warning' && !report.wasFixed)\n .map((report) => report.message);\n const fixed = reports\n .filter((report) => report.wasFixed)\n .map((report) => report.message);\n\n if (errors.length > 0) {\n compilation.errors.push(\n ...errors.map((error) => new WebpackError(error)),\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n ...warnings.map((warning) => new WebpackError(warning)),\n );\n }\n\n if (fixed.length > 0) {\n compilation.warnings.push(\n ...fixed.map((problem) => new WebpackError(`${problem} (fixed)`)),\n );\n }\n }\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"plugin.cjs","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;;;AAAA,mEAAgE;AAChE,mDAAsD;AACtD,qDAOoC;AAEpC,2CAAyC;AACzC,iCAAkC;AAClC,gDAA6B;AAC7B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,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC;qBACrE,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBACnC,MAAM,KAAK,GAAG,OAAO;qBAClB,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC;qBACnC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBAEnC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACtB,WAAW,CAAC,MAAM,CAAC,IAAI,CACrB,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,sBAAY,CAAC,KAAK,CAAC,CAAC,CAClD,CAAC;gBACJ,CAAC;gBAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACxB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,sBAAY,CAAC,OAAO,CAAC,CAAC,CACxD,CAAC;gBACJ,CAAC;gBAED,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACrB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,sBAAY,CAAC,GAAG,OAAO,UAAU,CAAC,CAAC,CAClE,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA1MD,qCA0MC","sourcesContent":["import { handlerEndowments } from '@metamask/snaps-rpc-methods';\nimport { getErrorMessage } from '@metamask/snaps-sdk';\nimport {\n checkManifest,\n evalBundle,\n logInfo,\n postProcessBundle,\n SnapEvalError,\n useTemporaryFile,\n} from '@metamask/snaps-utils/node';\nimport type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils/node';\nimport { assert } from '@metamask/utils';\nimport { blue, dim } from 'chalk';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nimport { writeManifest } from './manifest';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\n// Partial copy of `ora` types to avoid a dependency on `ora` in the plugin.\ntype Spinner = {\n clear(): void;\n frame(): void;\n};\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n readonly #spinner: Spinner | undefined;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n * @param spinner - The spinner to use for logging. For internal use only.\n */\n constructor(options?: Partial<Options>, spinner?: Spinner) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n try {\n const processed = postProcessBundle(source, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool\n ? (sourceMap as SourceMap)\n : undefined,\n });\n\n if (processed.warnings.length > 0) {\n const webpackErrors = processed.warnings.map(\n (warning) => new WebpackError(warning),\n );\n\n compilation.warnings.push(...webpackErrors);\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n source,\n sourceMap as SourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with\n // Webpack's own `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\n } catch (error) {\n compilation.errors.push(\n new WebpackError(getErrorMessage(error)),\n );\n }\n });\n },\n );\n });\n\n compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file);\n\n assert(compilation.outputOptions.path);\n const outputPath = compilation.outputOptions.path;\n\n const filePath = pathUtils.join(outputPath, file.name);\n\n assert(\n compiler.outputFileSystem,\n 'Expected compiler to have an output file system.',\n );\n const bundleFile = await promisify(\n compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem),\n )(filePath);\n assert(bundleFile);\n\n const bundleContent = bundleFile.toString();\n let exports: string[] | undefined;\n\n if (this.options.eval) {\n try {\n const output = await useTemporaryFile(\n 'snaps-bundle.js',\n bundleContent,\n async (path) => evalBundle(path),\n );\n\n this.#spinner?.clear();\n this.#spinner?.frame();\n\n logInfo(\n `${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`,\n );\n\n exports = output.exports;\n } catch (error) {\n const webpackError = new WebpackError(\n `Failed to evaluate Snap bundle in SES. This is likely due to an incompatibility with the SES environment in your Snap: ${getErrorMessage(error)}`,\n );\n\n if (error instanceof SnapEvalError) {\n // The constructor for `WebpackError` doesn't accept the details\n // property, so we need to set it manually.\n webpackError.details = error.output.stderr;\n }\n\n compilation.errors.push(webpackError);\n }\n }\n\n if (this.options.manifestPath) {\n const { reports } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n {\n updateAndWriteManifest: this.options.writeManifest,\n sourceCode: bundleContent,\n exports,\n handlerEndowments,\n writeFileFn: async (path, data) => {\n assert(\n compiler.outputFileSystem,\n 'Expected compiler to have an output file system.',\n );\n return writeManifest(\n path,\n data,\n promisify(compiler.outputFileSystem.writeFile),\n );\n },\n },\n );\n\n const errors = reports\n .filter((report) => report.severity === 'error' && !report.wasFixed)\n .map((report) => report.message);\n const warnings = reports\n .filter((report) => report.severity === 'warning' && !report.wasFixed)\n .map((report) => report.message);\n const fixed = reports\n .filter((report) => report.wasFixed)\n .map((report) => report.message);\n\n if (errors.length > 0) {\n compilation.errors.push(\n ...errors.map((error) => new WebpackError(error)),\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n ...warnings.map((warning) => new WebpackError(warning)),\n );\n }\n\n if (fixed.length > 0) {\n compilation.warnings.push(\n ...fixed.map((problem) => new WebpackError(`${problem} (fixed)`)),\n );\n }\n }\n });\n }\n}\n"]}
|
package/dist/plugin.d.cts
CHANGED
|
@@ -6,7 +6,12 @@ type PluginOptions = {
|
|
|
6
6
|
writeManifest?: boolean;
|
|
7
7
|
};
|
|
8
8
|
export type Options = PluginOptions & Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;
|
|
9
|
+
type Spinner = {
|
|
10
|
+
clear(): void;
|
|
11
|
+
frame(): void;
|
|
12
|
+
};
|
|
9
13
|
export default class SnapsWebpackPlugin {
|
|
14
|
+
#private;
|
|
10
15
|
readonly options: Partial<Options>;
|
|
11
16
|
/**
|
|
12
17
|
* Construct an instance of the plugin.
|
|
@@ -21,8 +26,9 @@ export default class SnapsWebpackPlugin {
|
|
|
21
26
|
* `process.cwd() + '/snap.manifest.json'`.
|
|
22
27
|
* @param options.writeManifest - Whether to fix the manifest.
|
|
23
28
|
* Defaults to `true`.
|
|
29
|
+
* @param spinner - The spinner to use for logging. For internal use only.
|
|
24
30
|
*/
|
|
25
|
-
constructor(options?: Partial<Options
|
|
31
|
+
constructor(options?: Partial<Options>, spinner?: Spinner);
|
|
26
32
|
/**
|
|
27
33
|
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
28
34
|
* stage to process the bundle.
|
package/dist/plugin.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.cts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"plugin.d.cts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,kBAAkB,EAAa,mCAAmC;AAKhF,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB;AAQxC,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG,aAAa,GACjC,IAAI,CAAC,kBAAkB,EAAE,WAAW,GAAG,gBAAgB,CAAC,CAAC;AAG3D,KAAK,OAAO,GAAG;IACb,KAAK,IAAI,IAAI,CAAC;IACd,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,kBAAkB;;IACrC,SAAgB,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAI1C;;;;;;;;;;;;;;OAcG;gBACS,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO;IAWzD;;;;;OAKG;IACH,KAAK,CAAC,QAAQ,EAAE,QAAQ;CAqKzB"}
|
package/dist/plugin.d.mts
CHANGED
|
@@ -6,7 +6,12 @@ type PluginOptions = {
|
|
|
6
6
|
writeManifest?: boolean;
|
|
7
7
|
};
|
|
8
8
|
export type Options = PluginOptions & Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;
|
|
9
|
+
type Spinner = {
|
|
10
|
+
clear(): void;
|
|
11
|
+
frame(): void;
|
|
12
|
+
};
|
|
9
13
|
export default class SnapsWebpackPlugin {
|
|
14
|
+
#private;
|
|
10
15
|
readonly options: Partial<Options>;
|
|
11
16
|
/**
|
|
12
17
|
* Construct an instance of the plugin.
|
|
@@ -21,8 +26,9 @@ export default class SnapsWebpackPlugin {
|
|
|
21
26
|
* `process.cwd() + '/snap.manifest.json'`.
|
|
22
27
|
* @param options.writeManifest - Whether to fix the manifest.
|
|
23
28
|
* Defaults to `true`.
|
|
29
|
+
* @param spinner - The spinner to use for logging. For internal use only.
|
|
24
30
|
*/
|
|
25
|
-
constructor(options?: Partial<Options
|
|
31
|
+
constructor(options?: Partial<Options>, spinner?: Spinner);
|
|
26
32
|
/**
|
|
27
33
|
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
28
34
|
* stage to process the bundle.
|
package/dist/plugin.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.mts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"plugin.d.mts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,kBAAkB,EAAa,mCAAmC;AAKhF,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB;AAQxC,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG,aAAa,GACjC,IAAI,CAAC,kBAAkB,EAAE,WAAW,GAAG,gBAAgB,CAAC,CAAC;AAG3D,KAAK,OAAO,GAAG;IACb,KAAK,IAAI,IAAI,CAAC;IACd,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,kBAAkB;;IACrC,SAAgB,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAI1C;;;;;;;;;;;;;;OAcG;gBACS,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO;IAWzD;;;;;OAKG;IACH,KAAK,CAAC,QAAQ,EAAE,QAAQ;CAqKzB"}
|
package/dist/plugin.mjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { handlerEndowments } from "@metamask/snaps-rpc-methods";
|
|
1
2
|
import { getErrorMessage } from "@metamask/snaps-sdk";
|
|
2
|
-
import { checkManifest, evalBundle, postProcessBundle, useTemporaryFile } from "@metamask/snaps-utils/node";
|
|
3
|
+
import { checkManifest, evalBundle, logInfo, postProcessBundle, SnapEvalError, useTemporaryFile } from "@metamask/snaps-utils/node";
|
|
3
4
|
import { assert } from "@metamask/utils";
|
|
5
|
+
import $chalk from "chalk";
|
|
6
|
+
const { blue, dim } = $chalk;
|
|
4
7
|
import pathUtils from "path";
|
|
5
8
|
import { promisify } from "util";
|
|
6
9
|
import $webpack from "webpack";
|
|
@@ -10,6 +13,8 @@ const { RawSource, SourceMapSource } = $webpacksources;
|
|
|
10
13
|
import { writeManifest } from "./manifest.mjs";
|
|
11
14
|
const PLUGIN_NAME = 'SnapsWebpackPlugin';
|
|
12
15
|
export default class SnapsWebpackPlugin {
|
|
16
|
+
options;
|
|
17
|
+
#spinner;
|
|
13
18
|
/**
|
|
14
19
|
* Construct an instance of the plugin.
|
|
15
20
|
*
|
|
@@ -23,14 +28,16 @@ export default class SnapsWebpackPlugin {
|
|
|
23
28
|
* `process.cwd() + '/snap.manifest.json'`.
|
|
24
29
|
* @param options.writeManifest - Whether to fix the manifest.
|
|
25
30
|
* Defaults to `true`.
|
|
31
|
+
* @param spinner - The spinner to use for logging. For internal use only.
|
|
26
32
|
*/
|
|
27
|
-
constructor(options) {
|
|
33
|
+
constructor(options, spinner) {
|
|
28
34
|
this.options = {
|
|
29
35
|
eval: true,
|
|
30
36
|
manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),
|
|
31
37
|
writeManifest: true,
|
|
32
38
|
...options,
|
|
33
39
|
};
|
|
40
|
+
this.#spinner = spinner;
|
|
34
41
|
}
|
|
35
42
|
/**
|
|
36
43
|
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
@@ -85,17 +92,37 @@ export default class SnapsWebpackPlugin {
|
|
|
85
92
|
assert(compilation.outputOptions.path);
|
|
86
93
|
const outputPath = compilation.outputOptions.path;
|
|
87
94
|
const filePath = pathUtils.join(outputPath, file.name);
|
|
95
|
+
assert(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
|
|
88
96
|
const bundleFile = await promisify(compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem))(filePath);
|
|
89
97
|
assert(bundleFile);
|
|
90
98
|
const bundleContent = bundleFile.toString();
|
|
99
|
+
let exports;
|
|
91
100
|
if (this.options.eval) {
|
|
92
|
-
|
|
101
|
+
try {
|
|
102
|
+
const output = await useTemporaryFile('snaps-bundle.js', bundleContent, async (path) => evalBundle(path));
|
|
103
|
+
this.#spinner?.clear();
|
|
104
|
+
this.#spinner?.frame();
|
|
105
|
+
logInfo(`${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`);
|
|
106
|
+
exports = output.exports;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
const webpackError = new WebpackError(`Failed to evaluate Snap bundle in SES. This is likely due to an incompatibility with the SES environment in your Snap: ${getErrorMessage(error)}`);
|
|
110
|
+
if (error instanceof SnapEvalError) {
|
|
111
|
+
// The constructor for `WebpackError` doesn't accept the details
|
|
112
|
+
// property, so we need to set it manually.
|
|
113
|
+
webpackError.details = error.output.stderr;
|
|
114
|
+
}
|
|
115
|
+
compilation.errors.push(webpackError);
|
|
116
|
+
}
|
|
93
117
|
}
|
|
94
118
|
if (this.options.manifestPath) {
|
|
95
119
|
const { reports } = await checkManifest(pathUtils.dirname(this.options.manifestPath), {
|
|
96
120
|
updateAndWriteManifest: this.options.writeManifest,
|
|
97
121
|
sourceCode: bundleContent,
|
|
122
|
+
exports,
|
|
123
|
+
handlerEndowments,
|
|
98
124
|
writeFileFn: async (path, data) => {
|
|
125
|
+
assert(compiler.outputFileSystem, 'Expected compiler to have an output file system.');
|
|
99
126
|
return writeManifest(path, data, promisify(compiler.outputFileSystem.writeFile));
|
|
100
127
|
},
|
|
101
128
|
});
|
package/dist/plugin.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.mjs","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,4BAA4B;AACtD,OAAO,EACL,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,gBAAgB,EACjB,mCAAmC;AAEpC,OAAO,EAAE,MAAM,EAAE,wBAAwB;AACzC,OAAO,SAAS,aAAa;AAC7B,OAAO,EAAE,SAAS,EAAE,aAAa;;;;;AAKjC,OAAO,EAAE,aAAa,EAAE,uBAAmB;AAE3C,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAWzC,MAAM,CAAC,OAAO,OAAO,kBAAkB;IAGrC;;;;;;;;;;;;;OAaG;IACH,YAAY,OAA0B;QACpC,IAAI,CAAC,OAAO,GAAG;YACb,IAAI,EAAE,IAAI;YACV,YAAY,EAAE,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,oBAAoB,CAAC;YACjE,aAAa,EAAE,IAAI;YACnB,GAAG,OAAO;SACX,CAAC;IACJ,CAAC;IA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AAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC;qBACnC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBAEnC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACtB,WAAW,CAAC,MAAM,CAAC,IAAI,CACrB,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,KAAK,CAAC,CAAC,CAClD,CAAC;gBACJ,CAAC;gBAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACxB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC,CACxD,CAAC;gBACJ,CAAC;gBAED,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACrB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,GAAG,OAAO,UAAU,CAAC,CAAC,CAClE,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { getErrorMessage } from '@metamask/snaps-sdk';\nimport {\n checkManifest,\n evalBundle,\n postProcessBundle,\n useTemporaryFile,\n} from '@metamask/snaps-utils/node';\nimport type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils/node';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nimport { writeManifest } from './manifest';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n */\n constructor(options?: Partial<Options>) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n try {\n const processed = postProcessBundle(source, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool\n ? (sourceMap as SourceMap)\n : undefined,\n });\n\n if (processed.warnings.length > 0) {\n const webpackErrors = processed.warnings.map(\n (warning) => new WebpackError(warning),\n );\n\n compilation.warnings.push(...webpackErrors);\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n source,\n sourceMap as SourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with\n // Webpack's own `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\n } catch (error) {\n compilation.errors.push(\n new WebpackError(getErrorMessage(error)),\n );\n }\n });\n },\n );\n });\n\n compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file);\n\n assert(compilation.outputOptions.path);\n const outputPath = compilation.outputOptions.path;\n\n const filePath = pathUtils.join(outputPath, file.name);\n\n const bundleFile = await promisify(\n compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem),\n )(filePath);\n assert(bundleFile);\n\n const bundleContent = bundleFile.toString();\n\n if (this.options.eval) {\n await useTemporaryFile('snaps-bundle.js', bundleContent, (path) =>\n evalBundle(path),\n );\n }\n\n if (this.options.manifestPath) {\n const { reports } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n {\n updateAndWriteManifest: this.options.writeManifest,\n sourceCode: bundleContent,\n writeFileFn: async (path, data) => {\n return writeManifest(\n path,\n data,\n promisify(compiler.outputFileSystem.writeFile),\n );\n },\n },\n );\n\n const errors = reports\n .filter((report) => report.severity === 'error' && !report.wasFixed)\n .map((report) => report.message);\n const warnings = reports\n .filter((report) => report.severity === 'warning' && !report.wasFixed)\n .map((report) => report.message);\n const fixed = reports\n .filter((report) => report.wasFixed)\n .map((report) => report.message);\n\n if (errors.length > 0) {\n compilation.errors.push(\n ...errors.map((error) => new WebpackError(error)),\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n ...warnings.map((warning) => new WebpackError(warning)),\n );\n }\n\n if (fixed.length > 0) {\n compilation.warnings.push(\n ...fixed.map((problem) => new WebpackError(`${problem} (fixed)`)),\n );\n }\n }\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"plugin.mjs","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAA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Content":["import { handlerEndowments } from '@metamask/snaps-rpc-methods';\nimport { getErrorMessage } from '@metamask/snaps-sdk';\nimport {\n checkManifest,\n evalBundle,\n logInfo,\n postProcessBundle,\n SnapEvalError,\n useTemporaryFile,\n} from '@metamask/snaps-utils/node';\nimport type { PostProcessOptions, SourceMap } from '@metamask/snaps-utils/node';\nimport { assert } from '@metamask/utils';\nimport { blue, dim } from 'chalk';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport type { Compiler } from 'webpack';\nimport { Compilation, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\n\nimport { writeManifest } from './manifest';\n\nconst PLUGIN_NAME = 'SnapsWebpackPlugin';\n\ntype PluginOptions = {\n eval?: boolean;\n manifestPath?: string;\n writeManifest?: boolean;\n};\n\nexport type Options = PluginOptions &\n Omit<PostProcessOptions, 'sourceMap' | 'inputSourceMap'>;\n\n// Partial copy of `ora` types to avoid a dependency on `ora` in the plugin.\ntype Spinner = {\n clear(): void;\n frame(): void;\n};\n\nexport default class SnapsWebpackPlugin {\n public readonly options: Partial<Options>;\n\n readonly #spinner: Spinner | undefined;\n\n /**\n * Construct an instance of the plugin.\n *\n * @param options - The post-process options.\n * @param options.stripComments - Whether to strip comments. Defaults to\n * `true`.\n * @param options.eval - Whether to evaluate the bundle to test SES\n * compatibility. Defaults to `true`.\n * @param options.manifestPath - The path to the manifest file. If provided,\n * the manifest will be validated. Defaults to\n * `process.cwd() + '/snap.manifest.json'`.\n * @param options.writeManifest - Whether to fix the manifest.\n * Defaults to `true`.\n * @param spinner - The spinner to use for logging. For internal use only.\n */\n constructor(options?: Partial<Options>, spinner?: Spinner) {\n this.options = {\n eval: true,\n manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),\n writeManifest: true,\n ...options,\n };\n\n this.#spinner = spinner;\n }\n\n /**\n * Apply the plugin to the Webpack compiler. Hooks into the `processAssets`\n * stage to process the bundle.\n *\n * @param compiler - The Webpack compiler.\n */\n apply(compiler: Compiler) {\n const { devtool } = compiler.options;\n\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {\n compilation.hooks.processAssets.tap(\n {\n name: PLUGIN_NAME,\n stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,\n additionalAssets: true,\n },\n (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const source = asset.source() as string;\n const sourceMap = asset.map();\n\n try {\n const processed = postProcessBundle(source, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool\n ? (sourceMap as SourceMap)\n : undefined,\n });\n\n if (processed.warnings.length > 0) {\n const webpackErrors = processed.warnings.map(\n (warning) => new WebpackError(warning),\n );\n\n compilation.warnings.push(...webpackErrors);\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n source,\n sourceMap as SourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with\n // Webpack's own `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\n } catch (error) {\n compilation.errors.push(\n new WebpackError(getErrorMessage(error)),\n );\n }\n });\n },\n );\n });\n\n compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {\n const file = compilation\n .getAssets()\n .find((asset) => asset.name.endsWith('.js'));\n\n assert(file);\n\n assert(compilation.outputOptions.path);\n const outputPath = compilation.outputOptions.path;\n\n const filePath = pathUtils.join(outputPath, file.name);\n\n assert(\n compiler.outputFileSystem,\n 'Expected compiler to have an output file system.',\n );\n const bundleFile = await promisify(\n compiler.outputFileSystem.readFile.bind(compiler.outputFileSystem),\n )(filePath);\n assert(bundleFile);\n\n const bundleContent = bundleFile.toString();\n let exports: string[] | undefined;\n\n if (this.options.eval) {\n try {\n const output = await useTemporaryFile(\n 'snaps-bundle.js',\n bundleContent,\n async (path) => evalBundle(path),\n );\n\n this.#spinner?.clear();\n this.#spinner?.frame();\n\n logInfo(\n `${blue('ℹ')} ${dim('Snap bundle evaluated successfully.')}`,\n );\n\n exports = output.exports;\n } catch (error) {\n const webpackError = new WebpackError(\n `Failed to evaluate Snap bundle in SES. This is likely due to an incompatibility with the SES environment in your Snap: ${getErrorMessage(error)}`,\n );\n\n if (error instanceof SnapEvalError) {\n // The constructor for `WebpackError` doesn't accept the details\n // property, so we need to set it manually.\n webpackError.details = error.output.stderr;\n }\n\n compilation.errors.push(webpackError);\n }\n }\n\n if (this.options.manifestPath) {\n const { reports } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n {\n updateAndWriteManifest: this.options.writeManifest,\n sourceCode: bundleContent,\n exports,\n handlerEndowments,\n writeFileFn: async (path, data) => {\n assert(\n compiler.outputFileSystem,\n 'Expected compiler to have an output file system.',\n );\n return writeManifest(\n path,\n data,\n promisify(compiler.outputFileSystem.writeFile),\n );\n },\n },\n );\n\n const errors = reports\n .filter((report) => report.severity === 'error' && !report.wasFixed)\n .map((report) => report.message);\n const warnings = reports\n .filter((report) => report.severity === 'warning' && !report.wasFixed)\n .map((report) => report.message);\n const fixed = reports\n .filter((report) => report.wasFixed)\n .map((report) => report.message);\n\n if (errors.length > 0) {\n compilation.errors.push(\n ...errors.map((error) => new WebpackError(error)),\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n ...warnings.map((warning) => new WebpackError(warning)),\n );\n }\n\n if (fixed.length > 0) {\n compilation.warnings.push(\n ...fixed.map((problem) => new WebpackError(`${problem} (fixed)`)),\n );\n }\n }\n });\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-webpack-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "A Webpack plugin to build MetaMask Snaps with Webpack",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -45,9 +45,9 @@
|
|
|
45
45
|
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn changelog:validate && yarn lint:dependencies",
|
|
46
46
|
"lint:ci": "yarn lint",
|
|
47
47
|
"lint:dependencies": "depcheck",
|
|
48
|
-
"lint:eslint": "eslint . --cache
|
|
48
|
+
"lint:eslint": "eslint . --cache",
|
|
49
49
|
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
|
|
50
|
-
"lint:misc": "prettier --no-error-on-unmatched-pattern --
|
|
50
|
+
"lint:misc": "prettier --no-error-on-unmatched-pattern --log-level warn \"**/*.json\" \"**/*.md\" \"**/*.html\" \"!CHANGELOG.md\" --ignore-path ../../.gitignore",
|
|
51
51
|
"publish:preview": "yarn npm publish --tag preview",
|
|
52
52
|
"since-latest-release": "../../scripts/since-latest-release.sh",
|
|
53
53
|
"test": "jest --reporters=jest-silent-reporter",
|
|
@@ -57,46 +57,34 @@
|
|
|
57
57
|
"test:watch": "jest --watch"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@metamask/snaps-
|
|
61
|
-
"@metamask/snaps-
|
|
62
|
-
"@metamask/utils": "^11.0.
|
|
63
|
-
"
|
|
60
|
+
"@metamask/snaps-rpc-methods": "^13.2.0",
|
|
61
|
+
"@metamask/snaps-sdk": "^9.0.0",
|
|
62
|
+
"@metamask/snaps-utils": "^11.0.0",
|
|
63
|
+
"@metamask/utils": "^11.4.0",
|
|
64
|
+
"chalk": "^4.1.2",
|
|
64
65
|
"webpack-sources": "^3.2.3"
|
|
65
66
|
},
|
|
66
67
|
"devDependencies": {
|
|
67
|
-
"@lavamoat/allow-scripts": "^3.
|
|
68
|
-
"@metamask/auto-changelog": "^
|
|
69
|
-
"@
|
|
70
|
-
"@
|
|
71
|
-
"@metamask/eslint-config-nodejs": "^12.1.0",
|
|
72
|
-
"@metamask/eslint-config-typescript": "^12.1.0",
|
|
73
|
-
"@swc/core": "1.3.78",
|
|
74
|
-
"@swc/jest": "^0.2.26",
|
|
68
|
+
"@lavamoat/allow-scripts": "^3.3.4",
|
|
69
|
+
"@metamask/auto-changelog": "^5.0.2",
|
|
70
|
+
"@swc/core": "1.11.31",
|
|
71
|
+
"@swc/jest": "^0.2.38",
|
|
75
72
|
"@ts-bridge/cli": "^0.6.1",
|
|
76
73
|
"@types/jest": "^27.5.1",
|
|
77
74
|
"@types/webpack-sources": "^3.2.0",
|
|
78
|
-
"@typescript-eslint/eslint-plugin": "^5.42.1",
|
|
79
|
-
"@typescript-eslint/parser": "^6.21.0",
|
|
80
75
|
"deepmerge": "^4.2.2",
|
|
81
76
|
"depcheck": "^1.4.7",
|
|
82
|
-
"eslint": "^
|
|
83
|
-
"eslint-config-prettier": "^8.5.0",
|
|
84
|
-
"eslint-plugin-import": "^2.26.0",
|
|
85
|
-
"eslint-plugin-jest": "^27.1.5",
|
|
86
|
-
"eslint-plugin-jsdoc": "^41.1.2",
|
|
87
|
-
"eslint-plugin-n": "^15.7.0",
|
|
88
|
-
"eslint-plugin-prettier": "^4.2.1",
|
|
89
|
-
"eslint-plugin-promise": "^6.1.1",
|
|
77
|
+
"eslint": "^9.11.0",
|
|
90
78
|
"jest": "^29.0.2",
|
|
91
79
|
"jest-it-up": "^2.0.0",
|
|
92
80
|
"jest-silent-reporter": "^0.6.0",
|
|
93
81
|
"memfs": "^3.4.13",
|
|
94
|
-
"prettier
|
|
82
|
+
"prettier": "^3.3.3",
|
|
95
83
|
"typescript": "~5.3.3",
|
|
96
|
-
"webpack": "^5.
|
|
84
|
+
"webpack": "^5.97.1"
|
|
97
85
|
},
|
|
98
86
|
"engines": {
|
|
99
|
-
"node": "^
|
|
87
|
+
"node": "^20 || >=22"
|
|
100
88
|
},
|
|
101
89
|
"publishConfig": {
|
|
102
90
|
"access": "public",
|