When a package name contains a dot (e.g. chart.js) and is imported via a subpath export (e.g. chart.js/auto), the server crashes with a warning and an error:
WARN Import 'chart.js/auto' contains a bad dot (.) import.
NX Invalid 'externals' config. Invalid imports paths detected, consider using a barrel import instead.
I debugged this a bit and it seems like checkForInvalidImports function looks at the full import path for its dot check instead of the filename segment only. It sees the dot in chart.js, which lies before the last /, and concludes it's a suspicious import.
The subpath is valid according to https://github.com/chartjs/Chart.js/blob/cb02e1d207bd4c4c40b20c259017f85f26f1e30a/package.json#L24
In my own repo, I patched it by only checking the last path segment for dots.
diff --git a/node_modules/@angular-architects/native-federation-v4/src/utils/check-for-invalid-imports.js b/node_modules/@angular-architects/native-federation-v4/src/utils/check-for-invalid-imports.js
index b3a2fd9..9ab2ab3 100644
--- a/node_modules/@angular-architects/native-federation-v4/src/utils/check-for-invalid-imports.js
+++ b/node_modules/@angular-architects/native-federation-v4/src/utils/check-for-invalid-imports.js
@@ -9,12 +9,15 @@ export function checkForInvalidImports(importList, type) {
const queryIndex = mappingImport.search(/[?#]/);
const sanitizedImport = queryIndex >= 0 ? mappingImport.slice(0, queryIndex) : mappingImport;
const segmentStart = sanitizedImport.lastIndexOf('/') + 1;
- const dotIndex = sanitizedImport.lastIndexOf('.');
- if (dotIndex < segmentStart) {
- importsWithDot.push(mappingImport);
+ const lastSegment = sanitizedImport.slice(segmentStart);
+ const dotIndex = lastSegment.lastIndexOf('.');
+ 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);
}
| Package |
Version |
| Angular |
21.2.0 |
| nx |
22.6.5 |
| primeng |
21.1.6 |
| chart.js |
4.5.1 |
| @angular-architects/native-federation-v4 |
21.2.3 |
| @softarc/native-federation |
4.1.3 |
| @softarc/native-federation-orchestrator |
4.2.2 |
(I'm mentioning primeng because that's where I get the chart.js/auto import from)
When a package name contains a dot (e.g. chart.js) and is imported via a subpath export (e.g. chart.js/auto), the server crashes with a warning and an error:
I debugged this a bit and it seems like
checkForInvalidImportsfunction looks at the full import path for its dot check instead of the filename segment only. It sees the dot in chart.js, which lies before the last/, and concludes it's a suspicious import.The subpath is valid according to https://github.com/chartjs/Chart.js/blob/cb02e1d207bd4c4c40b20c259017f85f26f1e30a/package.json#L24
In my own repo, I patched it by only checking the last path segment for dots.
(I'm mentioning primeng because that's where I get the chart.js/auto import from)