-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(template-compiler): dynamic components #3337
Changes from 9 commits
aad8a45
757e9fb
86cfb90
3c566fd
5b27c96
30b9bd0
412abb0
d7b1c63
a5c26e3
d9caaed
856fd97
db95aed
7be5c78
98211cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ const DEFAULT_OPTIONS = { | |
disableSyntheticShadowSupport: false, | ||
}; | ||
|
||
const DEFAULT_DYNAMIC_CMP_CONFIG: Required<DynamicComponentConfig> = { | ||
const DEFAULT_DYNAMIC_IMPORT_CONFIG: Required<DynamicImportConfig> = { | ||
loader: '', | ||
strictSpecifier: true, | ||
}; | ||
|
@@ -56,7 +56,7 @@ export interface OutputConfig { | |
minify?: boolean; | ||
} | ||
|
||
export interface DynamicComponentConfig { | ||
export interface DynamicImportConfig { | ||
loader?: string; | ||
strictSpecifier?: boolean; | ||
} | ||
|
@@ -65,7 +65,10 @@ export interface TransformOptions { | |
name?: string; | ||
namespace?: string; | ||
stylesheetConfig?: StylesheetConfig; | ||
experimentalDynamicComponent?: DynamicComponentConfig; | ||
dynamicImportConfig?: DynamicImportConfig; | ||
jmsjtu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO [#3331]: remove usage of lwc:dynamic in 246 | ||
experimentalDynamicDirective?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, these old types are best left alone for deprecation later. The cascading effect of changing the existing types will become a overhead to manage. Here are some the repos that depend on this type:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I debated about this initially but I think you're right. Changing these compiler options will lead to unnecessary overhead in managing the release of this feature to downstream dependencies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed We're planning to remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
enableDynamicComponents?: boolean; | ||
jmsjtu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
outputConfig?: OutputConfig; | ||
isExplicitImport?: boolean; | ||
preserveHtmlComments?: boolean; | ||
|
@@ -85,6 +88,8 @@ type RequiredTransformOptions = Omit< | |
| 'customRendererConfig' | ||
| 'enableLwcSpread' | ||
| 'enableScopedSlots' | ||
| 'enableDynamicComponents' | ||
| 'experimentalDynamicDirective' | ||
jmsjtu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>; | ||
export interface NormalizedTransformOptions extends RecursiveRequired<RequiredTransformOptions> { | ||
name?: string; | ||
|
@@ -93,6 +98,9 @@ export interface NormalizedTransformOptions extends RecursiveRequired<RequiredTr | |
customRendererConfig?: CustomRendererConfig; | ||
enableLwcSpread?: boolean; | ||
enableScopedSlots?: boolean; | ||
enableDynamicComponents?: boolean; | ||
// TODO [#3331]: remove usage of lwc:dynamic in 246 | ||
experimentalDynamicDirective?: boolean; | ||
} | ||
|
||
export function validateTransformOptions(options: TransformOptions): NormalizedTransformOptions { | ||
|
@@ -165,16 +173,16 @@ function normalizeOptions(options: TransformOptions): NormalizedTransformOptions | |
}, | ||
}; | ||
|
||
const experimentalDynamicComponent: Required<DynamicComponentConfig> = { | ||
...DEFAULT_DYNAMIC_CMP_CONFIG, | ||
...options.experimentalDynamicComponent, | ||
const dynamicImportConfig: Required<DynamicImportConfig> = { | ||
...DEFAULT_DYNAMIC_IMPORT_CONFIG, | ||
...options.dynamicImportConfig, | ||
}; | ||
|
||
return { | ||
...DEFAULT_OPTIONS, | ||
...options, | ||
stylesheetConfig, | ||
outputConfig, | ||
experimentalDynamicComponent, | ||
dynamicImportConfig, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,24 +28,29 @@ export default function templateTransform( | |
options: NormalizedTransformOptions | ||
): TransformResult { | ||
const { | ||
experimentalDynamicComponent, | ||
dynamicImportConfig, | ||
preserveHtmlComments, | ||
enableStaticContentOptimization, | ||
customRendererConfig, | ||
enableLwcSpread, | ||
enableScopedSlots, | ||
enableDynamicComponents, | ||
// TODO [#3331]: remove usage of lwc:dynamic in 246 | ||
experimentalDynamicDirective: deprecatedDynamicDirective, | ||
} = options; | ||
const experimentalDynamicDirective = Boolean(experimentalDynamicComponent); | ||
const experimentalDynamicDirective = deprecatedDynamicDirective ?? Boolean(dynamicImportConfig); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left a comment here regarding the flags but changing This is actually in place to preserve existing behavior for downstream consumers. The current behavior is to enable In 244 we want to allow the LCS team to disable access to The |
||
|
||
let result; | ||
try { | ||
result = compile(src, { | ||
// TODO [#3331]: remove usage of lwc:dynamic in 246 | ||
jmsjtu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
experimentalDynamicDirective, | ||
preserveHtmlComments, | ||
enableStaticContentOptimization, | ||
customRendererConfig, | ||
enableLwcSpread, | ||
enableScopedSlots, | ||
enableDynamicComponents, | ||
}); | ||
} catch (e) { | ||
throw normalizeToCompilerError(TransformerErrors.HTML_TRANSFORMER_ERROR, e, { filename }); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -533,9 +533,11 @@ function fid(url: string | undefined | null): string | null | undefined { | |
} | ||
|
||
/** | ||
* create a dynamic component via `<x-foo lwc:dynamic={Ctor}>` | ||
* [ddc] - create a (deprecated) dynamic component via `<x-foo lwc:dynamic={Ctor}>` | ||
* | ||
* TODO [#3331]: remove usage of lwc:dynamic in 246 | ||
*/ | ||
function dc( | ||
function ddc( | ||
pmdartus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sel: string, | ||
Ctor: LightningElementConstructor | null | undefined, | ||
data: VElementData, | ||
|
@@ -550,7 +552,7 @@ function dc( | |
); | ||
} | ||
// null or undefined values should produce a null value in the VNodes | ||
if (Ctor == null) { | ||
if (isNull(Ctor) || isUndefined(Ctor)) { | ||
return null; | ||
} | ||
if (!isComponentConstructor(Ctor)) { | ||
|
@@ -560,6 +562,35 @@ function dc( | |
return c(sel, Ctor, data, children); | ||
} | ||
|
||
/** | ||
* [dc] - create a dynamic component via `<lwc:component lwc:is={Ctor}>` | ||
*/ | ||
function dc( | ||
Ctor: LightningElementConstructor | null | undefined, | ||
data: VElementData, | ||
children: VNodes = EmptyArray | ||
): VCustomElement | null { | ||
if (process.env.NODE_ENV !== 'production') { | ||
assert.isTrue(isObject(data), `dc() 2nd argument data must be an object.`); | ||
assert.isTrue( | ||
arguments.length === 3 || isArray(children), | ||
`dc() 3rd argument data must be an array.` | ||
); | ||
} | ||
// null or undefined values should produce a null value in the VNodes | ||
if (isNull(Ctor) || isUndefined(Ctor)) { | ||
return null; | ||
} | ||
|
||
if (!isComponentConstructor(Ctor)) { | ||
throw new Error( | ||
`Invalid constructor ${toString(Ctor)} is not a LightningElement constructor.` | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
Comment on lines
+568
to
+592
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a dummy implementation and is not how the function is designed to work. The implementation can be found in part 2, here. This is only added here because the template compiler will call the |
||
|
||
/** | ||
* slow children collection marking mechanism. this API allows the compiler to signal | ||
* to the engine that a particular collection of children must be diffed using the slow | ||
|
@@ -628,6 +659,7 @@ const api = ObjectFreeze({ | |
fid, | ||
shc, | ||
ssf, | ||
ddc, | ||
}); | ||
|
||
export default api; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { rollup } from 'rollup'; | ||
import { rollup, RollupWarning } from 'rollup'; | ||
// @ts-ignore | ||
import lwcRollupPlugin from '@lwc/rollup-plugin'; | ||
import { isVoidElement, HTML_NAMESPACE } from '@lwc/shared'; | ||
|
@@ -29,6 +29,8 @@ jest.setTimeout(10_000 /* 10 seconds */); | |
async function compileFixture({ input, dirname }: { input: string; dirname: string }) { | ||
const modulesDir = path.resolve(dirname, './modules'); | ||
const outputFile = path.resolve(dirname, './dist/compiled.js'); | ||
// TODO [#3331]: this is only needed to silence warnings on lwc:dynamic, remove in 246. | ||
const warnings: RollupWarning[] = []; | ||
|
||
const bundle = await rollup({ | ||
input, | ||
|
@@ -43,6 +45,9 @@ async function compileFixture({ input, dirname }: { input: string; dirname: stri | |
], | ||
}), | ||
], | ||
onwarn(warning) { | ||
warnings.push(warning); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend checking whether or not the |
||
}, | ||
}); | ||
|
||
await bundle.write({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -548,7 +548,7 @@ export const ParserDiagnostics = { | |
LWC_INNER_HTML_INVALID_CUSTOM_ELEMENT: { | ||
code: 1140, | ||
message: | ||
'Invalid lwc:inner-html usage on element "{0}". The directive can\'t be used on a custom element.', | ||
'Invalid lwc:inner-html usage on element "{0}". The directive can\'t be used on a custom element or special LWC managed elements denoted with lwc:*.', | ||
level: DiagnosticLevel.Error, | ||
url: '', | ||
}, | ||
|
@@ -846,4 +846,49 @@ export const ParserDiagnostics = { | |
level: DiagnosticLevel.Warning, | ||
url: '', | ||
}, | ||
|
||
LWC_COMPONENT_TAG_WITHOUT_IS_DIRECTIVE: { | ||
code: 1183, | ||
message: `<lwc:component> must have an 'lwc:is' attribute.`, | ||
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I read this as "an ell" so I think it's fine to say |
||
level: DiagnosticLevel.Error, | ||
url: '', | ||
}, | ||
|
||
UNSUPPORTED_LWC_TAG_NAME: { | ||
code: 1184, | ||
message: '{0} is not a special LWC tag name and will be treated as an HTML element.', | ||
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. I know some Anglophones say "haitch" for "H", but "aitch" is more common AFAIK. |
||
level: DiagnosticLevel.Warning, | ||
url: '', | ||
}, | ||
|
||
INVALID_LWC_IS_DIRECTIVE_VALUE: { | ||
code: 1185, | ||
message: | ||
'Invalid lwc:is usage for value {0}. The value assigned to lwc:is must be an expression.', | ||
level: DiagnosticLevel.Error, | ||
url: '', | ||
}, | ||
|
||
LWC_IS_INVALID_ELEMENT: { | ||
code: 1186, | ||
message: | ||
'Invalid lwc:is usage for element {0}. The directive can only be used with <lwc:component>', | ||
level: DiagnosticLevel.Error, | ||
url: '', | ||
}, | ||
|
||
INVALID_OPTS_LWC_ENABLE_DYNAMIC_COMPONENTS: { | ||
code: 1187, | ||
message: | ||
'Invalid lwc:is usage. The LWC dynamic Directive must be enabled in order to use this feature.', | ||
jmsjtu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
level: DiagnosticLevel.Error, | ||
url: '', | ||
}, | ||
|
||
DEPRECATED_LWC_DYNAMIC_ATTRIBUTE: { | ||
code: 1188, | ||
message: `The lwc:dynamic directive is deprecated and will be removed in a future release. Please use lwc:is instead.`, | ||
level: DiagnosticLevel.Warning, | ||
url: '', | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change will need a co-ordinated change in
lwc-platform
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also needs a coordinated release as
experimentalDynamicDirective
should now be available as a compiler option.