From be0056d07243a2e3af14a0c92bedee835ea8a5dd Mon Sep 17 00:00:00 2001 From: Mohammed Saud Date: Wed, 15 Jan 2020 19:11:24 +0530 Subject: [PATCH] improve require() functions module detection --- webui-src/make-src/build.sh | 3 +- webui-src/make-src/template.js | 210 +++++++++++++++++---------------- 2 files changed, 108 insertions(+), 105 deletions(-) diff --git a/webui-src/make-src/build.sh b/webui-src/make-src/build.sh index 2ba36e0..08f6ec0 100755 --- a/webui-src/make-src/build.sh +++ b/webui-src/make-src/build.sh @@ -31,8 +31,9 @@ if [ "$2" = "" ]||[ "$2" = "app.js" ]; then echo - copy template.js ... cp $src/make-src/template.js $publicdest/app.js + js_source=$src/app/ for filename in $src/app/**/*.js; do - fname=$(basename "$filename") + fname="${filename#$js_source}" fname="${fname%.*}" echo - adding $fname ... echo require.register\(\"$fname\", function\(exports, require, module\) { >> $publicdest/app.js diff --git a/webui-src/make-src/template.js b/webui-src/make-src/template.js index e0f04f7..6af21c8 100644 --- a/webui-src/make-src/template.js +++ b/webui-src/make-src/template.js @@ -1,112 +1,114 @@ (function() { - 'use strict'; + 'use strict'; - var globals = typeof window === 'undefined' ? global : window; - if (typeof globals.require === 'function') return; + const globals = typeof window === 'undefined' ? global : window; + if (typeof globals.require === 'function') return; - var modules = {}; - var cache = {}; - var aliases = {}; - var has = ({}).hasOwnProperty; + let modules = {}, + cache = {}, + aliases = {}, + has = {}.hasOwnProperty; - var endsWith = function(str, suffix) { - return str.indexOf(suffix, str.length - suffix.length) !== -1; - }; - - var _cmp = 'components/'; - var unalias = function(alias, loaderPath) { - var start = 0; - if (loaderPath) { - if (loaderPath.indexOf(_cmp) === 0) { - start = _cmp.length; - } - if (loaderPath.indexOf('/', start) > 0) { - loaderPath = loaderPath.substring(start, loaderPath.indexOf('/', start)); - } - } - var result = aliases[alias + '/index.js'] || aliases[loaderPath + '/deps/' + alias + '/index.js']; - if (result) { - return _cmp + result.substring(0, result.length - '.js'.length); - } - return alias; - }; - - var _reg = /^\.\.?(\/|$)/; - var expand = function(root, name) { - var results = [], part; - var parts = (_reg.test(name) ? root + '/' + name : name).split('/'); - for (var i = 0, length = parts.length; i < length; i++) { - part = parts[i]; - if (part === '..') { - results.pop(); - } else if (part !== '.' && part !== '') { - results.push(part); - } - } - return results.join('/'); - }; - - var dirname = function(path) { - return path.split('/').slice(0, -1).join('/'); - }; - - var localRequire = function(path) { - return function expanded(name) { - var absolute = expand(dirname(path), name); - return globals.require(absolute, path); - }; - }; - - var initModule = function(name, definition) { - var module = {id: name, exports: {}}; - cache[name] = module; - definition(module.exports, localRequire(name), module); - return module.exports; - }; - - var require = function(name, loaderPath) { - var path = expand(name, '.'); - if (loaderPath == null) loaderPath = '/'; - path = unalias(name, loaderPath); - - if (has.call(cache, path)) return cache[path].exports; - if (has.call(modules, path)) return initModule(path, modules[path]); - - var dirIndex = expand(path, './index'); - if (has.call(cache, dirIndex)) return cache[dirIndex].exports; - if (has.call(modules, dirIndex)) return initModule(dirIndex, modules[dirIndex]); - - throw new Error('Cannot find module "' + name + '" from '+ '"' + loaderPath + '"'); - }; - - require.alias = function(from, to) { - aliases[to] = from; - }; - - require.register = require.define = function(bundle, fn) { - if (typeof bundle === 'object') { - for (var key in bundle) { - if (has.call(bundle, key)) { - modules[key] = bundle[key]; + const unalias = function(alias, loaderPath) { + const _cmp = 'components/'; + let start = 0; + if (loaderPath) { + if (loaderPath.startsWith(_cmp)) { + start = _cmp.length; + } + if (loaderPath.indexOf('/', start) > 0) { + loaderPath = loaderPath.substring( + start, + loaderPath.indexOf('/', start) + ); + } } - } - } else { - modules[bundle] = fn; - } - }; + const result = + aliases[alias + '/index.js'] || + aliases[loaderPath + '/deps/' + alias + '/index.js']; + if (result) { + return _cmp + result.substring(0, result.length - '.js'.length); + } + return alias; + }; - require.list = function() { - var result = []; - for (var item in modules) { - if (has.call(modules, item)) { - result.push(item); - } - } - return result; - }; + const expand = function(root, name) { + const _reg = /^\.\.?(\/|$)/; + let results = [], + parts = (_reg.test(name) ? root + '/' + name : name).split('/'); + for (let part of parts) { + if (part === '..') { + results.pop(); + } else if (part !== '.' && part !== '') { + results.push(part); + } + } + return results.join('/'); + }; - require.brunch = true; - require._cache = cache; - globals.require = require; + const dirname = function(path) { + return path + .split('/') + .slice(0, -1) + .join('/'); + }; + + const localRequire = function(path) { + return function(name) { + let absolute = expand(dirname(path), name); + return globals.require(absolute, path); + }; + }; + + const initModule = function(name, definition) { + let module = { id: name, exports: {} }; + cache[name] = module; + definition(module.exports, localRequire(name), module); + return module.exports; + }; + + const require = function(name, loaderPath) { + if (loaderPath === undefined) loaderPath = '/'; + let path = unalias(name, loaderPath); + + if (path in cache) return cache[path].exports; + if (path in modules) return initModule(path, modules[path]); + + let dirIndex = expand(path, './index'); + if (dirIndex in cache) return cache[dirIndex].exports; + if (dirIndex in modules) return initModule(dirIndex, modules[dirIndex]); + + throw new Error( + 'Cannot find module "' + name + '" from ' + '"' + loaderPath + '"' + ); + }; + + require.alias = function(from, to) { + aliases[to] = from; + }; + + require.register = require.define = function(bundle, fn) { + if (typeof bundle === 'object') { + for (let key in bundle) { + if (has.call(bundle, key)) { + modules[key] = bundle[key]; + } + } + } else { + modules[bundle] = fn; + } + }; + + require.list = function() { + let result = []; + for (let item in modules) { + if (has.call(modules, item)) { + result.push(item); + } + } + return result; + }; + + require._cache = cache; + globals.require = require; })(); -