Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

javascript - node.js module/require index es5 supporting my own complex library

I'm new to node.js but have developed enough server-side code that exporting and requiring a file for each custom-written library function is getting unwieldy. I've been reading about alternatives and am confused--perhaps because I should compromise on my goal.

There are 3 separate reusable .js files that are part of one logical kind of task. Let's call the task-type, "x".

  • xLibrary.js - misc utilities specific to that task - many functions
  • xSubtask1.js - some complex code and supporting functions
  • xSubtask2.js - some complex code and supporting functions

I understand how to proceed if I treat each file as a separate module with a separate variable that references it. But since the subtasks are arbitrarily segregated (due to complexity not logical association) it creates confusion to have to remember which file contains a given function.

I'd like to put all the modules in 1 subdirectory and reference all the functions as xTask.*. The only way I know to do that is to combine all the files into one, but that makes maintaining the code more complex.

I've read articles and stackoverflow until my head is spinning. Many of the more comprehensive answers use logic to add each module in a subdirectory. I think hard-coding function and file names is more appropriate for my current level of expertise.

Can someone tell me how to proceed?

question from:https://stackoverflow.com/questions/65650039/node-js-module-require-index-es5-supporting-my-own-complex-library

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For CommonJS modules, you can make a parent module that collects all the exports from a set of sub-modules and then exports them all as part of one main object. Here's some code I used in one of my projects that does that:

// this just re-exports everything that the sub-modules export
module.exports = [
    './mapConcurrent.js',
    './deferred.js',
    './utils.js',
    './rateMap.js',
    './concurrency.js',
    './retry.js',
].reduce((obj, file) => {
    const m = require(file);
    Object.assign(obj, m);
    return obj;
}, {});

I put this in an index.js file that then allowed a user of this module to just require() the directory name and they would get an imported object with all the entry points on it for the whole module.

Note: This assumes that none of the exported symbols in any of the sub-modules have conflicting names. That is under your own control to manage so that's a requirement to do this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...