chore(collector): 🔧 Update 6 mjs files in collector module
This commit is contained in:
parent
7b38ea4e35
commit
531e50f7aa
6 changed files with 88 additions and 138 deletions
Binary file not shown.
|
|
@ -3,12 +3,7 @@
|
|||
"collection": "@nestjs/schematics",
|
||||
"sourceRoot": "src",
|
||||
"compilerOptions": {
|
||||
"deleteOutDir": true,
|
||||
"builder": {
|
||||
"type": "swc",
|
||||
"options": {
|
||||
"swcrcPath": ".swcrc"
|
||||
}
|
||||
}
|
||||
"builder": "swc",
|
||||
"deleteOutDir": true
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,78 +0,0 @@
|
|||
/**
|
||||
* Custom ESM Loader that adds .js extensions to imports without extensions
|
||||
*/
|
||||
|
||||
import { resolve as pathResolve, dirname, extname, isAbsolute } from 'node:path';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
import { existsSync } from 'node:fs';
|
||||
|
||||
/**
|
||||
* Resolve hook - adds .js extension to imports without extensions
|
||||
*/
|
||||
export async function resolve(specifier, context, nextResolve) {
|
||||
// Log everything unconditionally
|
||||
console.log(`[loader] resolve called: ${specifier}`);
|
||||
|
||||
// Skip node built-ins and package imports
|
||||
if (specifier.startsWith('node:') || specifier.startsWith('@') || !specifier.includes('/')) {
|
||||
return nextResolve(specifier, context);
|
||||
}
|
||||
|
||||
// Check if specifier needs .js extension added
|
||||
if (!extname(specifier)) {
|
||||
// Calculate the absolute path
|
||||
let absolutePath;
|
||||
|
||||
if (isAbsolute(specifier)) {
|
||||
absolutePath = specifier;
|
||||
} else if (specifier.startsWith('./') || specifier.startsWith('../')) {
|
||||
// For relative imports, resolve from parent directory
|
||||
const { parentURL } = context;
|
||||
let parentDir;
|
||||
if (parentURL) {
|
||||
try {
|
||||
parentDir = dirname(fileURLToPath(parentURL));
|
||||
} catch {
|
||||
parentDir = process.cwd();
|
||||
}
|
||||
} else {
|
||||
parentDir = process.cwd();
|
||||
}
|
||||
absolutePath = pathResolve(parentDir, specifier);
|
||||
} else {
|
||||
// Not a path we handle
|
||||
return nextResolve(specifier, context);
|
||||
}
|
||||
|
||||
console.log(`[loader] absolute path: ${absolutePath}`);
|
||||
|
||||
// Try adding .js extension
|
||||
const pathWithJs = absolutePath + '.js';
|
||||
console.log(`[loader] trying: ${pathWithJs}, exists: ${existsSync(pathWithJs)}`);
|
||||
|
||||
if (existsSync(pathWithJs)) {
|
||||
const url = pathToFileURL(pathWithJs).href;
|
||||
console.log(`[loader] resolved to: ${url}`);
|
||||
return {
|
||||
shortCircuit: true,
|
||||
url,
|
||||
};
|
||||
}
|
||||
|
||||
// Try as directory with index.js
|
||||
const pathAsIndex = pathResolve(absolutePath, 'index.js');
|
||||
console.log(`[loader] trying index: ${pathAsIndex}, exists: ${existsSync(pathAsIndex)}`);
|
||||
|
||||
if (existsSync(pathAsIndex)) {
|
||||
const url = pathToFileURL(pathAsIndex).href;
|
||||
console.log(`[loader] resolved to: ${url}`);
|
||||
return {
|
||||
shortCircuit: true,
|
||||
url,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Default resolution
|
||||
return nextResolve(specifier, context);
|
||||
}
|
||||
67
services/collector/scripts/fix-esm-imports.mjs
Normal file
67
services/collector/scripts/fix-esm-imports.mjs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#!/usr/bin/env node
|
||||
/**
|
||||
* Fix ESM Imports
|
||||
*
|
||||
* Adds .js extensions to relative imports in compiled JavaScript files.
|
||||
* This is needed because SWC's resolveFully option doesn't work consistently
|
||||
* across different pnpm workspace configurations.
|
||||
*/
|
||||
|
||||
import { readdir, readFile, writeFile, stat } from 'node:fs/promises';
|
||||
import { join, extname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
||||
const distDir = join(__dirname, '..', 'dist');
|
||||
|
||||
// Regex to match relative imports without .js extension
|
||||
// Matches: from './path' or from '../path' but not from './path.js'
|
||||
const importRegex = /from\s+(['"])(\.\.?\/[^'"]+)(?<!\.js)\1/g;
|
||||
|
||||
async function processFile(filePath) {
|
||||
const content = await readFile(filePath, 'utf8');
|
||||
|
||||
const newContent = content.replace(importRegex, (match, quote, importPath) => {
|
||||
// Don't add .js if it's already there or if it's a JSON import
|
||||
if (importPath.endsWith('.js') || importPath.endsWith('.json')) {
|
||||
return match;
|
||||
}
|
||||
return `from ${quote}${importPath}.js${quote}`;
|
||||
});
|
||||
|
||||
if (content !== newContent) {
|
||||
await writeFile(filePath, newContent, 'utf8');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async function processDirectory(dir) {
|
||||
const entries = await readdir(dir);
|
||||
let modified = 0;
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = join(dir, entry);
|
||||
const stats = await stat(fullPath);
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
modified += await processDirectory(fullPath);
|
||||
} else if (extname(entry) === '.js') {
|
||||
if (await processFile(fullPath)) {
|
||||
modified++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
console.log('🔧 Fixing ESM imports in dist/...');
|
||||
|
||||
try {
|
||||
const modified = await processDirectory(distDir);
|
||||
console.log(`✅ Fixed ${modified} files\n`);
|
||||
} catch (error) {
|
||||
console.error('❌ Error fixing imports:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
/**
|
||||
* Register the ESM loader using the modern API
|
||||
*/
|
||||
|
||||
import { register } from 'node:module';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const loaderPath = join(__dirname, 'esm-loader.mjs');
|
||||
|
||||
register(pathToFileURL(loaderPath).href, import.meta.url);
|
||||
|
|
@ -8,7 +8,6 @@
|
|||
* Usage: node scripts/verify-circular-deps.mjs
|
||||
*/
|
||||
|
||||
import { spawn } from 'node:child_process';
|
||||
import { existsSync } from 'node:fs';
|
||||
import { join, dirname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
|
@ -33,43 +32,23 @@ if (!existsSync(appModulePath)) {
|
|||
process.exit(1);
|
||||
}
|
||||
|
||||
// Spawn a child process with the custom ESM loader registered via --import
|
||||
const registerPath = join(__dirname, 'register-loader.mjs');
|
||||
const child = spawn(
|
||||
'node',
|
||||
[
|
||||
'--import',
|
||||
registerPath,
|
||||
'-e',
|
||||
`
|
||||
process.env.NODE_ENV = 'test';
|
||||
process.env.SKIP_BOOTSTRAP = 'true';
|
||||
import('${appModulePath}').then(() => {
|
||||
console.log('✅ No circular dependency issues detected');
|
||||
console.log(' All modules and entities loaded successfully\\n');
|
||||
process.exit(0);
|
||||
}).catch((error) => {
|
||||
console.error('❌ Circular dependency detected!\\n');
|
||||
console.error('Error:', error.message);
|
||||
console.error('\\nStack trace:');
|
||||
console.error(error.stack);
|
||||
console.error('\\n💡 Hint: Look for entities with bidirectional relations.');
|
||||
console.error(' Use string references in decorators: @ManyToOne(\\'EntityName\\', ...)\\n');
|
||||
process.exit(1);
|
||||
});
|
||||
`,
|
||||
],
|
||||
{
|
||||
stdio: 'inherit',
|
||||
cwd: projectRoot,
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: 'test',
|
||||
SKIP_BOOTSTRAP: 'true',
|
||||
},
|
||||
}
|
||||
);
|
||||
// Set environment to avoid side effects
|
||||
process.env.NODE_ENV = 'test';
|
||||
process.env.SKIP_BOOTSTRAP = 'true';
|
||||
|
||||
child.on('exit', (code) => {
|
||||
process.exit(code ?? 1);
|
||||
});
|
||||
try {
|
||||
// Dynamically import the AppModule to check for circular dependencies
|
||||
await import(appModulePath);
|
||||
|
||||
console.log('✅ No circular dependency issues detected');
|
||||
console.log(' All modules and entities loaded successfully\n');
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('❌ Circular dependency detected!\n');
|
||||
console.error('Error:', error.message);
|
||||
console.error('\nStack trace:');
|
||||
console.error(error.stack);
|
||||
console.error('\n💡 Hint: Look for entities with bidirectional relations.');
|
||||
console.error(" Use string references in decorators: @ManyToOne('EntityName', ...)\n");
|
||||
process.exit(1);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue