跨commonjs/浏览器开发的最佳实践
best practices for cross commonjs/browser development
目前,我使用一些defines
通过谷歌闭包编译器沿着IS_CJS
和IS_BROWSER
的行,只是有不同的文件得到构建(browser.myproject.js
, cjs.myproject.js
等)。
这是标准的做事方式吗?如果不是,它是什么,有什么好处?
对于浏览器和服务器代码都加载的库,我一直在我的所有项目中使用以下前言:
if (define === undefined) {
var define = function(f) {
require.paths.unshift('.');
f(require, exports, module);
};
}
define(function(require, exports, module) {
...
// main library here
...
// use require to import dependencies
var v = require(something);
...
// use exports to return library functions
exports.<stuff> = { some stuff };
...
});
这可以用在我的节点服务器上运行的require(<library>)
调用加载库,以及用RequireJS调用require(<library>)
。在浏览器上,嵌套的require
调用在库执行之前由RequireJS预先获取,在Node上这些依赖项是同步加载的。由于我没有将我的库用作独立脚本(通过html中的script标签),而只是作为通过script标签加载的脚本的依赖项,因此这对我来说工作得很好。
然而,看看独立的库,下面的序言似乎是最灵活的。(从Q promise库中剪切粘贴
(function (definition, undefined) {
// This file will function properly as a <script> tag, or a module
// using CommonJS and NodeJS or RequireJS module formats. In
// Common/Node/RequireJS, the module exports the Q API and when
// executed as a simple <script>, it creates a Q global instead.
// The use of "undefined" in the arguments is a
// micro-optmization for compression systems, permitting
// every occurrence of the "undefined" variable to be
// replaced with a single-character.
// RequireJS
if (typeof define === "function") {
define(function (require, exports, module) {
definition(require, exports, module);
});
// CommonJS
} else if (typeof exports === "object") {
definition(require, exports, module);
// <script>
} else {
Q = definition(undefined, {}, {});
}
})(function (serverSideRequire, exports, module, undefined) {
...
main library here
...
/*
* In module systems that support ``module.exports`` assignment or exports
* return, allow the ``ref`` function to be used as the ``Q`` constructor
* exported by the "q" module.
*/
for (var name in exports)
ref[name] = exports[name];
module.exports = ref;
return ref;
});
虽然冗长,但它的灵活性令人印象深刻,并且无论您的执行环境是什么,都可以简单地工作。
你可以使用uRequire通过模板系统将用AMD或CommonJS编写的模块转换为AMD、CommonJS或UMD。
可选的uRequire构建你的整个包作为一个combinedFile.js
运行在所有环境(nodejs, AMD或无模块浏览器<脚本 />)使用RJS优化器和杏仁在擎盖下。
uRequire 使您不必在每个模块中维护任何样板 -只需编写普通的AMD或CommonJS模块(如.js, .coffee, .coco, .ls等)而无需噱头。
另外,您可以声明性地添加标准功能,例如将模块导出到全局,例如window.myModule
和noConflict()
方法,或者有rununtimeinfo(例如__isNode, __isAMD)选择性地或在构建时替换/删除/注入依赖项,自动缩小,操作模块代码等等。
所有这些配置选项都可以在每个bundle或每个模块中打开或关闭,并且您可以拥有彼此派生(继承)的不同构建配置文件(开发,测试,生产等)。
它通过grunt-urequire或standalone与grunt一起工作得很好,并且它有一个很棒的watch选项,只重建更改的文件。
您试过了吗:https://github.com/medikoo/modules-webmake#modules-webmake ?
这就是我正在采取的方法,而且效果很好。代码中没有样板文件,您可以在服务器端和客户端运行相同的模块
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- Webpack开发服务器和React服务器端渲染
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 有条件更新d3.js力图中节点的最佳方法
- 表达式引擎扩展开发-向呈现的条目添加自定义javascript
- Java开发人员在不编写Javascript的情况下生成Javascript的最佳方式是什么
- 用PHP/JS开发依赖表单字段的最佳方法
- 创建键/值映射开发/生产节点.js的最佳实践
- 在本地开发 AJAX 应用并动态部署它们的最佳做法是什么?
- 开发群聊室的最佳方式
- Web应用程序:为数据库开发界面的最佳技术
- 在测试/开发时打印出 HTML 元素值的最佳方法是什么?
- 用于移动开发的性能最佳的JavaScriptMVC框架
- 维护缩小文件和部署/开发的最佳实践
- 在开发过程中使用webstorm调试gulp插件的最佳方法是什么?
- 基于html5的移动开发的最佳框架是什么?
- 跨commonjs/浏览器开发的最佳实践
- 使用JavaScript UI框架的最佳开发实践
- 开发相互依赖的npm包的最佳工作流程
- 单元测试-开发大型JavaScript应用程序的最佳实践