Skip to content

Commit

Permalink
Wrap up
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Mar 14, 2024
1 parent 213aa6e commit bb39123
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/docs/scripts/generate-plugin-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import remarkStringify from 'remark-stringify';
import { unified } from 'unified';
import { u } from 'unist-builder';
import { base } from '../config.js';
import type { Plugin } from 'knip/dist/types/plugins.ts';
import type { Plugin } from '../../knip/src/types/plugins.js';
import type { Root } from 'mdast';
import type { Node } from 'unist';

Expand Down
Empty file.
6 changes: 0 additions & 6 deletions packages/knip/fixtures/plugins/msw/mockServiceWorker.js

This file was deleted.

Empty file.

This file was deleted.

1 change: 0 additions & 1 deletion packages/knip/fixtures/plugins/msw/src/mocks/browser.ts

This file was deleted.

Empty file.
Empty file.
Empty file.
1 change: 0 additions & 1 deletion packages/knip/fixtures/plugins/msw/src/mocks/server.ts

This file was deleted.

26 changes: 8 additions & 18 deletions packages/knip/src/plugins/msw/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// eslint-disable-next-line n/no-restricted-import
import path from 'node:path';
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
Expand All @@ -15,27 +15,17 @@ const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(d

const CONFIG_FILE_PATTERNS = ['package.json'];

const ENTRY_FILE_PATTERNS = [
'mockServiceWorker.js',
];
const ENTRY_FILE_PATTERNS = ['mockServiceWorker.js'];

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

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

if (workerDirectory) {
return [toEntryPattern(path.join(workerDirectory, '/mockServiceWorker.js'))];
}
const workerDirectory = localConfig?.workerDirectory ?? '.';

const entryPatterns = ENTRY_FILE_PATTERNS.map(toEntryPattern);

if (isProduction) return entryPatterns;

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

export default {
Expand Down
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;
}
15 changes: 5 additions & 10 deletions packages/knip/test/plugins/msw.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { main } from '../../src/index.js';
import { join, resolve } from '../../src/util/path.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 { issues, counters } = await main({
const { counters } = await main({
...baseArguments,
cwd,
isStrict: true,
});

assert.equal(issues.files.size, 13);
assert(issues.files.has(join(cwd, 'junk.js')));
assert(issues.files.has(join(cwd, 'mocks/junk.js')));
assert(issues.files.has(join(cwd, 'src/mocks/junk.js')));

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

0 comments on commit bb39123

Please sign in to comment.