Skip to content
Open
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
62 changes: 62 additions & 0 deletions packages/angular/src/utils/check-for-invalid-imports.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { logger } from '@softarc/native-federation/internal';

import { checkForInvalidImports } from './check-for-invalid-imports.js';

describe('checkForInvalidImports', () => {
const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => undefined);
const debugSpy = vi.spyOn(logger, 'debug').mockImplementation(() => undefined);

afterEach(() => {
warnSpy.mockClear();
debugSpy.mockClear();
});

afterAll(() => {
warnSpy.mockRestore();
debugSpy.mockRestore();
});

it('allows common import paths without throwing', () => {
const imports = [
'rxjs',
'@angular/core',
'@angular/common/http',
// plain package with a dot in its name
'chart.js',
// secondary entry points of packages whose name contains a dot
'chart.js/auto',
'chart.js/helpers',
'd3-scale/src/index.js',
'./polyfills.ts',
'./data.json',
'my-lib/runtime.mjs',
'my-lib/chunk.js?v=1',
'my-lib/chunk.ts#fragment',
];

expect(() => checkForInvalidImports(imports, 'externals')).not.toThrow();
expect(warnSpy).not.toHaveBeenCalled();
expect(debugSpy).not.toHaveBeenCalled();
});

it('throws for invalid dot imports and logs warnings', () => {
const imports = ['lodash.merge', '@scope/lib.v2', './feature'];

expect(() => checkForInvalidImports(imports, 'shared mappings')).toThrow(
"Invalid 'shared mappings' config. Invalid imports paths detected, consider using a barrel import instead. "
);
expect(warnSpy).toHaveBeenCalledTimes(imports.length);
expect(warnSpy).toHaveBeenNthCalledWith(
1,
"Import 'lodash.merge' contains a bad dot (.) import."
);
expect(warnSpy).toHaveBeenNthCalledWith(
2,
"Import '@scope/lib.v2' contains a bad dot (.) import."
);
expect(warnSpy).toHaveBeenNthCalledWith(3, "Import './feature' contains a bad dot (.) import.");
expect(debugSpy).toHaveBeenCalledWith(
'Bad import issue: https://github.com/vitejs/vite/issues/21036'
);
});
});
13 changes: 8 additions & 5 deletions packages/angular/src/utils/check-for-invalid-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ const ALLOWED_FILE_EXTENSIONS = new Set(['mjs', 'js', 'mts', 'ts', 'jsx', 'tsx',
export function checkForInvalidImports(importList: string[], type: string) {
const importsWithDot = [];
for (const mappingImport of importList) {
if (mappingImport.indexOf('.') < 0) {
if (!mappingImport.includes('.')) {
continue;
}

const queryIndex = mappingImport.search(/[?#]/);
const sanitizedImport = queryIndex >= 0 ? mappingImport.slice(0, queryIndex) : mappingImport;

const segmentStart = sanitizedImport.lastIndexOf('/') + 1;
const dotIndex = sanitizedImport.lastIndexOf('.');
const lastSegment = sanitizedImport.slice(segmentStart);
const dotIndex = lastSegment.lastIndexOf('.');

if (dotIndex < segmentStart) {
importsWithDot.push(mappingImport);
if (dotIndex < 0) {
if (sanitizedImport.startsWith('./') || sanitizedImport.startsWith('../')) {
importsWithDot.push(mappingImport);
}
continue;
}

const extension = sanitizedImport.slice(dotIndex + 1);
const extension = lastSegment.slice(dotIndex + 1);
if (!ALLOWED_FILE_EXTENSIONS.has(extension)) {
importsWithDot.push(mappingImport);
}
Expand Down
Loading