mnater / Hyphenopoly

Hyphenation for node and Polyfill for client-side hyphenation.
http://mnater.github.io/Hyphenopoly/
MIT License
689 stars 45 forks source link

Provide Hyphenopoly module path to loader() / loaderSync()? #207

Closed danburzo closed 9 months ago

danburzo commented 9 months ago

Since Hyphenopoly requires an explicit loader / loaderSync function in Node and other non-browser JS runtimes, it would be great if these functions received Hyphenopoly’s module URL as a second argument, to make it easier to locate the patterns folder.

For example, percollate, which uses Hyphenopoly for hyphenation, can be run either as a standalone CLI or a Node.js dependency. As I’ve just been reminded, only the former has node_modules/hyphenopoly/patterns.

Getting import.meta.url as the second argument to the loader() / loaderSync() functions — or, even better, maybe something like new URL('./patterns', import.meta.url) — would help us prevent things like createRequire() to locate the patterns folder:

// before
loaderSync: file => {
  return readFileSync(
    createRequire(import.meta.url).resolve(`hyphenopoly/patterns/${file}`)
  );
}

// after
loaderSync: (file, patternsFolder) => {
   return readFileSync(new URL(`./${file}`, patternsFolder));
}

cc @yashha

mnater commented 9 months ago

Thanks for pointing to this. It makes the loaders a lot simpler indeed.