mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 11:01:21 +08:00
Merge pull request #7505 from NativeScript/smaller-worker-chunks
feat: smaller worker chunks
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
// Require globals first so that snapshot takes __extends function.
|
||||
require("../globals");
|
||||
|
||||
import "../globals";
|
||||
import { Observable, EventData } from "../data/observable";
|
||||
import { View } from "../ui/core/view";
|
||||
import {
|
||||
|
25
tns-core-modules/globals/core/globals-core.ts
Normal file
25
tns-core-modules/globals/core/globals-core.ts
Normal file
@ -0,0 +1,25 @@
|
||||
// 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.
|
||||
// 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];
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
5
tns-core-modules/globals/core/package.json
Normal file
5
tns-core-modules/globals/core/package.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name" : "core",
|
||||
"main" : "globals-core",
|
||||
"nativescript": {}
|
||||
}
|
39
tns-core-modules/globals/decorators.ts
Normal file
39
tns-core-modules/globals/decorators.ts
Normal file
@ -0,0 +1,39 @@
|
||||
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`);
|
||||
|
||||
return originalMethod.apply(this, args);
|
||||
};
|
||||
|
||||
return descriptor;
|
||||
} else {
|
||||
console.log(`${(target && (<any>target).name || target)} is deprecated`);
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
global.Deprecated = Deprecated;
|
||||
|
||||
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`);
|
||||
|
||||
return originalMethod.apply(this, args);
|
||||
};
|
||||
|
||||
return descriptor;
|
||||
} else {
|
||||
console.log(`${(target && (<any>target).name || target)} is experimental`);
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
global.Experimental = Experimental;
|
@ -1,159 +1,8 @@
|
||||
// Required by TypeScript compiler
|
||||
import "./ts-helpers";
|
||||
import "./core";
|
||||
|
||||
import "./register-module-helpers";
|
||||
import "./polyfills/timers";
|
||||
import "./polyfills/dialogs";
|
||||
import "./polyfills/xhr";
|
||||
import "./polyfills/fetch";
|
||||
|
||||
// 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];
|
||||
}
|
||||
};
|
||||
|
||||
import * as timerModule from "../timer";
|
||||
import * as dialogsModule from "../ui/dialogs";
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
global.registerModule("timer", () => require("../timer"));
|
||||
global.registerModule("ui/dialogs", () => require("../ui/dialogs"));
|
||||
global.registerModule("xhr", () => require("../xhr"));
|
||||
global.registerModule("fetch", () => require("../fetch"));
|
||||
|
||||
(<any>global).System = {
|
||||
import(path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
resolve(global.require(path));
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function registerOnGlobalContext(name: string, module: string): void {
|
||||
Object.defineProperty(global, name, {
|
||||
get: function () {
|
||||
// We do not need to cache require() call since it is already cached in the runtime.
|
||||
let m = global.loadModule(module);
|
||||
|
||||
// Redefine the property to make sure the above code is executed only once.
|
||||
let resolvedValue = m[name];
|
||||
Object.defineProperty(global, name, { value: resolvedValue, configurable: true, writable: true });
|
||||
|
||||
return resolvedValue;
|
||||
},
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
|
||||
let snapshotGlobals;
|
||||
export function install() {
|
||||
if ((<any>global).__snapshot || (<any>global).__snapshotEnabled) {
|
||||
if (!snapshotGlobals) {
|
||||
// require in snapshot mode is cheap
|
||||
const timer: typeof timerModule = require("../timer");
|
||||
const dialogs: typeof dialogsModule = require("../ui/dialogs");
|
||||
const xhr = require("../xhr");
|
||||
const fetch = require("../fetch");
|
||||
|
||||
snapshotGlobals = snapshotGlobals || {
|
||||
setTimeout: timer.setTimeout,
|
||||
clearTimeout: timer.clearTimeout,
|
||||
setInterval: timer.setInterval,
|
||||
clearInterval: timer.clearInterval,
|
||||
|
||||
alert: dialogs.alert,
|
||||
confirm: dialogs.confirm,
|
||||
prompt: dialogs.prompt,
|
||||
login: dialogs.login,
|
||||
action: dialogs.action,
|
||||
|
||||
XMLHttpRequest: xhr.XMLHttpRequest,
|
||||
FormData: xhr.FormData,
|
||||
|
||||
fetch: fetch.fetch,
|
||||
Headers: fetch.Headers,
|
||||
Request: fetch.Request,
|
||||
Response: fetch.Response,
|
||||
};
|
||||
}
|
||||
const consoleModule = require("../console").Console;
|
||||
// Object.assign call will fire an error when trying to write to a read-only property of an object, such as 'console'
|
||||
global.console = global.console || new consoleModule();
|
||||
Object.assign(global, snapshotGlobals);
|
||||
} else {
|
||||
registerOnGlobalContext("setTimeout", "timer");
|
||||
registerOnGlobalContext("clearTimeout", "timer");
|
||||
registerOnGlobalContext("setInterval", "timer");
|
||||
registerOnGlobalContext("clearInterval", "timer");
|
||||
|
||||
registerOnGlobalContext("alert", "ui/dialogs");
|
||||
registerOnGlobalContext("confirm", "ui/dialogs");
|
||||
registerOnGlobalContext("prompt", "ui/dialogs");
|
||||
registerOnGlobalContext("login", "ui/dialogs");
|
||||
registerOnGlobalContext("action", "ui/dialogs");
|
||||
|
||||
registerOnGlobalContext("XMLHttpRequest", "xhr");
|
||||
registerOnGlobalContext("FormData", "xhr");
|
||||
|
||||
registerOnGlobalContext("fetch", "fetch");
|
||||
registerOnGlobalContext("Headers", "fetch");
|
||||
registerOnGlobalContext("Request", "fetch");
|
||||
registerOnGlobalContext("Response", "fetch");
|
||||
}
|
||||
}
|
||||
install();
|
||||
|
||||
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`);
|
||||
|
||||
return originalMethod.apply(this, args);
|
||||
};
|
||||
|
||||
return descriptor;
|
||||
} else {
|
||||
console.log(`${(target && (<any>target).name || target)} is deprecated`);
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
global.Deprecated = Deprecated;
|
||||
|
||||
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`);
|
||||
|
||||
return originalMethod.apply(this, args);
|
||||
};
|
||||
|
||||
return descriptor;
|
||||
} else {
|
||||
console.log(`${(target && (<any>target).name || target)} is experimental`);
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
global.Experimental = Experimental;
|
||||
import "./decorators";
|
||||
|
6
tns-core-modules/globals/polyfills/dialogs/dialogs.ts
Normal file
6
tns-core-modules/globals/polyfills/dialogs/dialogs.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import "../../core";
|
||||
import { installPolyfills } from "../polyfill-helpers";
|
||||
|
||||
global.registerModule("tns-core-modules/ui/dialogs", () => require("../../../ui/dialogs"));
|
||||
|
||||
installPolyfills("tns-core-modules/ui/dialogs", ["alert", "confirm", "prompt", "login", "action"]);
|
5
tns-core-modules/globals/polyfills/dialogs/package.json
Normal file
5
tns-core-modules/globals/polyfills/dialogs/package.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name" : "dialogs",
|
||||
"main" : "dialogs",
|
||||
"nativescript": {}
|
||||
}
|
8
tns-core-modules/globals/polyfills/fetch/fetch.ts
Normal file
8
tns-core-modules/globals/polyfills/fetch/fetch.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import "../../core";
|
||||
import "../../polyfills/xhr";
|
||||
|
||||
import { installPolyfills } from "../polyfill-helpers";
|
||||
|
||||
global.registerModule("fetch", () => require("../../../fetch"));
|
||||
|
||||
installPolyfills("fetch", ["fetch", "Headers", "Request", "Response"]);
|
5
tns-core-modules/globals/polyfills/fetch/package.json
Normal file
5
tns-core-modules/globals/polyfills/fetch/package.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name" : "fetch",
|
||||
"main" : "fetch",
|
||||
"nativescript": {}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name" : "polyfill-helpers",
|
||||
"main" : "polyfill-helpers",
|
||||
"nativescript": {}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
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));
|
||||
}
|
||||
}
|
5
tns-core-modules/globals/polyfills/timers/package.json
Normal file
5
tns-core-modules/globals/polyfills/timers/package.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name" : "timers",
|
||||
"main" : "timers",
|
||||
"nativescript": {}
|
||||
}
|
6
tns-core-modules/globals/polyfills/timers/timers.ts
Normal file
6
tns-core-modules/globals/polyfills/timers/timers.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import "../../core";
|
||||
import { installPolyfills } from "../polyfill-helpers";
|
||||
|
||||
global.registerModule("timer", () => require("../../../timer"));
|
||||
|
||||
installPolyfills("timer", ["setTimeout", "clearTimeout", "setInterval", "clearInterval"]);
|
5
tns-core-modules/globals/polyfills/xhr/package.json
Normal file
5
tns-core-modules/globals/polyfills/xhr/package.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name" : "xhr",
|
||||
"main" : "xhr",
|
||||
"nativescript": {}
|
||||
}
|
6
tns-core-modules/globals/polyfills/xhr/xhr.ts
Normal file
6
tns-core-modules/globals/polyfills/xhr/xhr.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import "../../core";
|
||||
import { installPolyfills } from "../polyfill-helpers";
|
||||
|
||||
global.registerModule("xhr", () => require("../../../xhr"));
|
||||
|
||||
installPolyfills("xhr", ["XMLHttpRequest", "FormData"]);
|
1
tns-core-modules/module.d.ts
vendored
1
tns-core-modules/module.d.ts
vendored
@ -82,6 +82,7 @@ declare namespace NodeJS {
|
||||
__onUncaughtError: (error: NativeScriptError) => void;
|
||||
__onDiscardedError: (error: NativeScriptError) => void;
|
||||
TNS_WEBPACK?: boolean;
|
||||
__snapshot?: boolean;
|
||||
__requireOverride?: (name: string, dir: string) => any;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ import { setActivityCallbacks, AndroidActivityCallbacks } from "./frame";
|
||||
import * as globals from "../../globals";
|
||||
import * as appModule from "../../application";
|
||||
|
||||
if ((<any>global).__snapshot || (<any>global).__snapshotEnabled) {
|
||||
if (global.__snapshot) {
|
||||
globals.install();
|
||||
}
|
||||
|
||||
|
@ -106,16 +106,15 @@ function _HandleAmpEntities(found, decimalValue, hexValue, wordValue) {
|
||||
}
|
||||
var res = _ampCodes.get(wordValue);
|
||||
if (res) {
|
||||
return String.fromCharCode(res);
|
||||
return String.fromCodePoint(res);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
if (decimalValue) {
|
||||
return String.fromCharCode(parseInt(decimalValue, 10));
|
||||
return String.fromCodePoint(parseInt(decimalValue, 10));
|
||||
}
|
||||
return String.fromCharCode(parseInt(hexValue, 16));
|
||||
return String.fromCodePoint(parseInt(hexValue, 16));
|
||||
}
|
||||
;
|
||||
var XmlParser = (function () {
|
||||
function XmlParser(onEvent, onError, processNamespaces) {
|
||||
this._processNamespaces = processNamespaces;
|
||||
|
@ -88,7 +88,7 @@ function _generateAmpMap(): any {
|
||||
}
|
||||
|
||||
// android-specific implementation, which pre-populates the map to get it saved into the heap blob
|
||||
if ((<any>global).__snapshot) {
|
||||
if (global.__snapshot) {
|
||||
_ampCodes = _generateAmpMap();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user