dev-define.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * cmd 内部定义
  3. */
  4. (function(global) {
  5. var _modules = {},
  6. loaded = {};
  7. global.inc = {
  8. base: '',
  9. config: function(options) {
  10. this.base = options.base || '';
  11. },
  12. use: function(id) {
  13. return require(id);
  14. },
  15. remove: function(node) {
  16. node.parentNode.removeChild(node);
  17. }
  18. };
  19. global.use = global.seajs.use;
  20. global.define = function(id, deps, f) {
  21. var argLen = arguments.length,
  22. module = null;
  23. var scriptNode;
  24. switch (argLen) {
  25. case 1:
  26. scriptNode = document.getElementsByTagName('script');
  27. f = id;
  28. id = scriptNode[scriptNode.length - 1].getAttribute('data-id');
  29. break;
  30. case 2:
  31. if (typeof id === 'string') {
  32. f = deps;
  33. } else {
  34. scriptNode = document.getElementsByTagName('script');
  35. f = deps;
  36. id = scriptNode[scriptNode.length - 1].getAttribute('data-id');
  37. }
  38. break;
  39. }
  40. module = _modules[id] = {
  41. exports: {},
  42. value: null,
  43. factory: null
  44. };
  45. loadDeps(f);
  46. if (typeof f === 'function') {
  47. module.factory = f;
  48. } else {
  49. module.value = f;
  50. }
  51. };
  52. function require(id) {
  53. var exports = {},
  54. module = _modules[id];
  55. if (module.value) {
  56. return module.value;
  57. }
  58. exports = module.factory(require, module.exports, module);
  59. if (exports) {
  60. module.exports = exports;
  61. }
  62. module.value = module.exports;
  63. module.exports = null;
  64. module.factory = null;
  65. return module.value;
  66. }
  67. function loadDeps(factory) {
  68. var deps = null,
  69. pathname = location.pathname,
  70. uri = location.protocol + '//' + location.host;
  71. pathname = pathname.split('/');
  72. if (pathname[pathname.length - 1] !== '') {
  73. pathname[pathname.length - 1] = '';
  74. }
  75. uri += pathname.join('/');
  76. if (typeof factory === 'function') {
  77. deps = loadDepsByFunction(factory);
  78. } else {
  79. // 未处理object的情况
  80. return;
  81. }
  82. for (var i = 0, len = deps.length; i < len; i++) {
  83. var key = deps[i];
  84. if (loaded[key]) {
  85. continue;
  86. }
  87. loaded[key] = true;
  88. document.write('<script src="' + uri + inc.base + '/' + key + '.js" ' +
  89. 'onload="inc.remove(this)"' + ' data-id="' + key + '"></script>');
  90. }
  91. }
  92. function loadDepsByFunction(factory) {
  93. var content = factory.toString(),
  94. match = null,
  95. deps = [],
  96. pattern = /require\s*\(\s*([^)]+?)\s*\)/g;
  97. while ((match = pattern.exec(content))) {
  98. deps.push(match[1].replace(/'|"/g, ''));
  99. }
  100. return deps;
  101. }
  102. })(this);