メインコンテンツまでスキップ
バージョン: v4.41

Debugging With VS Code

VS Code offers a streamlined debugging experience that can be started with a single click when using launch configurations.

If you're unfamiliar with using the VS Code debugger in general, please visit the VS Code debugger documentation for a primer.

Requirements for Debugging​

In order for a debugger to function, the Stencil project must be configured to generate source maps for the compiled web components back to the source code. As of Stencil v3, source maps are generated by default, but be sure to double-check the project's Stencil config does not disable this behavior. More information regarding source maps in Stencil can be found in the project configuration documentation.

Debugging Stencil Components In a Web App​

It's a common use case to want to step through a web component's code as it executes in the browser and VS Code makes that process simple. Combining the debugger with Stencil's dev server in watch mode will allow you to debug changes as they're made.

Configuring the VS Code Debugger​

To debug Stencil components as they run in a browser (web app), create (or edit) the .vscode/launch.json file with the following configuration:

.vscode/launch.json
{
...,
"configurations": [
...,
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3333",
"sourceMaps": true,
"sourceMapPathOverrides": {
"*": "${webRoot}/*"
}
}
]
}
注記

If your Stencil project is within a monorepo structure, you may want (or need) to add the webRoot option to the above config to point to the Stencil project's directory. For instance, if you have your Stencil project at /packages/stencil-library, you would add the following to the config:

{
...,
"webRoot": "${workspaceFolder}/packages/stencil-library",
}

This will create a configuration to open a Chrome debugger instance on port 3333 (the default port used by the Stencil dev server). To use this configuration, start a Stencil dev server (running npm start in a Stencil component starter project) and then run the configuration from the VS Code debugger tab. At this point, any breakpoints set in the component source code will pause browser execution when hit.

注記

If your Stencil project is configured to use a different port for the dev server, you will need to update the url property in the debugger configuration with the correct port.

Debugging Static Site Generation (SSG)​

静的サイト生成は、事前レンダリングとも呼ばれ、ビルド時にコンポーネントを実行して、レンダリングされたスタイルとマークアップのスナップショットを生成し、最初のリクエストで検索エンジンとユーザーに効率的に提供します。

このステップはブラウザーではなくNode.jsプロセスで実行されるため、ブラウザーで直接デバッグを行うことはできません。 ただし、既存のNode.jsデバッグ手法を使用すると、デバッグは簡単です。

Overview​

stencil build --prerenderコマンドは、最初にNodeJS環境のハイドレートスクリプトをビルドし、次にビルドを使用してサイトを事前レンダリングします。 本番ビルドの場合、これはおそらく理想的です。

ただし、デバッグ中は、ハイドレートスクリプトを再構築し続ける必要はなく、事前レンダリングプロセスを通じてデバッグするだけで済みます。 Stencilは、コンポーネントを実際に実行するために使用されるファイルを dist/hydrateに作成します。

事前レンダリングのみを行う(そして再構築を回避する)には、スクリプトへのパスをフラグとして使用して、 stencil prerender dist/hydrate/index.jsコマンドを使用できます。

Tips for Debugging Prerendering​

デフォルトでは、事前レンダリングはホームページをレンダリングすることから始まり、ホームページ内のリンクを見つけ、さらにリンクが見つかるとサイト全体をクロールし続けます。 デバッグ中は、サイト内のすべてのURLを クロールするのではなく、1ページだけを事前レンダリングする方が簡単な場合があります。 クロールを無効にするには、事前レンダリング構成 crawlUrls:falseを設定します。

次に、 entryUrls構成を使用して、ホームページから開始するのではなく、事前レンダリングするためのパスの配列を提供できます。

Additionally, console logs that are printed within the runtime are suppressed while prerendering (otherwise the terminal would be overloaded with logs). By setting runtimeLogging: true, the runtime console logs will be printed in the terminal. Below is an example setup for prerender debugging:

prerender.config.ts
import { PrerenderConfig } from '@stencil/core';

export const config: PrerenderConfig = {
crawlUrls: false,
entryUrls: ['/example'],
hydrateOptions: (_url) => {
return {
runtimeLogging: true,
};
},
};

Configuring the VS Code Debugger​

To debug the Stencil prerender process, create (or edit) the launch.json file with the following configuration:

launch.json
{
...,
"configurations": [
...,
{
"type": "node",
"request": "launch",
"name": "Prerender",
"args": [
"${workspaceFolder}/node_modules/@stencil/core/bin/stencil",
"prerender",
"${workspaceFolder}/dist/hydrate/index.js",
"--max-workers=0",
"--config=${workspaceFolder}/stencil.config.ts"
],
"protocol": "inspector"
}
]
}

これにより、アプリをハイドレイトするスクリプトを使用して新しいデバッグ構成が作成されます。 stencil prerenderコマンドを起動し、そこへのパスを提供しますハイドレートスクリプトが見つかります。 次に、 --max-workers = 0を使用しているため、デバッグが困難になるような多数のプロセスを各CPUにフォークしません。 the hydrate script can be found. Next we're using --max-workers=0 so we do not fork numerous processes to each of your CPUs which will make it difficult to debug.