I am using npm for my projects and I wonder how would I implement this in my project.I saw some code examples like this
function lazyCache(fn) {
var cache = {};
var proxy = function(mod, name) {
name = name || camelcase(mod);
// check both boolean and string in case `process.env` cases to string
if (process.env.UNLAZY === 'true' || process.env.UNLAZY === true || process.env.TRAVIS) {
cache[name] = fn(mod);
}
Object.defineProperty(proxy, name, {
enumerable: true,
configurable: true,
get: getter
});
function getter() {
if (cache.hasOwnProperty(name)) {
return cache[name];
}
return (cache[name] = fn(mod));
}
return getter;
};
return proxy;
}
module.exports = lazyCache;
in another js file
var om = require('lazyCahe');
How to implement this in my own project?
project
index.html
om.js
am.js
om.js
var om = function() {
console.log("oh my om");
}
module.exports = om;
am.js
var am = require('om');
(index):10 Uncaught ReferenceError: require is not defined
But if look into some code examples in node modules, example the laravel-elixir npm package, it does not show how the 'require' is configured.
This instructions are from NodeJS, and the Browser don't know it. If you want export and import modules like in this example you should build your app with rollup.js or webpack.js
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.