diff --git a/build/embed-packager.js b/build/embed-packager.js index 1f5fb5e9d..4c09777ef 100644 --- a/build/embed-packager.js +++ b/build/embed-packager.js @@ -132,11 +132,13 @@ module.exports = function (ret, pack, settings, opt) { } catch (e) { d = (/((?:https?|file)\:.*)$/.test(e.stack) && RegExp.$1).replace(/\\/[^\\/]*$/, ''); } - ${contents - .replace(/\"url\"\s*\:\s*('|")(\.\/.*)\1/g, function (_, quote, value) { - return `"url": d + ${quote}${value.substring(1)}${quote}`; - }) - .replace(/require\.resourceMap/, 'amis.require.resourceMap')} + ${contents.replace(/\"url\"\s*\:\s*('|")(\.\/.*)\1/g, function ( + _, + quote, + value + ) { + return `"url": d + ${quote}${value.substring(1)}${quote}`; + })} })()`; } diff --git a/examples/index.html b/examples/index.html index f6432c53e..711f734f1 100644 --- a/examples/index.html +++ b/examples/index.html @@ -62,7 +62,7 @@ })(); /* @require ./index.jsx 标记为同步依赖,提前加载 */ - require(['./index.jsx'], function (app) { + amis.require(['./index.jsx'], function (app) { var initialState = {}; app.bootstrap(document.getElementById('root'), initialState); }); diff --git a/examples/loadMonacoEditor.ts b/examples/loadMonacoEditor.ts new file mode 100644 index 000000000..c76ae8aa8 --- /dev/null +++ b/examples/loadMonacoEditor.ts @@ -0,0 +1,235 @@ +// 这是个特殊的方法,请看考 mod.js 里面的实现。 +export function __mod__async__load(callback: (exports: any) => void) { + // @ts-ignore + const monacoLoader = __uri('monaco-editor/min/vs/loader.js'); + + // @ts-ignore + const script = amis.require.loadJs(filterUrl(monacoLoader)); + script.onload = () => onLoad(window.require, callback); +} + +function __uri(url: string) { + return url; +} + +// 用于发布 sdk 版本的时候替换,因为不确定 sdk 版本怎么部署,而 worker 地址路径不可知。 +// 所以会被 fis3 替换成取相对的代码。 +function filterUrl(url: string) { + return url; +} + +function onLoad(req: any, callback: (result: any) => void) { + const config = { + 'vs/nls': { + availableLanguages: { + '*': 'zh-cn' + } + }, + 'paths': { + 'vs': __uri('monaco-editor/min/vs/editor/editor.main.js').replace( + /\/vs\/.*$/, + '' + ), + 'vs/base/worker/workerMain': __uri( + 'monaco-editor/min/vs/base/worker/workerMain.js' + ), + 'vs/basic-languages/apex/apex': __uri( + 'monaco-editor/min/vs/basic-languages/apex/apex' + ), + 'vs/basic-languages/azcli/azcli': __uri( + 'monaco-editor/min/vs/basic-languages/azcli/azcli' + ), + 'vs/basic-languages/clojure/clojure': __uri( + 'monaco-editor/min/vs/basic-languages/clojure/clojure' + ), + 'vs/basic-languages/bat/bat': __uri( + 'monaco-editor/min/vs/basic-languages/bat/bat' + ), + 'vs/basic-languages/coffee/coffee': __uri( + 'monaco-editor/min/vs/basic-languages/coffee/coffee' + ), + 'vs/basic-languages/cpp/cpp': __uri( + 'monaco-editor/min/vs/basic-languages/cpp/cpp' + ), + 'vs/basic-languages/csharp/csharp': __uri( + 'monaco-editor/min/vs/basic-languages/csharp/csharp' + ), + 'vs/basic-languages/css/css': __uri( + 'monaco-editor/min/vs/basic-languages/css/css' + ), + 'vs/basic-languages/dockerfile/dockerfile': __uri( + 'monaco-editor/min/vs/basic-languages/dockerfile/dockerfile' + ), + 'vs/basic-languages/fsharp/fsharp': __uri( + 'monaco-editor/min/vs/basic-languages/fsharp/fsharp' + ), + 'vs/basic-languages/go/go': __uri( + 'monaco-editor/min/vs/basic-languages/go/go' + ), + 'vs/basic-languages/handlebars/handlebars': __uri( + 'monaco-editor/min/vs/basic-languages/handlebars/handlebars' + ), + 'vs/basic-languages/html/html': __uri( + 'monaco-editor/min/vs/basic-languages/html/html' + ), + 'vs/basic-languages/ini/ini': __uri( + 'monaco-editor/min/vs/basic-languages/ini/ini' + ), + 'vs/basic-languages/java/java': __uri( + 'monaco-editor/min/vs/basic-languages/java/java' + ), + 'vs/basic-languages/javascript/javascript': __uri( + 'monaco-editor/min/vs/basic-languages/javascript/javascript' + ), + 'vs/basic-languages/less/less': __uri( + 'monaco-editor/min/vs/basic-languages/less/less' + ), + 'vs/basic-languages/lua/lua': __uri( + 'monaco-editor/min/vs/basic-languages/lua/lua' + ), + 'vs/basic-languages/markdown/markdown': __uri( + 'monaco-editor/min/vs/basic-languages/markdown/markdown' + ), + 'vs/basic-languages/msdax/msdax': __uri( + 'monaco-editor/min/vs/basic-languages/msdax/msdax' + ), + 'vs/basic-languages/objective-c/objective-c': __uri( + 'monaco-editor/min/vs/basic-languages/objective-c/objective-c' + ), + 'vs/basic-languages/php/php': __uri( + 'monaco-editor/min/vs/basic-languages/php/php' + ), + 'vs/basic-languages/postiats/postiats': __uri( + 'monaco-editor/min/vs/basic-languages/postiats/postiats' + ), + 'vs/basic-languages/powershell/powershell': __uri( + 'monaco-editor/min/vs/basic-languages/powershell/powershell' + ), + 'vs/basic-languages/pug/pug': __uri( + 'monaco-editor/min/vs/basic-languages/pug/pug' + ), + 'vs/basic-languages/python/python': __uri( + 'monaco-editor/min/vs/basic-languages/python/python' + ), + 'vs/basic-languages/r/r': __uri( + 'monaco-editor/min/vs/basic-languages/r/r' + ), + 'vs/basic-languages/razor/razor': __uri( + 'monaco-editor/min/vs/basic-languages/razor/razor' + ), + 'vs/basic-languages/redis/redis': __uri( + 'monaco-editor/min/vs/basic-languages/redis/redis' + ), + 'vs/basic-languages/redshift/redshift': __uri( + 'monaco-editor/min/vs/basic-languages/redshift/redshift' + ), + 'vs/basic-languages/ruby/ruby': __uri( + 'monaco-editor/min/vs/basic-languages/ruby/ruby' + ), + 'vs/basic-languages/rust/rust': __uri( + 'monaco-editor/min/vs/basic-languages/rust/rust' + ), + 'vs/basic-languages/sb/sb': __uri( + 'monaco-editor/min/vs/basic-languages/sb/sb' + ), + 'vs/basic-languages/scheme/scheme': __uri( + 'monaco-editor/min/vs/basic-languages/scheme/scheme' + ), + 'vs/basic-languages/scss/scss': __uri( + 'monaco-editor/min/vs/basic-languages/scss/scss' + ), + 'vs/basic-languages/shell/shell': __uri( + 'monaco-editor/min/vs/basic-languages/shell/shell' + ), + 'vs/basic-languages/solidity/solidity': __uri( + 'monaco-editor/min/vs/basic-languages/solidity/solidity' + ), + 'vs/basic-languages/sql/sql': __uri( + 'monaco-editor/min/vs/basic-languages/sql/sql' + ), + 'vs/basic-languages/st/st': __uri( + 'monaco-editor/min/vs/basic-languages/st/st' + ), + 'vs/basic-languages/swift/swift': __uri( + 'monaco-editor/min/vs/basic-languages/swift/swift' + ), + 'vs/basic-languages/typescript/typescript': __uri( + 'monaco-editor/min/vs/basic-languages/typescript/typescript' + ), + 'vs/basic-languages/vb/vb': __uri( + 'monaco-editor/min/vs/basic-languages/vb/vb' + ), + 'vs/basic-languages/xml/xml': __uri( + 'monaco-editor/min/vs/basic-languages/xml/xml' + ), + 'vs/basic-languages/yaml/yaml': __uri( + 'monaco-editor/min/vs/basic-languages/yaml/yaml' + ), + 'vs/editor/editor.main': __uri( + 'monaco-editor/min/vs/editor/editor.main.js' + ), + 'vs/editor/editor.main.css': __uri( + 'monaco-editor/min/vs/editor/editor.main.css' + ), + 'vs/editor/editor.main.nls': __uri( + 'monaco-editor/min/vs/editor/editor.main.nls.js' + ), + 'vs/editor/editor.main.nls.zh-cn': __uri( + 'monaco-editor/min/vs/editor/editor.main.nls.zh-cn.js' + ), + // 'vs/editor/contrib/suggest/media/String_16x.svg': __uri('monaco-editor/min/vs/editor/contrib/suggest/media/String_16x.svg'), + // 'vs/editor/contrib/suggest/media/String_inverse_16x.svg': __uri('monaco-editor/min/vs/editor/contrib/suggest/media/String_inverse_16x.svg'), + // 'vs/editor/standalone/browser/quickOpen/symbol-sprite.svg': __uri('monaco-editor/min/vs/editor/standalone/browser/quickOpen/symbol-sprite.svg'), + 'vs/language/typescript/tsMode': __uri( + 'monaco-editor/min/vs/language/typescript/tsMode.js' + ), + // 'vs/language/typescript/lib/typescriptServices': __uri('monaco-editor/min/vs/language/typescript/lib/typescriptServices.js'), + 'vs/language/typescript/tsWorker': __uri( + 'monaco-editor/min/vs/language/typescript/tsWorker.js' + ), + 'vs/language/json/jsonMode': __uri( + 'monaco-editor/min/vs/language/json/jsonMode.js' + ), + 'vs/language/json/jsonWorker': __uri( + 'monaco-editor/min/vs/language/json/jsonWorker.js' + ), + 'vs/language/html/htmlMode': __uri( + 'monaco-editor/min/vs/language/html/htmlMode.js' + ), + 'vs/language/html/htmlWorker': __uri( + 'monaco-editor/min/vs/language/html/htmlWorker.js' + ), + 'vs/language/css/cssMode': __uri( + 'monaco-editor/min/vs/language/css/cssMode.js' + ), + 'vs/language/css/cssWorker': __uri( + 'monaco-editor/min/vs/language/css/cssWorker.js' + ) + } + }; + Object.keys(config.paths).forEach((key: keyof typeof config.paths) => { + config.paths[key] = filterUrl(config.paths[key].replace(/\.js$/, '')); + }); + req.config(config); + + if (/^(https?:)?\/\//.test(config.paths.vs)) { + (window as any).MonacoEnvironment = { + getWorkerUrl: function () { + return `data:text/javascript;charset=utf-8,${encodeURIComponent(` + self.MonacoEnvironment = { + baseUrl: '${config.paths.vs}', + paths: ${JSON.stringify(config.paths)} + }; + importScripts('${__uri( + 'monaco-editor/min/vs/base/worker/workerMain.js' + )}');`)}`; + } + }; + } else { + delete (window as any).MonacoEnvironment; + } + + req(['vs/editor/editor.main'], function (ret: any) { + callback(ret); + }); +} diff --git a/examples/mod.js b/examples/mod.js index 033c6fa81..6f40ea4b3 100644 --- a/examples/mod.js +++ b/examples/mod.js @@ -1,287 +1,307 @@ /** - * @file: mod.js - * @author fis - * ver: 1.0.13 - * update: 2016/01/27 - * https://github.com/fex-team/mod + * @file 模块加载器,改成私有化的了,慎用。 */ -var require; - -/* eslint-disable no-unused-vars */ -var define; +/* eslint-disable */ (function (global) { + var require, define; + var amis = window.amis || {}; + // 避免重复加载而导致已定义模块丢失 + if (amis.require) { + return; + } - // 避免重复加载而导致已定义模块丢失 - if (require) { - return; + var head = document.getElementsByTagName('head')[0]; + var loadingMap = {}; + var factoryMap = {}; + var modulesMap = {}; + var scriptsMap = {}; + var resMap = {}; + var pkgMap = {}; + + var createScripts = function (queues, onerror) { + var docFrag = document.createDocumentFragment(); + + for (var i = 0, len = queues.length; i < len; i++) { + var id = queues[i].id; + var url = queues[i].url; + + if (url in scriptsMap) { + continue; + } + + scriptsMap[url] = true; + + var script = document.createElement('script'); + if (onerror) { + (function (script, id) { + var tid = setTimeout(function () { + onerror(id); + }, require.timeout); + + script.onerror = function () { + clearTimeout(tid); + onerror(id); + }; + + var onload = function () { + clearTimeout(tid); + }; + + if ('onload' in script) { + script.onload = onload; + } else { + script.onreadystatechange = function () { + if ( + this.readyState === 'loaded' || + this.readyState === 'complete' + ) { + onload(); + } + }; + } + })(script, id); + } + script.type = 'text/javascript'; + script.src = url; + + docFrag.appendChild(script); } - var head = document.getElementsByTagName('head')[0]; - var loadingMap = {}; - var factoryMap = {}; - var modulesMap = {}; - var scriptsMap = {}; - var resMap = {}; - var pkgMap = {}; + head.appendChild(docFrag); + }; - var createScripts = function (queues, onerror) { + var loadScripts = function (ids, callback, onerror) { + var queues = []; + for (var i = 0, len = ids.length; i < len; i++) { + var id = ids[i]; + var queue = loadingMap[id] || (loadingMap[id] = []); + queue.push(callback); - var docFrag = document.createDocumentFragment(); + // + // resource map query + // + var res = resMap[id] || resMap[id + '.js'] || {}; + var pkg = res.pkg; + var url; - for (var i = 0, len = queues.length; i < len; i++) { - var id = queues[i].id; - var url = queues[i].url; + if (pkg) { + url = pkgMap[pkg].url || pkgMap[pkg].uri; + } else { + url = res.url || res.uri || id; + } - if (url in scriptsMap) { - continue; - } + queues.push({ + id: id, + url: url + }); + } - scriptsMap[url] = true; + createScripts(queues, onerror); + }; - var script = document.createElement('script'); - if (onerror) { - (function (script, id) { - var tid = setTimeout(function () { - onerror(id); - }, require.timeout); + var runQueue = function (id) { + var queue = loadingMap[id]; + if (queue) { + for (var i = 0, n = queue.length; i < n; i++) { + queue[i](); + } + delete loadingMap[id]; + } + }; - script.onerror = function () { - clearTimeout(tid); - onerror(id); - }; + define = function (id, factory) { + id = id.replace(/\.js$/i, ''); + factoryMap[id] = factory; - var onload = function () { - clearTimeout(tid); - }; - - if ('onload' in script) { - script.onload = onload; - } - else { - script.onreadystatechange = function () { - if (this.readyState === 'loaded' || this.readyState === 'complete') { - onload(); - } - }; - } - })(script, id); - } - script.type = 'text/javascript'; - script.src = url; - - docFrag.appendChild(script); + if (~factory.toString().indexOf('__mod__async__load')) { + var mod = {exports: {}}; + factoryMap[id] = { + deffer: true, + callbacks: [], + loaded: false, + load: function () {} + }; + factory.apply(mod, [require, mod.exports, mod]); + var load = mod.exports.__mod__async__load; + factoryMap[id].load = function () { + if (this.loaded) { + return; } + this.loaded = true; - head.appendChild(docFrag); - }; - - var loadScripts = function (ids, callback, onerror) { - var queues = []; - for (var i = 0, len = ids.length; i < len; i++) { - var id = ids[i]; - var queue = loadingMap[id] || (loadingMap[id] = []); - queue.push(callback); - - // - // resource map query - // - var res = resMap[id] || resMap[id + '.js'] || {}; - var pkg = res.pkg; - var url; - - if (pkg) { - url = pkgMap[pkg].url || pkgMap[pkg].uri; - } - else { - url = res.url || res.uri || id; - } - - queues.push({ - id: id, - url: url - }); - } - - createScripts(queues, onerror); - }; - - define = function (id, factory) { - id = id.replace(/\.js$/i, ''); - factoryMap[id] = factory; - - var queue = loadingMap[id]; - if (queue) { - for (var i = 0, n = queue.length; i < n; i++) { - queue[i](); - } - delete loadingMap[id]; - } - }; - - require = function (id) { - - // compatible with require([dep, dep2...]) syntax. - if (id && id.splice) { - return require.async.apply(this, arguments); - } - - id = require.alias(id); - - var mod = modulesMap[id]; - if (mod) { - return mod.exports; - } - - // - // init module - // - var factory = factoryMap[id]; - if (!factory) { - throw '[ModJS] Cannot find module `' + id + '`'; - } - - mod = modulesMap[id] = { - exports: {} - }; - - // - // factory: function OR value - // - var ret = (typeof factory === 'function') ? factory.apply(mod, [require, mod.exports, mod]) : factory; - - if (ret) { - mod.exports = ret; - } - - return mod.exports; - }; - - require.async = function (names, onload, onerror) { - if (typeof names === 'string') { - names = [names]; - } - - var needMap = {}; - var needNum = 0; - var needLoad = []; - - function findNeed(depArr) { - var child; - - for (var i = 0, n = depArr.length; i < n; i++) { - // - // skip loading or loaded - // - var dep = require.alias(depArr[i]); - - if (dep in needMap) { - continue; - } - - needMap[dep] = true; - - if (dep in factoryMap) { - // check whether loaded resource's deps is loaded or not - child = resMap[dep] || resMap[dep + '.js']; - if (child && 'deps' in child) { - findNeed(child.deps); - } - continue; - } - - needLoad.push(dep); - needNum++; - - child = resMap[dep] || resMap[dep + '.js']; - if (child && 'deps' in child) { - findNeed(child.deps); - } - } - } - - function updateNeed() { - if (0 === needNum--) { - var args = []; - for (var i = 0, n = names.length; i < n; i++) { - args[i] = require(names[i]); - } - - onload && onload.apply(global, args); - } - } - - findNeed(names); - loadScripts(needLoad, updateNeed, onerror); - updateNeed(); - }; - - require.ensure = function (names, callback) { - require.async(names, function () { - callback && callback.call(this, require); + load(function (ret) { + var callbacks = factoryMap[id].callbacks; + factoryMap[id] = function () { + return ret; + }; + callbacks.forEach(function (fn) { + fn(); + }); + runQueue(id); }); + }; + if (loadingMap[id] && loadingMap[id].length) { + factoryMap[id].load(); + } + } else { + runQueue(id); + } + }; + + require = function (id) { + // compatible with require([dep, dep2...]) syntax. + if (id && id.splice) { + return require.async.apply(this, arguments); + } + + id = require.alias(id); + + var mod = modulesMap[id]; + if (mod) { + return mod.exports; + } + + // + // init module + // + var factory = factoryMap[id]; + if (!factory) { + throw '[ModJS] Cannot find module `' + id + '`'; + } + + mod = modulesMap[id] = { + exports: {} }; - require.resourceMap = function (obj) { - var k; - var col; + // + // factory: function OR value + // + var ret = + typeof factory === 'function' + ? factory.apply(mod, [require, mod.exports, mod]) + : factory; - // merge `res` & `pkg` fields - col = obj.res; - for (k in col) { - if (col.hasOwnProperty(k)) { - resMap[k] = col[k]; - } + if (ret) { + mod.exports = ret; + } + + return mod.exports; + }; + + require.async = function (names, onload, onerror) { + if (typeof names === 'string') { + names = [names]; + } + + var needMap = {}; + var needNum = 0; + var needLoad = []; + + function findNeed(depArr) { + var child; + + for (var i = 0, n = depArr.length; i < n; i++) { + // + // skip loading or loaded + // + var dep = require.alias(depArr[i]); + + if (dep in needMap) { + continue; } - col = obj.pkg; - for (k in col) { - if (col.hasOwnProperty(k)) { - pkgMap[k] = col[k]; - } - } - }; + needMap[dep] = true; - require.loadJs = function (url) { - if (url in scriptsMap) { - return; + if (dep in factoryMap) { + if (factoryMap[dep] && factoryMap[dep].deffer) { + needNum++; + factoryMap[dep].callbacks.push(updateNeed); + factoryMap[dep].load(); + } + + // check whether loaded resource's deps is loaded or not + child = resMap[dep] || resMap[dep + '.js']; + if (child && 'deps' in child) { + findNeed(child.deps); + } + continue; } - scriptsMap[url] = true; + needLoad.push(dep); + needNum++; - var script = document.createElement('script'); - script.type = 'text/javascript'; - script.src = url; - head.appendChild(script); - }; - - require.loadCss = function (cfg) { - if (cfg.content) { - var sty = document.createElement('style'); - sty.type = 'text/css'; - - if (sty.styleSheet) { // IE - sty.styleSheet.cssText = cfg.content; - } - else { - sty.innerHTML = cfg.content; - } - head.appendChild(sty); + child = resMap[dep] || resMap[dep + '.js']; + if (child && 'deps' in child) { + findNeed(child.deps); } - else if (cfg.url) { - var link = document.createElement('link'); - link.href = cfg.url; - link.rel = 'stylesheet'; - link.type = 'text/css'; - head.appendChild(link); + } + } + + function updateNeed() { + if (0 === needNum--) { + var args = []; + for (var i = 0, n = names.length; i < n; i++) { + args[i] = require(names[i]); } - }; + onload && onload.apply(global, args); + } + } - require.alias = function (id) { - return id.replace(/\.js$/i, ''); - }; + findNeed(names); + loadScripts(needLoad, updateNeed, onerror); + updateNeed(); + }; - require.timeout = 5000; + require.ensure = function (names, callback) { + require.async(names, function () { + callback && callback.call(this, require); + }); + }; + require.resourceMap = function (obj) { + var k; + var col; + + // merge `res` & `pkg` fields + col = obj.res; + for (k in col) { + if (col.hasOwnProperty(k)) { + resMap[k] = col[k]; + } + } + + col = obj.pkg; + for (k in col) { + if (col.hasOwnProperty(k)) { + pkgMap[k] = col[k]; + } + } + }; + + require.loadJs = function (url) { + if (url in scriptsMap) { + return; + } + + scriptsMap[url] = true; + + var script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = url; + head.appendChild(script); + return script; + }; + + require.alias = function (id) { + return id.replace(/\.js$/i, ''); + }; + + require.timeout = 5000; + + amis.require = require; + amis.define = define; + window.amis = amis; + // window.require = window.require || require; })(this); diff --git a/examples/sdk-mod.js b/examples/sdk-mod.js deleted file mode 100644 index 35ae95b01..000000000 --- a/examples/sdk-mod.js +++ /dev/null @@ -1,265 +0,0 @@ -/** - * @file mod-sandbox.js. - */ -/* eslint-disable */ - -(function (global) { - var require, define; - var amis = window.amis || {}; - - // 避免重复加载而导致已定义模块丢失 - if (amis.require) { - return; - } - - var head = document.getElementsByTagName('head')[0]; - var loadingMap = {}; - var factoryMap = {}; - var modulesMap = {}; - var scriptsMap = {}; - var resMap = {}; - var pkgMap = {}; - - var createScripts = function (queues, onerror) { - var docFrag = document.createDocumentFragment(); - - for (var i = 0, len = queues.length; i < len; i++) { - var id = queues[i].id; - var url = queues[i].url; - - if (url in scriptsMap) { - continue; - } - - scriptsMap[url] = true; - - var script = document.createElement('script'); - if (onerror) { - (function (script, id) { - var tid = setTimeout(function () { - onerror(id); - }, require.timeout); - - script.onerror = function () { - clearTimeout(tid); - onerror(id); - }; - - var onload = function () { - clearTimeout(tid); - }; - - if ('onload' in script) { - script.onload = onload; - } else { - script.onreadystatechange = function () { - if ( - this.readyState === 'loaded' || - this.readyState === 'complete' - ) { - onload(); - } - }; - } - })(script, id); - } - script.type = 'text/javascript'; - script.src = url; - - docFrag.appendChild(script); - } - - head.appendChild(docFrag); - }; - - var loadScripts = function (ids, callback, onerror) { - var queues = []; - for (var i = 0, len = ids.length; i < len; i++) { - var id = ids[i]; - var queue = loadingMap[id] || (loadingMap[id] = []); - queue.push(callback); - - // - // resource map query - // - var res = resMap[id] || resMap[id + '.js'] || {}; - var pkg = res.pkg; - var url; - - if (pkg) { - url = pkgMap[pkg].url || pkgMap[pkg].uri; - } else { - url = res.url || res.uri || id; - } - - queues.push({ - id: id, - url: url - }); - } - - createScripts(queues, onerror); - }; - - define = function (id, factory) { - id = id.replace(/\.js$/i, ''); - factoryMap[id] = factory; - - var queue = loadingMap[id]; - if (queue) { - for (var i = 0, n = queue.length; i < n; i++) { - queue[i](); - } - delete loadingMap[id]; - } - }; - - require = function (id) { - // compatible with require([dep, dep2...]) syntax. - if (id && id.splice) { - return require.async.apply(this, arguments); - } - - id = require.alias(id); - - var mod = modulesMap[id]; - if (mod) { - return mod.exports; - } - - // - // init module - // - var factory = factoryMap[id]; - if (!factory) { - throw '[ModJS] Cannot find module `' + id + '`'; - } - - mod = modulesMap[id] = { - exports: {} - }; - - // - // factory: function OR value - // - var ret = - typeof factory === 'function' - ? factory.apply(mod, [require, mod.exports, mod]) - : factory; - - if (ret) { - mod.exports = ret; - } - - return mod.exports; - }; - - require.async = function (names, onload, onerror) { - if (typeof names === 'string') { - names = [names]; - } - - var needMap = {}; - var needNum = 0; - var needLoad = []; - - function findNeed(depArr) { - var child; - - for (var i = 0, n = depArr.length; i < n; i++) { - // - // skip loading or loaded - // - var dep = require.alias(depArr[i]); - - if (dep in needMap) { - continue; - } - - needMap[dep] = true; - - if (dep in factoryMap) { - // check whether loaded resource's deps is loaded or not - child = resMap[dep] || resMap[dep + '.js']; - if (child && 'deps' in child) { - findNeed(child.deps); - } - continue; - } - - needLoad.push(dep); - needNum++; - - child = resMap[dep] || resMap[dep + '.js']; - if (child && 'deps' in child) { - findNeed(child.deps); - } - } - } - - function updateNeed() { - if (0 === needNum--) { - var args = []; - for (var i = 0, n = names.length; i < n; i++) { - args[i] = require(names[i]); - } - - onload && onload.apply(global, args); - } - } - - findNeed(names); - loadScripts(needLoad, updateNeed, onerror); - updateNeed(); - }; - - require.ensure = function (names, callback) { - require.async(names, function () { - callback && callback.call(this, require); - }); - }; - - require.resourceMap = function (obj) { - var k; - var col; - - // merge `res` & `pkg` fields - col = obj.res; - for (k in col) { - if (col.hasOwnProperty(k)) { - resMap[k] = col[k]; - } - } - - col = obj.pkg; - for (k in col) { - if (col.hasOwnProperty(k)) { - pkgMap[k] = col[k]; - } - } - }; - - require.loadJs = function (url) { - if (url in scriptsMap) { - return; - } - - scriptsMap[url] = true; - - var script = document.createElement('script'); - script.type = 'text/javascript'; - script.src = url; - head.appendChild(script); - }; - - require.alias = function (id) { - return id.replace(/\.js$/i, ''); - }; - - require.timeout = 5000; - - amis.require = require; - amis.define = define; - window.amis = amis; - // window.require = window.require || require; -})(this); diff --git a/examples/sdk-placeholder.html b/examples/sdk-placeholder.html index 760bd2a57..a843164c4 100644 --- a/examples/sdk-placeholder.html +++ b/examples/sdk-placeholder.html @@ -10,7 +10,7 @@ - +