Skip to content

Commit

Permalink
Add Mock Service Worker - msw (#513)
Browse files Browse the repository at this point in the history
* Add msw

* PR feedback

* Support Intellij project users

* PR review

* PR review

* PR review

* PR review

* PR review

* PR review

* PR review

* PR review

* PR review

* PR review

* Wrap up

* Add msw to schema & types

---------

Co-authored-by: Lars Kappert <[email protected]>
  • Loading branch information
jmcpeak and webpro authored Mar 14, 2024
1 parent a696dfc commit 1404348
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
6 changes: 6 additions & 0 deletions packages/knip/fixtures/plugins/msw/bob/mockServiceWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Mock Service Worker (2.2.0).
* @see https://github.com/mswjs/msw
* - Please do NOT modify this file.
* - Please do NOT serve this file on production.
*/
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/msw/mocks/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './handlers';
Empty file.
Empty file.
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/msw/mocks/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './handlers';
13 changes: 13 additions & 0 deletions packages/knip/fixtures/plugins/msw/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@fixtures/msw",
"version": "*",
"knip": {
"msw": true
},
"devDependencies": {
"msw": "*"
},
"msw": {
"workerDirectory": "bob"
}
}
4 changes: 4 additions & 0 deletions packages/knip/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@
"title": "Mocha plugin configuration (https://github.com/webpro/knip/blob/main/src/plugins/mocha/README.md)",
"$ref": "#/definitions/plugin"
},
"msw": {
"title": "Mocha plugin configuration (https://github.com/webpro/knip/blob/main/src/plugins/msw/README.md)",
"$ref": "#/definitions/plugin"
},
"netlify": {
"title": "Netlify plugin configuration (https://github.com/webpro/knip/blob/main/src/plugins/netlify/README.md)",
"$ref": "#/definitions/plugin"
Expand Down
1 change: 1 addition & 0 deletions packages/knip/src/ConfigurationValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const pluginsSchema = z.object({
linthtml: pluginSchema,
markdownlint: pluginSchema,
mocha: pluginSchema,
msw: pluginSchema,
netlify: pluginSchema,
next: pluginSchema,
'node-test-runner': pluginSchema,
Expand Down
1 change: 1 addition & 0 deletions packages/knip/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export { default as lintStaged } from './lint-staged/index.js';
export { default as linthtml } from './linthtml/index.js';
export { default as markdownlint } from './markdownlint/index.js';
export { default as mocha } from './mocha/index.js';
export { default as msw } from './msw/index.js';
export { default as netlify } from './netlify/index.js';
export { default as next } from './next/index.js';
export { default as nodeTestRunner } from './node-test-runner/index.js';
Expand Down
38 changes: 38 additions & 0 deletions packages/knip/src/plugins/msw/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { join } from '../../util/path.js';
import { basename } from '../../util/path.js';
import { hasDependency } from '../../util/plugin.js';
import { toEntryPattern } from '../../util/protocols.js';
import type { MSWConfig } from './types.js';
import type { GenericPluginCallback, IsPluginEnabledCallback } from '../../types/plugins.js';

// https://mswjs.io/docs/integrations/browser

const NAME = 'Mock Service Worker';

const ENABLERS = ['msw'];

const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

const CONFIG_FILE_PATTERNS = ['package.json'];

const ENTRY_FILE_PATTERNS = ['mockServiceWorker.js'];

const findDependencies: GenericPluginCallback = async (configFilePath, options) => {
const { manifest } = options;

// @ts-expect-error Bug in TS? (see other plugins with `await load`)
const localConfig: MSWConfig | undefined = basename(configFilePath) === 'package.json' ? manifest.msw : undefined;

const workerDirectory = localConfig?.workerDirectory ?? '.';

return ENTRY_FILE_PATTERNS.map(pattern => toEntryPattern(join(workerDirectory, pattern)));
};

export default {
NAME,
ENABLERS,
isEnabled,
CONFIG_FILE_PATTERNS,
ENTRY_FILE_PATTERNS,
findDependencies,
};
3 changes: 3 additions & 0 deletions packages/knip/src/plugins/msw/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface MSWConfig {
workerDirectory?: string;
}
3 changes: 1 addition & 2 deletions packages/knip/src/plugins/next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ const productionEntryFilePatternsWithoutSrc = [
'{instrumentation,middleware}.{js,ts}',
'app/global-error.{js,jsx,ts,tsx}',
'app/**/{error,layout,loading,not-found,page,template}.{js,jsx,ts,tsx}',
'app/**/route.{js,ts}',
'app/**/default.{js,jsx,ts,tsx}',
'app/**/{route,default}.{js,ts}',
'app/{manifest,sitemap,robots}.{js,ts}',
'app/**/{icon,apple-icon}.{js,jsx,ts,tsx}',
'app/**/{opengraph,twitter}-image.{js,jsx,ts,tsx}',
Expand Down
24 changes: 24 additions & 0 deletions packages/knip/test/plugins/msw.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { main } from '../../src/index.js';
import { resolve } from '../../src/util/path.js';
import baseArguments from '../helpers/baseArguments.js';
import baseCounters from '../helpers/baseCounters.js';

const cwd = resolve('fixtures/plugins/msw');

test('Should not see the msw files in issues', async () => {
const { counters } = await main({
...baseArguments,
cwd,
isStrict: true,
});

assert.deepEqual(counters, {
...baseCounters,
files: 4,
devDependencies: 1,
total: 5,
processed: 5,
});
});

0 comments on commit 1404348

Please sign in to comment.