@emahuni/directus
Version:
A wrapper around the Directus CLI to enhance its module resolution capabilities, allowing for more flexible and efficient management of dependencies in the Directus API/server.
36 lines (30 loc) • 1.3 kB
JavaScript
import path from 'node:path';
import fs from 'node:fs';
/**
* A custom plugin to resolve external dependencies from API node_modules
* Acts only as a fallback if normal resolution fails
*/
export default function resolveFromApiNodeModules (apiRoot) {
return {
name: 'resolve-from-api-node-modules',
async resolveId (source, importer) {
// Skip relative and absolute imports
if (source.startsWith('.') || path.isAbsolute(source)) {
return null;
}
// First, let Rollup (and other plugins) try
const resolved = await this.resolve(source, importer, { skipSelf: true });
if (resolved) return null; // already resolved by Rollup
// Try to resolve from API root node_modules
const modulePath = path.join(apiRoot, 'node_modules', source);
const pkgJsonPath = path.join(modulePath, 'package.json');
if (fs.existsSync(pkgJsonPath)) {
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
const mainFile = pkg.module || pkg.main || 'index.js';
return path.join(modulePath, mainFile);
}
// No match, let Rollup keep handling
return null;
},
};
}