I`m trying to combine javascript files, I tried uglifyjs and it works but I want to eliminate duplicate require for the same npm library. Use case: I have file1.js and file2.js. Both files are using for example request npm module, and when I combine these two files into one, it duplicates the require('request'). Is there an option or something I can eliminate this issue?
Thanks.
One way to do that is to pass the module as parameter to another file. For example:
file1.js
var request = require('request');
var file2 = require('./file2')(request);
module.exports = {...};
file2.js
module.exports = function(request) {
// use "request" parameter, instead of requiring.
return {...};
};
Update:
import * as file2 from './file2';
const file2Module = file2(request);