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

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

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

考虑到我正在使用 Gulp 工作流程来开发我的脚本,构建一个版本作为依赖项和一个版本用于独立 Web 集成的最佳方法是什么?

他们在 github 上是否有图书馆这样做的好例子?

我找到了 this solution。它测试模块和 module.exports 属性

// 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;
    }
})();