-
Notifications
You must be signed in to change notification settings - Fork 16
Description
Hi, I'm using the library in browser, via npm, bundling with esbuild. The code works fine, but bundled output file got much bigger than I expected.
npx esbuild --bundle --format=esm node_modules/eld/src/languageDetector.js > out.js
11M 11 18 19:57 out.js
The reason is all ngram data (XS~L) have been bundled into the single output file even though most of them are not actually imported. This is a result of bundler's complicated behavior. Bundled codes will lose entire relative path information at the end, so bundler tries to find and bundle all possible submodules within the project.
Unfortunately, this happens just using import()
statement with reachable path patterns. So, it's better to have static entry point for each size of ngram data. I propose to split exports for various environments by exports field in package.json.
package.json
"exports": {
".": {
"import": "./src/entries/dynamic.js",
},
"./src/entries/static.L60.js": {
"import": "./src/entries/static.L60.js",
},
"./src/entries/static.M60.js": {
"import": "./src/entries/static.M60.js",
},
"./src/entries/static.S60.js": {
"import": "./src/entries/static.S60.js",
},
"./src/entries/static.XS60.js": {
"import": "./src/entries/static.XS60.js",
}
},
static.S60.js
import { setNgrams } from "../languageData.js";
import { ngramsData } from "../ngrams/ngramsS60.js";
setNgrams(ngramsData);
export * from "../languageDetector.js";
dynamic.js
import { setNgrams, languageData } from "../languageData.js";
import { eld } from "../languageDetector.js";
/**
* @param {string} file File inside /ngrams/, with ELD ngrams data format
* @returns {boolean|undefined} true if file was loaded
*/
export async function loadNgrams(file) {
return await import("../ngrams/" + file).then((module) => {
setNgrams(module.ngramsData);
if (languageData.type) {
return true;
}
});
}
await loadNgrams("ngramsM60.js");
const withLoader = { ...eld, loadNgrams };
export { withLoader as eld };
Thank you. Please consider.