g-js / g.js Goto Github PK
View Code? Open in Web Editor NEWA AMD implementation inspired by Seajs
A AMD implementation inspired by Seajs
加载合并文件的时候能够不需要知道合并配置,就能避免零散请求.
在老的版本中,我们当我们需要加载某个合并文件的时候需要提前知道当前合并文件的所有子文件,以便将这些子文件标记为FETCHING
的状态。
这样可以避免之后再加载子文件时发起零散的请求,主要有两种情景:
1,手动加载子文件
2,子文件依赖链的处理可能导致加载其他子文件
例如:
G.config({
combine: {
"x.cmb.js": ["a.js", "b.js", "c.js"]
}
});
define('a.js', ['b.js', 'c.js'], function () {...});
define('b.js', [], function () {...});
define('c.js', [], function () {...});
G.use(['x.cmb.js']);
G.use('a.js'); // 手动加载某个子文件
FETCHING
。可以避免子文件的零散加载。否则文件会被分散加载。G.use(['x.cmb.js'], function () {
G.use('a.js', function () {
...
});
})
情景2就是合并文件中的内容描述的那样,当处理a.js
的依赖链的时候,b.js
, c.js
会被作为依赖项发起请求.
FETCHING
。可以避免子文件的零散加载。否则文件会被分散加载。define
时,仅记录下依赖项,而不发起请求.当interactiveScript
触发onload
事件时一起发起请求.(顺便可以在这里处理动态合并).G.use(['some.js']);
第一个参数必须是数组,我觉得应该支持传入 string,毕竟有时候是只需要 use 一个 js 文件的
G.use(['a.cmb.js'], function(a) {
// how to access modules in a.cmb.js?
});
a.cmb.js:
a.js
b.js
c.js
如何获取 a, b 和 c 呢?考虑传一个 Object 进来?类似于:
G.use(['a.cmb.js'], function(module) {
var a = module['a.js'],
b = module['b.js'],
c = module['c.js'];
});
Demo:
G.use(['a.js', 'b.js', 'c.js'], function (require) {
var a = require('a.js');
var b = require('b.js');
var c = require('c.js');
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.