payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.25k stars 1.47k forks source link

Custom admin route not taken into account in generate:importmap #7803

Open n-a-t-j-b opened 3 weeks ago

n-a-t-j-b commented 3 weeks ago

Link to reproduction

No response

Payload Version

3.0.0-beta.88

Node Version

20.15.0

Next.js Version

15.0.0-canary.104

Describe the Bug

When changing the admin/api routes in payload config, generate:importmap fails due to hardcoded path in \node_modules\payload\dist\bin\generateImportMap\index.js

Reproduction Steps

Change the folder names to app(payload)/custom-admin and app(payload)/custom-api to and put the corresponding names in payload.config.ts as follows:

routes: {
  admin: '/custom-admin',
  api: '/custom-api',
}

...then run generate:importmap and get:


Error: Could not find the payload admin directory. Looked in C:\<payload app dir>\app\(payload)\admin and C:\<payload app dir>\src\app\(payload)\admin
    at writeImportMap (C:\<payload app dir>\node_modules\payload\src\bin\generateImportMap\index.ts:181:11)
    at generateImportMap (C:\<payload app dir>\node_modules\payload\src\bin\generateImportMap\index.ts:150:9)
    at bin (C:\<payload app dir>\node_modules\payload\dist\bin\index.js:64:16)
    at async start (file:///C:/<payload app dir>/node_modules/payload/bin.js:30:7)

Node.js v20.15.0

Proposed fix (apologies I could not work out how to add a pull request for this):

export async function generateImportMap(config, options) {
    const shouldLog = options?.log ?? true;
...
...
    await writeImportMap({
        componentMap: importMap,
        fileName: 'importMap.js',
        force: options?.force,
        importMap: imports,
        log: shouldLog,
        rootDir,
        config
    });
}
export async function writeImportMap({ componentMap, fileName, force, importMap, log, rootDir, config }) {
    const adminPath = config.routes.admin || '/admin';
    let importMapFolderPath = '';
    if (fs.existsSync(path.resolve(rootDir, `app/(payload)${adminPath}/`))) {
        importMapFolderPath = path.resolve(rootDir, `app/(payload)${adminPath}/`);
    } else if (fs.existsSync(path.resolve(rootDir, `src/app/(payload)${adminPath}/`))) {
        importMapFolderPath = path.resolve(rootDir, `src/app/(payload)${adminPath}/`);
    } else {
        throw new Error(`Could not find the payload admin directory. Looked in ${path.resolve(rootDir, `app/(payload)${adminPath}/`)} and ${path.resolve(rootDir, `src/app/(payload)${adminPath}/`)}`);
    }
    const imports = [];
...
...

Changes tested locally and seem to work as intended. Cheers.

Adapters and Plugins

No response

jmikrut commented 3 weeks ago

Good call! On it