mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: cleanup
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
Globals module for defining functions part of the global context - you need only to call *require* for this module and all globals will be registered. For example:
|
||||
```js
|
||||
require("globals");
|
||||
|
||||
setTimeout(function(){ console.log("Test"); }, 2000);
|
||||
|
||||
```
|
||||
|
||||
Global functions/objects are: setTimeout, clearTimeout, setInterval, clearInterval and console.
|
||||
@@ -1,6 +1,5 @@
|
||||
// Required by TypeScript compiler
|
||||
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.
|
||||
@@ -1,6 +0,0 @@
|
||||
/**
|
||||
* Nothing exported. Side effects: registers ts-helpers and module-helpers
|
||||
* @module "globals/core"
|
||||
*/ /** */
|
||||
import "../ts-helpers";
|
||||
import "../register-module-helpers";
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "core",
|
||||
"main": "globals-core",
|
||||
"types": "globals-core.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
import "./core";
|
||||
|
||||
import "./polyfills/timers";
|
||||
import "./polyfills/animation";
|
||||
import "./polyfills/dialogs";
|
||||
import "./polyfills/text";
|
||||
import "./polyfills/xhr";
|
||||
import "./polyfills/fetch";
|
||||
|
||||
import "./decorators";
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
* Register globals such as setTimeout, setInterval, console etc.
|
||||
*/
|
||||
export function install();
|
||||
export function initGlobal();
|
||||
78
nativescript-core/globals/index.ts
Normal file
78
nativescript-core/globals/index.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
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);
|
||||
|
||||
// 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
|
||||
});
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
export function initGlobal() {
|
||||
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",
|
||||
]);
|
||||
|
||||
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("text", () => require("../text"));
|
||||
installPolyfills("text", ["TextDecoder", "TextEncoder"]);
|
||||
|
||||
global.registerModule("xhr", () => require("../xhr"));
|
||||
installPolyfills("xhr", [
|
||||
"XMLHttpRequest",
|
||||
"FormData",
|
||||
"Blob",
|
||||
"File",
|
||||
"FileReader",
|
||||
]);
|
||||
|
||||
global.registerModule("fetch", () => require("../fetch"));
|
||||
installPolyfills("fetch", ["fetch", "Headers", "Request", "Response"]);
|
||||
|
||||
// Custom decorators
|
||||
require("./decorators");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "globals",
|
||||
"main": "globals",
|
||||
"types": "globals.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
/**
|
||||
* Installs animations polyfill.
|
||||
* @module "globals/polyfills/animations"
|
||||
*/ /** */
|
||||
import "../../core";
|
||||
@@ -1,6 +0,0 @@
|
||||
import "../../core";
|
||||
import { installPolyfills } from "../polyfill-helpers";
|
||||
|
||||
global.registerModule("animation", () => require("../../../animation-frame"));
|
||||
|
||||
installPolyfills("animation", ["requestAnimationFrame", "cancelAnimationFrame"]);
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "animation",
|
||||
"main": "animation",
|
||||
"types": "animation.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
/**
|
||||
* Installs dialogs polyfill.
|
||||
* @module "globals/polyfills/dialogs"
|
||||
*/ /** */
|
||||
import "../../core";
|
||||
@@ -1,6 +0,0 @@
|
||||
import "../../core";
|
||||
import { installPolyfills } from "../polyfill-helpers";
|
||||
|
||||
global.registerModule("ui-dialogs", () => require("../../../ui/dialogs"));
|
||||
|
||||
installPolyfills("ui-dialogs", ["alert", "confirm", "prompt", "login", "action"]);
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "dialogs",
|
||||
"main": "dialogs",
|
||||
"types": "dialogs.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
/**
|
||||
* Installs fetch polyfill.
|
||||
* @module "globals/polyfills/fetch"
|
||||
*/ /** */
|
||||
import "../../core";
|
||||
import "../../polyfills/xhr";
|
||||
@@ -1,8 +0,0 @@
|
||||
import "../../core";
|
||||
import "../../polyfills/xhr";
|
||||
|
||||
import { installPolyfills } from "../polyfill-helpers";
|
||||
|
||||
global.registerModule("fetch", () => require("../../../fetch"));
|
||||
|
||||
installPolyfills("fetch", ["fetch", "Headers", "Request", "Response"]);
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "fetch",
|
||||
"main": "fetch",
|
||||
"types": "fetch.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "polyfill-helpers",
|
||||
"main": "polyfill-helpers",
|
||||
"types": "polyfill-helpers.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
/**
|
||||
* Global polyfills helpers.
|
||||
* @module "globals/polyfills/polyfill-helpers"
|
||||
*/ /** */
|
||||
import "../../core";
|
||||
export declare function installPolyfills(moduleName: string, exportNames: string[]): void;
|
||||
@@ -1,26 +0,0 @@
|
||||
import "../../core";
|
||||
|
||||
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);
|
||||
|
||||
// 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
|
||||
});
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"name" : "text",
|
||||
"main" : "text",
|
||||
"nativescript": {}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import "../../core";
|
||||
import "../../polyfills/xhr";
|
||||
|
||||
import { installPolyfills } from "../polyfill-helpers";
|
||||
|
||||
global.registerModule("text", () => require("../../../text"));
|
||||
|
||||
installPolyfills("text", ["TextDecoder", "TextEncoder"]);
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "timers",
|
||||
"main": "timers",
|
||||
"types": "timers.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
/**
|
||||
* Installs timers polyfill.
|
||||
* @module "globals/polyfills/fetch"
|
||||
*/ /** */
|
||||
import "../../core";
|
||||
@@ -1,6 +0,0 @@
|
||||
import "../../core";
|
||||
import { installPolyfills } from "../polyfill-helpers";
|
||||
|
||||
global.registerModule("timer", () => require("../../../timer"));
|
||||
|
||||
installPolyfills("timer", ["setTimeout", "clearTimeout", "setInterval", "clearInterval"]);
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "xhr",
|
||||
"main": "xhr",
|
||||
"types": "xhr.d.ts",
|
||||
"nativescript": {}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
/**
|
||||
* Installs xhr polyfill.
|
||||
* @module "globals/polyfills/fetch"
|
||||
*/ /** */
|
||||
import "../../core";
|
||||
@@ -1,6 +0,0 @@
|
||||
import "../../core";
|
||||
import { installPolyfills } from "../polyfill-helpers";
|
||||
|
||||
global.registerModule("xhr", () => require("../../../xhr"));
|
||||
|
||||
installPolyfills("xhr", ["XMLHttpRequest", "FormData", "Blob", "File", "FileReader"]);
|
||||
Reference in New Issue
Block a user