@arcmantle/lit-jsx 1.0.32 → 1.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,18 @@
1
1
  import type { BabelFile, PluginPass } from '@babel/core';
2
2
 
3
+
3
4
  export type BabelPlugins = NonNullable<NonNullable<babel.TransformOptions['parserOpts']>['plugins']>;
4
5
 
5
6
 
6
- export const babelPlugins: Set<BabelPlugins[number]> = new Set([ 'jsx', 'typescript', 'decorators', 'decoratorAutoAccessors' ]);
7
+ export const babelPlugins: Set<BabelPlugins[number]> = new Set([
8
+ 'jsx',
9
+ 'typescript',
10
+ 'decorators',
11
+ 'decoratorAutoAccessors',
12
+ ]);
7
13
  export const debugMode = { value: false };
8
14
 
9
-
15
+ export const CE_ATTR_IDENTIFIER = 'static';
10
16
  export const COMPONENT_LITERAL_PREFIX = '__$';
11
17
  export const WHITESPACE_TAGS: string[] = [ 'pre', 'textarea' ];
12
18
  export const SPECIAL_TAGS: string[] = [];
@@ -97,26 +103,40 @@ export const ERROR_MESSAGES = {
97
103
  } as const;
98
104
 
99
105
 
106
+ interface PluginOptions {
107
+ useCompiledTemplates?: boolean;
108
+ useImportDiscovery?: boolean;
109
+ }
110
+
111
+
100
112
  export const initializePluginOptions = (file: BabelFile): { useCompiledTemplates?: boolean; } => {
101
113
  if (optionsInitialized)
102
114
  return options;
103
115
 
104
116
  optionsInitialized = true;
105
117
 
106
- const plugin = file.opts?.plugins
107
- ?.find(plugin => (plugin as PluginPass).key === 'lit-jsx-transform') as { options?: { useCompiledTemplates?: boolean; }; };
118
+ const plugin = file.opts?.plugins?.find(plugin =>
119
+ (plugin as PluginPass).key === 'lit-jsx-transform') as { options?: PluginOptions; };
108
120
 
109
121
  const pluginOptions = plugin?.options || {};
122
+ pluginOptions.useCompiledTemplates ??= true;
110
123
 
111
124
  for (const [ key, value ] of Object.entries(pluginOptions))
112
125
  options[key as keyof typeof options] = value as any;
113
126
 
114
127
  console.log(`Lit JSX plugin options initialized:`, options);
115
128
 
116
-
117
129
  return pluginOptions;
118
130
  };
119
131
 
120
132
 
133
+ export const getPluginOptions = (file: BabelFile): PluginOptions => {
134
+ const plugin = file.opts?.plugins?.find(plugin =>
135
+ (plugin as PluginPass).key === 'lit-jsx-transform') as { options?: PluginOptions; };
136
+
137
+ return plugin.options ?? {};
138
+ };
139
+
140
+
121
141
  let optionsInitialized = false;
122
- export const options: { useCompiledTemplates?: boolean; } = {};
142
+ export const options: PluginOptions = {};
@@ -16,6 +16,7 @@ import {
16
16
  isJSXElementStatic,
17
17
  isJSXFragmentPath,
18
18
  isValidOpeningElement,
19
+ normalizeText,
19
20
  } from './compiler-utils.js';
20
21
  import {
21
22
  Ensure,
@@ -160,10 +161,14 @@ export class TemplateTranspiler extends JSXTranspiler<TemplateContext> {
160
161
  override children(context: TemplateContext): void {
161
162
  for (const [ index, child ] of context.path.node.children.entries()) {
162
163
  if (t.isJSXText(child)) {
163
- if (WHITESPACE_TAGS.includes(context.tagName))
164
+ if (WHITESPACE_TAGS.includes(context.tagName)) {
164
165
  context.builder.addText(child.value);
165
- else
166
- context.builder.addText(child.value.trim());
166
+ }
167
+ else {
168
+ const normalizedValue = normalizeText(child.value);
169
+ if (normalizedValue)
170
+ context.builder.addText(normalizedValue);
171
+ }
167
172
  }
168
173
  else if (t.isJSXExpressionContainer(child)) {
169
174
  if (t.isJSXEmptyExpression(child.expression))
@@ -300,9 +305,14 @@ export class TemplateTranspiler extends JSXTranspiler<TemplateContext> {
300
305
  const child = childPath.node;
301
306
 
302
307
  if (t.isJSXText(child)) {
303
- const trimmedValue = child.value.trim();
304
- if (trimmedValue)
305
- childrenArray.push(t.stringLiteral(trimmedValue));
308
+ if (WHITESPACE_TAGS.includes(context.tagName)) {
309
+ childrenArray.push(t.stringLiteral(child.value));
310
+ }
311
+ else {
312
+ const normalizedValue = normalizeText(child.value);
313
+ if (normalizedValue)
314
+ childrenArray.push(t.stringLiteral(normalizedValue));
315
+ }
306
316
  }
307
317
  else if (t.isJSXExpressionContainer(child)) {
308
318
  if (t.isJSXEmptyExpression(child.expression))
@@ -463,10 +473,14 @@ export class CompiledTranspiler extends JSXTranspiler<CompiledContext> {
463
473
  const child = childPath.node;
464
474
 
465
475
  if (t.isJSXText(child)) {
466
- if (WHITESPACE_TAGS.includes(context.tagName))
476
+ if (WHITESPACE_TAGS.includes(context.tagName)) {
467
477
  context.builder.addText(child.value);
468
- else
469
- context.builder.addText(child.value.trim());
478
+ }
479
+ else {
480
+ const normalizedValue = normalizeText(child.value);
481
+ if (normalizedValue)
482
+ context.builder.addText(normalizedValue);
483
+ }
470
484
  }
471
485
  else if (t.isJSXExpressionContainer(child)) {
472
486
  if (t.isJSXEmptyExpression(child.expression))
@@ -560,9 +574,14 @@ export class CompiledTranspiler extends JSXTranspiler<CompiledContext> {
560
574
  const child = childPath.node;
561
575
 
562
576
  if (t.isJSXText(child)) {
563
- const trimmedValue = child.value.trim();
564
- if (trimmedValue)
565
- childrenArray.push(t.stringLiteral(trimmedValue));
577
+ if (WHITESPACE_TAGS.includes(context.tagName)) {
578
+ childrenArray.push(t.stringLiteral(child.value));
579
+ }
580
+ else {
581
+ const normalizedValue = normalizeText(child.value);
582
+ if (normalizedValue)
583
+ childrenArray.push(t.stringLiteral(normalizedValue));
584
+ }
566
585
  }
567
586
  else if (t.isJSXExpressionContainer(child)) {
568
587
  if (t.isJSXEmptyExpression(child.expression))
@@ -16,8 +16,10 @@ import { ImportDiscovery } from './import-discovery.js';
16
16
  export const litJsx = (options: {
17
17
  /** Enable legacy decorators support */
18
18
  legacyDecorators?: boolean;
19
- /** Enables experimental support for compiled templates */
19
+ /** Enables support for experimental compiled templates @default true */
20
20
  useCompiledTemplates?: boolean;
21
+ /** Opts into the automatic discovery is custom elements instead of using the static attribute */
22
+ useImportDiscovery?: boolean;
21
23
  /** Enable debug mode for additional logging */
22
24
  debug?: boolean;
23
25
  /** Options for the Babel transform */
@@ -65,6 +67,7 @@ export const litJsx = (options: {
65
67
  plugins: [
66
68
  litJsxBabelPlugin({
67
69
  useCompiledTemplates: options.useCompiledTemplates,
70
+ useImportDiscovery: options.useImportDiscovery,
68
71
  }),
69
72
  ],
70
73
  ast: false,