Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 35 additions & 29 deletions packages/core/src/config/__tests__/config-resolvers.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'node:path';
import { after } from 'node:test';
import { fileURLToPath } from 'node:url';
import util from 'node:util';

Expand All @@ -10,13 +9,27 @@
import recommended from '../recommended.js';
import type { RawUniversalConfig, RawGovernanceConfig } from '../types.js';

vi.mock('node:module', () => ({
default: {
createRequire: () => ({
resolve: (path: string) => `/mock/path/${path}`,
}),
},
}));
vi.mock('node:module', async (importOriginal) => {
const { default: nodeModule } = await importOriginal<typeof import('node:module')>();

Check failure on line 13 in packages/core/src/config/__tests__/config-resolvers.test.ts

View workflow job for this annotation

GitHub Actions / build-and-unit

Property 'default' does not exist on type 'typeof Module'.
return {
default: {
...nodeModule,
createRequire(baseUrl: string) {
const req = nodeModule.createRequire(baseUrl);
return {
...req,
resolve: (id: string, opts?: any) => {
try {
return req.resolve(id, opts);
} catch {
return `/mock/path/${id}`;
}
},
};
},
},
};
});

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -638,28 +651,21 @@
});

it('should work with npm dependencies', async () => {
after(() => {
(globalThis as any).__webpack_require__ = undefined;
(globalThis as any).__non_webpack_require__ = undefined;
});

(globalThis as any).__webpack_require__ = () => {};
(globalThis as any).__non_webpack_require__ = (p: string) =>
p === '/mock/path/test-plugin'
? {
id: 'npm-test-plugin',
}
: {
id: 'local-test-plugin',
};
vi.doMock('/mock/path/test-plugin', () => ({ default: { id: 'npm-test-plugin' } }));
vi.doMock('/mock/path/fixtures/plugin.cjs', () => ({ default: { id: 'local-test-plugin' } }));

const { resolvedConfig } = await resolveConfig({
rawConfigDocument: makeDocument(
{ plugins: ['test-plugin', 'fixtures/plugin.cjs'] },
configPath
),
});
expect(resolvedConfig.plugins).toEqual(['test-plugin', 'fixtures/plugin.cjs']);
try {
const { resolvedConfig } = await resolveConfig({
rawConfigDocument: makeDocument(
{ plugins: ['test-plugin', 'fixtures/plugin.cjs'] },
configPath
),
});
expect(resolvedConfig.plugins).toEqual(['test-plugin', 'fixtures/plugin.cjs']);
} finally {
vi.doUnmock('/mock/path/test-plugin');
vi.doUnmock('/mock/path/fixtures/plugin.cjs');
}
});

it('should work with nested schema', async () => {
Expand Down
23 changes: 11 additions & 12 deletions packages/core/src/config/config-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ import {

// Cache instantiated plugins during a single execution
const pluginsCache: Map<string, Plugin[]> = new Map();
let pluginsCacheVersion = 0;

export const clearPluginsCache = (): void => {
pluginsCache.clear();
pluginsCacheVersion++;
};

export type PluginResolveInfo = {
absolutePath: string;
Expand Down Expand Up @@ -215,18 +221,11 @@ export async function resolvePlugins(
).absolutePath;

if (!pluginsCache.has(absolutePluginPath)) {
let requiredPlugin: ImportedPlugin | undefined;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore FIXME: investigate if we still need this (2.0)
if (typeof __webpack_require__ === 'function') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore FIXME: investigate if we still need this (2.0)
requiredPlugin = __non_webpack_require__(absolutePluginPath);
} else {
const mod = await import(url.pathToFileURL(absolutePluginPath).pathname);
requiredPlugin = mod.default || mod;
}
const pluginUrl = url.pathToFileURL(absolutePluginPath).pathname;
const mod = await import(
pluginsCacheVersion ? `${pluginUrl}?v=${pluginsCacheVersion}` : pluginUrl
);
const requiredPlugin: ImportedPlugin | undefined = mod.default || mod;

const pluginCreatorOptions = { contentDir: configDir };

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
createConfig,
findConfig,
resolvePlugins,
clearPluginsCache,
ConfigValidationError,
Config, // FIXME: export it as a type
type RawUniversalConfig,
Expand Down
Loading