mirror of
https://github.com/RetroShare/RSNewWebUI.git
synced 2026-09-14 11:05:47 +05:00
improve require() functions module detection
This commit is contained in:
parent
9271d49e8d
commit
be0056d072
@ -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
|
||||
|
||||
@ -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;
|
||||
})();
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user