Skip to content

Commit 68360e6

Browse files
committed
Handle Webpack and Prettier breaking changes
1 parent 0cd680f commit 68360e6

File tree

12 files changed

+51
-57
lines changed

12 files changed

+51
-57
lines changed

jest.config.base.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ module.exports = {
121121
// A preset that is used as a base for Jest's configuration
122122
// preset: '',
123123

124+
// The path to the Prettier executable used to format snapshots
125+
// Jest doesn't support Prettier 3 yet, so we use Prettier 2
126+
prettierPath: require.resolve('prettier-2'),
127+
124128
// Run tests from one or more projects
125129
// projects: undefined,
126130

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"lodash": "^4.17.21",
113113
"minimatch": "^7.4.1",
114114
"prettier": "^3.3.3",
115+
"prettier-2": "npm:prettier@^2.8.8",
115116
"prettier-plugin-packagejson": "^2.5.8",
116117
"rimraf": "^4.1.2",
117118
"semver": "^7.5.4",

packages/examples/packages/json-rpc/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "An0r5dIP8lHzsMGaYcJqJIq1ynF0MZADyjoAo4trirs=",
10+
"shasum": "A5COdI8IhqSN9zwvjqbms2my1m1PfmiSrkkZKkgaXC8=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/snaps-cli/src/commands/build/implementation.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type * as webpack from '../../webpack';
1616
import type * as utils from '../../webpack/utils';
1717
import { BROWSERSLIST_FILE } from '../../webpack/utils';
1818
import { build } from './implementation';
19+
import { SemVerVersion } from '@metamask/utils';
1920

2021
const { promises: fs } = normalFs;
2122

@@ -36,7 +37,10 @@ jest.mock('../../webpack', () => ({
3637
.requireActual<typeof webpack>('../../webpack')
3738
.getCompiler(...args);
3839

40+
// @ts-expect-error: Type mismatch.
3941
compiler.inputFileSystem = normalFs;
42+
43+
// @ts-expect-error: Type mismatch.
4044
compiler.outputFileSystem = normalFs;
4145

4246
return compiler;
@@ -48,6 +52,7 @@ jest.mock('../../webpack/utils', () => ({
4852
getDefaultLoader: jest.fn<
4953
ReturnType<typeof utils.getDefaultLoader>,
5054
Parameters<typeof utils.getDefaultLoader>
55+
// @ts-expect-error: Type mismatch.
5156
>(async (config) => {
5257
if (config.legacy) {
5358
return {
@@ -66,7 +71,7 @@ describe('build', () => {
6671
beforeEach(async () => {
6772
const { manifest } = await getMockSnapFilesWithUpdatedChecksum({
6873
manifest: getSnapManifest({
69-
platformVersion: getPlatformVersion(),
74+
platformVersion: getPlatformVersion() as SemVerVersion,
7075
}),
7176
});
7277

@@ -129,7 +134,7 @@ describe('build', () => {
129134

130135
const output = await fs.readFile('/snap/output.js', 'utf8');
131136
expect(output).toMatchInlineSnapshot(
132-
`"(()=>{var r={67:r=>{r.exports.onRpcRequest=({request:r})=>{console.log("Hello, world!");const{method:e,id:o}=r;return e+o}}},e={};var o=function o(t){var s=e[t];if(void 0!==s)return s.exports;var n=e[t]={exports:{}};return r[t](n,n.exports,o),n.exports}(67);module.exports=o})();"`,
137+
`"(()=>{var r={157:r=>{r.exports.onRpcRequest=({request:r})=>{console.log("Hello, world!");const{method:e,id:o}=r;return e+o}}},e={};var o=function o(t){var s=e[t];if(void 0!==s)return s.exports;var n=e[t]={exports:{}};return r[t](n,n.exports,o),n.exports}(157);module.exports=o})();"`,
133138
);
134139
});
135140

@@ -165,7 +170,7 @@ describe('build', () => {
165170
expect(output).toMatchInlineSnapshot(`
166171
"(() => {
167172
var __webpack_modules__ = {
168-
67: module => {
173+
157: module => {
169174
module.exports.onRpcRequest = ({
170175
request
171176
}) => {
@@ -190,7 +195,7 @@ describe('build', () => {
190195
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
191196
return module.exports;
192197
}
193-
var __webpack_exports__ = __webpack_require__(67);
198+
var __webpack_exports__ = __webpack_require__(157);
194199
module.exports = __webpack_exports__;
195200
})();"
196201
`);

packages/snaps-cli/src/commands/watch/implementation.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ jest.mock('../../webpack', () => ({
2424
.requireActual<typeof webpack>('../../webpack')
2525
.getCompiler(...args);
2626

27+
// @ts-expect-error: Type mismatch.
2728
compiler.inputFileSystem = normalFs;
29+
30+
// @ts-expect-error: Type mismatch.
2831
compiler.outputFileSystem = normalFs;
2932

3033
return compiler;
@@ -57,6 +60,7 @@ describe('watch', () => {
5760
// @ts-expect-error - Partial mock.
5861
mock.mockImplementationOnce(() => ({
5962
watch: watchMock,
63+
watching: {},
6064
}));
6165

6266
await watch(
@@ -78,6 +82,7 @@ describe('watch', () => {
7882
// @ts-expect-error - Partial mock.
7983
mock.mockImplementationOnce(() => ({
8084
watch: watchMock,
85+
watching: {},
8186
}));
8287

8388
await expect(

packages/snaps-cli/src/commands/watch/implementation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Watching } from 'webpack';
44
import type { ProcessedWebpackConfig } from '../../config';
55
import type { WebpackOptions } from '../../webpack';
66
import { getCompiler } from '../../webpack';
7+
import { assert } from '@metamask/utils';
78

89
/**
910
* Build the snap bundle and watch for changes. This uses Webpack to build the
@@ -39,6 +40,7 @@ export async function watch(
3940
return;
4041
}
4142

43+
assert(compiler.watching, 'Expected `compiler.watching` to be set.');
4244
resolve(compiler.watching);
4345
},
4446
);

packages/snaps-cli/src/webpack/plugins.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { Ora } from 'ora';
55
import type {
66
Compiler,
77
ProvidePlugin,
8-
ResolvePluginInstance,
98
Resolver,
109
StatsError,
1110
WebpackPluginInstance,
@@ -241,13 +240,23 @@ export type SnapsBuiltInResolverOptions = {
241240
ignore?: string[];
242241
};
243242

243+
/**
244+
* A Webpack resolve plugin.
245+
*
246+
* Copied from Webpack's own types, because the `ResolvePluginInstance` type is
247+
* a union, and can't be used as a type for a class.
248+
*/
249+
type ResolvePlugin = {
250+
apply: (resolver: Resolver) => void;
251+
}
252+
244253
/**
245254
* A plugin that logs a message when a built-in module is not resolved. The
246255
* MetaMask Snaps CLI does not support built-in modules by default, and this
247256
* plugin is used to warn the user when they try to import a built-in module,
248257
* when no fallback is configured.
249258
*/
250-
export class SnapsBuiltInResolver implements ResolvePluginInstance {
259+
export class SnapsBuiltInResolver implements ResolvePlugin {
251260
/**
252261
* The built-in modules that have been imported, but not resolved.
253262
*/

packages/snaps-webpack-plugin/src/__snapshots__/plugin.test.ts.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
exports[`SnapsWebpackPlugin applies a transform 1`] = `
44
"(() => {
5-
var __webpack_exports__ = {};
65
const foo = 'bar';
76
})();"
87
`;
98

109
exports[`SnapsWebpackPlugin forwards the options 1`] = `
1110
"/******/(() => {
1211
// webpackBootstrap
13-
var __webpack_exports__ = {};
1412
1513
// foo bar
1614
/* baz qux */

packages/snaps-webpack-plugin/src/manifest.test.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,44 +47,6 @@ describe('writeManifest', () => {
4747
`);
4848
});
4949

50-
it('uses a custom Prettier config if found', async () => {
51-
const manifest = JSON.stringify(getSnapManifest());
52-
await writeManifest(
53-
resolve(__dirname, '__fixtures__', 'foo.json'),
54-
manifest,
55-
);
56-
57-
expect(jest.mocked(fs.writeFile).mock.calls[0][1]).toMatchInlineSnapshot(`
58-
"{
59-
"version": "1.0.0",
60-
"description": "The test example snap!",
61-
"proposedName": "@metamask/example-snap",
62-
"repository": {
63-
"type": "git",
64-
"url": "https://github.com/MetaMask/example-snap.git"
65-
},
66-
"source": {
67-
"shasum": "/17SwI03+Cn9sk45Z6Czp+Sktru1oLzOmkJW+YbP9WE=",
68-
"location": {
69-
"npm": {
70-
"filePath": "dist/bundle.js",
71-
"packageName": "@metamask/example-snap",
72-
"registry": "https://registry.npmjs.org",
73-
"iconPath": "images/icon.svg"
74-
}
75-
}
76-
},
77-
"initialPermissions": {
78-
"snap_dialog": {},
79-
"endowment:rpc": { "snaps": true, "dapps": false }
80-
},
81-
"platformVersion": "1.0.0",
82-
"manifestVersion": "0.1"
83-
}
84-
"
85-
`);
86-
});
87-
8850
it('accepts a custom write function', async () => {
8951
const fn = jest.fn();
9052
const manifest = JSON.stringify(getSnapManifest());

packages/snaps-webpack-plugin/src/manifest.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import type { WriteFileFunction } from '@metamask/snaps-utils/node';
22
import { promises as fs } from 'fs';
3-
import { format, resolveConfig } from 'prettier';
3+
import { format } from 'prettier/standalone';
4+
import * as babel from 'prettier/plugins/babel';
5+
import * as estree from 'prettier/plugins/estree';
46

57
/**
68
* Format the manifest data with Prettier and write it to disk.
79
*
8-
* It uses the Prettier configuration found in the project directory (if any),
9-
* or the default Prettier configuration if none is found.
10-
*
1110
* @param path - The path to write the manifest to.
1211
* @param data - The manifest data.
1312
* @param writeFileFn - The function to use to write the manifest.
@@ -18,14 +17,11 @@ export async function writeManifest(
1817
data: string,
1918
writeFileFn: WriteFileFunction = fs.writeFile,
2019
) {
21-
const config = await resolveConfig(path, {
22-
editorconfig: true,
23-
});
24-
25-
const formattedManifest = format(data, {
26-
...config,
20+
const formattedManifest = await format(data, {
21+
tabWidth: 2,
2722
parser: 'json',
2823
filepath: path,
24+
plugins: [babel, estree],
2925
});
3026

3127
await writeFileFn(path, formattedManifest);

0 commit comments

Comments
 (0)