Skip to content

Commit 12fec21

Browse files
committed
feat(setPlatformOptions): add setPlatformOptions for ce builds
For custom element builds, export `setPlatformOptions(opts)`, which is similar to options that can be set within a lazy load `defineCustomElements()` options.
1 parent 1aa9cae commit 12fec21

File tree

6 files changed

+84
-25
lines changed

6 files changed

+84
-25
lines changed

src/compiler/output-targets/dist-custom-elements-bundle/custom-elements-bundle-types.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ import { isOutputTargetDistCustomElementsBundle } from '../output-utils';
33
import { dirname, join, relative } from 'path';
44
import { normalizePath, dashToPascalCase } from '@utils';
55

6-
export const generateCustomElementsBundleTypes = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, distDtsFilePath: string) => {
6+
export const generateCustomElementsBundleTypes = async (
7+
config: d.Config,
8+
compilerCtx: d.CompilerCtx,
9+
buildCtx: d.BuildCtx,
10+
distDtsFilePath: string,
11+
) => {
712
const outputTargets = config.outputTargets.filter(isOutputTargetDistCustomElementsBundle);
813

9-
await Promise.all(outputTargets.map(outputTarget => generateCustomElementsTypesOutput(config, compilerCtx, buildCtx, distDtsFilePath, outputTarget)));
14+
await Promise.all(
15+
outputTargets.map(outputTarget =>
16+
generateCustomElementsTypesOutput(config, compilerCtx, buildCtx, distDtsFilePath, outputTarget),
17+
),
18+
);
1019
};
1120

1221
const generateCustomElementsTypesOutput = async (
@@ -51,8 +60,15 @@ const generateCustomElementsTypesOutput = async (
5160
` */`,
5261
`export declare const setAssetPath: (path: string) => void;`,
5362
``,
63+
`export interface SetPlatformOptions {`,
64+
` raf?: (c: FrameRequestCallback) => number;`,
65+
` ael?: (el: EventTarget, eventName: string, listener: EventListenerOrEventListenerObject, options: boolean | AddEventListenerOptions) => void;`,
66+
` rel?: (el: EventTarget, eventName: string, listener: EventListenerOrEventListenerObject, options: boolean | AddEventListenerOptions) => void;`,
67+
`}`,
68+
`export declare const setPlatformOptions: (opts: SetPlatformOptions) => void;`,
69+
``,
5470
`export type { Components, JSX };`,
55-
``
71+
``,
5672
];
5773

5874
const usersIndexJsPath = join(config.srcDir, 'index.ts');
@@ -64,7 +80,9 @@ const generateCustomElementsTypesOutput = async (
6480
code.push(`export * from '${componentsDtsRelPath}';`);
6581
}
6682

67-
await compilerCtx.fs.writeFile(customElementsDtsPath, code.join('\n') + `\n`, { outputTargetType: outputTarget.type });
83+
await compilerCtx.fs.writeFile(customElementsDtsPath, code.join('\n') + `\n`, {
84+
outputTargetType: outputTarget.type,
85+
});
6886
};
6987

7088
const generateCustomElementType = (cmp: d.ComponentCompilerMeta) => {

src/compiler/output-targets/dist-custom-elements-bundle/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const bundleCustomElements = async (
6666
hoistTransitiveImports: false,
6767
preferConst: true,
6868
});
69-
69+
7070
const minify = outputTarget.externalRuntime || outputTarget.minify !== true ? false : config.minifyJs;
7171
const files = rollupOutput.output.map(async bundle => {
7272
if (bundle.type === 'chunk') {
@@ -99,7 +99,7 @@ const generateEntryPoint = (outputTarget: d.OutputTargetDistCustomElementsBundle
9999

100100
imp.push(
101101
`import { proxyCustomElement } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
102-
`export { setAssetPath } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
102+
`export { setAssetPath, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
103103
`export * from '${USER_INDEX_ENTRY_ID}';`,
104104
);
105105

src/compiler/output-targets/dist-custom-elements/custom-elements-types.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ import { isOutputTargetDistCustomElements } from '../output-utils';
33
import { dirname, join, relative } from 'path';
44
import { normalizePath, dashToPascalCase } from '@utils';
55

6-
export const generateCustomElementsTypes = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, distDtsFilePath: string) => {
6+
export const generateCustomElementsTypes = async (
7+
config: d.Config,
8+
compilerCtx: d.CompilerCtx,
9+
buildCtx: d.BuildCtx,
10+
distDtsFilePath: string,
11+
) => {
712
const outputTargets = config.outputTargets.filter(isOutputTargetDistCustomElements);
813

9-
await Promise.all(outputTargets.map(outputTarget => generateCustomElementsTypesOutput(config, compilerCtx, buildCtx, distDtsFilePath, outputTarget)));
14+
await Promise.all(
15+
outputTargets.map(outputTarget =>
16+
generateCustomElementsTypesOutput(config, compilerCtx, buildCtx, distDtsFilePath, outputTarget),
17+
),
18+
);
1019
};
1120

1221
export const generateCustomElementsTypesOutput = async (
@@ -36,8 +45,15 @@ export const generateCustomElementsTypesOutput = async (
3645
` */`,
3746
`export declare const setAssetPath: (path: string) => void;`,
3847
``,
48+
`export interface SetPlatformOptions {`,
49+
` raf?: (c: FrameRequestCallback) => number;`,
50+
` ael?: (el: EventTarget, eventName: string, listener: EventListenerOrEventListenerObject, options: boolean | AddEventListenerOptions) => void;`,
51+
` rel?: (el: EventTarget, eventName: string, listener: EventListenerOrEventListenerObject, options: boolean | AddEventListenerOptions) => void;`,
52+
`}`,
53+
`export declare const setPlatformOptions: (opts: SetPlatformOptions) => void;`,
54+
``,
3955
`export type { Components, JSX };`,
40-
``
56+
``,
4157
];
4258

4359
const usersIndexJsPath = join(config.srcDir, 'index.ts');
@@ -49,15 +65,19 @@ export const generateCustomElementsTypesOutput = async (
4965
code.push(`export * from '${componentsDtsRelPath}';`);
5066
}
5167

52-
await compilerCtx.fs.writeFile(customElementsDtsPath, code.join('\n') + `\n`, { outputTargetType: outputTarget.type });
68+
await compilerCtx.fs.writeFile(customElementsDtsPath, code.join('\n') + `\n`, {
69+
outputTargetType: outputTarget.type,
70+
});
5371

5472
const components = buildCtx.components.filter(m => !m.isCollectionDependency);
55-
await Promise.all(components.map(async cmp => {
56-
const dtsCode = generateCustomElementType(componentsDtsRelPath, cmp);
57-
const fileName = `${cmp.tagName}.d.ts`;
58-
const filePath = join(outputTarget.dir, fileName);
59-
await compilerCtx.fs.writeFile(filePath, dtsCode, { outputTargetType: outputTarget.type });
60-
}));
73+
await Promise.all(
74+
components.map(async cmp => {
75+
const dtsCode = generateCustomElementType(componentsDtsRelPath, cmp);
76+
const fileName = `${cmp.tagName}.d.ts`;
77+
const filePath = join(outputTarget.dir, fileName);
78+
await compilerCtx.fs.writeFile(filePath, dtsCode, { outputTargetType: outputTarget.type });
79+
}),
80+
);
6181
};
6282

6383
const generateCustomElementType = (componentsDtsRelPath: string, cmp: d.ComponentCompilerMeta) => {

src/compiler/output-targets/dist-custom-elements/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ import { removeCollectionImports } from '../../transformers/remove-collection-im
1111
import { STENCIL_INTERNAL_CLIENT_ID, USER_INDEX_ENTRY_ID, STENCIL_APP_GLOBALS_ID } from '../../bundle/entry-alias-ids';
1212
import { updateStencilCoreImports } from '../../transformers/update-stencil-core-import';
1313

14-
export const outputCustomElements = async (
15-
config: d.Config,
16-
compilerCtx: d.CompilerCtx,
17-
buildCtx: d.BuildCtx,
18-
) => {
14+
export const outputCustomElements = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) => {
1915
if (!config.buildDist) {
2016
return;
2117
}
@@ -94,14 +90,18 @@ const bundleCustomElements = async (
9490
}
9591
};
9692

97-
const addCustomElementInputs = (_outputTarget: d.OutputTargetDistCustomElements, buildCtx: d.BuildCtx, bundleOpts: BundleOptions) => {
93+
const addCustomElementInputs = (
94+
_outputTarget: d.OutputTargetDistCustomElements,
95+
buildCtx: d.BuildCtx,
96+
bundleOpts: BundleOptions,
97+
) => {
9898
const components = buildCtx.components;
9999
components.forEach(cmp => {
100100
const exp: string[] = [];
101101
const exportName = dashToPascalCase(cmp.tagName);
102102
const importName = cmp.componentClassName;
103103
const importAs = `$Cmp${exportName}`;
104-
const coreKey = `\0${exportName}`
104+
const coreKey = `\0${exportName}`;
105105

106106
if (cmp.isPlain) {
107107
exp.push(`export { ${importName} as ${exportName} } from '${cmp.sourceFilePath}';`);
@@ -116,14 +116,14 @@ const addCustomElementInputs = (_outputTarget: d.OutputTargetDistCustomElements,
116116
bundleOpts.inputs[cmp.tagName] = coreKey;
117117
bundleOpts.loader[coreKey] = exp.join('\n');
118118
});
119-
}
119+
};
120120

121121
const generateEntryPoint = (outputTarget: d.OutputTargetDistCustomElements, _buildCtx: d.BuildCtx) => {
122122
const imp: string[] = [];
123123
const exp: string[] = [];
124124

125125
imp.push(
126-
`export { setAssetPath } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
126+
`export { setAssetPath, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
127127
`export * from '${USER_INDEX_ENTRY_ID}';`,
128128
);
129129

src/runtime/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export { forceUpdate, postUpdateComponent, getRenderingRef } from './update-comp
1616
export { proxyComponent } from './proxy-component';
1717
export { renderVdom } from './vdom/vdom-render';
1818
export { setMode, getMode } from './mode';
19+
export { setPlatformOptions } from './platform-options';
1920
export { Fragment } from './fragment';

src/runtime/platform-options.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { plt } from '@platform';
2+
3+
interface SetPlatformOptions {
4+
raf?: (c: FrameRequestCallback) => number;
5+
ael?: (
6+
el: EventTarget,
7+
eventName: string,
8+
listener: EventListenerOrEventListenerObject,
9+
options: boolean | AddEventListenerOptions,
10+
) => void;
11+
rel?: (
12+
el: EventTarget,
13+
eventName: string,
14+
listener: EventListenerOrEventListenerObject,
15+
options: boolean | AddEventListenerOptions,
16+
) => void;
17+
ce?: (eventName: string, opts?: any) => CustomEvent;
18+
}
19+
20+
export const setPlatformOptions = (opts: SetPlatformOptions) => Object.assign(plt, opts);

0 commit comments

Comments
 (0)