Skip to content

Commit

Permalink
Fix CLI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Jan 23, 2024
1 parent 7faad45 commit e5a06d0
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 135 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"semver": "^7.5.4",
"simple-git-hooks": "^2.7.0",
"ts-node": "^10.9.1",
"tsup": "^8.0.1",
"typescript": "~4.8.4"
},
"engines": {
Expand All @@ -115,7 +116,8 @@
"geckodriver": true,
"ts-node>@swc/core": true,
"@swc/core": true,
"favicons>sharp": true
"favicons>sharp": true,
"tsup>esbuild": true
}
},
"packageManager": "yarn@3.6.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/wasm/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "X7t7kcdzitxdD/V2UfZHzLKpvjuDeNIzPU7ihpq6Sw0=",
"shasum": "IAG28rbZSwC9MyJfQeS/1X71vWhaof5WKdM9dugfRQA=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
11 changes: 0 additions & 11 deletions packages/snaps-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./loaders/wasm": {
"import": "./dist/webpack/loaders/wasm.mjs",
"require": "./dist/webpack/loaders/wasm.js",
"types": "./dist/types/webpack/loaders/wasm.d.ts"
},
"./loaders/browserify": {
"import": "./dist/webpack/loaders/browserify.mjs",
"require": "./dist/webpack/loaders/browserify.js",
"types": "./dist/types/webpack/loaders/browserify.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
Expand Down Expand Up @@ -76,7 +66,6 @@
"browserify-zlib": "^0.2.0",
"buffer": "^6.0.3",
"chalk": "^4.1.2",
"chokidar": "^3.5.2",
"console-browserify": "^1.2.0",
"constants-browserify": "^1.0.0",
"crypto-browserify": "^3.12.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/snaps-cli/src/test-utils/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ export function normalizeConfig(config: Configuration): Configuration {
};
}

if (rule.use.loader.includes('function.ts')) {
return {
...rule,
use: {
...rule.use,
loader: '/foo/bar/src/webpack/loaders/function.ts',
},
};
}

return rule;
});

Expand Down
14 changes: 10 additions & 4 deletions packages/snaps-cli/src/webpack/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,10 @@ exports[`getDefaultConfiguration returns the default Webpack configuration for t
{
"test": /\\\\\\.wasm\\$/u,
"use": {
"loader": "/foo/bar/loaders/wasm",
"loader": "/foo/bar/src/webpack/loaders/function.ts",
"options": {
"fn": [Function],
},
},
},
],
Expand Down Expand Up @@ -2028,12 +2031,13 @@ exports[`getDefaultConfiguration returns the default Webpack configuration for t
"exclude": /node_modules/u,
"test": /\\\\\\.\\[tj\\]sx\\?\\$/u,
"use": {
"loader": "/foo/bar/loaders/browserify",
"loader": "/Users/morten/Development/MetaMask/snaps/packages/snaps-cli/src/webpack/loaders/function.ts",
"options": {
"bundlerCustomizer": undefined,
"depsToTranspile": [],
"dist": "/foo/bar/dist",
"eval": true,
"fn": [Function],
"manifest": true,
"outfileName": "bundle.js",
"port": 8081,
Expand Down Expand Up @@ -2162,12 +2166,13 @@ exports[`getDefaultConfiguration returns the default Webpack configuration for t
"exclude": /node_modules/u,
"test": /\\\\\\.\\[tj\\]sx\\?\\$/u,
"use": {
"loader": "/foo/bar/loaders/browserify",
"loader": "/Users/morten/Development/MetaMask/snaps/packages/snaps-cli/src/webpack/loaders/function.ts",
"options": {
"bundlerCustomizer": undefined,
"depsToTranspile": [],
"dist": "/foo/bar/dist",
"eval": true,
"fn": [Function],
"manifest": true,
"outfileName": "bundle.js",
"port": 8081,
Expand Down Expand Up @@ -2296,12 +2301,13 @@ exports[`getDefaultConfiguration returns the default Webpack configuration for t
"exclude": /node_modules/u,
"test": /\\\\\\.\\[tj\\]sx\\?\\$/u,
"use": {
"loader": "/foo/bar/loaders/browserify",
"loader": "/Users/morten/Development/MetaMask/snaps/packages/snaps-cli/src/webpack/loaders/function.ts",
"options": {
"bundlerCustomizer": undefined,
"depsToTranspile": [],
"dist": "/foo/bar/dist",
"eval": true,
"fn": [Function],
"manifest": true,
"outfileName": "bundle.js",
"port": 8081,
Expand Down
5 changes: 2 additions & 3 deletions packages/snaps-cli/src/webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Configuration } from 'webpack';
import { EnvironmentPlugin, ProgressPlugin, ProvidePlugin } from 'webpack';

import type { ProcessedWebpackConfig } from '../config';
import { getFunctionLoader, wasm } from './loaders';
import {
SnapsBuiltInResolver,
SnapsBundleWarningsPlugin,
Expand Down Expand Up @@ -195,9 +196,7 @@ export async function getDefaultConfiguration(

config.experimental.wasm && {
test: /\.wasm$/u,
use: {
loader: require.resolve('@metamask/snaps-cli/loaders/wasm'),
},
use: getFunctionLoader(wasm),
},
],
},
Expand Down
38 changes: 38 additions & 0 deletions packages/snaps-cli/src/webpack/loaders/function.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { resolve } from 'path';

import loader, { getFunctionLoader } from './function';

describe('getFunctionLoader', () => {
it('returns a loader definition', () => {
const fn = jest.fn();
expect(
getFunctionLoader(fn, {
foo: 'bar',
}),
).toStrictEqual({
loader: resolve(__dirname, 'function.ts'),
options: {
fn,
foo: 'bar',
},
});
});
});

describe('loader', () => {
it('executes the function', async () => {
const fn = jest.fn();

await loader.call(
// @ts-expect-error - Partial `this` object.
{
getOptions: () => ({
fn,
}),
},
'test',
);

expect(fn).toHaveBeenCalledWith('test');
});
});
61 changes: 61 additions & 0 deletions packages/snaps-cli/src/webpack/loaders/function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { LoaderDefinitionFunction } from 'webpack';

/**
* Options for the function loader.
*/
export type FunctionLoaderOptions = {
/**
* The function to execute. This is bound to the loader context, so it can
* access the loader options and other properties.
*/
fn: LoaderDefinitionFunction;
};

/**
* A loader that executes a function. See {@link getFunctionLoader} for more
* information.
*
* @param content - The input file contents as a `Uint8Array`.
* @returns The output of the function.
*/
const loader: LoaderDefinitionFunction<FunctionLoaderOptions> = function (
content,
) {
const { fn } = this.getOptions();
return fn.bind(this)(content);
};

export default loader;

/**
* Get a loader that executes the given function. This is useful for executing
* loaders without needing to pass a file to Webpack.
*
* @param fn - The function to execute.
* @param options - The options to pass to the loader.
* @returns The loader definition.
*/
export function getFunctionLoader<Options>(
fn: LoaderDefinitionFunction<Options>,
options: Options,
) {
return {
// We use `__filename` as the loader, so Webpack will execute the loader in
// this file, with the actual function in the options.
loader: __filename,
options: {
fn,
...options,
},
};
}

// When running as CJS, we need to export the loader as a default export, since
// `tsup` exports it as `loader_default`.
// istanbul ignore next 3
// eslint-disable-next-line n/no-process-env
if (typeof module !== 'undefined' && process?.env?.NODE_ENV !== 'test') {
module.exports = loader;
module.exports.getFunctionLoader = getFunctionLoader;
module.exports.raw = true;
}
3 changes: 3 additions & 0 deletions packages/snaps-cli/src/webpack/loaders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './function';
export { default as browserify } from './browserify';
export { default as wasm } from './wasm';
8 changes: 6 additions & 2 deletions packages/snaps-cli/src/webpack/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { dim } from 'chalk';

import type { ProcessedWebpackConfig } from '../config';
import { getMockConfig } from '../test-utils';
import { browserify } from './loaders';
import {
WEBPACK_FALLBACKS,
getBrowserslistTargets,
Expand All @@ -16,8 +17,11 @@ describe('getDefaultLoader', () => {
it('returns the Browserify loader if `legacy` is set', async () => {
const config = getMockConfig('browserify');
expect(await getDefaultLoader(config)).toStrictEqual({
loader: expect.stringContaining('browserify'),
options: config.legacy,
loader: expect.stringContaining('function'),
options: {
...config.legacy,
fn: browserify,
},
});
});

Expand Down
23 changes: 8 additions & 15 deletions packages/snaps-cli/src/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { dirname, resolve } from 'path';
import type { Configuration } from 'webpack';

import type { ProcessedWebpackConfig } from '../config';
import { browserify, getFunctionLoader } from './loaders';

export const BROWSERSLIST_FILE = resolve(
dirname(
Expand Down Expand Up @@ -71,21 +72,13 @@ export async function getDefaultLoader({
sourceMap,
}: ProcessedWebpackConfig) {
if (legacy) {
return {
/**
* If the snap uses the legacy config, we use the custom `browserify`
* loader. This uses the legacy Browserify config to transpile the code.
* This is necessary for backwards compatibility with the
* `bundlerCustomizer` function.
*/
loader: require.resolve('@metamask/snaps-cli/loaders/browserify'),

/**
* The options for the `browserify` loader. These can be overridden in the
* snap config.
*/
options: legacy,
};
/**
* If the snap uses the legacy config, we use the custom `browserify`
* loader. This uses the legacy Browserify config to transpile the code.
* This is necessary for backwards compatibility with the
* `bundlerCustomizer` function.
*/
return getFunctionLoader(browserify, legacy);
}

const targets = await getBrowserslistTargets();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"TextEncoder": true
},
"packages": {
"@swc/cli>semver": true,
"browserify>buffer": true,
"depcheck>semver": true,
"superstruct": true,
"tsup>debug": true
}
Expand Down Expand Up @@ -156,8 +156,8 @@
"@metamask/utils>@noble/hashes": true,
"@metamask/utils>@scure/base": true,
"@metamask/utils>pony-cause": true,
"@swc/cli>semver": true,
"browserify>buffer": true,
"depcheck>semver": true,
"superstruct": true,
"tsup>debug": true
}
Expand All @@ -174,20 +174,6 @@
"TextEncoder": true
}
},
"@swc/cli>semver": {
"globals": {
"console.error": true
},
"packages": {
"@swc/cli>semver>lru-cache": true,
"browserify>process": true
}
},
"@swc/cli>semver>lru-cache": {
"packages": {
"@swc/cli>semver>lru-cache>yallist": true
}
},
"browserify>browser-pack>safe-buffer": {
"packages": {
"browserify>buffer": true
Expand Down Expand Up @@ -218,6 +204,20 @@
"browserify>browser-pack>safe-buffer": true
}
},
"depcheck>semver": {
"globals": {
"console.error": true
},
"packages": {
"browserify>process": true,
"depcheck>semver>lru-cache": true
}
},
"depcheck>semver>lru-cache": {
"packages": {
"depcheck>semver>lru-cache>yallist": true
}
},
"external:../snaps-sdk/src/error-wrappers.ts": {
"packages": {
"@metamask/rpc-errors": true,
Expand Down
Loading

0 comments on commit e5a06d0

Please sign in to comment.