-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate YAML-based Regex Rules to CommonJS Modules #215
Conversation
This commit introduces a new utility script that converts YAML regex files to CommonJS modules. The script recursively processes all YAML files in the regexes directory and generates corresponding .js files with the same structure. This is a preparatory step for removing the filesystem dependency in the main package, making it more suitable for various JavaScript environments.
This commit transforms the regexes detection rules from YAML to CommonJS format as part of the ongoing effort to remove filesystem dependencies. The conversion maintains all existing regex patterns and device detection logic while making the rules more suitable for direct consumption in JavaScript environments.
This commit completes the migration from YAML to CommonJS modules by removing all YAML file loading logic and replacing it with direct requires. The changes include removing the base regex directory path constant, collection map caching, and file loading methods from the abstract parser. All parser classes now directly require their respective CommonJS regex modules instead of loading YAML files at runtime.
if (/\.ya?ml$/.test(file) === false) { | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to avoid checking the new .js
files.
static ready() { | ||
return collection !== void 0; | ||
} | ||
|
||
static init() { | ||
if (helper.hasFile(path)) { | ||
collection = helper.loadYMLFile(path); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer required as the collection is ready once this module is resolved.
static ready() { | ||
return collection !== void 0; | ||
} | ||
|
||
static init() { | ||
if (helper.hasFile(path)) { | ||
collection = helper.loadYMLFile(path); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer required as the collection is ready once this module is resolved.
get collection() { | ||
if (!this.hasLoadCollection()) { | ||
return null; | ||
} | ||
return collectionMap[this.fixtureFile]; | ||
} | ||
|
||
hasLoadCollection() { | ||
return collectionMap[this.fixtureFile] !== void 0 | ||
} | ||
|
||
/** | ||
* load collection | ||
*/ | ||
loadCollection() { | ||
if (!this.hasLoadCollection()) { | ||
collectionMap[this.fixtureFile] = this.loadYMLFile(this.fixtureFile); | ||
} | ||
} | ||
|
||
/** | ||
* load yaml file | ||
* @param {string} file | ||
* @returns {*} | ||
*/ | ||
loadYMLFile(file) { | ||
return helper.loadYMLFile(BASE_REGEXES_DIR + file); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer required as the collections are in memory.
Why?
|
Well, looked through all new code, everything's fine. Starting with version Thank you for contribution. |
Hello !
First, thank you for building and maintaining this library—it’s been a huge help. While bundling our application into a Docker container, We ran into issues because this library depends on the file system at runtime (YAML regexes). We patched it locally by converting everything to CommonJS modules, and I’m sharing that patch here in case others encounter the same problem.
Issue
pnpx esbuild ./index.js --bundle --platform=node > out.js
Workaround
server.js
file. This is an issue for us.pnpx esbuild ./index.js --bundle --platform=node --packages=external > out.js
Solution
node-device-detector
and its regex definitions can be bundled together in a single output without relying on the presence ofnode_modules
.Considerations
require
statements might raise overhead concerns, module resolution caches them by default, so performance impact should be minimal.Changes
.js
modules.require
these.js
modules directly.Let me know what you think and thanks again for all your work on this project!