Skip to content

Commit 821da79

Browse files
authored
feat(compiler): include getAssetPath in generated export statement (#4683)
* feat(compiler): include `getAssetPath` in generated export statement * add typedef for `getAssetPath` * update typedef signature & add typedef to tests
1 parent 65d60fb commit 821da79

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ const generateCustomElementsTypesOutput = async (
9494
]
9595
: []),
9696
`/**`,
97+
` * Get the base path to where the assets can be found. Use "setAssetPath(path)"`,
98+
` * if the path needs to be customized.`,
99+
` */`,
100+
`export declare const getAssetPath: (path: string) => string;`,
101+
``,
102+
`/**`,
97103
` * Used to manually set the base path where assets can be found.`,
98104
` * If the script is used as "module", it's recommended to use "import.meta.url",`,
99105
` * such as "setAssetPath(import.meta.url)". Other options include`,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export const generateEntryPoint = (
257257

258258
// Exports that are always present
259259
exports.push(
260-
`export { setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
260+
`export { getAssetPath, setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';`,
261261
`export * from '${USER_INDEX_ENTRY_ID}';`,
262262
);
263263

src/compiler/output-targets/test/custom-elements-types.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ describe('Custom Elements Typedef generation', () => {
8181
`export { MyBestComponent as MyBestComponent } from '../types_dir/components/the-other-component/my-real-best-component';`,
8282
`export { defineCustomElement as defineCustomElementMyBestComponent } from './my-best-component';`,
8383
'',
84+
`/**`,
85+
` * Get the base path to where the assets can be found. Use "setAssetPath(path)"`,
86+
` * if the path needs to be customized.`,
87+
` */`,
88+
`export declare const getAssetPath: (path: string) => string;`,
89+
'',
8490
'/**',
8591
' * Used to manually set the base path where assets can be found.',
8692
' * If the script is used as "module", it\'s recommended to use "import.meta.url",',
@@ -129,6 +135,12 @@ describe('Custom Elements Typedef generation', () => {
129135
`export { MyBestComponent as MyBestComponent } from './types_dir/components/the-other-component/my-real-best-component';`,
130136
`export { defineCustomElement as defineCustomElementMyBestComponent } from './my-best-component';`,
131137
'',
138+
`/**`,
139+
` * Get the base path to where the assets can be found. Use "setAssetPath(path)"`,
140+
` * if the path needs to be customized.`,
141+
` */`,
142+
`export declare const getAssetPath: (path: string) => string;`,
143+
'',
132144
'/**',
133145
' * Used to manually set the base path where assets can be found.',
134146
' * If the script is used as "module", it\'s recommended to use "import.meta.url",',
@@ -188,6 +200,12 @@ describe('Custom Elements Typedef generation', () => {
188200
await generateCustomElementsTypes(config, compilerCtx, buildCtx, 'types_dir');
189201

190202
const expectedTypedefOutput = [
203+
`/**`,
204+
` * Get the base path to where the assets can be found. Use "setAssetPath(path)"`,
205+
` * if the path needs to be customized.`,
206+
` */`,
207+
`export declare const getAssetPath: (path: string) => string;`,
208+
'',
191209
'/**',
192210
' * Used to manually set the base path where assets can be found.',
193211
' * If the script is used as "module", it\'s recommended to use "import.meta.url",',
@@ -244,6 +262,12 @@ describe('Custom Elements Typedef generation', () => {
244262
await generateCustomElementsTypes(config, compilerCtx, buildCtx, 'types_dir');
245263

246264
const expectedTypedefOutput = [
265+
`/**`,
266+
` * Get the base path to where the assets can be found. Use "setAssetPath(path)"`,
267+
` * if the path needs to be customized.`,
268+
` */`,
269+
`export declare const getAssetPath: (path: string) => string;`,
270+
'',
247271
'/**',
248272
' * Used to manually set the base path where assets can be found.',
249273
' * If the script is used as "module", it\'s recommended to use "import.meta.url",',

src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('Custom Elements output target', () => {
7272
});
7373

7474
expect(entryPoint).toEqual(`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';
75-
export { setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
75+
export { getAssetPath, setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
7676
export * from '${USER_INDEX_ENTRY_ID}';
7777
7878
globalScripts();
@@ -86,7 +86,7 @@ globalScripts();
8686
});
8787

8888
expect(entryPoint)
89-
.toEqual(`export { setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
89+
.toEqual(`export { getAssetPath, setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
9090
export * from '${USER_INDEX_ENTRY_ID}';
9191
`);
9292
});
@@ -164,7 +164,7 @@ export * from '${USER_INDEX_ENTRY_ID}';
164164
addCustomElementInputs(buildCtx, bundleOptions, config.outputTargets[0] as OutputTargetDistCustomElements);
165165
expect(bundleOptions.loader['\0core']).toEqual(
166166
`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';
167-
export { setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
167+
export { getAssetPath, setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
168168
export * from '${USER_INDEX_ENTRY_ID}';
169169
170170
globalScripts();
@@ -197,7 +197,7 @@ globalScripts();
197197
addCustomElementInputs(buildCtx, bundleOptions, config.outputTargets[0] as OutputTargetDistCustomElements);
198198
expect(bundleOptions.loader['\0core']).toEqual(
199199
`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';
200-
export { setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
200+
export { getAssetPath, setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
201201
export * from '${USER_INDEX_ENTRY_ID}';
202202
export { StubCmp, defineCustomElement as defineCustomElementStubCmp } from '\0StubCmp';
203203
export { MyBestComponent, defineCustomElement as defineCustomElementMyBestComponent } from '\0MyBestComponent';
@@ -224,7 +224,7 @@ globalScripts();
224224
addCustomElementInputs(buildCtx, bundleOptions, config.outputTargets[0] as OutputTargetDistCustomElements);
225225
expect(bundleOptions.loader['\0core']).toEqual(
226226
`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';
227-
export { setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
227+
export { getAssetPath, setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
228228
export * from '${USER_INDEX_ENTRY_ID}';
229229
export { ComponentWithJsx, defineCustomElement as defineCustomElementComponentWithJsx } from '\0ComponentWithJsx';
230230
@@ -259,7 +259,7 @@ globalScripts();
259259
`import { globalScripts } from '${STENCIL_APP_GLOBALS_ID}';
260260
import { StubCmp } from '\0StubCmp';
261261
import { MyBestComponent } from '\0MyBestComponent';
262-
export { setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
262+
export { getAssetPath, setAssetPath, setNonce, setPlatformOptions } from '${STENCIL_INTERNAL_CLIENT_ID}';
263263
export * from '${USER_INDEX_ENTRY_ID}';
264264
265265
globalScripts();

0 commit comments

Comments
 (0)