Stencil Config
Most projects need little or no configuration. Stencil's defaults cover the common case: a project with no stencil.config.ts at all builds as a component library, with the loader-bundle output. Keep the config as small as you can. Every property below is optional.
stencil.config.tsの例:
import { Config } from '@stencil/core';
export const config: Config = {
namespace: 'MyApp',
srcDir: 'src'
};
allowInlineScripts
デフォルト: true
In a production build of the www output, Stencil inlines the entry script into index.html when it's under 1 KB, which saves a request. Set it to false to always load the entry script as a separate file.
autoprefixCss
default: false
When true, Stencil adds vendor prefixes to your components' CSS, such as -webkit-backdrop-filter alongside backdrop-filter, for a modern set of browsers. To choose the browsers, pass an object with a browserslist targets array instead:
autoprefixCss: { targets: ['last 2 versions', 'safari >= 15'] }
bundles
Stencilはデフォルトでアプリケーションを静的に分析し、すべてのコンポーネントがどのように相互接続されているかを示すコンポーネントグラフを生成します。コンポーネントグラフから、アプリ内での相互の使用状況に応じて、コンポーネントをどのようにグループ化するかを最適に判断できます。そうすることで、ネットワークへのリクエストを減らすためにコンポーネントをバンドルすることができます。バンドルはbundlesを使用して手動で生成することもできます。
bundlesは、コンポーネントが遅延ロードされたバンドルにグループ化される方法を表すオブジェクトの配列です。Stencilがバックグラウンドで自動的に処理するため、この構成はほとんど必要ありません。
bundles: [
{ components: ['ion-button'] },
{ components: ['ion-card', 'ion-card-header'] }
]
cacheDir
default: '.stencil'
The directory where sub-directories will be created for caching when enableCache is set true.
A Stencil config like the following:
import { Config } from '@stencil/core';
export const config: Config = {
...,
enableCache: true,
cacheDir: '.cache',
}
Will result in the following file structure:
stencil-project-root
└── .cache
└── .build <-- Where build related file caching is written
collections
default: []
npm packages whose collection output this project should compile in alongside its own components.
export const config: Config = {
collections: ['@my-org/design-system'],
};
Without this option, a collection is only picked up from a side-effect import (import '@my-org/design-system';) somewhere in your source.
devServer
Dev-Server docsをご覧ください。
docs
Please see the docs config.
enableCache
デフォルト: true
Stencilは再ビルドを高速化するために、ビルド結果をキャッシュします。この機能を無効にするには、enableCacheをfalseに設定します。
enableCache: true
excludeComponents
default: []
An array of component tag names to exclude from production builds. Useful to remove test, demo or experimental components from final output.
Supports glob patterns for matching multiple components:
['demo-*']- Excludes all components starting with "demo-"['*-test', '*-demo']- Excludes components ending with "-test" or "-demo"['my-component']- Excludes a specific component
Components matching these patterns will be completely excluded from all output targets when not using the --dev flag.
excludeUnusedDependencies
default: false
When true, a component from one of your collections is only built if one of your own components uses it. By default, every component in each collection is built.
compat
Please see the Compat docs.
env
default: {}
An object that can hold environment variables for your components to import and use. These variables can hold data objects depending on the environment you compile the components for. For example, let's say we want to provide an URL to our API based on a specific environment, we could provide it as such:
import { Config } from '@stencil/core';
export const config: Config = {
...,
env: {
API_BASE_URL: process.env.API_BASE_URL
}
}
Now when you build your components with this environment variable set, you can import it in your component as follows:
import { Component, Env, Host } from '@stencil/core';
@Component({
tag: 'api-component',
})
export class APIComponent {
async connectedCallback () {
const res = await fetch(Env.API_BASE_URL)
// ...
}
}
generateExportMaps
default: false
Set to true to have production builds write an exports map into your package.json, with an entry for each thing your library publishes:
| Entry | Points at | Generated when |
|---|---|---|
. | The loader-bundle entry, or standalone's if there's no loader-bundle | either is configured |
./loader | The lazy loader | loader-bundle |
./standalone | The standalone output's index | standalone |
./<tag> | One entry per component, for example ./my-button | standalone |
./components | The type definitions for every component | types |
./ssr | The server-side rendering module | ssr |
./ssr-prop | ssrProp(), for framework templates | ssr |
./assets/* | Component assets and global stylesheets | assets or global-style |
generateExportMaps: true
Once a package has an exports map, consumers can only import the paths it lists, so my-library/dist/... imports stop resolving. Add any extra entries you need to package.json yourself. Stencil keeps entries it didn't generate, and removes the entry of a component that no longer exists.
globalScript
The global script config option takes a file path as a string. If it isn't set, Stencil uses src/global.ts or src/global.js when one exists.
実行されるコードは、global scriptによってエクスポートされるデフォルトの関数内に配置する必要があります。global scriptのすべてのコードが、エクスポートされる関数でラップされていることを確認してください。
global scriptのオプションは、ファイルパスを文字列として受け取ります。
export default function() { // or export default async function()
initServerConnection();
}
The exported function can also be async but be aware that this can have implications on the performance of your application as all rendering operations will be deferred until after the global script finishes.
Imported methods from Stencil, such as setMode, BUILD and others, can only be accessed within the function exported by the script. They will not be available in the global scope outside of this function.
import { setMode, BUILD } from '@stencil/core';
// ❌ This won't work as Stencil primitives are not initialized at this point
// console.log('Build mode:', BUILD.isDev);
export default function() {
// ✅ This works - Stencil methods are available within the exported function
if (BUILD.isDev) {
console.log('Development mode detected');
setMode((elm) => elm.getAttribute('mode') || 'dev');
} else {
console.log('Production mode');
setMode((elm) => elm.getAttribute('mode') || 'prod');
}
}
globalStyle
デフォルト: undefined
A top-level path to a global stylesheet, predating the global-style output target. Setting globalStyle auto-generates a global-style output target for you, so it still works, but new projects should configure global-style directly instead - it supports more than one global stylesheet, a custom output filename, and control over shadow-root injection, none of which globalStyle alone gives you.
globalStyle: 'src/global/app.css'
hydratedFlag
When using the loader-bundle output target, Stencil
has support for automatically applying a class or attribute to a component and
all of its child components when they have finished hydrating. This can be used
to prevent a flash of unstyled content
(FOUC), a
typically-undesired 'flicker' of unstyled HTML that might otherwise occur
during component rendering while various components are asynchronously
downloaded and rendered.
By default, Stencil will add the hydrated CSS class to elements to indicate
hydration. The hydratedFlag config field allows this behavior to be
customized, by changing the name of the applied CSS class, setting it to use an
attribute to indicate hydration, or changing which type of CSS properties and
values are assigned before and after hydrating. This config can also be used to
turn off this behavior by setting it to null.
If a Stencil configuration does not supply a value for hydratedFlag then
Stencil will automatically generate the following default configuration:
const defaultHydratedFlag: HydratedFlag = {
hydratedValue: 'inherit',
initialValue: 'hidden',
name: 'hydrated',
property: 'visibility',
selector: 'class',
};