Skip to main content
Version: Next 🚧

Custom Elements

The dist-custom-elements output target creates custom elements that directly extend HTMLElement and provides simple utility functions for easily defining these elements on the Custom Element Registry. This output target excels in use in frontend frameworks and projects that will handle bundling, lazy-loading, and custom element registration themselves.

This target can be used outside of frameworks as well, if lazy-loading functionality is not required or desired. For using lazily loaded Rindo components, please refer to the dist output target.

To generate components using the dist-custom-elements output target, add it to a project's rindo.config.ts file like so:

rindo.config.ts
import { Config } from '@rindo/core';

export const config: Config = {
// Other top-level config options here
outputTargets: [
{
type: 'dist-custom-elements',
// Output target config options here
},
// Other output targets here
],
};

Config​

copy​

default: undefined

An array of copy tasks to be executed during the build process.

customElementsExportBehavior​

default: 'default'

By default, the dist-custom-elements output target generates a single file per component, and exports each of those files individually.

In some cases, library authors may want to change this behavior, for instance to automatically define component children, provide a single file containing all component exports, etc.

This config option provides additional behaviors that will alter the default component export OR custom element definition behaviors for this target. The desired behavior can be set via the following in a project's Rindo config:

// rindo.config.ts
import { Config } from '@rindo/core';

export const config: Config = {
outputTargets: [
{
type: 'dist-custom-elements',
customElementsExportBehavior: 'default' | 'auto-define-custom-elements' | 'bundle' | 'single-export-module',
},
// ...
],
// ...
};
OptionDescription
defaultNo additional re-export or auto-definition behavior will be performed.

This value will be used if no explicit value is set in the config, or if a given value is not a valid option.
auto-define-custom-elementsA component and its children will be automatically defined with the CustomElementRegistry when the component's module is imported.
bundleA utility defineCustomElements() function is exported from the index.js file of the output directory. This function can be used to quickly define all Rindo components in a project on the custom elements registry.
single-export-moduleAll component and custom element definition helper functions will be exported from the index.js file in the output directory. This file can be used as the root module when distributing your component library, see Publishing for more details.
note

At this time, components that do not use JSX cannot be automatically defined. This is a known limitation of Rindo that users should be aware of.

dir​

default: 'dist/components'

This config option allows you to change the output directory where the compiled output for this output target will be written.

empty​

default: true

Setting this flag to true will remove the contents of the output directory between builds.

externalRuntime​

default: true

Setting this flag to true results in the following behaviors:

  1. Minification will follow what is specified in the Rindo config.
  2. Filenames will not be hashed.
  3. All imports from packages under @rindo/core/* will be marked as external and therefore not included in the generated Rollup bundle.

Ensure that @rindo/core is included in your list of dependencies if you set this option to true. This is crucial to prevent any runtime errors.

generateTypeDeclarations​

default: true

By default, Rindo will generate type declaration files (.d.ts files) as a part of the dist-custom-elements output target through the generateTypeDeclarations field on the target options. Type declaration files will be placed in the dist/types directory in the root of a Rindo project. At this time, this output destination is not able to be configured.

Setting this flag to false will not generate type declaration files for the dist-custom-elements output target.

note

When set to generate type declarations, Rindo respects the export behavior selected via customElementsExportBehavior and generates type declarations specific to the content of the generated output directory's index.js file.

includeGlobalScripts​

default: false

Setting this flag to true will include global scripts in the bundle and execute them once the bundle entry point in loaded.

isPrimaryPackageOutputTarget​

default: false

If true, this output target will be used to validate package.json fields for your project's distribution. See the overview of primary package output target validation for more information.

minify​

default: false

Setting this flag to true will cause file minification to follow what is specified in the Rindo config. However, if externalRuntime is enabled, it will override this option and always result in minification being disabled.

Making Assets Available​

For performance reasons, the generated bundle does not include local assets built within the JavaScript output, 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 than either welding their content into the JS file, or adding many URLs for the bundler to add to the output. One method to ensure assets are available to external builds and http servers is to set the asset path using setAssetPath().

The setAssetPath() function is used to manually set the base path where static assets can be found. For the lazy-loaded output target the asset path is automatically set and assets copied to the correct build directory. However, for custom elements builds, the setAssetPath(path) should be used to customize the asset path depending on where they are found on the http server.

If the component's script is a type="module", it's recommended to use import.meta.url, such as setAssetPath(import.meta.url). Other options include setAssetPath(document.currentScript.src), or using a bundler's replace plugin to dynamically set the path at build time, such as setAssetPath(process.env.ASSET_PATH).

import { setAssetPath } from 'my-library/dist/components';

setAssetPath(document.currentScript.src);

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 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. The configs below provide examples of how to do this automatically with popular bundlers.

Example Bundler Configs​

Instructions for consuming the custom elements bundle vary depending on the bundler you're using. These examples will help your users consume your components with webpack and Rollup.

The following examples assume your component library is published to NPM as my-library. You should change this to the name you actually publish your library with.

Users will need to install your library before importing them.

npm install my-library

webpack.config.js​

A webpack config will look something like the one below. Note how assets are copied from the library's node_module folder to dist/assets via the CopyPlugin utility. This is important if your library includes local assets.

const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
plugins: [
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, 'node_modules/my-library/dist/my-library/assets'),
to: path.resolve(__dirname, 'dist/assets'),
},
],
}),
],
};

rollup.config.js​

A Rollup config will look something like the one below. Note how assets are copied from the library's node_module folder to dist/assets via the rollup-copy-plugin utility. This is important if your library includes local assets.

import path from 'path';
import commonjs from '@rollup/plugin-commonjs';
import copy from 'rollup-plugin-copy';
import postcss from 'rollup-plugin-postcss';
import resolve from '@rollup/plugin-node-resolve';

export default {
input: 'src/index.js',
output: [{ dir: path.resolve('dist/'), format: 'es' }],
plugins: [
resolve(),
commonjs(),
postcss({
extensions: ['.css'],
}),
copy({
targets: [
{
src: path.resolve(__dirname, 'node_modules/my-library/dist/my-library/assets'),
dest: path.resolve(__dirname, 'dist'),
},
],
}),
],
};