chore: cleanup

This commit is contained in:
Nathan Walker
2020-07-06 14:25:11 -07:00
parent 94cf5c4706
commit 6336eac4b5
477 changed files with 59087 additions and 74961 deletions

View File

@@ -1,36 +1,36 @@
// Required by TypeScript compiler
import "../ts-helpers";
import "../register-module-helpers";
import './ts-helpers';
import './register-module-helpers';
// This method iterates all the keys in the source exports object and copies them to the destination exports one.
// Note: the method will not check for naming collisions and will override any already existing entries in the destination exports.
global.moduleMerge = function (sourceExports: any, destExports: any) {
for (let key in sourceExports) {
destExports[key] = sourceExports[key];
}
for (let key in sourceExports) {
destExports[key] = sourceExports[key];
}
};
global.zonedCallback = function (callback: Function): Function {
if ((<any>global).zone) {
// Zone v0.5.* style callback wrapping
return (<any>global).zone.bind(callback);
}
if ((<any>global).Zone) {
// Zone v0.6.* style callback wrapping
return (<any>global).Zone.current.wrap(callback);
} else {
return callback;
}
if ((<any>global).zone) {
// Zone v0.5.* style callback wrapping
return (<any>global).zone.bind(callback);
}
if ((<any>global).Zone) {
// Zone v0.6.* style callback wrapping
return (<any>global).Zone.current.wrap(callback);
} else {
return callback;
}
};
(<any>global).System = {
import(path) {
return new Promise((resolve, reject) => {
try {
resolve(global.require(path));
} catch (e) {
reject(e);
}
});
},
import(path) {
return new Promise((resolve, reject) => {
try {
resolve(global.require(path));
} catch (e) {
reject(e);
}
});
},
};

View File

@@ -1,51 +1,39 @@
export function Deprecated(
target: Object,
key?: string | symbol,
descriptor?: any
) {
if (descriptor) {
const originalMethod = descriptor.value;
export function Deprecated(target: Object, key?: string | symbol, descriptor?: any) {
if (descriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`${key.toString()} is deprecated`);
descriptor.value = function (...args: any[]) {
console.log(`${key.toString()} is deprecated`);
return originalMethod.apply(this, args);
};
return originalMethod.apply(this, args);
};
return descriptor;
} else {
console.log(
`${(target && (<any>target).name) || target} is deprecated`
);
return descriptor;
} else {
console.log(`${(target && (<any>target).name) || target} is deprecated`);
return target;
}
return target;
}
}
global.Deprecated = Deprecated;
export function Experimental(
target: Object,
key?: string | symbol,
descriptor?: any
) {
if (descriptor) {
const originalMethod = descriptor.value;
export function Experimental(target: Object, key?: string | symbol, descriptor?: any) {
if (descriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`${key.toString()} is experimental`);
descriptor.value = function (...args: any[]) {
console.log(`${key.toString()} is experimental`);
return originalMethod.apply(this, args);
};
return originalMethod.apply(this, args);
};
return descriptor;
} else {
console.log(
`${(target && (<any>target).name) || target} is experimental`
);
return descriptor;
} else {
console.log(`${(target && (<any>target).name) || target} is experimental`);
return target;
}
return target;
}
}
global.Experimental = Experimental;

View File

@@ -1,6 +1,3 @@
export declare var hasInitGlobal: boolean;
export function installPolyfills(
moduleName: string,
exportNames: string[]
): void;
export function installPolyfills(moduleName: string, exportNames: string[]): void;
export function initGlobal(): void;

View File

@@ -1,85 +1,61 @@
export let hasInitGlobal = false;
function registerOnGlobalContext(moduleName: string, exportName: string): void {
Object.defineProperty(global, exportName, {
get: function () {
// We do not need to cache require() call since it is already cached in the runtime.
let m = global.loadModule(moduleName);
Object.defineProperty(global, exportName, {
get: function () {
// We do not need to cache require() call since it is already cached in the runtime.
let m = global.loadModule(moduleName);
// Redefine the property to make sure the above code is executed only once.
let resolvedValue = m[exportName];
Object.defineProperty(global, exportName, {
value: resolvedValue,
configurable: true,
writable: true,
});
// Redefine the property to make sure the above code is executed only once.
let resolvedValue = m[exportName];
Object.defineProperty(global, exportName, {
value: resolvedValue,
configurable: true,
writable: true,
});
return resolvedValue;
},
configurable: true,
});
return resolvedValue;
},
configurable: true,
});
}
export function installPolyfills(moduleName: string, exportNames: string[]) {
if (global.__snapshot) {
const loadedModule = global.loadModule(moduleName);
exportNames.forEach(
(exportName) => (global[exportName] = loadedModule[exportName])
);
} else {
exportNames.forEach((exportName) =>
registerOnGlobalContext(moduleName, exportName)
);
}
if (global.__snapshot) {
const loadedModule = global.loadModule(moduleName);
exportNames.forEach((exportName) => (global[exportName] = loadedModule[exportName]));
} else {
exportNames.forEach((exportName) => registerOnGlobalContext(moduleName, exportName));
}
}
export function initGlobal() {
console.log("initPolyfills called, init:", hasInitGlobal);
if (!hasInitGlobal) {
hasInitGlobal = true;
// apply global polyfills first
require("./core");
console.log('initPolyfills called, init:', hasInitGlobal);
if (!hasInitGlobal) {
hasInitGlobal = true;
// apply global polyfills first
require('./core');
// DOM api polyfills
global.registerModule("timer", () => require("../timer"));
installPolyfills("timer", [
"setTimeout",
"clearTimeout",
"setInterval",
"clearInterval",
]);
// DOM api polyfills
global.registerModule('timer', () => require('../timer'));
installPolyfills('timer', ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']);
global.registerModule("animation", () => require("../animation-frame"));
installPolyfills("animation", [
"requestAnimationFrame",
"cancelAnimationFrame",
]);
global.registerModule('animation', () => require('../animation-frame'));
installPolyfills('animation', ['requestAnimationFrame', 'cancelAnimationFrame']);
global.registerModule("ui-dialogs", () => require("../ui/dialogs"));
installPolyfills("ui-dialogs", [
"alert",
"confirm",
"prompt",
"login",
"action",
]);
global.registerModule('ui-dialogs', () => require('../ui/dialogs'));
installPolyfills('ui-dialogs', ['alert', 'confirm', 'prompt', 'login', 'action']);
global.registerModule("text", () => require("../text"));
installPolyfills("text", ["TextDecoder", "TextEncoder"]);
global.registerModule('text', () => require('../text'));
installPolyfills('text', ['TextDecoder', 'TextEncoder']);
global.registerModule("xhr", () => require("../xhr"));
installPolyfills("xhr", [
"XMLHttpRequest",
"FormData",
"Blob",
"File",
"FileReader",
]);
global.registerModule('xhr', () => require('../xhr'));
installPolyfills('xhr', ['XMLHttpRequest', 'FormData', 'Blob', 'File', 'FileReader']);
global.registerModule("fetch", () => require("../fetch"));
installPolyfills("fetch", ["fetch", "Headers", "Request", "Response"]);
global.registerModule('fetch', () => require('../fetch'));
installPolyfills('fetch', ['fetch', 'Headers', 'Request', 'Response']);
// Custom decorators
require("./decorators");
}
// Custom decorators
require('./decorators');
}
}

View File

@@ -1,142 +1,128 @@
type ModuleLoader = (name?: string) => any;
interface Context {
keys(): string[];
(key: string): any;
keys(): string[];
(key: string): any;
}
interface ExtensionMap {
[originalFileExtension: string]: string;
[originalFileExtension: string]: string;
}
const modules: Map<
string,
{ moduleId: string; loader: ModuleLoader }
> = new Map<string, { moduleId: string; loader: ModuleLoader }>();
const modules: Map<string, { moduleId: string; loader: ModuleLoader }> = new Map<string, { moduleId: string; loader: ModuleLoader }>();
const modulesLoadedForUI = new Set<string>();
const defaultExtensionMap: ExtensionMap = {
".js": ".js",
".ts": ".js",
".kt": ".js",
".css": ".css",
".scss": ".css",
".less": ".css",
".sass": ".css",
".xml": ".xml",
'.js': '.js',
'.ts': '.js',
'.kt': '.js',
'.css': '.css',
'.scss': '.css',
'.less': '.css',
'.sass': '.css',
'.xml': '.xml',
};
// Cast to <any> because moduleResolvers is read-only in definitions
(<any>global).moduleResolvers = [global.require];
global.registerModule = function (name: string, loader: ModuleLoader): void {
modules.set(name, { loader, moduleId: name });
modules.set(name, { loader, moduleId: name });
};
global._unregisterModule = function _unregisterModule(name: string): void {
modules.delete(name);
modules.delete(name);
};
global._isModuleLoadedForUI = function _isModuleLoadedForUI(
moduleName: string
): boolean {
return modulesLoadedForUI.has(moduleName);
global._isModuleLoadedForUI = function _isModuleLoadedForUI(moduleName: string): boolean {
return modulesLoadedForUI.has(moduleName);
};
global.registerWebpackModules = function registerWebpackModules(
context: Context,
extensionMap: ExtensionMap = {}
) {
context.keys().forEach((moduleId) => {
const extDotIndex = moduleId.lastIndexOf(".");
const base = moduleId.substr(0, extDotIndex);
const originalExt = moduleId.substr(extDotIndex);
const registerExt =
extensionMap[originalExt] ||
defaultExtensionMap[originalExt] ||
originalExt;
global.registerWebpackModules = function registerWebpackModules(context: Context, extensionMap: ExtensionMap = {}) {
context.keys().forEach((moduleId) => {
const extDotIndex = moduleId.lastIndexOf('.');
const base = moduleId.substr(0, extDotIndex);
const originalExt = moduleId.substr(extDotIndex);
const registerExt = extensionMap[originalExt] || defaultExtensionMap[originalExt] || originalExt;
// We prefer source files for webpack scenarios before compilation leftovers,
// e. g. if we get a .js and .ts for the same module, the .js is probably the compiled version of the .ts file,
// so we register the .ts with higher priority, similar is the case with us preferring the .scss to .css
const isSourceFile = originalExt !== registerExt;
const registerName = base + registerExt;
// We prefer source files for webpack scenarios before compilation leftovers,
// e. g. if we get a .js and .ts for the same module, the .js is probably the compiled version of the .ts file,
// so we register the .ts with higher priority, similar is the case with us preferring the .scss to .css
const isSourceFile = originalExt !== registerExt;
const registerName = base + registerExt;
const registerWithName = (nickName: string) => {
modules.set(nickName, {
moduleId,
loader: () => {
return context(moduleId);
},
});
};
const registerWithName = (nickName: string) => {
modules.set(nickName, {
moduleId,
loader: () => {
return context(moduleId);
},
});
};
if (registerName.startsWith("./") && registerName.endsWith(".js")) {
const jsNickNames = [
// This is extremely short version like "main-page" that was promoted to be used with global.registerModule("module-name", loaderFunc);
registerName.substr(2, registerName.length - 5),
// This is for supporting module names like "./main/main-page"
registerName.substr(0, registerName.length - 3),
// This is for supporting module names like "main/main-page.js"
registerName.substr(2),
];
if (registerName.startsWith('./') && registerName.endsWith('.js')) {
const jsNickNames = [
// This is extremely short version like "main-page" that was promoted to be used with global.registerModule("module-name", loaderFunc);
registerName.substr(2, registerName.length - 5),
// This is for supporting module names like "./main/main-page"
registerName.substr(0, registerName.length - 3),
// This is for supporting module names like "main/main-page.js"
registerName.substr(2),
];
jsNickNames.forEach((jsNickName) => {
if (isSourceFile || !global.moduleExists(jsNickName)) {
registerWithName(jsNickName);
}
});
} else if (registerName.startsWith("./")) {
const moduleNickNames = [
// This is for supporting module names like "main/main-page.xml"
registerName.substr(2),
];
jsNickNames.forEach((jsNickName) => {
if (isSourceFile || !global.moduleExists(jsNickName)) {
registerWithName(jsNickName);
}
});
} else if (registerName.startsWith('./')) {
const moduleNickNames = [
// This is for supporting module names like "main/main-page.xml"
registerName.substr(2),
];
moduleNickNames.forEach((moduleNickName) => {
if (!global.moduleExists(moduleNickName)) {
registerWithName(moduleNickName);
}
});
}
moduleNickNames.forEach((moduleNickName) => {
if (!global.moduleExists(moduleNickName)) {
registerWithName(moduleNickName);
}
});
}
if (isSourceFile || !global.moduleExists(registerName)) {
registerWithName(registerName);
}
});
if (isSourceFile || !global.moduleExists(registerName)) {
registerWithName(registerName);
}
});
};
global.moduleExists = function moduleExists(name: string): boolean {
return modules.has(name);
return modules.has(name);
};
global.loadModule = function loadModule(
name: string,
isUIModule: boolean = false
): any {
const moduleInfo = modules.get(name);
if (moduleInfo) {
if (isUIModule) {
modulesLoadedForUI.add(moduleInfo.moduleId);
}
global.loadModule = function loadModule(name: string, isUIModule: boolean = false): any {
const moduleInfo = modules.get(name);
if (moduleInfo) {
if (isUIModule) {
modulesLoadedForUI.add(moduleInfo.moduleId);
}
const result = moduleInfo.loader(name);
const result = moduleInfo.loader(name);
if (result.enableAutoAccept) {
result.enableAutoAccept();
}
if (result.enableAutoAccept) {
result.enableAutoAccept();
}
return result;
}
return result;
}
for (let resolver of (<any>global).moduleResolvers) {
const result = resolver(name);
if (result) {
modules.set(name, { moduleId: name, loader: () => result });
for (let resolver of (<any>global).moduleResolvers) {
const result = resolver(name);
if (result) {
modules.set(name, { moduleId: name, loader: () => result });
return result;
}
}
return result;
}
}
};
global.getRegisteredModules = function getRegisteredModules(): string[] {
return Array.from(modules.keys());
return Array.from(modules.keys());
};

View File

@@ -1,35 +1,32 @@
// Required by V8 snapshot generator
if (!global.__extends) {
global.__extends = function (d, b) {
for (let p in b) {
if (b.hasOwnProperty(p)) {
d[p] = b[p];
}
}
function __() {
this.constructor = d;
}
d.prototype =
b === null
? Object.create(b)
: ((__.prototype = b.prototype), new __());
};
global.__extends = function (d, b) {
for (let p in b) {
if (b.hasOwnProperty(p)) {
d[p] = b[p];
}
}
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
};
}
import * as tslib from "tslib";
import * as tslib from 'tslib';
// Bind the tslib helpers to global scope.
// This is needed when we don't use importHelpers, which
// breaks extending native-classes
for (const fnName of Object.keys(tslib)) {
if (typeof tslib[fnName] !== "function") {
continue;
}
if (typeof tslib[fnName] !== 'function') {
continue;
}
if (fnName in global) {
// Don't override globals that are already defined (ex. __extends)
continue;
}
if (fnName in global) {
// Don't override globals that are already defined (ex. __extends)
continue;
}
global[fnName] = tslib[fnName];
global[fnName] = tslib[fnName];
}