Skip to content

Commit afb2cf7

Browse files
authoredJul 14, 2023
🏗 Update Preact and TypeScript dependencies (ampproject#39252)
* Bump Preact to v10.16.0 * Change how Preact's `render`/`hydrate` functions are re-export via the `preact/dom` namespace to match changes in upstream * Bump Typescript to v4.9.5 * Fix typing issues * Revert change in build-system/tasks/build-story-localization.js and replace it with type casting
1 parent b174c99 commit afb2cf7

14 files changed

+46
-55
lines changed
 

‎build-system/common/update-packages.js

-9
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,6 @@ function patchShadowDom() {
179179

180180
writeIfUpdated(patchedName, file);
181181
}
182-
/**
183-
* Adds a missing export statement to the preact module.
184-
*/
185-
function patchPreact() {
186-
fs.ensureDirSync('node_modules/preact/dom');
187-
const file = `export { render, hydrate } from 'preact';`;
188-
writeIfUpdated('node_modules/preact/dom/index.js', file);
189-
}
190182

191183
/**
192184
* Deletes the map file for rrule, which breaks closure compiler.
@@ -235,7 +227,6 @@ function updatePackages() {
235227
patchIntersectionObserver();
236228
patchResizeObserver();
237229
patchShadowDom();
238-
patchPreact();
239230
removeRruleSourcemap();
240231
if (isCiBuild()) {
241232
runNpmChecks();

‎build-system/tasks/build-story-localization.js

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ function getLanguageCodeFallbacks(languageCode) {
2323
if (!languageCode) {
2424
return [FALLBACK_LANGUAGE_CODE];
2525
}
26+
27+
/** @type {string[]} */
2628
const matches = languageCode.match(LANGUAGE_CODE_CHUNK_REGEX) || [];
2729
return matches.reduce(
2830
(fallbackLanguageCodeList, _, index) => {

‎build-system/tasks/e2e/amp-driver.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const EnvironmentBehaviorMap = {
119119

120120
/**
121121
* @param {string} url
122-
* @param {{isEmail: boolean}=} opts
122+
* @param {{isEmail: boolean}} opts
123123
* @return {string}
124124
*/
125125
function getViewerUrl(url, {isEmail} = {isEmail: false}) {

‎package-lock.json

+14-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"jss-preset-default": "10.8.2",
3131
"moment": "2.29.4",
3232
"obj-str": "1.1.0",
33-
"preact": "10.5.15",
33+
"preact": "10.16.0",
3434
"react-day-picker": "8.0.0-beta.37",
3535
"react-jss": "10.8.2",
3636
"resize-observer-polyfill": "1.5.1",
@@ -180,7 +180,7 @@
180180
"tempy": "1.0.1",
181181
"terser": "5.14.2",
182182
"traverse": "0.6.6",
183-
"typescript": "4.5.4",
183+
"typescript": "4.9.5",
184184
"util": "0.12.4"
185185
}
186186
}

‎src/core/dom/globals.d.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,11 @@ declare global {
1818
}
1919

2020
interface ShadowRoot {
21-
adoptedStyleSheets?: CSSStyleSheet[];
21+
adoptedStyleSheets: CSSStyleSheet[];
2222
}
2323

2424
interface CSSStyleSheet {
25-
replaceSync?: (text: string) => void;
26-
}
27-
28-
interface Event {
29-
// We assign an `Object` at times, though Typescript's dom lib supports
30-
// string or null, so here we allow all three (plus unedfined).
31-
data?: Object | string | null;
25+
replaceSync: (text: string) => void;
3226
}
3327

3428
// Fullscreen proprties

‎src/core/dom/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,9 @@ export function dispatchCustomEvent(node, name, opt_data, opt_options) {
507507
const data = opt_data || {};
508508
// Constructors of events need to come from the correct window. Sigh.
509509
devAssert(node.ownerDocument);
510-
const event = node.ownerDocument.createEvent('Event');
511-
event.data = data;
512510

513511
const {bubbles, cancelable} = opt_options || DEFAULT_CUSTOM_EVENT_OPTIONS;
514-
event.initEvent(name, bubbles, cancelable);
512+
const event = new MessageEvent(name, {data, bubbles, cancelable});
515513
node.dispatchEvent(event);
516514
}
517515

‎src/preact/bento-ce.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ let win;
8282
* @param {typeof import('./base-element').PreactBaseElement} BaseElement
8383
* @param {typeof globalThis} _win
8484
* @return {typeof HTMLElement}
85-
*/
85+
* @template {{
86+
* readyState?: string | undefined;
87+
* pause?: (() => void) | undefined;
88+
* }} T */
8689
function createBentoElementClass(BaseElement, _win = self) {
8790
if (!ExtendableHTMLElement || win !== _win) {
8891
win = _win;
@@ -96,7 +99,6 @@ function createBentoElementClass(BaseElement, _win = self) {
9699

97100
/**
98101
* @type {import('./base-element').PreactBaseElement<T>}
99-
* @template T
100102
*/
101103
this.implementation = new BaseElement(
102104
/** @type {AmpElement} */ (/** @type {?} */ (this))

‎src/preact/component/3p-frame.js

+4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ function ProxyIframeEmbedWithRef(
6464
);
6565
}
6666

67+
/** @type {import('preact/hooks').MutableRef<any>} */
6768
const contentRef = useRef(null);
69+
// TODO: this should be IFrameEmbedApi, but it causes some minor type issues.
70+
/** @type {import('preact/hooks').MutableRef<any>} */
6871
const iframeRef = useRef(null);
6972
const count = useMemo(() => {
7073
if (!countGenerators[type]) {
@@ -75,6 +78,7 @@ function ProxyIframeEmbedWithRef(
7578

7679
const [nameAndSrc, setNameAndSrc] = useState({name: nameProp, src: srcProp});
7780
const {name, src} = nameAndSrc;
81+
/** @type {import('preact/hooks').MutableRef<string?>} */
7882
const sentinelRef = useRef(null);
7983

8084
useLayoutEffect(() => {

‎src/preact/component/iframe.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export function IframeEmbedWithRef(
7171
[onReadyStateRef]
7272
);
7373

74+
/** @type {import('preact/hooks').MutableRef<HTMLIFrameElement?>} */
7475
const iframeRef = useRef(null);
7576

7677
// Component API: IframeEmbedDef.Api.
@@ -112,7 +113,7 @@ export function IframeEmbedWithRef(
112113
iframe.src = iframe.src;
113114
} else {
114115
const parent = iframe.parentNode;
115-
parent.insertBefore(iframe, iframe.nextSibling);
116+
parent?.insertBefore(iframe, iframe.nextSibling);
116117
}
117118
}
118119
}, [playable]);
@@ -137,8 +138,8 @@ export function IframeEmbedWithRef(
137138
};
138139

139140
const {defaultView} = iframe.ownerDocument;
140-
defaultView.addEventListener('message', handler);
141-
return () => defaultView.removeEventListener('message', handler);
141+
defaultView?.addEventListener('message', handler);
142+
return () => defaultView?.removeEventListener('message', handler);
142143
}, [matchesMessagingOrigin, messageHandler, mount, ready]);
143144

144145
return (

‎src/preact/component/intersection-observer.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {useCallback, useEffect, useRef, useState} from '#preact';
1010
* @return {function(Element):void}
1111
*/
1212
export function useIntersectionObserver(callback, ioOptions) {
13+
/** @type {import('preact/hooks').MutableRef<import('#core/types/function/types').UnlistenCallback?>} */
1314
const unobserveRef = useRef(null);
1415
const refCb = useCallback(
1516
/** @param {Element} node */

‎src/preact/preact-dom.d.ts

-12
This file was deleted.

‎src/preact/preact-dom.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Re-export the `render`/`hydrate` functions from 'preact' as 'preact/dom' (via
3+
* ../../tsconfig.base.json) to make it more consistent with React. This makes
4+
* remapping the imports for React builds simple (preact/dom --> react-dom).
5+
*
6+
* @fileoverview
7+
*/
8+
export {hydrate, render} from 'preact';

‎tsconfig.base.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
"#test/*": ["./test/*"],
4040
"#testing/*": ["./testing/*"],
4141

42-
"#third_party/*": ["./third_party/*"]
42+
"#third_party/*": ["./third_party/*"],
43+
44+
"preact/dom": ["./src/preact/preact-dom.ts"]
4345
}
4446
},
4547

0 commit comments

Comments
 (0)
Please sign in to comment.