Answer the question
In order to leave comments, you need to log in
How to replace a module when building a JavaScript package?
There is a certain library, in the code of which the logger module is requested :
// Исходный не собранный в пакет код, может выполнятся без сбоки в node.js
var logger = require('../../tools/logger');
module.exports = {
// ...
};
The implementation of the logger module is environment dependent, so if building for the browser, a different logger module is used . require('../../src/logger');
'../../src/logger/browser'
Answer the question
In order to leave comments, you need to log in
Now for these purposes I use a folder node_modules
inside the project.
For example:
src/node_modules/_project/logger/index.js
src/node_modules/_project/logger/browser/index.js
_project/logger/index.js
. src/node_modules
in .gitignorenode_modules
!src/node_modules
You need the "interface" pettern, i.e. the same appearance of the logger and different implementation, loaded depending on the platform. Look here the interface is implemented through impurities: habrahabr.ru/post/183188 Here is the library itself with examples https://github.com/tshemsedinov/global.js And a live example of using this method can be found here https://github.com/ tshemsedinov/impress /lib/ directory has impress.security.js and impress.security.mongodb.js is its implementation or db.js is interface and its implementations are db.mysql.js, db.mongodb. js, db.memcached.js. In the latter case (with the db interface), they not only implement the interface, but also extend it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question