Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit a5fe09b

Browse files
JiaLiPassionmhevery
authored andcommitted
build: make legacy standalone bundle (#1201)
1 parent fcdd559 commit a5fe09b

17 files changed

+198
-132
lines changed

Diff for: file-size-limit.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{
22
"targets": [
33
{
4-
"path": "dist/zone.min.js",
4+
"path": "dist/zone-evergreen.min.js",
55
"checkTarget": true,
66
"limit": 43000
7+
},
8+
{
9+
"path": "dist/zone.min.js",
10+
"checkTarget": true,
11+
"limit": 44000
712
}
813
]
914
}

Diff for: lib/browser/api-util.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {bindArguments, patchMacroTask, patchMethod, patchOnProperties} from '../common/utils';
9+
import {globalSources, patchEventPrototype, patchEventTarget, zoneSymbolEventNames} from '../common/events';
10+
import {ADD_EVENT_LISTENER_STR, ArraySlice, attachOriginToPatched, bindArguments, FALSE_STR, isBrowser, isIEOrEdge, isMix, isNode, ObjectCreate, ObjectDefineProperty, ObjectGetOwnPropertyDescriptor, patchClass, patchMacroTask, patchMethod, patchOnProperties, REMOVE_EVENT_LISTENER_STR, TRUE_STR, wrapWithCurrentZone, ZONE_SYMBOL_PREFIX} from '../common/utils';
11+
12+
import {patchCallbacks} from './browser-util';
13+
import {_redefineProperty} from './define-property';
14+
import {eventNames, filterProperties} from './property-descriptor';
1015

1116
Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
1217
api.patchOnProperties = patchOnProperties;
@@ -28,4 +33,30 @@ Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
2833
(Zone as any)[SYMBOL_BLACK_LISTED_EVENTS] = (Zone as any)[SYMBOL_UNPATCHED_EVENTS] =
2934
global[SYMBOL_BLACK_LISTED_EVENTS];
3035
}
36+
api.patchEventPrototype = patchEventPrototype;
37+
api.patchEventTarget = patchEventTarget;
38+
api.isIEOrEdge = isIEOrEdge;
39+
api.ObjectDefineProperty = ObjectDefineProperty;
40+
api.ObjectGetOwnPropertyDescriptor = ObjectGetOwnPropertyDescriptor;
41+
api.ObjectCreate = ObjectCreate;
42+
api.ArraySlice = ArraySlice;
43+
api.patchClass = patchClass;
44+
api.wrapWithCurrentZone = wrapWithCurrentZone;
45+
api.filterProperties = filterProperties;
46+
api.attachOriginToPatched = attachOriginToPatched;
47+
api._redefineProperty = _redefineProperty;
48+
api.patchCallbacks = patchCallbacks;
49+
api.getGlobalObjects = () => ({
50+
globalSources,
51+
zoneSymbolEventNames,
52+
eventNames,
53+
isBrowser,
54+
isMix,
55+
isNode,
56+
TRUE_STR,
57+
FALSE_STR,
58+
ZONE_SYMBOL_PREFIX,
59+
ADD_EVENT_LISTENER_STR,
60+
REMOVE_EVENT_LISTENER_STR
61+
});
3162
});

Diff for: lib/browser/browser-legacy.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ import {eventTargetLegacyPatch} from './event-target-legacy';
1414
import {propertyDescriptorLegacyPatch} from './property-descriptor-legacy';
1515
import {registerElementPatch} from './register-element';
1616

17-
Zone.__load_patch('registerElement', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
18-
registerElementPatch(global);
19-
});
17+
(function(_global: any) {
18+
_global['__zone_symbol__legacyPatch'] = function() {
19+
const Zone = _global['Zone'];
20+
Zone.__load_patch('registerElement', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
21+
registerElementPatch(global, api);
22+
});
2023

21-
Zone.__load_patch('EventTargetLegacy', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
22-
eventTargetLegacyPatch(global, api);
23-
propertyDescriptorLegacyPatch(api, global);
24-
});
24+
Zone.__load_patch('EventTargetLegacy', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
25+
eventTargetLegacyPatch(global, api);
26+
propertyDescriptorLegacyPatch(api, global);
27+
});
28+
};
29+
})(typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global);

Diff for: lib/browser/browser-util.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
export function patchCallbacks(
9+
api: _ZonePrivate, target: any, targetName: string, method: string, callbacks: string[]) {
10+
const symbol = Zone.__symbol__(method);
11+
if (target[symbol]) {
12+
return;
13+
}
14+
const nativeDelegate = target[symbol] = target[method];
15+
target[method] = function(name: any, opts: any, options?: any) {
16+
if (opts && opts.prototype) {
17+
callbacks.forEach(function(callback) {
18+
const source = `${targetName}.${method}::` + callback;
19+
const prototype = opts.prototype;
20+
if (prototype.hasOwnProperty(callback)) {
21+
const descriptor = api.ObjectGetOwnPropertyDescriptor(prototype, callback);
22+
if (descriptor && descriptor.value) {
23+
descriptor.value = api.wrapWithCurrentZone(descriptor.value, source);
24+
api._redefineProperty(opts.prototype, callback, descriptor);
25+
} else if (prototype[callback]) {
26+
prototype[callback] = api.wrapWithCurrentZone(prototype[callback], source);
27+
}
28+
} else if (prototype[callback]) {
29+
prototype[callback] = api.wrapWithCurrentZone(prototype[callback], source);
30+
}
31+
});
32+
}
33+
34+
return nativeDelegate.call(target, name, opts, options);
35+
};
36+
37+
api.attachOriginToPatched(target[method], nativeDelegate);
38+
}

Diff for: lib/browser/browser.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ import {patchCustomElements} from './custom-elements';
1818
import {propertyPatch} from './define-property';
1919
import {eventTargetPatch, patchEvent} from './event-target';
2020
import {propertyDescriptorPatch} from './property-descriptor';
21-
import {registerElementPatch} from './register-element';
21+
22+
Zone.__load_patch('legacy', (global: any) => {
23+
const legacyPatch = global[Zone.__symbol__('legacyPatch')];
24+
if (legacyPatch) {
25+
legacyPatch();
26+
}
27+
});
2228

2329
Zone.__load_patch('timers', (global: any) => {
2430
const set = 'set';
@@ -66,7 +72,7 @@ Zone.__load_patch('on_property', (global: any, Zone: ZoneType, api: _ZonePrivate
6672
});
6773

6874
Zone.__load_patch('customElements', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
69-
patchCustomElements(global);
75+
patchCustomElements(global, api);
7076
});
7177

7278
Zone.__load_patch('XHR', (global: any, Zone: ZoneType) => {

Diff for: lib/browser/custom-elements.ts

+3-38
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {attachOriginToPatched, isBrowser, isMix, ObjectGetOwnPropertyDescriptor, wrapWithCurrentZone} from '../common/utils';
10-
11-
import {_redefineProperty} from './define-property';
12-
13-
export function patchCallbacks(
14-
target: any, targetName: string, method: string, callbacks: string[]) {
15-
const symbol = Zone.__symbol__(method);
16-
if (target[symbol]) {
17-
return;
18-
}
19-
const nativeDelegate = target[symbol] = target[method];
20-
target[method] = function(name: any, opts: any, options?: any) {
21-
if (opts && opts.prototype) {
22-
callbacks.forEach(function(callback) {
23-
const source = `${targetName}.${method}::` + callback;
24-
const prototype = opts.prototype;
25-
if (prototype.hasOwnProperty(callback)) {
26-
const descriptor = ObjectGetOwnPropertyDescriptor(prototype, callback);
27-
if (descriptor && descriptor.value) {
28-
descriptor.value = wrapWithCurrentZone(descriptor.value, source);
29-
_redefineProperty(opts.prototype, callback, descriptor);
30-
} else if (prototype[callback]) {
31-
prototype[callback] = wrapWithCurrentZone(prototype[callback], source);
32-
}
33-
} else if (prototype[callback]) {
34-
prototype[callback] = wrapWithCurrentZone(prototype[callback], source);
35-
}
36-
});
37-
}
38-
39-
return nativeDelegate.call(target, name, opts, options);
40-
};
41-
42-
attachOriginToPatched(target[method], nativeDelegate);
43-
}
44-
45-
export function patchCustomElements(_global: any) {
9+
export function patchCustomElements(_global: any, api: _ZonePrivate) {
10+
const {isBrowser, isMix} = api.getGlobalObjects()!;
4611
if ((!isBrowser && !isMix) || !('customElements' in _global)) {
4712
return;
4813
}
4914

5015
const callbacks =
5116
['connectedCallback', 'disconnectedCallback', 'adoptedCallback', 'attributeChangedCallback'];
5217

53-
patchCallbacks(_global.customElements, 'customElements', 'define', callbacks);
18+
api.patchCallbacks(api, _global.customElements, 'customElements', 'define', callbacks);
5419
}

Diff for: lib/browser/define-property.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {zoneSymbol} from '../common/utils';
109
/*
1110
* This is necessary for Chrome and Chrome mobile, to enable
1211
* things like redefining `createdCallback` on an element.
1312
*/
1413

14+
const zoneSymbol = Zone.__symbol__;
1515
const _defineProperty = (Object as any)[zoneSymbol('defineProperty')] = Object.defineProperty;
1616
const _getOwnPropertyDescriptor = (Object as any)[zoneSymbol('getOwnPropertyDescriptor')] =
1717
Object.getOwnPropertyDescriptor;

Diff for: lib/browser/event-target-legacy.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {globalSources, patchEventPrototype, patchEventTarget, zoneSymbolEventNames} from '../common/events';
10-
import {FALSE_STR, isIEOrEdge, TRUE_STR, ZONE_SYMBOL_PREFIX} from '../common/utils';
11-
12-
import {eventNames} from './property-descriptor';
13-
149
export function eventTargetLegacyPatch(_global: any, api: _ZonePrivate) {
10+
const {eventNames, globalSources, zoneSymbolEventNames, TRUE_STR, FALSE_STR, ZONE_SYMBOL_PREFIX} =
11+
api.getGlobalObjects()!;
1512
const WTF_ISSUE_555 =
1613
'Anchor,Area,Audio,BR,Base,BaseFont,Body,Button,Canvas,Content,DList,Directory,Div,Embed,FieldSet,Font,Form,Frame,FrameSet,HR,Head,Heading,Html,IFrame,Image,Input,Keygen,LI,Label,Legend,Link,Map,Marquee,Media,Menu,Meta,Meter,Mod,OList,Object,OptGroup,Option,Output,Paragraph,Pre,Progress,Quote,Script,Select,Source,Span,Style,TableCaption,TableCell,TableCol,Table,TableRow,TableSection,TextArea,Title,Track,UList,Unknown,Video';
1714
const NO_EVENT_TARGET =
@@ -36,7 +33,7 @@ export function eventTargetLegacyPatch(_global: any, api: _ZonePrivate) {
3633

3734
const isDisableIECheck = _global['__Zone_disable_IE_check'] || false;
3835
const isEnableCrossContextCheck = _global['__Zone_enable_cross_context_check'] || false;
39-
const ieOrEdge = isIEOrEdge();
36+
const ieOrEdge = api.isIEOrEdge();
4037

4138
const ADD_EVENT_LISTENER_SOURCE = '.addEventListener:';
4239
const FUNCTION_WRAPPER = '[object FunctionWrapper]';
@@ -103,11 +100,11 @@ export function eventTargetLegacyPatch(_global: any, api: _ZonePrivate) {
103100
}
104101
// vh is validateHandler to check event handler
105102
// is valid or not(for security check)
106-
patchEventTarget(_global, apiTypes, {vh: checkIEAndCrossContext});
103+
api.patchEventTarget(_global, apiTypes, {vh: checkIEAndCrossContext});
107104

108105
return true;
109106
}
110107

111108
export function patchEvent(global: any, api: _ZonePrivate) {
112-
patchEventPrototype(global, api);
109+
api.patchEventPrototype(global, api);
113110
}

Diff for: lib/browser/event-target.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {globalSources, patchEventPrototype, patchEventTarget, zoneSymbolEventNames} from '../common/events';
10-
import {FALSE_STR, TRUE_STR, ZONE_SYMBOL_PREFIX} from '../common/utils';
11-
12-
import {eventNames} from './property-descriptor';
13-
149
export function eventTargetPatch(_global: any, api: _ZonePrivate) {
10+
const {eventNames, zoneSymbolEventNames, TRUE_STR, FALSE_STR, ZONE_SYMBOL_PREFIX} =
11+
api.getGlobalObjects()!;
1512
// predefine all __zone_symbol__ + eventName + true/false string
1613
for (let i = 0; i < eventNames.length; i++) {
1714
const eventName = eventNames[i];
@@ -28,12 +25,11 @@ export function eventTargetPatch(_global: any, api: _ZonePrivate) {
2825
if (!EVENT_TARGET || !EVENT_TARGET.prototype) {
2926
return;
3027
}
31-
patchEventTarget(_global, [EVENT_TARGET && EVENT_TARGET.prototype]);
32-
api.patchEventTarget = patchEventTarget;
28+
api.patchEventTarget(_global, [EVENT_TARGET && EVENT_TARGET.prototype]);
3329

3430
return true;
3531
}
3632

3733
export function patchEvent(global: any, api: _ZonePrivate) {
38-
patchEventPrototype(global, api);
34+
api.patchEventPrototype(global, api);
3935
}

Diff for: lib/browser/property-descriptor-legacy.ts

+19-29
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,42 @@
1010
* @suppress {globalThis}
1111
*/
1212

13-
import {isBrowser, isMix, isNode, ObjectDefineProperty, ObjectGetOwnPropertyDescriptor, patchClass, patchOnProperties, wrapWithCurrentZone, zoneSymbol} from '../common/utils';
14-
15-
import {eventNames, filterProperties, IgnoreProperty} from './property-descriptor';
1613
import * as webSocketPatch from './websocket';
1714

18-
export function patchFilteredProperties(
19-
target: any, onProperties: string[], ignoreProperties: IgnoreProperty[], prototype?: any) {
20-
// check whether target is available, sometimes target will be undefined
21-
// because different browser or some 3rd party plugin.
22-
if (!target) {
23-
return;
24-
}
25-
const filteredProperties: string[] = filterProperties(target, onProperties, ignoreProperties);
26-
patchOnProperties(target, filteredProperties, prototype);
27-
}
28-
2915
export function propertyDescriptorLegacyPatch(api: _ZonePrivate, _global: any) {
16+
const {isNode, isMix} = api.getGlobalObjects()!;
3017
if (isNode && !isMix) {
3118
return;
3219
}
3320

3421
const supportsWebSocket = typeof WebSocket !== 'undefined';
35-
if (!canPatchViaPropertyDescriptor()) {
22+
if (!canPatchViaPropertyDescriptor(api)) {
3623
// Safari, Android browsers (Jelly Bean)
37-
patchViaCapturingAllTheEvents();
38-
patchClass('XMLHttpRequest');
24+
patchViaCapturingAllTheEvents(api);
25+
api.patchClass('XMLHttpRequest');
3926
if (supportsWebSocket) {
4027
webSocketPatch.apply(api, _global);
4128
}
4229
(Zone as any)[api.symbol('patchEvents')] = true;
4330
}
4431
}
4532

46-
function canPatchViaPropertyDescriptor() {
47-
if ((isBrowser || isMix) && !ObjectGetOwnPropertyDescriptor(HTMLElement.prototype, 'onclick') &&
33+
function canPatchViaPropertyDescriptor(api: _ZonePrivate) {
34+
const {isBrowser, isMix} = api.getGlobalObjects()!;
35+
if ((isBrowser || isMix) &&
36+
!api.ObjectGetOwnPropertyDescriptor(HTMLElement.prototype, 'onclick') &&
4837
typeof Element !== 'undefined') {
4938
// WebKit https://bugs.webkit.org/show_bug.cgi?id=134364
5039
// IDL interface attributes are not configurable
51-
const desc = ObjectGetOwnPropertyDescriptor(Element.prototype, 'onclick');
40+
const desc = api.ObjectGetOwnPropertyDescriptor(Element.prototype, 'onclick');
5241
if (desc && !desc.configurable) return false;
5342
}
5443

5544
const ON_READY_STATE_CHANGE = 'onreadystatechange';
5645
const XMLHttpRequestPrototype = XMLHttpRequest.prototype;
5746

58-
const xhrDesc = ObjectGetOwnPropertyDescriptor(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE);
47+
const xhrDesc =
48+
api.ObjectGetOwnPropertyDescriptor(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE);
5949

6050
// add enumerable and configurable here because in opera
6151
// by default XMLHttpRequest.prototype.onreadystatechange is undefined
@@ -64,7 +54,7 @@ function canPatchViaPropertyDescriptor() {
6454
// and if XMLHttpRequest.prototype.onreadystatechange is undefined,
6555
// we should set a real desc instead a fake one
6656
if (xhrDesc) {
67-
ObjectDefineProperty(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, {
57+
api.ObjectDefineProperty(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, {
6858
enumerable: true,
6959
configurable: true,
7060
get: function() {
@@ -74,11 +64,11 @@ function canPatchViaPropertyDescriptor() {
7464
const req = new XMLHttpRequest();
7565
const result = !!req.onreadystatechange;
7666
// restore original desc
77-
ObjectDefineProperty(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, xhrDesc || {});
67+
api.ObjectDefineProperty(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, xhrDesc || {});
7868
return result;
7969
} else {
80-
const SYMBOL_FAKE_ONREADYSTATECHANGE = zoneSymbol('fake');
81-
ObjectDefineProperty(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, {
70+
const SYMBOL_FAKE_ONREADYSTATECHANGE = api.symbol('fake');
71+
api.ObjectDefineProperty(XMLHttpRequestPrototype, ON_READY_STATE_CHANGE, {
8272
enumerable: true,
8373
configurable: true,
8474
get: function() {
@@ -97,12 +87,12 @@ function canPatchViaPropertyDescriptor() {
9787
}
9888
}
9989

100-
const unboundKey = zoneSymbol('unbound');
101-
10290
// Whenever any eventListener fires, we check the eventListener target and all parents
10391
// for `onwhatever` properties and replace them with zone-bound functions
10492
// - Chrome (for now)
105-
function patchViaCapturingAllTheEvents() {
93+
function patchViaCapturingAllTheEvents(api: _ZonePrivate) {
94+
const {eventNames} = api.getGlobalObjects()!;
95+
const unboundKey = api.symbol('unbound');
10696
for (let i = 0; i < eventNames.length; i++) {
10797
const property = eventNames[i];
10898
const onproperty = 'on' + property;
@@ -115,7 +105,7 @@ function patchViaCapturingAllTheEvents() {
115105
}
116106
while (elt) {
117107
if (elt[onproperty] && !elt[onproperty][unboundKey]) {
118-
bound = wrapWithCurrentZone(elt[onproperty], source);
108+
bound = api.wrapWithCurrentZone(elt[onproperty], source);
119109
bound[unboundKey] = elt[onproperty];
120110
elt[onproperty] = bound;
121111
}

0 commit comments

Comments
 (0)