Skip to content

Commit a791514

Browse files
author
Steve Orvell
authored
Makes properties that should remain unminified compatible with tools … (lit#1972)
Makes properties that should remain unminified compatible with tools other than rollup/terser; fixes lit#1970.
1 parent 56e8efd commit a791514

15 files changed

+94
-58
lines changed

.changeset/warm-berries-drive.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@lit-labs/ssr': patch
3+
'lit-element': patch
4+
'lit-html': patch
5+
'@lit/reactive-element': patch
6+
---
7+
8+
Properties that must remain unminified are now compatible with build tools other than rollup/terser.

packages/labs/ssr/src/lib/lit-element-renderer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export class LitElementRenderer extends ElementRenderer {
2020
element: LitElement;
2121

2222
static matchesClass(ctor: typeof HTMLElement) {
23-
return (ctor as unknown as typeof LitElement)._$litElement$;
23+
// This property needs to remain unminified.
24+
return (ctor as unknown as typeof LitElement)['_$litElement$'];
2425
}
2526

2627
constructor(tagName: string) {

packages/labs/ssr/src/lib/render-lit-html.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ const patchedDirectiveCache: WeakMap<DirectiveClass, DirectiveClass> =
7474
* with a subclass that calls `render` rather than `update`
7575
*/
7676
const patchIfDirective = (value: unknown) => {
77-
const directiveCtor = (value as DirectiveResult)?._$litDirective$;
77+
// This property needs to remain unminified.
78+
const directiveCtor = (value as DirectiveResult)?.['_$litDirective$'];
7879
if (directiveCtor !== undefined) {
7980
let patchedCtor = patchedDirectiveCache.get(directiveCtor);
8081
if (patchedCtor === undefined) {
@@ -88,7 +89,8 @@ const patchIfDirective = (value: unknown) => {
8889
);
8990
patchedDirectiveCache.set(directiveCtor, patchedCtor);
9091
}
91-
(value as DirectiveResult)._$litDirective$ = patchedCtor;
92+
// This property needs to remain unminified.
93+
(value as DirectiveResult)['_$litDirective$'] = patchedCtor;
9294
}
9395
return value;
9496
};
@@ -277,7 +279,11 @@ const getTemplateOpcodes = (result: TemplateResult) => {
277279
if (template !== undefined) {
278280
return template;
279281
}
280-
const [html, attrNames] = getTemplateHtml(result.strings, result._$litType$);
282+
// The property '_$litType$' needs to remain unminified.
283+
const [html, attrNames] = getTemplateHtml(
284+
result.strings,
285+
result['_$litType$']
286+
);
281287

282288
/**
283289
* The html string is parsed into a parse5 AST with source code information

packages/lit-element/src/lit-element.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ export class LitElement extends ReactiveElement {
8787
*/
8888
protected static ['finalized'] = true;
8989

90-
static _$litElement$ = true;
90+
// This property needs to remain unminified.
91+
static ['_$litElement$'] = true;
9192

9293
/**
9394
* @category rendering

packages/lit-html/src/async-directive.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ const setChildrenConnected = (
153153
// `_$setChildPartConnected`, which exists `ChildParts` which are also in
154154
// this list
155155
// Disconnect Directive (and any nested directives contained within)
156-
(obj as AsyncDirective)._$setDirectiveConnected?.(isConnected, false);
156+
// This property needs to remain unminified.
157+
(obj as AsyncDirective)['_$setDirectiveConnected']?.(isConnected, false);
157158
// Disconnect Part/TemplateInstance
158159
setChildrenConnected(obj, isConnected);
159160
}
@@ -305,6 +306,7 @@ export abstract class AsyncDirective extends Directive {
305306
super._$initialize(part, parent, attributeIndex);
306307
addDisconnectableToParent(this);
307308
}
309+
// This property needs to remain unminified.
308310
/**
309311
* Called from the core code when a directive is going away from a part (in
310312
* which case `shouldRemoveFromParent` should be true), and from the
@@ -317,7 +319,10 @@ export abstract class AsyncDirective extends Directive {
317319
* removed; false when the tree is being disconnected
318320
* @internal
319321
*/
320-
_$setDirectiveConnected(isConnected: boolean, isClearingDirective = true) {
322+
['_$setDirectiveConnected'](
323+
isConnected: boolean,
324+
isClearingDirective = true
325+
) {
321326
this._setConnected(isConnected);
322327
if (isClearingDirective) {
323328
setChildrenConnected(this, isConnected);

packages/lit-html/src/directive-helpers.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,23 @@ export const isTemplateResult = (
5252
type?: TemplateResultType
5353
): value is TemplateResult =>
5454
type === undefined
55-
? (value as TemplateResult)?._$litType$ !== undefined
56-
: (value as TemplateResult)?._$litType$ === type;
55+
? // This property needs to remain unminified.
56+
(value as TemplateResult)?.['_$litType$'] !== undefined
57+
: (value as TemplateResult)?.['_$litType$'] === type;
5758

5859
/**
5960
* Tests if a value is a DirectiveResult.
6061
*/
6162
export const isDirectiveResult = (value: unknown): value is DirectiveResult =>
62-
(value as DirectiveResult)?._$litDirective$ !== undefined;
63+
// This property needs to remain unminified.
64+
(value as DirectiveResult)?.['_$litDirective$'] !== undefined;
6365

6466
/**
6567
* Retrieves the Directive class for a DirectiveResult
6668
*/
6769
export const getDirectiveClass = (value: unknown): DirectiveClass | undefined =>
68-
(value as DirectiveResult)?._$litDirective$;
70+
// This property needs to remain unminified.
71+
(value as DirectiveResult)?.['_$litDirective$'];
6972

7073
/**
7174
* Tests whether a part has only a single-expression with no strings to

packages/lit-html/src/directive.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ export type DirectiveParameters<C extends Directive> = Parameters<C['render']>;
3131
* returns a DirectiveResult object that captures the arguments.
3232
*/
3333
export interface DirectiveResult<C extends DirectiveClass = DirectiveClass> {
34-
/** @internal */
35-
_$litDirective$: C;
34+
/**
35+
* This property needs to remain unminified.
36+
* @internal */
37+
['_$litDirective$']: C;
3638
/** @internal */
3739
values: DirectiveParameters<InstanceType<C>>;
3840
}
@@ -82,7 +84,8 @@ export type PartInfo = ChildPartInfo | AttributePartInfo | ElementPartInfo;
8284
export const directive =
8385
<C extends DirectiveClass>(c: C) =>
8486
(...values: DirectiveParameters<InstanceType<C>>): DirectiveResult<C> => ({
85-
_$litDirective$: c,
87+
// This property needs to remain unminified.
88+
['_$litDirective$']: c,
8689
values,
8790
});
8891

@@ -105,8 +108,9 @@ export abstract class Directive {
105108
// These will only exist on the AsyncDirective subclass
106109
//@internal
107110
_$disconnectableChildren?: Set<Disconnectable>;
111+
// This property needs to remain unminified.
108112
//@internal
109-
_$setDirectiveConnected?(isConnected: boolean): void;
113+
['_$setDirectiveConnected']?(isConnected: boolean): void;
110114

111115
constructor(_partInfo: PartInfo) {}
112116

packages/lit-html/src/directives/unsafe-html.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export class UnsafeHTMLDirective extends Directive {
5555
return (this._templateResult = {
5656
// Cast to a known set of integers that satisfy ResultType so that we
5757
// don't have to export ResultType and possibly encourage this pattern.
58-
_$litType$: (this.constructor as typeof UnsafeHTMLDirective)
58+
// This property needs to remain unminified.
59+
['_$litType$']: (this.constructor as typeof UnsafeHTMLDirective)
5960
.resultType as 1 | 2,
6061
strings,
6162
values: [],

packages/lit-html/src/experimental-hydrate.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ export const hydrate = (
115115
options: Partial<RenderOptions> = {}
116116
) => {
117117
// TODO(kschaaf): Do we need a helper for _$litPart$ ("part for node")?
118+
// This property needs to remain unminified.
118119
// eslint-disable-next-line @typescript-eslint/no-explicit-any
119-
if ((container as any)._$litPart$ !== undefined) {
120+
if ((container as any)['_$litPart$'] !== undefined) {
120121
throw new Error('container already contains a live render');
121122
}
122123

@@ -173,8 +174,9 @@ export const hydrate = (
173174
rootPart !== undefined,
174175
'there should be exactly one root part in a render container'
175176
);
177+
// This property needs to remain unminified.
176178
// eslint-disable-next-line @typescript-eslint/no-explicit-any
177-
(container as any)._$litPart$ = rootPart;
179+
(container as any)['_$litPart$'] = rootPart;
178180
};
179181

180182
const openChildPart = (

packages/lit-html/src/lit-html.ts

+25-31
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ const COMMENT_PART = 7;
241241
* The return type of the template tag functions.
242242
*/
243243
export type TemplateResult<T extends ResultType = ResultType> = {
244-
_$litType$: T;
244+
// This property needs to remain unminified.
245+
['_$litType$']: T;
245246
// TODO (justinfagnani): consider shorter names, like `s` and `v`. This is a
246247
// semi-public API though. We can't just let Terser rename them for us,
247248
// because we need TemplateResults to work between compatible versions of
@@ -257,7 +258,8 @@ export type SVGTemplateResult = TemplateResult<typeof SVG_RESULT>;
257258
export interface CompiledTemplateResult {
258259
// This is a factory in order to make template initialization lazy
259260
// and allow ShadyRenderOptions scope to be passed in.
260-
_$litType$: CompiledTemplate;
261+
// This property needs to remain unminified.
262+
['_$litType$']: CompiledTemplate;
261263
values: unknown[];
262264
}
263265

@@ -274,9 +276,10 @@ export interface CompiledTemplate extends Omit<Template, 'el'> {
274276
* the given result type.
275277
*/
276278
const tag =
277-
<T extends ResultType>(_$litType$: T) =>
279+
<T extends ResultType>(type: T) =>
278280
(strings: TemplateStringsArray, ...values: unknown[]): TemplateResult<T> => ({
279-
_$litType$,
281+
// This property needs to remain unminified.
282+
['_$litType$']: type,
280283
strings,
281284
values,
282285
});
@@ -355,8 +358,9 @@ export const render = (
355358
options?: RenderOptions
356359
): ChildPart => {
357360
const partOwnerNode = options?.renderBefore ?? container;
361+
// This property needs to remain unminified.
358362
// eslint-disable-next-line @typescript-eslint/no-explicit-any
359-
let part: ChildPart = (partOwnerNode as any)._$litPart$;
363+
let part: ChildPart = (partOwnerNode as any)['_$litPart$'];
360364
if (part === undefined) {
361365
// Internal modification: don't clear container to match lit-html 2.0
362366
if (
@@ -367,8 +371,9 @@ export const render = (
367371
container.childNodes.forEach((c) => c.remove());
368372
}
369373
const endNode = options?.renderBefore ?? null;
374+
// This property needs to remain unminified.
370375
// eslint-disable-next-line @typescript-eslint/no-explicit-any
371-
(partOwnerNode as any)._$litPart$ = part = new ChildPart(
376+
(partOwnerNode as any)['_$litPart$'] = part = new ChildPart(
372377
container.insertBefore(createMarker(), endNode),
373378
endNode,
374379
undefined,
@@ -589,7 +594,8 @@ class Template {
589594
parts: Array<TemplatePart> = [];
590595

591596
constructor(
592-
{strings, _$litType$: type}: TemplateResult,
597+
// This property needs to remain unminified.
598+
{strings, ['_$litType$']: type}: TemplateResult,
593599
options?: RenderOptions
594600
) {
595601
let node: Node | null;
@@ -749,9 +755,11 @@ function resolveDirective(
749755
: (parent as ChildPart | ElementPart | Directive).__directive;
750756
const nextDirectiveConstructor = isPrimitive(value)
751757
? undefined
752-
: (value as DirectiveResult)._$litDirective$;
758+
: // This property needs to remain unminified.
759+
(value as DirectiveResult)['_$litDirective$'];
753760
if (currentDirective?.constructor !== nextDirectiveConstructor) {
754-
currentDirective?._$setDirectiveConnected?.(false);
761+
// This property needs to remain unminified.
762+
currentDirective?.['_$setDirectiveConnected']?.(false);
755763
if (nextDirectiveConstructor === undefined) {
756764
currentDirective = undefined;
757765
} else {
@@ -1011,7 +1019,8 @@ class ChildPart {
10111019
} else if (value !== this._$committedValue && value !== noChange) {
10121020
this._commitText(value);
10131021
}
1014-
} else if ((value as TemplateResult)._$litType$ !== undefined) {
1022+
// This property needs to remain unminified.
1023+
} else if ((value as TemplateResult)['_$litType$'] !== undefined) {
10151024
this._commitTemplateResult(value as TemplateResult);
10161025
} else if ((value as Node).nodeType !== undefined) {
10171026
this._commitNode(value as Node);
@@ -1091,20 +1100,18 @@ class ChildPart {
10911100
private _commitTemplateResult(
10921101
result: TemplateResult | CompiledTemplateResult
10931102
): void {
1094-
const {values, _$litType$} = result;
1103+
// This property needs to remain unminified.
1104+
const {values, ['_$litType$']: type} = result;
10951105
// If $litType$ is a number, result is a plain TemplateResult and we get
10961106
// the template from the template cache. If not, result is a
10971107
// CompiledTemplateResult and _$litType$ is a CompiledTemplate and we need
10981108
// to create the <template> element the first time we see it.
10991109
const template: Template | CompiledTemplate =
1100-
typeof _$litType$ === 'number'
1110+
typeof type === 'number'
11011111
? this._$getTemplate(result as TemplateResult)
1102-
: (_$litType$.el === undefined &&
1103-
(_$litType$.el = Template.createElement(
1104-
_$litType$.h,
1105-
this.options
1106-
)),
1107-
_$litType$);
1112+
: (type.el === undefined &&
1113+
(type.el = Template.createElement(type.h, this.options)),
1114+
type);
11081115

11091116
if ((this._$committedValue as TemplateInstance)?._$template === template) {
11101117
(this._$committedValue as TemplateInstance)._update(values);
@@ -1233,12 +1240,6 @@ class AttributePart {
12331240
_$disconnectableChildren?: Set<Disconnectable> = undefined;
12341241

12351242
protected _sanitizer: ValueSanitizer | undefined;
1236-
/** @internal */
1237-
_setDirectiveConnected?: (
1238-
directive: Directive | undefined,
1239-
isConnected: boolean,
1240-
removeFromParent?: boolean
1241-
) => void = undefined;
12421243

12431244
get tagName() {
12441245
return this.element.tagName;
@@ -1489,13 +1490,6 @@ class ElementPart {
14891490
/** @internal */
14901491
_$disconnectableChildren?: Set<Disconnectable> = undefined;
14911492

1492-
/** @internal */
1493-
_setDirectiveConnected?: (
1494-
directive: Directive | undefined,
1495-
isConnected: boolean,
1496-
removeFromParent?: boolean
1497-
) => void = undefined;
1498-
14991493
options: RenderOptions | undefined;
15001494

15011495
constructor(

packages/lit-html/src/polyfill-support.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ interface RenderOptions {
3232

3333
interface ShadyTemplateResult {
3434
strings: TemplateStringsArray;
35-
_$litType$?: string;
35+
// This property needs to remain unminified.
36+
['_$litType$']?: string;
3637
}
3738

3839
// Note, this is a dummy type as the full type here is big.
@@ -233,7 +234,8 @@ const ENABLE_SHADYDOM_NOPATCH = true;
233234

234235
// Get the template for this result or create a dummy one if a result
235236
// is not being rendered.
236-
const template = (value as ShadyTemplateResult)?._$litType$
237+
// This property needs to remain unminified.
238+
const template = (value as ShadyTemplateResult)?.['_$litType$']
237239
? (this._$committedValue as PatchableTemplateInstance)._$template.el
238240
: document.createElement('template');
239241
prepareStyles(scope!, template);

packages/lit-html/src/test/directives/style-map_test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ suite('styleMap', () => {
3636
test('render() only properties', () => {
3737
// Get the StyleMapDirective class indirectly, since it's not exported
3838
const result = styleMap({});
39-
const StyleMapDirective = result._$litDirective$;
39+
// This property needs to remain unminified.
40+
const StyleMapDirective = result['_$litDirective$'];
4041

4142
// Extend StyleMapDirective so we can test its render() method
4243
class TestStyleMapDirective extends StyleMapDirective {

packages/lit-html/src/test/lit-html_test.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,8 @@ suite('lit-html', () => {
15291529
};
15301530
assertRender(
15311531
{
1532-
_$litType$: _$lit_template_1,
1532+
// This property needs to remain unminified.
1533+
['_$litType$']: _$lit_template_1,
15331534
values: ['A'],
15341535
},
15351536
'A'
@@ -1543,7 +1544,8 @@ suite('lit-html', () => {
15431544
parts: [{type: 2, index: 1}],
15441545
};
15451546
const result = {
1546-
_$litType$: _$lit_template_1,
1547+
// This property needs to remain unminified.
1548+
['_$litType$']: _$lit_template_1,
15471549
values: ['A'],
15481550
};
15491551
assertRender(result, '<div>A</div>');
@@ -1564,7 +1566,8 @@ suite('lit-html', () => {
15641566
],
15651567
};
15661568
const result = {
1567-
_$litType$: _$lit_template_1,
1569+
// This property needs to remain unminified.
1570+
['_$litType$']: _$lit_template_1,
15681571
values: ['A'],
15691572
};
15701573
assertRender(result, '<div foo="A"></div>');
@@ -1578,7 +1581,8 @@ suite('lit-html', () => {
15781581
parts: [{type: 6, index: 0}],
15791582
};
15801583
const result = {
1581-
_$litType$: _$lit_template_1,
1584+
// This property needs to remain unminified.
1585+
['_$litType$']: _$lit_template_1,
15821586
values: [ref(r)],
15831587
};
15841588
assertRender(result, '<div></div>');

0 commit comments

Comments
 (0)