TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Require.js Library Skeleton

32 点作者 sahat超过 11 年前

7 条评论

k3n超过 11 年前
I believe this is actually promoting an anti-pattern, and will result in code that is hard to test and which isn&#x27;t expressly modular.<p>From the source[1]:<p>* core.js creates the &quot;myLib&quot; object<p>* module1.js &amp; module2.js augment &quot;myLib&quot; with additional properties<p>* myLib.js takes the final, augmented object, and assigns it to a global variable<p>There are many problems with this setup, but the main one is going to be that the module1.js&#x2F;module2.js both alter the module defined by core.js. The augmentation is permanent (until page reload).<p>So, what you end up with is a situation where you cannot include module1&#x2F;module2 without first including core; this is not modular code and will make testing those modules more challenging and brittle -- you effectively can&#x27;t test them independently. Rather, you&#x27;d have to test the relevant properties on the myLib object or stub-out myLib with a mock.<p>When working with modules, you typically don&#x27;t want to build up some giant, monolithic object -- rather, you should be pulling in modules as you need and using them independently. Each module should return it&#x27;s own object, which you can freely use within the context of another module but which has no adverse side-effects on any other modules.<p>1. <a href="https://github.com/sahat/requirejs-library/tree/master/src" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sahat&#x2F;requirejs-library&#x2F;tree&#x2F;master&#x2F;src</a>
评论 #6683452 未加载
评论 #6683429 未加载
ricardobeat超过 11 年前
The main file is exporting a global variable (window.mylib) directly, when it should assume require.js is present and export itself as a module. Only when creating a stand-alone&#x2F;static build for non-requirejs environments it should export a global. CommonJS support could be added too.
possibilistic超过 11 年前
Entirely earnest question -- wouldn&#x27;t it be disadvantageous to create a <i>library</i> using Require.js? AFAIK you can&#x27;t compile out the Require.js dependency. Even Almond.js leaves this cruft. Shouldn&#x27;t you design consumable libraries to be entirely clear of runtime dependency mapping&#x2F;loading?<p>As a part of my workflow, libraries are built at compile time by a Grunt task. These modules are concatenated based on an externally specified dependency graph. The end result is no Require.js in the final production code.<p>I&#x27;m kind of against R.js as it adds complexity at runtime. Has anyone found a way around this? If it were compile time overhead alone, I wouldn&#x27;t be so opposed.
williamcotton超过 11 年前
I would recommend following the patterns outlined here: <a href="https://github.com/umdjs/umd" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;umdjs&#x2F;umd</a>
paddy_m超过 11 年前
Nice work sahat. We are using this as the basis for our refactor of js portion of the bokeh plotting library at Continuum.
philfreo超过 11 年前
We built Close.io off of RequireJS and Backbone.js using a modified version of this: <a href="https://github.com/backbone-boilerplate/backbone-boilerplate/" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;backbone-boilerplate&#x2F;backbone-boilerplate...</a>
pwmckenna超过 11 年前
have you thought about making this a yo generator?
评论 #6683395 未加载