Skip to content

Commit c6be6ef

Browse files
authored
feat(output-targets): add section for customElementsExportBehavior (#909)
This commit modifies the docs for `dist-custom-elements` to add documentation for the `customElementsExportBehavior` config option
1 parent 8a4e7e4 commit c6be6ef

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

src/docs/output-targets/custom-elements.md

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This directory can be configured using the output target's `dir` config. The
2727
generated files will each export a component class and will already have the
2828
styles bundled. However, this build does not define the custom elements or
2929
apply any polyfills. Any dependencies of your imported component will need to
30-
be loaded as well.
30+
be loaded as well.
3131

3232
Below is an example of defining a custom element:
3333

@@ -37,24 +37,45 @@ import { HelloWorld } from 'my-library/dist/components/hello-world';
3737
customElements.define('hello-world', HelloWorld);
3838
```
3939

40-
The output directory will also contain an `index.js` file which exports all of
41-
your components and their respective `defineCustomElement` helper functions,
42-
looking something like this:
40+
The output directory will also contain an `index.js` file which exports some helper methods by default. The contents of the file
41+
will look something like:
4342

44-
```tsx
43+
```js
4544
export { setAssetPath, setPlatformOptions } from '@stencil/core/internal/client';
46-
export { MyComponent, defineCustomElement as defineCustomElementMyComponent } from './my-component.js';
47-
export {
48-
MyOtherComponent,
49-
defineCustomElement as defineCustomElementMyOtherComponent
50-
} from './my-other-component.js';
5145
```
5246

53-
This file can be used as the root module when distributing your component
54-
library, see [below](#distributing-custom-elements) for more details.
47+
> Note: The contents may look different if [`customElementsExportBehavior`](#customelementsexportbehavior) is specified!
5548
5649
## Config
5750

51+
### customElementsExportBehavior
52+
53+
This config option provides additional behaviors that will alter the default component export OR custom element definition behaviors
54+
for this target. The desired behavior can be set via the following in a project's Stencil config:
55+
56+
```ts
57+
// stencil.config.ts
58+
import { Config } from '@stencil/core';
59+
60+
export const config: Config = {
61+
outputTargets: [
62+
{
63+
type: 'dist-custom-elements',
64+
customElementsExportBehavior: 'default' | 'single-export-module',
65+
},
66+
// ...
67+
],
68+
// ...
69+
};
70+
```
71+
72+
| Option | Description |
73+
| ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
74+
| `default` | No additional re-export or auto-definition behavior will be performed.<br><br>This value will be used if no explicit value is set in the config, or if a given value is not a valid option. |
75+
| `single-export-module` | All component and custom element definition helper functions will be exported from the `index.js` file in the output directory (see [Defining Exported Custom Elements](#defining-exported-custom-elements) for more information on this file's purpose). This file can be used as the root module when distributing your component library, see [below](#distributing-custom-elements) for more details. |
76+
77+
<!-- TODO(STENCIL-457): Move this info to the appropriate option on `customElementsExportBehavior` -->
78+
5879
### autoDefineCustomElements
5980

6081
By default, consumers of the `dist-custom-elements` output target need to
@@ -66,8 +87,8 @@ will also automatically recursively define any child components as well.
6687
This flag defaults to `false` when omitted from a Stencil configuration file.
6788

6889
> Note: At this time, components created not using JSX may not be automatically
69-
defined. This is a known limitation of the API and users should be aware of
70-
it
90+
> defined. This is a known limitation of the API and users should be aware of
91+
> it
7192
7293
### generateTypeDeclarations
7394

@@ -77,14 +98,16 @@ Setting this flag to `false` will not generate type declaration files for the `d
7798

7899
This flag defaults to `true` when omitted from a Stencil configuration file.
79100

101+
> When set to generate type declarations, Stencil respects the export behavior selected via `customElementsExportBehavior` and generates type declarations specific to the content of that file.
102+
80103
## Making Assets Available
81104

82-
For performance reasons, the generated bundle does not include [local assets](/docs/assets) built within the JavaScript output,
105+
For performance reasons, the generated bundle does not include [local assets](/docs/assets) built within the JavaScript output,
83106
but instead it's recommended to keep static assets as external files. By keeping them external this ensures they can be requested on-demand, rather
84-
than either welding their content into the JS file, or adding many URLs for the bundler to add to the output.
107+
than either welding their content into the JS file, or adding many URLs for the bundler to add to the output.
85108
One method to ensure [assets](/docs/assets) are available to external builds and http servers is to set the asset path using `setAssetPath()`.
86109

87-
The `setAssetPath()` function is used to manually set the base path where static assets can be found.
110+
The `setAssetPath()` function is used to manually set the base path where static assets can be found.
88111
For the lazy-loaded output target the asset path is automatically set and assets copied to the correct
89112
build directory. However, for custom elements builds, the `setAssetPath(path)` should be
90113
used to customize the asset path depending on where they are found on the http server.
@@ -93,15 +116,14 @@ If the component's script is a `type="module"`, it's recommended to use `import.
93116
as `setAssetPath(import.meta.url)`. Other options include `setAssetPath(document.currentScript.src)`, or using a bundler's replace plugin to
94117
dynamically set the path at build time, such as `setAssetPath(process.env.ASSET_PATH)`.
95118

96-
97119
```tsx
98120
import { setAssetPath } from 'my-library/dist/components';
99121

100122
setAssetPath(document.currentScript.src);
101123
```
102124

103125
Make sure to copy the assets over to a public directory in your app. This configuration depends on how your script is bundled, or lack of
104-
bundling, and where your assets can be loaded from. How the files are copied to the production build directory depends on the bundler or tooling.
126+
bundling, and where your assets can be loaded from. How the files are copied to the production build directory depends on the bundler or tooling.
105127
The configs below provide examples of how to do this automatically with popular bundlers.
106128

107129
## Distributing Custom Elements
@@ -129,9 +151,9 @@ To make the custom elements index the entry module for a package, set the
129151
Be sure to set `@stencil/core` as a dependency of the package as well.
130152

131153
> Note: If you are distributing the output of both the
132-
[`dist`](/docs/output-targets/dist) and `dist-custom-elements` targets, then
133-
it's up to you to choose which one of them should be available in the
134-
`module` entry.
154+
> [`dist`](/docs/output-targets/dist) and `dist-custom-elements` targets, then
155+
> it's up to you to choose which one of them should be available in the
156+
> `module` entry.
135157
136158
Consumers of your library can then either import components from their
137159
individual files, like so:

0 commit comments

Comments
 (0)