Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/mnater/hyphenopoly
Browse files Browse the repository at this point in the history
  • Loading branch information
mnater committed Oct 16, 2022
2 parents fc4c051 + 635466d commit 9997d85
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 19 additions & 0 deletions docs/Node-Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ const hyphenator = hyphenopoly.config({
````
This is useful if the module is transformed to a script used in a web browser (e.g. by using [browserify](http://browserify.org)).
You can also add your custom loader to handle the load of the language files. To do this, just set a callback function as a `loader`.
It should return a `promise` that resolves with a buffer of the language file.
````javascript
async function loader(file) {
if (process.browser) {
const res = await fetch(file)
return res.arrayBuffer()
}
const fs = require('fs/promises')
return fs.readFile(file)
}
const hyphenator = hyphenopoly.config({
"require": […],
"loader": loader
});
````
### other options
For documentation about the other options see the `Hyphenopoly.js`-documentation:
Expand Down
7 changes: 6 additions & 1 deletion hyphenopoly.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ H.supportedLanguages = [
* @returns {undefined}
*/
function readFile(file, cb, sync) {
if (H.c.loader === "fs") {
if (typeof H.c.loader === "function") {
H.c.loader(file).then(
(res) => { cb(null, res); },
(err) => { cb(err); }
);
} else if (H.c.loader === "fs") {
/* eslint-disable security/detect-non-literal-fs-filename */
if (sync) {
return loader.readFileSync(file);
Expand Down

0 comments on commit 9997d85

Please sign in to comment.