Assets
Stencil components may need one or more static files as a part of their design. These types of files are referred to as 'assets', and include images, fonts, etc.
In this guide, we describe different strategies for resolving assets on the filesystem.
CSS files are handled differently than assets; for more on using CSS, please see the styling documentation.
Asset Base Path
The asset base path is the directory that Stencil will use to resolve assets. When a component uses an asset, the asset's location is resolved relative to the asset base path.
The asset base path is automatically set for the following output targets:
For all other output targets, assets must be moved and the asset base path must be manually set.
For each instance of the Stencil runtime that is loaded, there is a single asset base path. Oftentimes, this means there is only one asset base path per application using Stencil.
Resolution Overview
The process of resolving an asset involves asking Stencil to build a path to the asset on the filesystem.
When an asset's path is built, the resolution is always done in a project's compiled output, not the directory containing the original source code.
The example below uses the output of the www
output target to demonstrate how assets are resolved.
Although the example uses the output of www
builds, the general principle of how an asset is found holds for all output targets.
When using the www
output target, a build/
directory is automatically created and set as the asset base path.
An example build/
directory and the assets it contains can be found below.
www/
├── build/
│ ├── assets/
│ │ ├── logo.png
│ │ └── scenery/
│ │ ├── beach.png
│ │ └── sunset.png
│ └── other-assets/
│ └── font.tiff
└── ...
To resolve the path to an asset, Stencil's getAssetPath()
API may be used.
When using getAssetPath
, the assets in the directory structure above are resolved relative to build/
.
The code sample below demonstrates the return value of getAssetPath
for different path
arguments.
The return value is a path that Stencil has built to retrieve the asset on the filesystem.
import { getAssetPath } from '@stencil/core';
// with an asset base path of "/build/":
// '/build/assets/logo.png'
getAssetPath('assets/logo.png');
// '/build/assets/scenery/beach.png'
getAssetPath('assets/scenery/beach.png');
// '/build/other-assets/font.tiff'
getAssetPath('other-assets/font.tiff');
Making Assets Available
In order to be able to find assets at runtime, they need to be found on the filesystem from the output of a Stencil build. In other words, we need to ensure they exist in the distribution directory. This section describes how to make assets available under the asset base path.
assetsDirs
The @Component
decorator can be configured with the assetsDirs
option.
assetsDirs
takes an array of strings, where each entry is a relative path from the component to a directory containing the assets the component requires.
When using the dist
or www
output targets, setting assetsDirs
instructs Stencil to copy that folder into the distribution folder.
When using other output targets, Stencil will not copy assets into the distribution folder.
Below is an example project's directory structure containing an example component and an assets directory.
src/
└── components/
├── assets/
│ ├── beach.jpg
│ └── sunset.jpg
└── my-component.tsx
Below, the my-component
component will correctly load the assets based on it's image
prop.
// file: my-component.tsx
// 1. getAssetPath is imported from '@stencil/core'
import { Component, Prop, getAssetPath, h } from '@stencil/core';
@Component({
tag: 'my-component',
// 2. assetsDirs lists the 'assets' directory as a relative
// (sibling) directory
assetsDirs: ['assets']
})
export class MyComponent {
@Prop() image = "sunset.jpg";
render() {
// 3. the asset path is retrieved relative to the asset
// base path to use in the <img> tag
const imageSrc = getAssetPath(`./assets/${this.image}`);
return <img src={imageSrc} />
}
}
In the example above, the following allows my-component
to display the provided asset:
getAssetPath()
is imported from@stencil/core
- The
my-component
's component decorator has theassetsDirs
property, and lists the sibling directory,assets
. This will copyassets
over to the distribution directory. getAssetPath
is used to retrieve the path to the image to be used in the<img>
tag