feat(monorepo): build with esm modules (#8729)

fixes https://github.com/NativeScript/NativeScript/issues/8739
This commit is contained in:
Martin Guillon
2020-07-23 22:58:08 +02:00
committed by Nathan Walker
parent 5658e638d5
commit fc64fc3ed9
373 changed files with 2295 additions and 1453 deletions

View File

@@ -1,8 +1,7 @@
// Require globals first so that snapshot takes __extends function.
// apply polyfills first
import { initGlobal } from '../globals';
if (!(<any>global).hasInitGlobal) {
initGlobal();
const nsGlobals = require('../globals');
if (!global.NativeScriptHasInitGlobal) {
nsGlobals.initGlobal();
}
// Types
@@ -13,32 +12,14 @@ import { View } from '../ui/core/view';
// Requires
import { Observable } from '../data/observable';
import { trace as profilingTrace, time, uptime, level as profilingLevel } from '../profiling';
import * as bindableResources from '../ui/core/bindable/bindable-resources';
import { CLASS_PREFIX, pushToSystemCssClasses, removeSystemCssClass } from '../css/system-classes';
import { DeviceOrientation, SystemAppearance } from '../ui/enums';
import { CSSUtils } from '../css/system-classes';
import { Enums } from '../ui/enums';
export * from './application-interfaces';
const events = new Observable();
let launched = false;
function setLaunched() {
launched = true;
events.off('launch', setLaunched);
}
events.on('launch', setLaunched);
if (profilingLevel() > 0) {
events.on('displayed', () => {
const duration = uptime();
const end = time();
const start = end - duration;
profilingTrace(`Displayed in ${duration.toFixed(2)}ms`, start, end);
});
}
export function hasLaunched(): boolean {
return launched;
return global.NativeScriptGlobals && global.NativeScriptGlobals.launched;
}
export const launchEvent = 'launch';
@@ -52,9 +33,9 @@ export const discardedErrorEvent = 'discardedError';
export const orientationChangedEvent = 'orientationChanged';
export const systemAppearanceChangedEvent = 'systemAppearanceChanged';
const ORIENTATION_CSS_CLASSES = [`${CLASS_PREFIX}${DeviceOrientation.portrait}`, `${CLASS_PREFIX}${DeviceOrientation.landscape}`, `${CLASS_PREFIX}${DeviceOrientation.unknown}`];
const ORIENTATION_CSS_CLASSES = [`${CSSUtils.CLASS_PREFIX}${Enums.DeviceOrientation.portrait}`, `${CSSUtils.CLASS_PREFIX}${Enums.DeviceOrientation.landscape}`, `${CSSUtils.CLASS_PREFIX}${Enums.DeviceOrientation.unknown}`];
const SYSTEM_APPEARANCE_CSS_CLASSES = [`${CLASS_PREFIX}${SystemAppearance.light}`, `${CLASS_PREFIX}${SystemAppearance.dark}`];
const SYSTEM_APPEARANCE_CSS_CLASSES = [`${CSSUtils.CLASS_PREFIX}${Enums.SystemAppearance.light}`, `${CSSUtils.CLASS_PREFIX}${Enums.SystemAppearance.dark}`];
let cssFile: string = './app.css';
@@ -69,18 +50,20 @@ export function setResources(res: any) {
export let android: AndroidApplication = undefined;
export let ios: iOSApplication = undefined;
export const on: typeof events.on = events.on.bind(events);
export const off: typeof events.off = events.off.bind(events);
export const notify: typeof events.notify = events.notify.bind(events);
export const hasListeners: typeof events.hasListeners = events.hasListeners.bind(events);
export const on = global.NativeScriptGlobals.events.on.bind(global.NativeScriptGlobals.events);
export const off = global.NativeScriptGlobals.events.off.bind(global.NativeScriptGlobals.events);
export const notify = global.NativeScriptGlobals.events.notify.bind(global.NativeScriptGlobals.events);
export const hasListeners = global.NativeScriptGlobals.events.hasListeners.bind(global.NativeScriptGlobals.events);
let app: iOSApplication | AndroidApplication;
export function setApplication(instance: iOSApplication | AndroidApplication): void {
app = instance;
// signal when the application instance is ready globally
global.NativeScriptGlobals.appInstanceReady = true;
}
export function livesync(rootView: View, context?: ModuleContext) {
events.notify(<EventData>{ eventName: 'livesync', object: app });
global.NativeScriptGlobals.events.notify(<EventData>{ eventName: 'livesync', object: app });
const liveSyncCore = global.__onLiveSyncCore;
let reapplyAppStyles = false;
@@ -102,7 +85,7 @@ export function livesync(rootView: View, context?: ModuleContext) {
export function setCssFileName(cssFileName: string) {
cssFile = cssFileName;
events.notify(<CssChangedEventData>{
global.NativeScriptGlobals.events.notify(<CssChangedEventData>{
eventName: 'cssChanged',
object: app,
cssFile: cssFileName,
@@ -115,7 +98,7 @@ export function getCssFileName(): string {
export function loadAppCss(): void {
try {
events.notify(<LoadAppCSSEventData>{
global.NativeScriptGlobals.events.notify(<LoadAppCSSEventData>{
eventName: 'loadAppCss',
object: app,
cssFile: getCssFileName(),
@@ -126,12 +109,12 @@ export function loadAppCss(): void {
}
function addCssClass(rootView: View, cssClass: string) {
pushToSystemCssClasses(cssClass);
CSSUtils.pushToSystemCssClasses(cssClass);
rootView.cssClasses.add(cssClass);
}
function removeCssClass(rootView: View, cssClass: string) {
removeSystemCssClass(cssClass);
CSSUtils.removeSystemCssClass(cssClass);
rootView.cssClasses.delete(cssClass);
}
@@ -157,7 +140,7 @@ export function orientationChanged(rootView: View, newOrientation: 'portrait' |
return;
}
const newOrientationCssClass = `${CLASS_PREFIX}${newOrientation}`;
const newOrientationCssClass = `${CSSUtils.CLASS_PREFIX}${newOrientation}`;
applyCssClass(rootView, ORIENTATION_CSS_CLASSES, newOrientationCssClass);
const rootModalViews = <Array<View>>rootView._getRootModalViews();
@@ -171,7 +154,7 @@ export function systemAppearanceChanged(rootView: View, newSystemAppearance: 'da
return;
}
const newSystemAppearanceCssClass = `${CLASS_PREFIX}${newSystemAppearance}`;
const newSystemAppearanceCssClass = `${CSSUtils.CLASS_PREFIX}${newSystemAppearance}`;
applyCssClass(rootView, SYSTEM_APPEARANCE_CSS_CLASSES, newSystemAppearanceCssClass);
const rootModalViews = <Array<View>>rootView._getRootModalViews();
@@ -181,7 +164,7 @@ export function systemAppearanceChanged(rootView: View, newSystemAppearance: 'da
}
global.__onUncaughtError = function (error: NativeScriptError) {
events.notify(<UnhandledErrorEventData>{
global.NativeScriptGlobals.events.notify(<UnhandledErrorEventData>{
eventName: uncaughtErrorEvent,
object: app,
android: error,
@@ -191,7 +174,7 @@ global.__onUncaughtError = function (error: NativeScriptError) {
};
global.__onDiscardedError = function (error: NativeScriptError) {
events.notify(<DiscardedErrorEventData>{
global.NativeScriptGlobals.events.notify(<DiscardedErrorEventData>{
eventName: discardedErrorEvent,
object: app,
error: error,

View File

@@ -1,15 +1,16 @@
// Types.
import { AndroidApplication as AndroidApplicationDefinition } from '.';
import { AndroidActivityBackPressedEventData, AndroidActivityBundleEventData, AndroidActivityEventData, AndroidActivityNewIntentEventData, AndroidActivityRequestPermissionsEventData, AndroidActivityResultEventData, ApplicationEventData, CssChangedEventData, OrientationChangedEventData, SystemAppearanceChangedEventData } from './application-interfaces';
// Use requires to ensure order of imports is maintained
const appCommon = require('./application-common');
// First reexport so that app module is initialized.
export * from './application-common';
import { View } from '../ui/core/view';
import { NavigationEntry, AndroidActivityCallbacks } from '../ui/frame/frame-interfaces';
import { Observable } from '../data/observable';
// Requires
import { displayedEvent, hasListeners, livesync, lowMemoryEvent, notify, orientationChanged, orientationChangedEvent, setApplication, suspendEvent, systemAppearanceChanged, systemAppearanceChangedEvent } from './application-common';
// First reexport so that app module is initialized.
export * from './application-common';
import { profile } from '../profiling';
const ActivityCreated = 'activityCreated';
@@ -60,7 +61,6 @@ export class AndroidApplication extends Observable implements AndroidApplication
this.nativeApp = nativeApp;
this.packageName = nativeApp.getPackageName();
this.context = nativeApp.getApplicationContext();
// we store those callbacks and add a function for clearing them later so that the objects will be eligable for GC
this.callbacks.lifecycleCallbacks = initLifecycleCallbacks();
this.callbacks.componentCallbacks = initComponentCallbacks();
@@ -154,8 +154,7 @@ export interface AndroidApplication {
const androidApp = new AndroidApplication();
export { androidApp as android };
setApplication(androidApp);
appCommon.setApplication(androidApp);
let mainEntry: NavigationEntry;
let started = false;
@@ -174,7 +173,7 @@ export function run(entry?: NavigationEntry | string) {
}
export function addCss(cssText: string, attributeScoped?: boolean): void {
notify(<CssChangedEventData>{
appCommon.notify(<CssChangedEventData>{
eventName: 'cssChanged',
object: androidApp,
cssText: cssText,
@@ -263,7 +262,7 @@ global.__onLiveSync = function __onLiveSync(context?: ModuleContext) {
}
const rootView = getRootView();
livesync(rootView, context);
appCommon.livesync(rootView, context);
};
function getOrientationValue(configuration: android.content.res.Configuration): 'portrait' | 'landscape' | 'unknown' {
@@ -318,8 +317,8 @@ function initLifecycleCallbacks() {
// store the listener not to trigger GC collection before collecting the method
global.onGlobalLayoutListener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({
onGlobalLayout() {
notify({
eventName: displayedEvent,
appCommon.notify({
eventName: appCommon.displayedEvent,
object: androidApp,
activity,
});
@@ -340,7 +339,7 @@ function initLifecycleCallbacks() {
notifyActivityCreated(activity, <any>savedInstanceState, undefined);
if (hasListeners(displayedEvent)) {
if (appCommon.hasListeners(appCommon.displayedEvent)) {
subscribeForGlobalLayout(activity, undefined, undefined);
}
}),
@@ -366,8 +365,8 @@ function initLifecycleCallbacks() {
onActivityPaused: <any>profile('onActivityPaused', function (activity: androidx.appcompat.app.AppCompatActivity) {
if ((<any>activity).isNativeScriptActivity) {
androidApp.paused = true;
notify(<ApplicationEventData>{
eventName: suspendEvent,
appCommon.notify(<ApplicationEventData>{
eventName: appCommon.suspendEvent,
object: androidApp,
android: activity,
});
@@ -424,8 +423,8 @@ function initComponentCallbacks() {
onLowMemory: <any>profile('onLowMemory', function () {
gc();
java.lang.System.gc();
notify(<ApplicationEventData>{
eventName: lowMemoryEvent,
appCommon.notify(<ApplicationEventData>{
eventName: appCommon.lowMemoryEvent,
object: this,
android: this,
});
@@ -441,10 +440,10 @@ function initComponentCallbacks() {
if (androidApp.orientation !== newOrientation) {
androidApp.orientation = newOrientation;
orientationChanged(rootView, newOrientation);
appCommon.orientationChanged(rootView, newOrientation);
notify(<OrientationChangedEventData>{
eventName: orientationChangedEvent,
appCommon.notify(<OrientationChangedEventData>{
eventName: appCommon.orientationChangedEvent,
android: androidApp.nativeApp,
newValue: androidApp.orientation,
object: androidApp,
@@ -457,10 +456,10 @@ function initComponentCallbacks() {
if (androidApp.systemAppearance !== newSystemAppearance) {
androidApp.systemAppearance = newSystemAppearance;
systemAppearanceChanged(rootView, newSystemAppearance);
appCommon.systemAppearanceChanged(rootView, newSystemAppearance);
notify(<SystemAppearanceChangedEventData>{
eventName: systemAppearanceChangedEvent,
appCommon.notify(<SystemAppearanceChangedEventData>{
eventName: appCommon.systemAppearanceChangedEvent,
android: androidApp.nativeApp,
newValue: androidApp.systemAppearance,
object: androidApp,
@@ -478,22 +477,51 @@ function ensureBroadCastReceiverClass() {
return;
}
class BroadcastReceiver extends android.content.BroadcastReceiver {
private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
super();
const BroadcastReceiver = (<any>android.content.BroadcastReceiver).extend({
init(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
// super();
this._onReceiveCallback = onReceiveCallback;
return global.__native(this);
}
public onReceive(context: android.content.Context, intent: android.content.Intent) {
// return global.__native(this);
},
_onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
if (this._onReceiveCallback) {
this._onReceiveCallback(context, intent);
}
}
}
},
});
// @NativeClass
// class BroadcastReceiver extends android.content.BroadcastReceiver {
// private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
// constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
// super();
// this._onReceiveCallback = onReceiveCallback;
// return global.__native(this);
// }
// public onReceive(context: android.content.Context, intent: android.content.Intent) {
// if (this._onReceiveCallback) {
// this._onReceiveCallback(context, intent);
// }
// }
// }
// var BroadcastReceiver = (function (_super) {
// __extends(BroadcastReceiver, _super);
// function BroadcastReceiver(onReceiveCallback) {
// var _this = _super.call(this) || this;
// _this._onReceiveCallback = onReceiveCallback;
// return global.__native(_this);
// }
// BroadcastReceiver.prototype.onReceive = function (context, intent) {
// if (this._onReceiveCallback) {
// this._onReceiveCallback(context, intent);
// }
// };
// return BroadcastReceiver;
// })(android.content.BroadcastReceiver);
BroadcastReceiverClass = BroadcastReceiver;
}

View File

@@ -54,6 +54,13 @@ export const orientationChangedEvent: string;
*/
export const systemAppearanceChangedEvent: string;
/**
* Updates root view classes including those of modals
* @param rootView the root view
* @param newSystemAppearance the new appearance change
*/
export function systemAppearanceChanged(rootView: View, newSystemAppearance: 'dark' | 'light'): void;
/**
* Event data containing information for the application events.
*/

View File

@@ -2,8 +2,8 @@
import { iOSApplication as iOSApplicationDefinition } from '.';
import { ApplicationEventData, CssChangedEventData, LaunchEventData, LoadAppCSSEventData, OrientationChangedEventData, SystemAppearanceChangedEventData } from './application-interfaces';
// Require
import { displayedEvent, exitEvent, getCssFileName, launchEvent, livesync, lowMemoryEvent, notify, on, orientationChanged, orientationChangedEvent, resumeEvent, setApplication, suspendEvent, systemAppearanceChanged, systemAppearanceChangedEvent } from './application-common';
// Use requires to ensure order of imports is maintained
const { displayedEvent, exitEvent, getCssFileName, launchEvent, livesync, lowMemoryEvent, notify, on, orientationChanged, orientationChangedEvent, resumeEvent, setApplication, suspendEvent, systemAppearanceChanged, systemAppearanceChangedEvent } = require('./application-common');
// First reexport so that app module is initialized.
export * from './application-common';
@@ -11,7 +11,7 @@ import { View } from '../ui/core/view';
import { NavigationEntry } from '../ui/frame/frame-interfaces';
// TODO: Remove this and get it from global to decouple builder for angular
import { Builder } from '../ui/builder';
import { CLASS_PREFIX, getSystemCssClasses, pushToSystemCssClasses, ROOT_VIEW_CSS_CLASS } from '../css/system-classes';
import { CSSUtils } from '../css/system-classes';
import { IOSHelper } from '../ui/core/view/view-helper';
import { Device } from '../platform';
import { profile } from '../profiling';
@@ -25,6 +25,7 @@ const majorVersion = iOSNativeHelper.MajorVersion;
// NOTE: UIResponder with implementation of window - related to https://github.com/NativeScript/ios-runtime/issues/430
// TODO: Refactor the UIResponder to use Typescript extends when this issue is resolved:
// https://github.com/NativeScript/ios-runtime/issues/1012
const Responder = (<any>UIResponder).extend(
{
get window() {
@@ -39,6 +40,7 @@ const Responder = (<any>UIResponder).extend(
}
);
@NativeClass
class NotificationObserver extends NSObject {
private _onReceiveCallback: (notification: NSNotification) => void;
@@ -61,6 +63,8 @@ class NotificationObserver extends NSObject {
let displayedOnce = false;
let displayedLinkTarget;
let displayedLink;
@NativeClass
class CADisplayLinkTarget extends NSObject {
onDisplayed(link: CADisplayLink) {
link.invalidate();
@@ -358,9 +362,11 @@ function createRootView(v?: View) {
if (!mainEntry) {
throw new Error('Main entry is missing. App cannot be started. Verify app bootstrap.');
} else {
// console.log('createRootView mainEntry:', mainEntry);
rootView = Builder.createViewFromEntry(mainEntry);
}
}
// console.log('createRootView rootView:', rootView);
setRootViewsCssClasses(rootView);
@@ -489,19 +495,19 @@ function setViewControllerView(view: View): void {
function setRootViewsCssClasses(rootView: View): void {
const deviceType = Device.deviceType.toLowerCase();
pushToSystemCssClasses(`${CLASS_PREFIX}${IOS_PLATFORM}`);
pushToSystemCssClasses(`${CLASS_PREFIX}${deviceType}`);
pushToSystemCssClasses(`${CLASS_PREFIX}${iosApp.orientation}`);
CSSUtils.pushToSystemCssClasses(`${CSSUtils.CLASS_PREFIX}${IOS_PLATFORM}`);
CSSUtils.pushToSystemCssClasses(`${CSSUtils.CLASS_PREFIX}${deviceType}`);
CSSUtils.pushToSystemCssClasses(`${CSSUtils.CLASS_PREFIX}${iosApp.orientation}`);
rootView.cssClasses.add(ROOT_VIEW_CSS_CLASS);
const rootViewCssClasses = getSystemCssClasses();
rootView.cssClasses.add(CSSUtils.ROOT_VIEW_CSS_CLASS);
const rootViewCssClasses = CSSUtils.getSystemCssClasses();
rootViewCssClasses.forEach((c) => rootView.cssClasses.add(c));
}
function setRootViewsSystemAppearanceCssClass(rootView: View): void {
if (majorVersion >= 13) {
const systemAppearanceCssClass = `${CLASS_PREFIX}${iosApp.systemAppearance}`;
pushToSystemCssClasses(systemAppearanceCssClass);
const systemAppearanceCssClass = `${CSSUtils.CLASS_PREFIX}${iosApp.systemAppearance}`;
CSSUtils.pushToSystemCssClasses(systemAppearanceCssClass);
rootView.cssClasses.add(systemAppearanceCssClass);
}
}