如何在gump工作流中同时在独立文件和依赖文件中输出javascript

How to output javascript in both a standalone and a dependency file in a gulp workflow

本文关键字:文件 输出 javascript 独立 依赖 gump 工作流      更新时间:2023-11-15

我用javascript开发了很多小项目,这些项目可以用作独立脚本或其他脚本的依赖项。如果我的所有脚本的末尾都有module.exports = ObjName,这样它们就可以在CommonJS环境中用作资源,那么当在web环境中用作独立脚本时,它们将生成错误。

考虑到我正在使用Gulp工作流来开发我的脚本,构建依赖版本和独立web集成版本的最佳方式是什么?

他们的库在github上这样做有什么好的例子吗?

我找到了这个解决方案。它测试模块和模块。导出属性

// new namespace object defined by this file
var mylib = {};
// import used by this file
var dependency = dependency || require('./dependency');  
(function(){
    // define properties on mylib, use dependency
    // export the namespace object
    if (typeof module !== 'undefined' && module.exports) {
        module.exports = mylib;
    }
})();