CloudCannon / pagefind

Static low-bandwidth search at scale
https://pagefind.app
MIT License
3.34k stars 100 forks source link

Consider a CJS build for the wrapper npm package #471

Open njfamirm opened 10 months ago

njfamirm commented 10 months ago

Hi 🙌🏻

when i want to use this in the 11ty with cjs, with below config,

this is my current config:

const {createIndex, addDirectory,writeFiles, close} = require('pagefind');

async function indexContent() {
  await createIndex();
  await addDirectory({path: 'dist'});
  await writeFiles({outputPath: 'dist/pagefind'});
  await close();
}

module.exports = {indexContent};
eleventyConfig.on('eleventy.after', indexContent);

have this error:

[11ty] Eleventy CLI Fatal Error: (more in DEBUG output)
[11ty] 1. Error in your Eleventy config file 'eleventy.config.cjs'. (via EleventyConfigError)
[11ty] 2. No "exports" main defined in /Users/njfamirm/repo/blog/node_modules/pagefind/package.json (via Error)
[11ty]
[11ty] Original error stack trace: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/njfamirm/repo/blog/node_modules/pagefind/package.json
[11ty]     at new NodeError (node:internal/errors:393:5)
[11ty]     at exportsNotFound (node:internal/modules/esm/resolve:295:10)
[11ty]     at packageExportsResolve (node:internal/modules/esm/resolve:575:13)
[11ty]     at resolveExports (node:internal/modules/cjs/loader:538:36)
[11ty]     at Module._findPath (node:internal/modules/cjs/loader:607:31)
[11ty]     at Module._resolveFilename (node:internal/modules/cjs/loader:1025:27)
[11ty]     at Module._load (node:internal/modules/cjs/loader:885:27)
[11ty]     at Module.require (node:internal/modules/cjs/loader:1105:19)
[11ty]     at require (node:internal/modules/cjs/helpers:103:18)
[11ty]     at Object.<anonymous> (/Users/njfamirm/repo/blog/config/pagefind.js:1:55)

how can solve it?

thanks...

bglw commented 10 months ago

Hey @njfamirm 👋

Currently Pagefind's npm interface only supports being used as an ESM module via import, rather than as a CJS package through require.

I haven't run it to confirm, but changing to an async import should work for you. Also, addDirectory and writeFiles aren't imported from the pagefind module itself, but are instead methods on the index returned from createIndex:

async function indexContent() {
  const { createIndex, close } = await import('pagefind');
  const { index } = await createIndex();
  await index.addDirectory({path: 'dist'});
  await index.writeFiles({outputPath: 'dist/pagefind'});
  await close();
}

module.exports = {indexContent};
njfamirm commented 10 months ago

Thanks @bglw! I'm test it.

would you like to include cjs in the output with the build tools? and publish to npm? I don't know the process, but probably in the next few days I want to do this for our own packages. If you want, I can do it for this project as well 🙌🏻

bglw commented 10 months ago

For now I'd like to wait and see how often this crops up. The wrapper package currently doesn't go through any build process, so I'm hesitant to add a build process there when the package can be async imported in a pinch.