@metamask/snaps-webpack-plugin 0.35.0-flask.1 → 1.0.0-prerelease.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 +3 -25
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin.js +94 -0
- package/dist/plugin.js.map +1 -0
- package/package.json +15 -53
- package/dist/cjs/index.js +0 -18
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/plugin.js +0 -110
- package/dist/cjs/plugin.js.map +0 -1
- package/dist/esm/index.js +0 -3
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/plugin.js +0 -96
- package/dist/esm/plugin.js.map +0 -1
- /package/dist/{types/index.d.ts → index.d.ts} +0 -0
- /package/dist/{types/plugin.d.ts → plugin.d.ts} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -6,25 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
-
## [0.
|
|
10
|
-
### Changed
|
|
11
|
-
- **BREAKING:** Build packages as both CJS and ESM ([#1519](https://github.com/MetaMask/snaps/pull/1519), ([#1532](https://github.com/MetaMask/snaps/pull/1532)))
|
|
12
|
-
- This is breaking in the sense that imports to `dist/` will now require you to import either `dist/cjs` or `dist/esm`.
|
|
13
|
-
- Add `sideEffects: false` ([#1486](https://github.com/MetaMask/snaps/pull/1486))
|
|
14
|
-
|
|
15
|
-
## [0.34.1-flask.1]
|
|
16
|
-
### Changed
|
|
17
|
-
- No changes this release.
|
|
18
|
-
|
|
19
|
-
## [0.34.0-flask.1]
|
|
20
|
-
### Changed
|
|
21
|
-
- No changes this release.
|
|
22
|
-
|
|
23
|
-
## [0.33.1-flask.1]
|
|
24
|
-
### Changed
|
|
25
|
-
- No changes this release.
|
|
26
|
-
|
|
27
|
-
## [0.33.0-flask.1]
|
|
9
|
+
## [1.0.0-prerelease.1]
|
|
28
10
|
### Changed
|
|
29
11
|
- No changes this release.
|
|
30
12
|
|
|
@@ -160,12 +142,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
160
142
|
### Added
|
|
161
143
|
- Initial release ([#420](https://github.com/MetaMask/snaps-monorepo/pull/420))
|
|
162
144
|
|
|
163
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/
|
|
164
|
-
[0.
|
|
165
|
-
[0.34.1-flask.1]: https://github.com/MetaMask/snaps/compare/v0.34.0-flask.1...v0.34.1-flask.1
|
|
166
|
-
[0.34.0-flask.1]: https://github.com/MetaMask/snaps/compare/v0.33.1-flask.1...v0.34.0-flask.1
|
|
167
|
-
[0.33.1-flask.1]: https://github.com/MetaMask/snaps/compare/v0.33.0-flask.1...v0.33.1-flask.1
|
|
168
|
-
[0.33.0-flask.1]: https://github.com/MetaMask/snaps/compare/v0.32.2...v0.33.0-flask.1
|
|
145
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/v1.0.0-prerelease.1...HEAD
|
|
146
|
+
[1.0.0-prerelease.1]: https://github.com/MetaMask/snaps/compare/v0.32.2...v1.0.0-prerelease.1
|
|
169
147
|
[0.32.2]: https://github.com/MetaMask/snaps/compare/v0.32.1...v0.32.2
|
|
170
148
|
[0.32.1]: https://github.com/MetaMask/snaps/compare/v0.32.0...v0.32.1
|
|
171
149
|
[0.32.0]: https://github.com/MetaMask/snaps/compare/v0.31.0...v0.32.0
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var plugin_1 = require("./plugin");
|
|
8
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(plugin_1).default; } });
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,mCAAmC;AAA1B,kHAAA,OAAO,OAAA","sourcesContent":["export { default } from './plugin';\nexport type { Options } from './plugin';\n"]}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
7
|
+
const utils_1 = require("@metamask/utils");
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const util_1 = require("util");
|
|
10
|
+
const webpack_1 = require("webpack");
|
|
11
|
+
const webpack_sources_1 = require("webpack-sources");
|
|
12
|
+
const PLUGIN_NAME = 'SnapsWebpackPlugin';
|
|
13
|
+
class SnapsWebpackPlugin {
|
|
14
|
+
/**
|
|
15
|
+
* Construct an instance of the plugin.
|
|
16
|
+
*
|
|
17
|
+
* @param options - The post-process options.
|
|
18
|
+
* @param options.stripComments - Whether to strip comments. Defaults to
|
|
19
|
+
* `true`.
|
|
20
|
+
* @param options.eval - Whether to evaluate the bundle to test SES
|
|
21
|
+
* compatibility. Defaults to `true`.
|
|
22
|
+
* @param options.manifestPath - The path to the manifest file. If provided,
|
|
23
|
+
* the manifest will be validated. Defaults to
|
|
24
|
+
* `process.cwd() + '/snap.manifest.json'`.
|
|
25
|
+
* @param options.writeManifest - Whether to fix the manifest.
|
|
26
|
+
* Defaults to `true`.
|
|
27
|
+
*/
|
|
28
|
+
constructor(options) {
|
|
29
|
+
this.options = {
|
|
30
|
+
eval: true,
|
|
31
|
+
manifestPath: path_1.default.join(process.cwd(), 'snap.manifest.json'),
|
|
32
|
+
writeManifest: true,
|
|
33
|
+
...options,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
38
|
+
* stage to process the bundle.
|
|
39
|
+
*
|
|
40
|
+
* @param compiler - The Webpack compiler.
|
|
41
|
+
*/
|
|
42
|
+
apply(compiler) {
|
|
43
|
+
const { devtool } = compiler.options;
|
|
44
|
+
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|
45
|
+
compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets) => {
|
|
46
|
+
Object.keys(assets)
|
|
47
|
+
.filter((assetName) => assetName.endsWith('.js'))
|
|
48
|
+
.forEach((assetName) => {
|
|
49
|
+
const asset = assets[assetName];
|
|
50
|
+
const processed = (0, snaps_utils_1.postProcessBundle)(asset.source(), {
|
|
51
|
+
...this.options,
|
|
52
|
+
sourceMap: Boolean(devtool),
|
|
53
|
+
inputSourceMap: devtool ? asset.map() : undefined,
|
|
54
|
+
});
|
|
55
|
+
if (processed.warnings.length > 0) {
|
|
56
|
+
compilation.warnings.push(new webpack_1.WebpackError(`${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\n${processed.warnings.join('\n')}`));
|
|
57
|
+
}
|
|
58
|
+
const replacement = processed.sourceMap
|
|
59
|
+
? new webpack_sources_1.SourceMapSource(processed.code, assetName, processed.sourceMap)
|
|
60
|
+
: new webpack_sources_1.RawSource(processed.code);
|
|
61
|
+
// For some reason the type of `RawSource` is not compatible with Webpack's own
|
|
62
|
+
// `Source`, but works fine when casting it to `any`.
|
|
63
|
+
compilation.updateAsset(assetName, replacement);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation) => {
|
|
68
|
+
const file = compilation
|
|
69
|
+
.getAssets()
|
|
70
|
+
.find((asset) => asset.name.endsWith('.js'));
|
|
71
|
+
(0, utils_1.assert)(file);
|
|
72
|
+
(0, utils_1.assert)(compilation.outputOptions.path);
|
|
73
|
+
const outputPath = compilation.outputOptions.path;
|
|
74
|
+
const filePath = path_1.default.join(outputPath, file.name);
|
|
75
|
+
const bundleFile = await (0, util_1.promisify)(compiler.outputFileSystem.readFile)(filePath);
|
|
76
|
+
(0, utils_1.assert)(bundleFile);
|
|
77
|
+
const bundleContent = bundleFile.toString();
|
|
78
|
+
if (this.options.eval) {
|
|
79
|
+
await (0, snaps_utils_1.useTemporaryFile)('snaps-bundle.js', bundleContent, (path) => (0, snaps_utils_1.evalBundle)(path));
|
|
80
|
+
}
|
|
81
|
+
if (this.options.manifestPath) {
|
|
82
|
+
const { errors, warnings } = await (0, snaps_utils_1.checkManifest)(path_1.default.dirname(this.options.manifestPath), this.options.writeManifest, bundleContent);
|
|
83
|
+
if (!this.options.writeManifest && errors.length > 0) {
|
|
84
|
+
throw new Error(`Manifest Error: The manifest is invalid.\n${errors.join('\n')}`);
|
|
85
|
+
}
|
|
86
|
+
if (warnings.length > 0) {
|
|
87
|
+
compilation.warnings.push(new webpack_1.WebpackError(`${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\n${warnings.join('\n')}`));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.default = SnapsWebpackPlugin;
|
|
94
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;;;AAAA,uDAO+B;AAC/B,2CAAyC;AACzC,gDAA6B;AAC7B,+BAAiC;AACjC,qCAAiD;AACjD,qDAA6D;AAE7D,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAWzC,MAAqB,kBAAkB;IAGrC;;;;;;;;;;;;;OAaG;IACH,YAAY,OAA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AU,EAAC,IAAI,CAAC,CACjB,CAAC;aACH;YAED,IAAI,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE;gBAC7B,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,IAAA,2BAAa,EAC9C,cAAS,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,EAC5C,IAAI,CAAC,OAAO,CAAC,aAAa,EAC1B,aAAa,CACd,CAAC;gBAEF,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;oBACpD,MAAM,IAAI,KAAK,CACb,6CAA6C,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CACjE,CAAC;iBACH;gBAED,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;oBACvB,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,IAAI,sBAAY,CACd,GAAG,WAAW,kFAAkF,QAAQ,CAAC,IAAI,CAC3G,IAAI,CACL,EAAE,CACJ,CACF,CAAC;iBACH;aACF;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA1HD,qCA0HC","sourcesContent":["import {\n checkManifest,\n evalBundle,\n postProcessBundle,\n PostProcessOptions,\n SourceMap,\n useTemporaryFile,\n} from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport { Compiler, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\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(PLUGIN_NAME, (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const processed = postProcessBundle(asset.source() as string, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool ? (asset.map() as SourceMap) : undefined,\n });\n\n if (processed.warnings.length > 0) {\n compilation.warnings.push(\n new WebpackError(\n `${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\\n${processed.warnings.join(\n '\\n',\n )}`,\n ),\n );\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with Webpack's own\n // `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\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(compiler.outputFileSystem.readFile)(\n filePath,\n );\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 { errors, warnings } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n this.options.writeManifest,\n bundleContent,\n );\n\n if (!this.options.writeManifest && errors.length > 0) {\n throw new Error(\n `Manifest Error: The manifest is invalid.\\n${errors.join('\\n')}`,\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n new WebpackError(\n `${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\\n${warnings.join(\n '\\n',\n )}`,\n ),\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": "0.
|
|
3
|
+
"version": "1.0.0-prerelease.1",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"webpack",
|
|
6
6
|
"plugin"
|
|
@@ -9,78 +9,39 @@
|
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/MetaMask/snaps.git"
|
|
11
11
|
},
|
|
12
|
-
"
|
|
13
|
-
"exports": {
|
|
14
|
-
".": {
|
|
15
|
-
"import": "./dist/esm/index.js",
|
|
16
|
-
"require": "./dist/cjs/index.js",
|
|
17
|
-
"types": "./dist/types/index.d.ts"
|
|
18
|
-
},
|
|
19
|
-
"./dist/cjs/*": {
|
|
20
|
-
"require": "./dist/cjs/*.js",
|
|
21
|
-
"types": "./dist/types/*.d.ts"
|
|
22
|
-
},
|
|
23
|
-
"./dist/esm/*": {
|
|
24
|
-
"import": "./dist/esm/*.js",
|
|
25
|
-
"types": "./dist/types/*.d.ts"
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
"main": "./dist/cjs/index.js",
|
|
29
|
-
"module": "./dist/esm/index.js",
|
|
30
|
-
"types": "./dist/types/index.d.ts",
|
|
31
|
-
"typesVersions": {
|
|
32
|
-
"*": {
|
|
33
|
-
"*": [
|
|
34
|
-
"./dist/types/*"
|
|
35
|
-
],
|
|
36
|
-
"./dist/cjs/*": [
|
|
37
|
-
"./dist/types/*"
|
|
38
|
-
],
|
|
39
|
-
"./dist/esm/*": [
|
|
40
|
-
"./dist/types/*"
|
|
41
|
-
]
|
|
42
|
-
}
|
|
43
|
-
},
|
|
12
|
+
"main": "dist/index.js",
|
|
44
13
|
"files": [
|
|
45
|
-
"dist/
|
|
46
|
-
"dist/esm/**",
|
|
47
|
-
"dist/types/**"
|
|
14
|
+
"dist/"
|
|
48
15
|
],
|
|
49
16
|
"scripts": {
|
|
50
17
|
"test": "jest && yarn posttest",
|
|
51
18
|
"posttest": "jest-it-up",
|
|
52
19
|
"test:ci": "yarn test",
|
|
53
|
-
"lint:eslint": "eslint . --cache --ext js,ts
|
|
54
|
-
"lint:misc": "prettier
|
|
55
|
-
"lint": "yarn lint:eslint && yarn lint:misc --check
|
|
20
|
+
"lint:eslint": "eslint . --cache --ext js,ts",
|
|
21
|
+
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' --ignore-path ../../.gitignore",
|
|
22
|
+
"lint": "yarn lint:eslint && yarn lint:misc --check",
|
|
56
23
|
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
|
|
57
24
|
"lint:changelog": "yarn auto-changelog validate",
|
|
58
|
-
"build": "
|
|
59
|
-
"build
|
|
60
|
-
"build:types": "tsc --project tsconfig.build.json",
|
|
61
|
-
"build:esm": "swc src --out-dir dist/esm --config-file ../../.swcrc.build.json --config module.type=es6",
|
|
62
|
-
"build:cjs": "swc src --out-dir dist/cjs --config-file ../../.swcrc.build.json --config module.type=commonjs",
|
|
25
|
+
"build:tsc": "tsc --project tsconfig.build.json",
|
|
26
|
+
"build": "yarn build:tsc",
|
|
63
27
|
"build:clean": "yarn clean && yarn build",
|
|
64
28
|
"clean": "rimraf '*.tsbuildinfo' 'dist/*'",
|
|
65
29
|
"prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
|
|
66
30
|
"publish:preview": "yarn npm publish --tag preview",
|
|
67
|
-
"
|
|
31
|
+
"publish:package": "../../scripts/publish-package.sh"
|
|
68
32
|
},
|
|
69
33
|
"dependencies": {
|
|
70
|
-
"@metamask/snaps-utils": "^0.
|
|
34
|
+
"@metamask/snaps-utils": "^1.0.0-prerelease.1",
|
|
71
35
|
"@metamask/utils": "^6.0.1",
|
|
72
36
|
"webpack-sources": "^3.2.3"
|
|
73
37
|
},
|
|
74
38
|
"devDependencies": {
|
|
75
|
-
"@lavamoat/allow-scripts": "^2.3
|
|
39
|
+
"@lavamoat/allow-scripts": "^2.0.3",
|
|
76
40
|
"@metamask/auto-changelog": "^3.1.0",
|
|
77
41
|
"@metamask/eslint-config": "^11.0.0",
|
|
78
42
|
"@metamask/eslint-config-jest": "^11.0.0",
|
|
79
43
|
"@metamask/eslint-config-nodejs": "^11.0.1",
|
|
80
44
|
"@metamask/eslint-config-typescript": "^11.0.0",
|
|
81
|
-
"@swc/cli": "^0.1.62",
|
|
82
|
-
"@swc/core": "^1.3.66",
|
|
83
|
-
"@swc/jest": "^0.2.26",
|
|
84
45
|
"@types/jest": "^27.5.1",
|
|
85
46
|
"@types/webpack-sources": "^3.2.0",
|
|
86
47
|
"@typescript-eslint/eslint-plugin": "^5.42.1",
|
|
@@ -95,12 +56,13 @@
|
|
|
95
56
|
"eslint-plugin-prettier": "^4.2.1",
|
|
96
57
|
"jest": "^29.0.2",
|
|
97
58
|
"jest-it-up": "^2.0.0",
|
|
98
|
-
"memfs": "^3.4.
|
|
59
|
+
"memfs": "^3.4.1",
|
|
99
60
|
"prettier": "^2.7.1",
|
|
100
61
|
"prettier-plugin-packagejson": "^2.2.11",
|
|
101
|
-
"rimraf": "^
|
|
62
|
+
"rimraf": "^3.0.2",
|
|
63
|
+
"ts-jest": "^29.0.0",
|
|
102
64
|
"typescript": "~4.8.4",
|
|
103
|
-
"webpack": "^5.
|
|
65
|
+
"webpack": "^5.72.1"
|
|
104
66
|
},
|
|
105
67
|
"engines": {
|
|
106
68
|
"node": ">=16.0.0"
|
package/dist/cjs/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "default", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return _plugin.default;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _plugin = /*#__PURE__*/ _interop_require_default(require("./plugin"));
|
|
12
|
-
function _interop_require_default(obj) {
|
|
13
|
-
return obj && obj.__esModule ? obj : {
|
|
14
|
-
default: obj
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export { default } from './plugin';\nexport type { Options } from './plugin';\n"],"names":["default"],"mappings":";;;;+BAASA;;;eAAAA,eAAO;;;+DAAQ"}
|
package/dist/cjs/plugin.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "default", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return SnapsWebpackPlugin;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _snapsutils = require("@metamask/snaps-utils");
|
|
12
|
-
const _utils = require("@metamask/utils");
|
|
13
|
-
const _path = /*#__PURE__*/ _interop_require_default(require("path"));
|
|
14
|
-
const _util = require("util");
|
|
15
|
-
const _webpack = require("webpack");
|
|
16
|
-
const _webpacksources = require("webpack-sources");
|
|
17
|
-
function _define_property(obj, key, value) {
|
|
18
|
-
if (key in obj) {
|
|
19
|
-
Object.defineProperty(obj, key, {
|
|
20
|
-
value: value,
|
|
21
|
-
enumerable: true,
|
|
22
|
-
configurable: true,
|
|
23
|
-
writable: true
|
|
24
|
-
});
|
|
25
|
-
} else {
|
|
26
|
-
obj[key] = value;
|
|
27
|
-
}
|
|
28
|
-
return obj;
|
|
29
|
-
}
|
|
30
|
-
function _interop_require_default(obj) {
|
|
31
|
-
return obj && obj.__esModule ? obj : {
|
|
32
|
-
default: obj
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
const PLUGIN_NAME = 'SnapsWebpackPlugin';
|
|
36
|
-
class SnapsWebpackPlugin {
|
|
37
|
-
/**
|
|
38
|
-
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
39
|
-
* stage to process the bundle.
|
|
40
|
-
*
|
|
41
|
-
* @param compiler - The Webpack compiler.
|
|
42
|
-
*/ apply(compiler) {
|
|
43
|
-
const { devtool } = compiler.options;
|
|
44
|
-
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation)=>{
|
|
45
|
-
compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets)=>{
|
|
46
|
-
Object.keys(assets).filter((assetName)=>assetName.endsWith('.js')).forEach((assetName)=>{
|
|
47
|
-
const asset = assets[assetName];
|
|
48
|
-
const processed = (0, _snapsutils.postProcessBundle)(asset.source(), {
|
|
49
|
-
...this.options,
|
|
50
|
-
sourceMap: Boolean(devtool),
|
|
51
|
-
inputSourceMap: devtool ? asset.map() : undefined
|
|
52
|
-
});
|
|
53
|
-
if (processed.warnings.length > 0) {
|
|
54
|
-
compilation.warnings.push(new _webpack.WebpackError(`${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\n${processed.warnings.join('\n')}`));
|
|
55
|
-
}
|
|
56
|
-
const replacement = processed.sourceMap ? new _webpacksources.SourceMapSource(processed.code, assetName, processed.sourceMap) : new _webpacksources.RawSource(processed.code);
|
|
57
|
-
// For some reason the type of `RawSource` is not compatible with Webpack's own
|
|
58
|
-
// `Source`, but works fine when casting it to `any`.
|
|
59
|
-
compilation.updateAsset(assetName, replacement);
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation)=>{
|
|
64
|
-
const file = compilation.getAssets().find((asset)=>asset.name.endsWith('.js'));
|
|
65
|
-
(0, _utils.assert)(file);
|
|
66
|
-
(0, _utils.assert)(compilation.outputOptions.path);
|
|
67
|
-
const outputPath = compilation.outputOptions.path;
|
|
68
|
-
const filePath = _path.default.join(outputPath, file.name);
|
|
69
|
-
const bundleFile = await (0, _util.promisify)(compiler.outputFileSystem.readFile)(filePath);
|
|
70
|
-
(0, _utils.assert)(bundleFile);
|
|
71
|
-
const bundleContent = bundleFile.toString();
|
|
72
|
-
if (this.options.eval) {
|
|
73
|
-
await (0, _snapsutils.useTemporaryFile)('snaps-bundle.js', bundleContent, (path)=>(0, _snapsutils.evalBundle)(path));
|
|
74
|
-
}
|
|
75
|
-
if (this.options.manifestPath) {
|
|
76
|
-
const { errors, warnings } = await (0, _snapsutils.checkManifest)(_path.default.dirname(this.options.manifestPath), this.options.writeManifest, bundleContent);
|
|
77
|
-
if (!this.options.writeManifest && errors.length > 0) {
|
|
78
|
-
throw new Error(`Manifest Error: The manifest is invalid.\n${errors.join('\n')}`);
|
|
79
|
-
}
|
|
80
|
-
if (warnings.length > 0) {
|
|
81
|
-
compilation.warnings.push(new _webpack.WebpackError(`${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\n${warnings.join('\n')}`));
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Construct an instance of the plugin.
|
|
88
|
-
*
|
|
89
|
-
* @param options - The post-process options.
|
|
90
|
-
* @param options.stripComments - Whether to strip comments. Defaults to
|
|
91
|
-
* `true`.
|
|
92
|
-
* @param options.eval - Whether to evaluate the bundle to test SES
|
|
93
|
-
* compatibility. Defaults to `true`.
|
|
94
|
-
* @param options.manifestPath - The path to the manifest file. If provided,
|
|
95
|
-
* the manifest will be validated. Defaults to
|
|
96
|
-
* `process.cwd() + '/snap.manifest.json'`.
|
|
97
|
-
* @param options.writeManifest - Whether to fix the manifest.
|
|
98
|
-
* Defaults to `true`.
|
|
99
|
-
*/ constructor(options){
|
|
100
|
-
_define_property(this, "options", void 0);
|
|
101
|
-
this.options = {
|
|
102
|
-
eval: true,
|
|
103
|
-
manifestPath: _path.default.join(process.cwd(), 'snap.manifest.json'),
|
|
104
|
-
writeManifest: true,
|
|
105
|
-
...options
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
//# sourceMappingURL=plugin.js.map
|
package/dist/cjs/plugin.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/plugin.ts"],"sourcesContent":["import {\n checkManifest,\n evalBundle,\n postProcessBundle,\n PostProcessOptions,\n SourceMap,\n useTemporaryFile,\n} from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport { Compiler, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\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(PLUGIN_NAME, (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const processed = postProcessBundle(asset.source() as string, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool ? (asset.map() as SourceMap) : undefined,\n });\n\n if (processed.warnings.length > 0) {\n compilation.warnings.push(\n new WebpackError(\n `${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\\n${processed.warnings.join(\n '\\n',\n )}`,\n ),\n );\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with Webpack's own\n // `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\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(compiler.outputFileSystem.readFile)(\n filePath,\n );\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 { errors, warnings } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n this.options.writeManifest,\n bundleContent,\n );\n\n if (!this.options.writeManifest && errors.length > 0) {\n throw new Error(\n `Manifest Error: The manifest is invalid.\\n${errors.join('\\n')}`,\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n new WebpackError(\n `${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\\n${warnings.join(\n '\\n',\n )}`,\n ),\n );\n }\n }\n });\n }\n}\n"],"names":["SnapsWebpackPlugin","PLUGIN_NAME","apply","compiler","devtool","options","hooks","compilation","tap","processAssets","assets","Object","keys","filter","assetName","endsWith","forEach","asset","processed","postProcessBundle","source","sourceMap","Boolean","inputSourceMap","map","undefined","warnings","length","push","WebpackError","join","replacement","SourceMapSource","code","RawSource","updateAsset","afterEmit","tapPromise","file","getAssets","find","name","assert","outputOptions","path","outputPath","filePath","pathUtils","bundleFile","promisify","outputFileSystem","readFile","bundleContent","toString","eval","useTemporaryFile","evalBundle","manifestPath","errors","checkManifest","dirname","writeManifest","Error","constructor","process","cwd"],"mappings":";;;;;;;eAyBqBA;;;4BAlBd;uBACgB;6DACD;sBACI;yBACa;gCACI;;;;;;;;;;;;;;;;;;;AAE3C,MAAMC,cAAc;AAWL,MAAMD;IA0BnB;;;;;GAKC,GACDE,MAAMC,QAAkB,EAAE;QACxB,MAAM,EAAEC,OAAO,EAAE,GAAGD,SAASE,OAAO;QAEpCF,SAASG,KAAK,CAACC,WAAW,CAACC,GAAG,CAACP,aAAa,CAACM;YAC3CA,YAAYD,KAAK,CAACG,aAAa,CAACD,GAAG,CAACP,aAAa,CAACS;gBAChDC,OAAOC,IAAI,CAACF,QACTG,MAAM,CAAC,CAACC,YAAcA,UAAUC,QAAQ,CAAC,QACzCC,OAAO,CAAC,CAACF;oBACR,MAAMG,QAA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,4BAAgB,EAAC,mBAAmBH,eAAe,CAACR,OACxDY,IAAAA,sBAAU,EAACZ;YAEf;YAEA,IAAI,IAAI,CAACvC,OAAO,CAACoD,YAAY,EAAE;gBAC7B,MAAM,EAAEC,MAAM,EAAEhC,QAAQ,EAAE,GAAG,MAAMiC,IAAAA,yBAAa,EAC9CZ,aAAS,CAACa,OAAO,CAAC,IAAI,CAACvD,OAAO,CAACoD,YAAY,GAC3C,IAAI,CAACpD,OAAO,CAACwD,aAAa,EAC1BT;gBAGF,IAAI,CAAC,IAAI,CAAC/C,OAAO,CAACwD,aAAa,IAAIH,OAAO/B,MAAM,GAAG,GAAG;oBACpD,MAAM,IAAImC,MACR,CAAC,0CAA0C,EAAEJ,OAAO5B,IAAI,CAAC,MAAM,CAAC;gBAEpE;gBAEA,IAAIJ,SAASC,MAAM,GAAG,GAAG;oBACvBpB,YAAYmB,QAAQ,CAACE,IAAI,CACvB,IAAIC,qBAAY,CACd,CAAC,EAAE5B,YAAY,+EAA+E,EAAEyB,SAASI,IAAI,CAC3G,MACA,CAAC;gBAGT;YACF;QACF;IACF;IAtHA;;;;;;;;;;;;;GAaC,GACDiC,YAAY1D,OAA0B,CAAE;QAhBxC,uBAAgBA,WAAhB,KAAA;QAiBE,IAAI,CAACA,OAAO,GAAG;YACbiD,MAAM;YACNG,cAAcV,aAAS,CAACjB,IAAI,CAACkC,QAAQC,GAAG,IAAI;YAC5CJ,eAAe;YACf,GAAGxD,OAAO;QACZ;IACF;AAkGF"}
|
package/dist/esm/index.js
DELETED
package/dist/esm/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export { default } from './plugin';\nexport type { Options } from './plugin';\n"],"names":["default"],"mappings":"AAAA,SAASA,OAAO,QAAQ,WAAW"}
|
package/dist/esm/plugin.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
function _define_property(obj, key, value) {
|
|
2
|
-
if (key in obj) {
|
|
3
|
-
Object.defineProperty(obj, key, {
|
|
4
|
-
value: value,
|
|
5
|
-
enumerable: true,
|
|
6
|
-
configurable: true,
|
|
7
|
-
writable: true
|
|
8
|
-
});
|
|
9
|
-
} else {
|
|
10
|
-
obj[key] = value;
|
|
11
|
-
}
|
|
12
|
-
return obj;
|
|
13
|
-
}
|
|
14
|
-
import { checkManifest, evalBundle, postProcessBundle, useTemporaryFile } from '@metamask/snaps-utils';
|
|
15
|
-
import { assert } from '@metamask/utils';
|
|
16
|
-
import pathUtils from 'path';
|
|
17
|
-
import { promisify } from 'util';
|
|
18
|
-
import { WebpackError } from 'webpack';
|
|
19
|
-
import { RawSource, SourceMapSource } from 'webpack-sources';
|
|
20
|
-
const PLUGIN_NAME = 'SnapsWebpackPlugin';
|
|
21
|
-
class SnapsWebpackPlugin {
|
|
22
|
-
/**
|
|
23
|
-
* Apply the plugin to the Webpack compiler. Hooks into the `processAssets`
|
|
24
|
-
* stage to process the bundle.
|
|
25
|
-
*
|
|
26
|
-
* @param compiler - The Webpack compiler.
|
|
27
|
-
*/ apply(compiler) {
|
|
28
|
-
const { devtool } = compiler.options;
|
|
29
|
-
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation)=>{
|
|
30
|
-
compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets)=>{
|
|
31
|
-
Object.keys(assets).filter((assetName)=>assetName.endsWith('.js')).forEach((assetName)=>{
|
|
32
|
-
const asset = assets[assetName];
|
|
33
|
-
const processed = postProcessBundle(asset.source(), {
|
|
34
|
-
...this.options,
|
|
35
|
-
sourceMap: Boolean(devtool),
|
|
36
|
-
inputSourceMap: devtool ? asset.map() : undefined
|
|
37
|
-
});
|
|
38
|
-
if (processed.warnings.length > 0) {
|
|
39
|
-
compilation.warnings.push(new WebpackError(`${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\n${processed.warnings.join('\n')}`));
|
|
40
|
-
}
|
|
41
|
-
const replacement = processed.sourceMap ? new SourceMapSource(processed.code, assetName, processed.sourceMap) : new RawSource(processed.code);
|
|
42
|
-
// For some reason the type of `RawSource` is not compatible with Webpack's own
|
|
43
|
-
// `Source`, but works fine when casting it to `any`.
|
|
44
|
-
compilation.updateAsset(assetName, replacement);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
compiler.hooks.afterEmit.tapPromise(PLUGIN_NAME, async (compilation)=>{
|
|
49
|
-
const file = compilation.getAssets().find((asset)=>asset.name.endsWith('.js'));
|
|
50
|
-
assert(file);
|
|
51
|
-
assert(compilation.outputOptions.path);
|
|
52
|
-
const outputPath = compilation.outputOptions.path;
|
|
53
|
-
const filePath = pathUtils.join(outputPath, file.name);
|
|
54
|
-
const bundleFile = await promisify(compiler.outputFileSystem.readFile)(filePath);
|
|
55
|
-
assert(bundleFile);
|
|
56
|
-
const bundleContent = bundleFile.toString();
|
|
57
|
-
if (this.options.eval) {
|
|
58
|
-
await useTemporaryFile('snaps-bundle.js', bundleContent, (path)=>evalBundle(path));
|
|
59
|
-
}
|
|
60
|
-
if (this.options.manifestPath) {
|
|
61
|
-
const { errors, warnings } = await checkManifest(pathUtils.dirname(this.options.manifestPath), this.options.writeManifest, bundleContent);
|
|
62
|
-
if (!this.options.writeManifest && errors.length > 0) {
|
|
63
|
-
throw new Error(`Manifest Error: The manifest is invalid.\n${errors.join('\n')}`);
|
|
64
|
-
}
|
|
65
|
-
if (warnings.length > 0) {
|
|
66
|
-
compilation.warnings.push(new WebpackError(`${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\n${warnings.join('\n')}`));
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Construct an instance of the plugin.
|
|
73
|
-
*
|
|
74
|
-
* @param options - The post-process options.
|
|
75
|
-
* @param options.stripComments - Whether to strip comments. Defaults to
|
|
76
|
-
* `true`.
|
|
77
|
-
* @param options.eval - Whether to evaluate the bundle to test SES
|
|
78
|
-
* compatibility. Defaults to `true`.
|
|
79
|
-
* @param options.manifestPath - The path to the manifest file. If provided,
|
|
80
|
-
* the manifest will be validated. Defaults to
|
|
81
|
-
* `process.cwd() + '/snap.manifest.json'`.
|
|
82
|
-
* @param options.writeManifest - Whether to fix the manifest.
|
|
83
|
-
* Defaults to `true`.
|
|
84
|
-
*/ constructor(options){
|
|
85
|
-
_define_property(this, "options", void 0);
|
|
86
|
-
this.options = {
|
|
87
|
-
eval: true,
|
|
88
|
-
manifestPath: pathUtils.join(process.cwd(), 'snap.manifest.json'),
|
|
89
|
-
writeManifest: true,
|
|
90
|
-
...options
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
export { SnapsWebpackPlugin as default };
|
|
95
|
-
|
|
96
|
-
//# sourceMappingURL=plugin.js.map
|
package/dist/esm/plugin.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/plugin.ts"],"sourcesContent":["import {\n checkManifest,\n evalBundle,\n postProcessBundle,\n PostProcessOptions,\n SourceMap,\n useTemporaryFile,\n} from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\nimport pathUtils from 'path';\nimport { promisify } from 'util';\nimport { Compiler, WebpackError } from 'webpack';\nimport { RawSource, SourceMapSource } from 'webpack-sources';\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(PLUGIN_NAME, (assets) => {\n Object.keys(assets)\n .filter((assetName) => assetName.endsWith('.js'))\n .forEach((assetName) => {\n const asset = assets[assetName];\n const processed = postProcessBundle(asset.source() as string, {\n ...this.options,\n sourceMap: Boolean(devtool),\n inputSourceMap: devtool ? (asset.map() as SourceMap) : undefined,\n });\n\n if (processed.warnings.length > 0) {\n compilation.warnings.push(\n new WebpackError(\n `${PLUGIN_NAME}: Bundle Warning: Processing of the Snap bundle completed with warnings.\\n${processed.warnings.join(\n '\\n',\n )}`,\n ),\n );\n }\n\n const replacement = processed.sourceMap\n ? new SourceMapSource(\n processed.code,\n assetName,\n processed.sourceMap,\n )\n : new RawSource(processed.code);\n\n // For some reason the type of `RawSource` is not compatible with Webpack's own\n // `Source`, but works fine when casting it to `any`.\n compilation.updateAsset(assetName, replacement as any);\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(compiler.outputFileSystem.readFile)(\n filePath,\n );\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 { errors, warnings } = await checkManifest(\n pathUtils.dirname(this.options.manifestPath),\n this.options.writeManifest,\n bundleContent,\n );\n\n if (!this.options.writeManifest && errors.length > 0) {\n throw new Error(\n `Manifest Error: The manifest is invalid.\\n${errors.join('\\n')}`,\n );\n }\n\n if (warnings.length > 0) {\n compilation.warnings.push(\n new WebpackError(\n `${PLUGIN_NAME}: Manifest Warning: Validation of snap.manifest.json completed with warnings.\\n${warnings.join(\n '\\n',\n )}`,\n ),\n );\n }\n }\n });\n }\n}\n"],"names":["checkManifest","evalBundle","postProcessBundle","useTemporaryFile","assert","pathUtils","promisify","WebpackError","RawSource","SourceMapSource","PLUGIN_NAME","SnapsWebpackPlugin","apply","compiler","devtool","options","hooks","compilation","tap","processAssets","assets","Object","keys","filter","assetName","endsWith","forEach","asset","processed","source","sourceMap","Boolean","inputSourceMap","map","undefined","warnings","length","push","join","replacement","code","updateAsset","afterEmit","tapPromise","file","getAssets","find","name","outputOptions","path","outputPath","filePath","bundleFile","outputFileSystem","readFile","bundleContent","toString","eval","manifestPath","errors","dirname","writeManifest","Error","constructor","process","cwd"],"mappings":";;;;;;;;;;;;;AAAA,SACEA,aAAa,EACbC,UAAU,EACVC,iBAAiB,EAGjBC,gBAAgB,QACX,wBAAwB;AAC/B,SAASC,MAAM,QAAQ,kBAAkB;AACzC,OAAOC,eAAe,OAAO;AAC7B,SAASC,SAAS,QAAQ,OAAO;AACjC,SAAmBC,YAAY,QAAQ,UAAU;AACjD,SAASC,SAAS,EAAEC,eAAe,QAAQ,kBAAkB;AAE7D,MAAMC,cAAc;AAWL,MAAMC;IA0BnB;;;;;GAKC,GACDC,MAAMC,QAAkB,EAAE;QACxB,MAAM,EAAEC,OAAO,EAAE,GAAGD,SAASE,OAAO;QAEpCF,SAASG,KAAK,CAACC,WAAW,CAACC,GAAG,CAACR,aAAa,CAACO;YAC3CA,YAAYD,KAAK,CAACG,aAAa,CAACD,GAAG,CAACR,aAA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