@stencil/dev-server 5.0.0-alpha.3 → 5.0.0-alpha.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -0
- package/dist/client/index.d.ts +0 -1
- package/dist/client/index.js +10 -8
- package/dist/index.d.mts +0 -1
- package/dist/index.mjs +173 -36
- package/package.json +11 -11
- /package/{LICENSE.md → LICENSE} +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# @stencil/dev-server
|
|
2
|
+
|
|
3
|
+
Development server for Stencil with DOM-based Hot Module Replacement (HMR).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save-dev @stencil/dev-server @stencil/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
The dev server is configured via `stencil.config.ts` and started automatically with `stencil build --dev --watch --serve`.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// stencil.config.ts
|
|
17
|
+
import { Config } from '@stencil/core';
|
|
18
|
+
|
|
19
|
+
export const config: Config = {
|
|
20
|
+
devServer: {
|
|
21
|
+
port: 3333,
|
|
22
|
+
openBrowser: true,
|
|
23
|
+
reloadStrategy: 'pageReload', // or 'hmr'
|
|
24
|
+
},
|
|
25
|
+
outputTargets: [{ type: 'www' }],
|
|
26
|
+
};
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npx stencil build --dev --watch --serve
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Programmatic API
|
|
34
|
+
|
|
35
|
+
### `start(config, logger, watcher?)` — start the dev server
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { start } from '@stencil/dev-server';
|
|
39
|
+
import { createNodeLogger } from '@stencil/core/sys/node';
|
|
40
|
+
|
|
41
|
+
const server = await start(
|
|
42
|
+
{
|
|
43
|
+
root: './www', // directory to serve
|
|
44
|
+
port: 3333,
|
|
45
|
+
openBrowser: true,
|
|
46
|
+
reloadStrategy: 'hmr', // 'hmr' | 'pageReload' | null
|
|
47
|
+
},
|
|
48
|
+
createNodeLogger(),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
console.log(`Listening at ${server.browserUrl}`);
|
|
52
|
+
|
|
53
|
+
// Shut down when done
|
|
54
|
+
await server.close();
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### With a Stencil compiler watcher (full HMR)
|
|
58
|
+
|
|
59
|
+
Pass a `CompilerWatcher` to connect build output to the dev server — file changes trigger HMR or a page reload automatically.
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import * as coreCompiler from '@stencil/core/compiler';
|
|
63
|
+
import { start } from '@stencil/dev-server';
|
|
64
|
+
import { createNodeLogger, createNodeSys } from '@stencil/core/sys/node';
|
|
65
|
+
|
|
66
|
+
const logger = createNodeLogger();
|
|
67
|
+
const sys = createNodeSys();
|
|
68
|
+
|
|
69
|
+
const { config } = await coreCompiler.loadConfig({
|
|
70
|
+
configPath: './stencil.config.ts',
|
|
71
|
+
logger,
|
|
72
|
+
sys,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const compiler = await coreCompiler.createCompiler(config);
|
|
76
|
+
const watcher = await compiler.createWatcher();
|
|
77
|
+
|
|
78
|
+
const server = await start(
|
|
79
|
+
{ root: './www', port: 3333, reloadStrategy: 'hmr' },
|
|
80
|
+
logger,
|
|
81
|
+
watcher,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
// Start watching — builds trigger HMR updates in the browser
|
|
85
|
+
await watcher.start();
|
|
86
|
+
|
|
87
|
+
// To shut down:
|
|
88
|
+
// await watcher.close();
|
|
89
|
+
// await server.close();
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The returned `DevServer` exposes `address`, `browserUrl`, `protocol`, `port`, `root`, and `close()`.
|
|
93
|
+
|
|
94
|
+
#### Key config options
|
|
95
|
+
|
|
96
|
+
| Option | Default | Description |
|
|
97
|
+
|---|---|---|
|
|
98
|
+
| `root` | `'.'` | Directory to serve files from |
|
|
99
|
+
| `port` | `3333` | Port to listen on |
|
|
100
|
+
| `address` | `'0.0.0.0'` | Network interface to bind |
|
|
101
|
+
| `reloadStrategy` | `'hmr'` | `'hmr'`, `'pageReload'`, or `null` to disable |
|
|
102
|
+
| `openBrowser` | `false` | Open browser on start |
|
|
103
|
+
| `https` | — | `{ cert, key }` to enable HTTPS |
|
|
104
|
+
| `worker` | `true` | Fork server into a child process (disable for debugging) |
|
|
105
|
+
| `ssr` | `false` | Server-side render each page on request (dev only - you must have a `ssr` output target setup) |
|
package/dist/client/index.d.ts
CHANGED
package/dist/client/index.js
CHANGED
|
@@ -16,7 +16,7 @@ const NORMAL_CLOSURE_CODE = 1e3;
|
|
|
16
16
|
const REQUEST_BUILD_RESULTS_INTERVAL_MS = 500;
|
|
17
17
|
//#endregion
|
|
18
18
|
//#region src/client/error.css?inline
|
|
19
|
-
var error_default = "#dev-server-modal * {\n box-sizing: border-box !important;\n}\n\n#dev-server-modal {\n -webkit-font-smoothing: antialiased;\n text-rendering: optimizelegibility;\n text-size-adjust: none;\n word-wrap: break-word;\n user-select: auto;\n display: block;\n z-index: 99999 !important;\n -webkit-overflow-scrolling: touch !important;\n direction: ltr !important;\n background: #000000a8 !important;\n width: 100% !important;\n height: 100% !important;\n margin: 0 !important;\n padding: 0 !important;\n font-family: -apple-system, Roboto, BlinkMacSystemFont, Segoe UI, Helvetica Neue, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol !important;\n font-size: 14px !important;\n line-height: 1.5 !important;\n position: fixed !important;\n top: 0 !important;\n left: 0 !important;\n overflow-y: auto !important;\n}\n\n#dev-server-modal-inner {\n color: #333 !important;\n background-color: #fff !important;\n border-radius: 8px !important;\n max-width: 80vw !important;\n margin: 30px auto !important;\n padding: 25px !important;\n position: relative !important;\n box-shadow: 0 10px 40px #00000080 !important;\n}\n\n.dev-server-diagnostic {\n border: 1px solid #ddd !important;\n border-radius: 3px !important;\n margin: 20px !important;\n}\n\n.dev-server-diagnostic-masthead {\n padding: 8px 12px 12px !important;\n}\n\n.dev-server-diagnostic-title {\n color: #222 !important;\n margin: 0 !important;\n font-weight: bold !important;\n}\n\n.dev-server-diagnostic-message {\n color: #555 !important;\n margin-top: 4px !important;\n}\n\n.dev-server-diagnostic-file {\n border-top: 1px solid #ddd !important;\n position: relative !important;\n}\n\n.dev-server-diagnostic-file-header {\n color: #555 !important;\n background-color: #f9f9f9 !important;\n border-bottom: 1px solid #ddd !important;\n border-top-left-radius: 2px !important;\n border-top-right-radius: 2px !important;\n padding: 5px 10px !important;\n font-family: Consolas, Liberation Mono, Menlo, Courier, monospace !important;\n font-size: 12px !important;\n display: block !important;\n}\n\na.dev-server-diagnostic-file-header {\n color: #00e !important;\n text-decoration: underline !important;\n}\n\na.dev-server-diagnostic-file-header:hover {\n background-color: #f4f4f4 !important;\n text-decoration: none !important;\n}\n\n.dev-server-diagnostic-file-name {\n font-weight: bold !important;\n}\n\n.dev-server-diagnostic-blob {\n border-bottom-right-radius: 3px !important;\n border-bottom-left-radius: 3px !important;\n overflow: auto hidden !important;\n}\n\n.dev-server-diagnostic-table {\n -moz-tab-size: 2;\n tab-size: 2;\n border-spacing: 0 !important;\n border-collapse: collapse !important;\n border-style: none !important;\n border-width: 0 !important;\n margin: 0 !important;\n padding: 0 !important;\n}\n\n.dev-server-diagnostic-table td, .dev-server-diagnostic-table th {\n border-style: none !important;\n border-width: 0 !important;\n padding: 0 !important;\n}\n\ntd.dev-server-diagnostic-blob-num {\n -webkit-user-select: none;\n -moz-user-select: none;\n -ms-user-select: none;\n user-select: none;\n color: #0000004d !important;\n text-align: right !important;\n white-space: nowrap !important;\n vertical-align: top !important;\n border: 0 solid #eee !important;\n border-right-width: 1px !important;\n width: 1% !important;\n min-width: 50px !important;\n padding-left: 10px !important;\n padding-right: 10px !important;\n font-family: Consolas, Liberation Mono, Menlo, Courier, monospace !important;\n font-size: 12px !important;\n line-height: 20px !important;\n}\n\ntd.dev-server-diagnostic-blob-num:before {\n content: attr(data-line-number) !important;\n}\n\n.dev-server-diagnostic-error-line td.dev-server-diagnostic-blob-num {\n background-color: #fdd !important;\n border-color: #ffc9c9 !important;\n}\n\n.dev-server-diagnostic-error-line td.dev-server-diagnostic-blob-code {\n
|
|
19
|
+
var error_default = "#dev-server-modal * {\n box-sizing: border-box !important;\n}\n\n#dev-server-modal {\n -webkit-font-smoothing: antialiased;\n text-rendering: optimizelegibility;\n text-size-adjust: none;\n word-wrap: break-word;\n user-select: auto;\n display: block;\n z-index: 99999 !important;\n -webkit-overflow-scrolling: touch !important;\n direction: ltr !important;\n background: #000000a8 !important;\n width: 100% !important;\n height: 100% !important;\n margin: 0 !important;\n padding: 0 !important;\n font-family: -apple-system, Roboto, BlinkMacSystemFont, Segoe UI, Helvetica Neue, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol !important;\n font-size: 14px !important;\n line-height: 1.5 !important;\n position: fixed !important;\n top: 0 !important;\n left: 0 !important;\n overflow-y: auto !important;\n}\n\n#dev-server-modal-inner {\n color: #333 !important;\n background-color: #fff !important;\n border-radius: 8px !important;\n max-width: 80vw !important;\n margin: 30px auto !important;\n padding: 25px !important;\n position: relative !important;\n box-shadow: 0 10px 40px #00000080 !important;\n}\n\n.dev-server-diagnostic {\n border: 1px solid #ddd !important;\n border-radius: 3px !important;\n margin: 20px !important;\n}\n\n.dev-server-diagnostic-masthead {\n padding: 8px 12px 12px !important;\n}\n\n.dev-server-diagnostic-title {\n color: #222 !important;\n margin: 0 !important;\n font-weight: bold !important;\n}\n\n.dev-server-diagnostic-message {\n color: #555 !important;\n margin-top: 4px !important;\n}\n\n.dev-server-diagnostic-file {\n border-top: 1px solid #ddd !important;\n position: relative !important;\n}\n\n.dev-server-diagnostic-file-header {\n color: #555 !important;\n background-color: #f9f9f9 !important;\n border-bottom: 1px solid #ddd !important;\n border-top-left-radius: 2px !important;\n border-top-right-radius: 2px !important;\n padding: 5px 10px !important;\n font-family: Consolas, Liberation Mono, Menlo, Courier, monospace !important;\n font-size: 12px !important;\n display: block !important;\n}\n\na.dev-server-diagnostic-file-header {\n color: #00e !important;\n text-decoration: underline !important;\n}\n\na.dev-server-diagnostic-file-header:hover {\n background-color: #f4f4f4 !important;\n text-decoration: none !important;\n}\n\n.dev-server-diagnostic-file-name {\n font-weight: bold !important;\n}\n\n.dev-server-diagnostic-blob {\n border-bottom-right-radius: 3px !important;\n border-bottom-left-radius: 3px !important;\n overflow: auto hidden !important;\n}\n\n.dev-server-diagnostic-table {\n -moz-tab-size: 2;\n tab-size: 2;\n border-spacing: 0 !important;\n border-collapse: collapse !important;\n border-style: none !important;\n border-width: 0 !important;\n margin: 0 !important;\n padding: 0 !important;\n}\n\n.dev-server-diagnostic-table td, .dev-server-diagnostic-table th {\n border-style: none !important;\n border-width: 0 !important;\n padding: 0 !important;\n}\n\ntd.dev-server-diagnostic-blob-num {\n -webkit-user-select: none;\n -moz-user-select: none;\n -ms-user-select: none;\n user-select: none;\n color: #0000004d !important;\n text-align: right !important;\n white-space: nowrap !important;\n vertical-align: top !important;\n border: 0 solid #eee !important;\n border-right-width: 1px !important;\n width: 1% !important;\n min-width: 50px !important;\n padding-left: 10px !important;\n padding-right: 10px !important;\n font-family: Consolas, Liberation Mono, Menlo, Courier, monospace !important;\n font-size: 12px !important;\n line-height: 20px !important;\n}\n\ntd.dev-server-diagnostic-blob-num:before {\n content: attr(data-line-number) !important;\n}\n\n.dev-server-diagnostic-error-line td.dev-server-diagnostic-blob-num {\n background-color: #fdd !important;\n border-color: #ffc9c9 !important;\n}\n\n.dev-server-diagnostic-error-line td.dev-server-diagnostic-blob-code {\n background: #ffdddd40 !important;\n}\n\n.dev-server-diagnostic-open-in-editor td.dev-server-diagnostic-blob-num:hover {\n cursor: pointer;\n font-weight: bold;\n background-color: #ffffe3 !important;\n}\n\n.dev-server-diagnostic-open-in-editor.dev-server-diagnostic-error-line td.dev-server-diagnostic-blob-num:hover {\n background-color: #ffdada !important;\n}\n\ntd.dev-server-diagnostic-blob-code {\n vertical-align: top !important;\n color: #333 !important;\n word-wrap: normal !important;\n white-space: pre !important;\n padding-left: 10px !important;\n padding-right: 10px !important;\n font-family: Consolas, Liberation Mono, Menlo, Courier, monospace !important;\n font-size: 12px !important;\n line-height: 20px !important;\n position: relative !important;\n overflow: visible !important;\n}\n\ntd.dev-server-diagnostic-blob-code:before {\n content: \"\" !important;\n}\n\n.dev-server-diagnostic-error-chr {\n position: relative !important;\n}\n\n.dev-server-diagnostic-error-chr:before {\n z-index: -1;\n content: \"\" !important;\n background-color: #fdd !important;\n width: 8px !important;\n height: 20px !important;\n position: absolute !important;\n top: -3px !important;\n left: 0 !important;\n}\n\n.hljs-comment, .hljs-meta {\n color: #969896;\n}\n\n.hljs-string, .hljs-variable, .hljs-template-variable, .hljs-strong, .hljs-emphasis, .hljs-quote {\n color: #df5000;\n}\n\n.hljs-keyword, .hljs-selector-tag, .hljs-type {\n color: #a71d5d;\n}\n\n.hljs-literal, .hljs-symbol, .hljs-bullet, .hljs-attribute {\n color: #0086b3;\n}\n\n.hljs-section, .hljs-name {\n color: #63a35c;\n}\n\n.hljs-tag {\n color: #333;\n}\n\n.hljs-title, .hljs-attr, .hljs-selector-id, .hljs-selector-class, .hljs-selector-attr, .hljs-selector-pseudo {\n color: #795da3;\n}\n\n.hljs-addition {\n color: #55a532;\n background-color: #eaffea;\n}\n\n.hljs-deletion {\n color: #bd2c00;\n background-color: #ffecec;\n}\n\n.hljs-link {\n text-decoration: underline;\n}\n\n.dev-server-error-badge {\n z-index: 99998 !important;\n color: #fff !important;\n cursor: pointer !important;\n background: #f55 !important;\n border: none !important;\n border-radius: 8px !important;\n align-items: center !important;\n gap: 8px !important;\n padding: 10px 16px !important;\n font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, sans-serif !important;\n font-size: 14px !important;\n font-weight: 600 !important;\n transition: transform .2s, box-shadow .2s !important;\n display: flex !important;\n position: fixed !important;\n bottom: 20px !important;\n left: 20px !important;\n box-shadow: 0 4px 12px #0000004d !important;\n}\n\n.dev-server-error-badge:hover {\n transform: translateY(-2px) !important;\n box-shadow: 0 6px 16px #0006 !important;\n}\n\n.error-badge-icon {\n color: #f55 !important;\n background: #fff !important;\n border-radius: 50% !important;\n justify-content: center !important;\n align-items: center !important;\n width: 20px !important;\n height: 20px !important;\n font-size: 14px !important;\n font-weight: bold !important;\n display: flex !important;\n}\n\n.error-badge-count {\n font-size: 14px !important;\n font-weight: 600 !important;\n}\n";
|
|
20
20
|
//#endregion
|
|
21
21
|
//#region src/client/error.ts
|
|
22
22
|
let errorCount = 0;
|
|
@@ -396,9 +396,7 @@ const hmrStyleSheetImages = (cssImageProps, styleSheet, versionId, imageFileName
|
|
|
396
396
|
case CSSRule.STYLE_RULE:
|
|
397
397
|
hmrStyleSheetRuleImages(cssImageProps, cssRule, versionId, imageFileNames);
|
|
398
398
|
break;
|
|
399
|
-
case CSSRule.MEDIA_RULE:
|
|
400
|
-
hmrStyleSheetImages(cssImageProps, cssRule, versionId, imageFileNames);
|
|
401
|
-
break;
|
|
399
|
+
case CSSRule.MEDIA_RULE: hmrStyleSheetImages(cssImageProps, cssRule, versionId, imageFileNames);
|
|
402
400
|
}
|
|
403
401
|
}
|
|
404
402
|
} catch (e) {
|
|
@@ -507,6 +505,7 @@ const hmrInlineStylesTraverse = (elm, versionId, trackers) => {
|
|
|
507
505
|
if (hasShadowRoot(elm)) hmrInlineStylesTraverse(elm.shadowRoot, versionId, trackers);
|
|
508
506
|
if (elm.children) for (let i = 0; i < elm.children.length; i++) hmrInlineStylesTraverse(elm.children[i], versionId, trackers);
|
|
509
507
|
};
|
|
508
|
+
const SLOT_FB_CSS = "slot-fb{display:contents}slot-fb[hidden]{display:none}";
|
|
510
509
|
/**
|
|
511
510
|
* Update or remove a style element based on the HMR update.
|
|
512
511
|
* @param elm - the style element to update
|
|
@@ -517,7 +516,8 @@ const hmrInlineStylesTraverse = (elm, versionId, trackers) => {
|
|
|
517
516
|
const hmrStyleElement = (elm, versionId, stylesUpdated) => {
|
|
518
517
|
if (elm.getAttribute(STYLE_ID_ATTR) === stylesUpdated.styleId) {
|
|
519
518
|
if (stylesUpdated.styleText) {
|
|
520
|
-
|
|
519
|
+
const slotFbSuffix = elm.hasAttribute("data-slot-fb") ? SLOT_FB_CSS : "";
|
|
520
|
+
elm.innerHTML = stylesUpdated.styleText.replace(/\\n/g, "\n") + slotFbSuffix;
|
|
521
521
|
elm.setAttribute("data-hmr", versionId);
|
|
522
522
|
} else elm.remove();
|
|
523
523
|
return true;
|
|
@@ -926,15 +926,17 @@ const appUpdate = (win, config, buildResults) => {
|
|
|
926
926
|
win["s-build-id"] = buildResults.buildId;
|
|
927
927
|
clearAppErrorModal({ window: win });
|
|
928
928
|
if (buildResults.hasError) {
|
|
929
|
+
const hasEditors = Array.isArray(config.editors) && config.editors.length > 0;
|
|
929
930
|
const errorResults = appError({
|
|
930
931
|
window: win,
|
|
931
932
|
buildResults,
|
|
932
|
-
openInEditor:
|
|
933
|
-
const
|
|
933
|
+
openInEditor: hasEditors ? (data) => {
|
|
934
|
+
const params = new URLSearchParams({
|
|
934
935
|
file: data.file,
|
|
935
936
|
line: String(data.line),
|
|
936
937
|
column: String(data.column)
|
|
937
|
-
})
|
|
938
|
+
});
|
|
939
|
+
const url = `${OPEN_IN_EDITOR_URL}?${params.toString()}`;
|
|
938
940
|
win.fetch(url).catch((err) => {
|
|
939
941
|
console.error("Failed to open in editor:", err);
|
|
940
942
|
});
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { CompilerBuildResults, CompilerRequestResponse, CompilerWatcher, DevServer, DevServerConfig, DevServerConfig as DevServerConfig$1, Logger, StencilDevServerConfig } from "@stencil/core/compiler";
|
|
2
|
-
|
|
3
2
|
//#region src/server/types.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Internal dev server message protocol for communication between
|
package/dist/index.mjs
CHANGED
|
@@ -510,6 +510,110 @@ function getEditors() {
|
|
|
510
510
|
]);
|
|
511
511
|
}
|
|
512
512
|
//#endregion
|
|
513
|
+
//#region src/server/dev-preview.ts
|
|
514
|
+
/**
|
|
515
|
+
* Generate a virtual HTML page for the dev server when no index.html is present.
|
|
516
|
+
*
|
|
517
|
+
* Renders all discovered components. For each component, usage markdown files
|
|
518
|
+
* (src/components/my-cmp/usage/*.md) are scanned for ```html``` code blocks, which
|
|
519
|
+
* are used as preview snippets. Falls back to a bare tag if none are found.
|
|
520
|
+
* @param buildResults The compiler build results containing component metadata and output file information.
|
|
521
|
+
* @param filterDirPath Optional directory path to filter components by (e.g. when visiting /src/my-component/).
|
|
522
|
+
* @returns A string of HTML to be served as the dev preview page.
|
|
523
|
+
*/
|
|
524
|
+
const generateDevPreview = (buildResults, filterDirPath) => {
|
|
525
|
+
const { namespace } = buildResults;
|
|
526
|
+
let { components } = buildResults;
|
|
527
|
+
if (filterDirPath) {
|
|
528
|
+
const normalized = path.normalize(filterDirPath);
|
|
529
|
+
components = components.filter((c) => path.normalize(path.dirname(c.sourceFilePath)) === normalized);
|
|
530
|
+
}
|
|
531
|
+
const title = filterDirPath ? `${namespace} / ${path.basename(filterDirPath)}` : namespace;
|
|
532
|
+
const loaderUrl = getLoaderUrl(buildResults);
|
|
533
|
+
const globalCssUrls = getGlobalCssUrls(buildResults);
|
|
534
|
+
const sections = components.length > 0 ? components.map((c) => renderComponentSection(c)) : [` <p style="color:color-mix(in oklab,CanvasText 50%,Canvas 50%)">No Stencil components found in this directory.</p>`];
|
|
535
|
+
return `<!DOCTYPE html>
|
|
536
|
+
<html lang="en">
|
|
537
|
+
<head>
|
|
538
|
+
<meta charset="UTF-8">
|
|
539
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
540
|
+
<title>${title} — Stencil Dev</title>
|
|
541
|
+
<style>
|
|
542
|
+
*, *::before, *::after { box-sizing: border-box; }
|
|
543
|
+
body { font-family: system-ui, sans-serif; margin: 0; padding: 2rem; }
|
|
544
|
+
h1 { font-size: 1rem; font-weight: 500; margin: 0 0 2rem; color: color-mix(in oklab, CanvasText 90%, Canvas 10%); }
|
|
545
|
+
.component { border: 1px solid color-mix(in oklab, CanvasText 20%, Canvas 80%); border-radius: 6px; margin-bottom: 1.5rem; overflow: hidden; }
|
|
546
|
+
.component-header { padding: 0.5rem 1rem; background: color-mix(in oklab, CanvasText 5%, Canvas 95%); font-family: monospace; font-size: 0.875rem; }
|
|
547
|
+
.component-preview { padding: 1.5rem; }
|
|
548
|
+
.component-preview + .component-preview { border-top: 1px dashed color-mix(in oklab, CanvasText 20%, Canvas 80%); }
|
|
549
|
+
</style>
|
|
550
|
+
${[loaderUrl ? ` <script type="module" src="${loaderUrl}"><\/script>` : "", ...globalCssUrls.map((url) => ` <link rel="stylesheet" href="${url}">`)].filter(Boolean).join("\n")}
|
|
551
|
+
</head>
|
|
552
|
+
<body>
|
|
553
|
+
<h1>${title}</h1>
|
|
554
|
+
${sections.join("\n")}
|
|
555
|
+
</body>
|
|
556
|
+
</html>`;
|
|
557
|
+
};
|
|
558
|
+
const renderComponentSection = (component) => {
|
|
559
|
+
const snippets = getUsageSnippets(component);
|
|
560
|
+
const previews = snippets.length > 0 ? snippets.map((html) => ` <div class="component-preview">\n ${html}\n </div>`).join("\n") : ` <div class="component-preview"><${component.tagName}></${component.tagName}></div>`;
|
|
561
|
+
return ` <div class="component">
|
|
562
|
+
<div class="component-header"><${component.tagName}></div>
|
|
563
|
+
${previews}
|
|
564
|
+
</div>`;
|
|
565
|
+
};
|
|
566
|
+
const getUsageSnippets = (component) => {
|
|
567
|
+
const usageDir = path.join(path.dirname(component.sourceFilePath), "usage");
|
|
568
|
+
try {
|
|
569
|
+
const files = fs.readdirSync(usageDir).filter((f) => f.toLowerCase().endsWith(".md"));
|
|
570
|
+
const snippets = [];
|
|
571
|
+
for (const file of files.sort()) {
|
|
572
|
+
const content = fs.readFileSync(path.join(usageDir, file), "utf-8");
|
|
573
|
+
for (const match of content.matchAll(/```html\n([\s\S]*?)```/g)) snippets.push(match[1].trim());
|
|
574
|
+
}
|
|
575
|
+
return snippets;
|
|
576
|
+
} catch {
|
|
577
|
+
return [];
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
/**
|
|
581
|
+
* Convert an absolute output file path to a server-relative URL.
|
|
582
|
+
* @param absPath The absolute file path.
|
|
583
|
+
* @param rootDir The root directory to which the path should be relative.
|
|
584
|
+
* @returns A server-relative URL.
|
|
585
|
+
*/
|
|
586
|
+
const toServePath = (absPath, rootDir) => "/" + path.relative(rootDir, absPath).split(path.sep).join("/");
|
|
587
|
+
/**
|
|
588
|
+
* Derive the loader script URL from build outputs.
|
|
589
|
+
* Prefers loader-bundle (always built in dev); falls back to standalone auto-loader if present.
|
|
590
|
+
* @param buildResults The compiler build results containing output file information.
|
|
591
|
+
* @returns A server-relative URL to a loader script, or null if not found.
|
|
592
|
+
*/
|
|
593
|
+
const getLoaderUrl = ({ outputs, fsNamespace, rootDir }) => {
|
|
594
|
+
const distLazy = outputs.find((o) => o.type === "dist-lazy");
|
|
595
|
+
if (distLazy) {
|
|
596
|
+
const file = distLazy.files.find((f) => f.endsWith(`/${fsNamespace}.js`));
|
|
597
|
+
if (file) return toServePath(file, rootDir);
|
|
598
|
+
}
|
|
599
|
+
const standalone = outputs.find((o) => o.type === "standalone");
|
|
600
|
+
if (standalone) {
|
|
601
|
+
const file = standalone.files.find((f) => /\/loader\.js$/.test(f));
|
|
602
|
+
if (file) return toServePath(file, rootDir);
|
|
603
|
+
}
|
|
604
|
+
return null;
|
|
605
|
+
};
|
|
606
|
+
/**
|
|
607
|
+
* Collect server-relative URLs for all global-style CSS outputs.
|
|
608
|
+
* @param buildResults The compiler build results containing output file information.
|
|
609
|
+
* @returns An array of server-relative URLs to global-style CSS files.
|
|
610
|
+
*/
|
|
611
|
+
const getGlobalCssUrls = ({ outputs, rootDir }) => {
|
|
612
|
+
const globalStyle = outputs.find((o) => o.type === "global-style");
|
|
613
|
+
if (!globalStyle) return [];
|
|
614
|
+
return globalStyle.files.filter((f) => f.endsWith(".css") && !f.endsWith(".css.map")).map((f) => toServePath(f, rootDir));
|
|
615
|
+
};
|
|
616
|
+
//#endregion
|
|
513
617
|
//#region src/server/ssr.ts
|
|
514
618
|
/**
|
|
515
619
|
* SSR (Server-Side Rendering) request handling.
|
|
@@ -519,10 +623,10 @@ async function ssrPageRequest(devServerConfig, serverCtx, req, res) {
|
|
|
519
623
|
try {
|
|
520
624
|
let status = 500;
|
|
521
625
|
let content = "";
|
|
522
|
-
const {
|
|
626
|
+
const { ssrApp, srcIndexHtml, diagnostics } = await setupHydrateApp(devServerConfig, serverCtx);
|
|
523
627
|
if (!diagnostics.some((diagnostic) => diagnostic.level === "error")) try {
|
|
524
628
|
const opts = getSsrHydrateOptions(devServerConfig, serverCtx, req.url);
|
|
525
|
-
const ssrResults = await
|
|
629
|
+
const ssrResults = await ssrApp.renderToString(srcIndexHtml, opts);
|
|
526
630
|
diagnostics.push(...ssrResults.diagnostics);
|
|
527
631
|
status = ssrResults.httpStatus ?? 500;
|
|
528
632
|
content = ssrResults.html ?? "";
|
|
@@ -549,11 +653,11 @@ async function ssrStaticDataRequest(devServerConfig, serverCtx, req, res) {
|
|
|
549
653
|
try {
|
|
550
654
|
const data = {};
|
|
551
655
|
let httpCache = false;
|
|
552
|
-
const {
|
|
656
|
+
const { ssrApp, srcIndexHtml, diagnostics } = await setupHydrateApp(devServerConfig, serverCtx);
|
|
553
657
|
if (!diagnostics.some((diagnostic) => diagnostic.level === "error")) try {
|
|
554
658
|
const { ssrPath, hasQueryString } = getSsrStaticDataPath(req);
|
|
555
659
|
const opts = getSsrHydrateOptions(devServerConfig, serverCtx, new URL(ssrPath, req.url));
|
|
556
|
-
const ssrResults = await
|
|
660
|
+
const ssrResults = await ssrApp.renderToString(srcIndexHtml, opts);
|
|
557
661
|
diagnostics.push(...ssrResults.diagnostics);
|
|
558
662
|
ssrResults.staticData.forEach((s) => {
|
|
559
663
|
if (s.type === "application/json") data[s.id] = JSON.parse(s.content);
|
|
@@ -580,18 +684,18 @@ async function ssrStaticDataRequest(devServerConfig, serverCtx, req, res) {
|
|
|
580
684
|
}
|
|
581
685
|
async function setupHydrateApp(devServerConfig, serverCtx) {
|
|
582
686
|
let srcIndexHtml = null;
|
|
583
|
-
let
|
|
687
|
+
let ssrApp = null;
|
|
584
688
|
const buildResults = await serverCtx.getBuildResults();
|
|
585
689
|
const diagnostics = [];
|
|
586
690
|
if (serverCtx.prerenderConfig == null && isString(devServerConfig.prerenderConfig)) try {
|
|
587
|
-
const prerenderConfigResults = (await import("@stencil/core/compiler")).nodeRequire(devServerConfig.prerenderConfig);
|
|
691
|
+
const prerenderConfigResults = await (await import("@stencil/core/compiler")).nodeRequire(devServerConfig.prerenderConfig);
|
|
588
692
|
diagnostics.push(...prerenderConfigResults.diagnostics);
|
|
589
693
|
if (prerenderConfigResults.module?.config) serverCtx.prerenderConfig = prerenderConfigResults.module.config;
|
|
590
694
|
} catch (e) {
|
|
591
695
|
catchError(diagnostics, e);
|
|
592
696
|
}
|
|
593
|
-
if (!isString(buildResults.
|
|
594
|
-
messageText: "Missing
|
|
697
|
+
if (!isString(buildResults.ssrAppFilePath)) diagnostics.push({
|
|
698
|
+
messageText: "Missing ssrAppFilePath",
|
|
595
699
|
level: "error",
|
|
596
700
|
type: "ssr",
|
|
597
701
|
lines: []
|
|
@@ -611,19 +715,19 @@ async function setupHydrateApp(devServerConfig, serverCtx) {
|
|
|
611
715
|
type: "ssr"
|
|
612
716
|
});
|
|
613
717
|
else {
|
|
614
|
-
const
|
|
718
|
+
const ssrAppFilePath = path.resolve(buildResults.ssrAppFilePath);
|
|
615
719
|
try {
|
|
616
|
-
const hydrateUrl = pathToFileURL(
|
|
720
|
+
const hydrateUrl = pathToFileURL(ssrAppFilePath);
|
|
617
721
|
hydrateUrl.search = `?t=${Date.now()}`;
|
|
618
722
|
const hydrateModule = await import(hydrateUrl.href);
|
|
619
|
-
|
|
723
|
+
ssrApp = hydrateModule.default || hydrateModule;
|
|
620
724
|
} catch (e) {
|
|
621
725
|
catchError(diagnostics, e);
|
|
622
726
|
}
|
|
623
727
|
}
|
|
624
728
|
}
|
|
625
729
|
return {
|
|
626
|
-
|
|
730
|
+
ssrApp,
|
|
627
731
|
srcIndexHtml,
|
|
628
732
|
diagnostics
|
|
629
733
|
};
|
|
@@ -643,14 +747,14 @@ function getSsrHydrateOptions(devServerConfig, serverCtx, url) {
|
|
|
643
747
|
prettyHtml: true
|
|
644
748
|
};
|
|
645
749
|
const prerenderConfig = serverCtx?.prerenderConfig;
|
|
646
|
-
if (isFunction(prerenderConfig?.
|
|
647
|
-
const userOpts = prerenderConfig.
|
|
750
|
+
if (isFunction(prerenderConfig?.prerenderOptions)) {
|
|
751
|
+
const userOpts = prerenderConfig.prerenderOptions(url);
|
|
648
752
|
if (userOpts) Object.assign(opts, userOpts);
|
|
649
753
|
}
|
|
650
754
|
if (isFunction(serverCtx.sys.applyPrerenderGlobalPatch)) {
|
|
651
|
-
const orgBeforeHydrate = opts.
|
|
755
|
+
const orgBeforeHydrate = opts.beforeSsr;
|
|
652
756
|
const applyPatch = serverCtx.sys.applyPrerenderGlobalPatch;
|
|
653
|
-
opts.
|
|
757
|
+
opts.beforeSsr = (document) => {
|
|
654
758
|
const devServerHostUrl = new URL(devServerConfig.browserUrl).origin;
|
|
655
759
|
applyPatch({
|
|
656
760
|
devServerHostUrl,
|
|
@@ -871,7 +975,8 @@ function updateStyleUrls(url, oldCss) {
|
|
|
871
975
|
return newCss;
|
|
872
976
|
}
|
|
873
977
|
function appendDevServerClientScript(devServerConfig, req, content) {
|
|
874
|
-
|
|
978
|
+
const devServerClientUrl = getDevServerClientUrl(devServerConfig, req.headers?.["x-forwarded-host"] ?? req.host, req.headers?.["x-forwarded-proto"]);
|
|
979
|
+
return appendDevServerClientIframe(content, `<iframe title="Stencil Dev Server Connector ${VERSION} ⚡" src="${devServerClientUrl}" style="display:block;width:0;height:0;border:0;visibility:hidden" aria-hidden="true"></iframe>`);
|
|
875
980
|
}
|
|
876
981
|
function appendDevServerClientIframe(content, iframe) {
|
|
877
982
|
if (content.includes("</body>")) return content.replace("</body>", `${iframe}</body>`);
|
|
@@ -884,7 +989,7 @@ async function serveDevClient(devServerConfig, serverCtx, req, res) {
|
|
|
884
989
|
if (isDevServerClient(req.pathname)) return serveDevClientScript(devServerConfig, serverCtx, req, res);
|
|
885
990
|
if (isInitialDevServerLoad(req.pathname)) req.filePath = path.join(devServerConfig.devServerDir, "templates", "initial-load.html");
|
|
886
991
|
else {
|
|
887
|
-
const subPath = req.pathname.replace(
|
|
992
|
+
const subPath = req.pathname.replace("/~dev-server/", "");
|
|
888
993
|
if (subPath.startsWith("client/")) req.filePath = path.join(devServerConfig.devServerDir, subPath);
|
|
889
994
|
else req.filePath = path.join(devServerConfig.devServerDir, "static", subPath);
|
|
890
995
|
}
|
|
@@ -948,6 +1053,15 @@ async function serveDirectoryIndex(devServerConfig, serverCtx, req, res) {
|
|
|
948
1053
|
if (!req.pathname.endsWith("/")) return serverCtx.serve302(req, res, req.pathname + "/");
|
|
949
1054
|
try {
|
|
950
1055
|
const dirFilePaths = await serverCtx.sys.readDir(req.filePath);
|
|
1056
|
+
const hasTsx = dirFilePaths.some((f) => f.endsWith(".tsx"));
|
|
1057
|
+
const hasHtml = dirFilePaths.some((f) => f.endsWith(".html") || f.endsWith(".htm"));
|
|
1058
|
+
if (hasTsx && !hasHtml) return serveDevPreview(devServerConfig, serverCtx, req, res, req.filePath);
|
|
1059
|
+
if (req.pathname === "/" || req.pathname === devServerConfig.basePath) {
|
|
1060
|
+
const srcPath = path.join(req.filePath, "src");
|
|
1061
|
+
try {
|
|
1062
|
+
if ((await serverCtx.sys.stat(srcPath)).isDirectory) return serverCtx.serve302(req, res, "/src/");
|
|
1063
|
+
} catch {}
|
|
1064
|
+
}
|
|
951
1065
|
try {
|
|
952
1066
|
if (serverCtx.dirTemplate == null) {
|
|
953
1067
|
const dirTemplatePath = path.join(devServerConfig.devServerDir, "templates", "directory-index.html");
|
|
@@ -969,6 +1083,21 @@ async function serveDirectoryIndex(devServerConfig, serverCtx, req, res) {
|
|
|
969
1083
|
return serverCtx.serve404(req, res, "serveDirectoryIndex");
|
|
970
1084
|
}
|
|
971
1085
|
}
|
|
1086
|
+
async function serveDevPreview(devServerConfig, serverCtx, req, res, filterDirPath) {
|
|
1087
|
+
try {
|
|
1088
|
+
const buildResults = await serverCtx.getBuildResults();
|
|
1089
|
+
const html = appendDevServerClientScript(devServerConfig, req, generateDevPreview(buildResults, filterDirPath));
|
|
1090
|
+
const buf = Buffer.from(html, "utf-8");
|
|
1091
|
+
serverCtx.logRequest(req, 200);
|
|
1092
|
+
res.writeHead(200, responseHeaders({
|
|
1093
|
+
"content-type": "text/html;charset=utf-8",
|
|
1094
|
+
"content-length": buf.byteLength
|
|
1095
|
+
}));
|
|
1096
|
+
res.end(buf);
|
|
1097
|
+
} catch (e) {
|
|
1098
|
+
serverCtx.serve500(req, res, e, "serveDevPreview");
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
972
1101
|
async function getDirectoryFiles(sys, baseUrl, dirItemNames) {
|
|
973
1102
|
const items = await getDirectoryItems(sys, baseUrl, dirItemNames);
|
|
974
1103
|
if (baseUrl.pathname !== "/") items.unshift({
|
|
@@ -1178,7 +1307,8 @@ function initServerProcess(sendMsg) {
|
|
|
1178
1307
|
* @returns Function to send messages from main to worker process
|
|
1179
1308
|
*/
|
|
1180
1309
|
function initServerProcessWorkerProxy(sendToMain) {
|
|
1181
|
-
|
|
1310
|
+
const workerPath = path.join(import.meta.dirname, "worker-thread.js");
|
|
1311
|
+
const forkOpts = {
|
|
1182
1312
|
execArgv: process.execArgv.filter((v) => !/^--(debug|inspect)/.test(v)),
|
|
1183
1313
|
env: process.env,
|
|
1184
1314
|
cwd: process.cwd(),
|
|
@@ -1188,7 +1318,8 @@ function initServerProcessWorkerProxy(sendToMain) {
|
|
|
1188
1318
|
"pipe",
|
|
1189
1319
|
"ipc"
|
|
1190
1320
|
]
|
|
1191
|
-
}
|
|
1321
|
+
};
|
|
1322
|
+
let serverProcess = fork(workerPath, [], forkOpts);
|
|
1192
1323
|
/**
|
|
1193
1324
|
* Send a message from main to the worker process
|
|
1194
1325
|
*
|
|
@@ -1339,27 +1470,33 @@ function startServer(devServerConfig, logger, watcher, initServerProcessFn, reso
|
|
|
1339
1470
|
resolve(devServer);
|
|
1340
1471
|
};
|
|
1341
1472
|
const requestLog = (msg) => {
|
|
1342
|
-
if (devServerConfig.logRequests && msg.requestLog)
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1473
|
+
if (devServerConfig.logRequests && msg.requestLog) {
|
|
1474
|
+
if (msg.requestLog.status >= 500) logger.info(logger.red(`${msg.requestLog.method} ${msg.requestLog.url} (${msg.requestLog.status})`));
|
|
1475
|
+
else if (msg.requestLog.status >= 400) logger.info(logger.dim(logger.red(`${msg.requestLog.method} ${msg.requestLog.url} (${msg.requestLog.status})`)));
|
|
1476
|
+
else if (msg.requestLog.status >= 300) logger.info(logger.dim(logger.magenta(`${msg.requestLog.method} ${msg.requestLog.url} (${msg.requestLog.status})`)));
|
|
1477
|
+
else logger.info(logger.dim(`${logger.cyan(msg.requestLog.method)} ${msg.requestLog.url}`));
|
|
1478
|
+
}
|
|
1346
1479
|
};
|
|
1347
1480
|
const serverError = async (msg) => {
|
|
1348
|
-
if (msg.error)
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1481
|
+
if (msg.error) {
|
|
1482
|
+
if (hasStarted) logger.error(msg.error.message + " " + msg.error.stack);
|
|
1483
|
+
else {
|
|
1484
|
+
await close();
|
|
1485
|
+
reject(msg.error.message);
|
|
1486
|
+
}
|
|
1352
1487
|
}
|
|
1353
1488
|
};
|
|
1354
1489
|
const requestBuildResults = () => {
|
|
1355
|
-
if (sendToServer)
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1490
|
+
if (sendToServer) {
|
|
1491
|
+
if (lastBuildResults != null) {
|
|
1492
|
+
const msg = {
|
|
1493
|
+
buildResults: { ...lastBuildResults },
|
|
1494
|
+
isActivelyBuilding
|
|
1495
|
+
};
|
|
1496
|
+
delete msg.buildResults.hmr;
|
|
1497
|
+
sendToServer(msg);
|
|
1498
|
+
} else sendToServer({ isActivelyBuilding: true });
|
|
1499
|
+
}
|
|
1363
1500
|
};
|
|
1364
1501
|
const compilerRequest = async (compilerRequestPath) => {
|
|
1365
1502
|
if (watcher?.request && sendToServer) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stencil/dev-server",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.30",
|
|
4
4
|
"description": "Development server for Stencil with DOM-based HMR",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dev server",
|
|
@@ -35,21 +35,21 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"launch-editor": "^2.
|
|
38
|
+
"launch-editor": "^2.14.1",
|
|
39
39
|
"open": "^11.0.0",
|
|
40
|
-
"ws": "^8.
|
|
40
|
+
"ws": "^8.21.3"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@tsdown/css": "^0.
|
|
44
|
-
"@types/ws": "^8.
|
|
45
|
-
"tsdown": "^0.
|
|
46
|
-
"typescript": "
|
|
47
|
-
"vitest": "^4.1.
|
|
48
|
-
"vitest-environment-stencil": "^1.
|
|
49
|
-
"@stencil/core": "5.0.0-alpha.
|
|
43
|
+
"@tsdown/css": "^0.22.14",
|
|
44
|
+
"@types/ws": "^8.18.1",
|
|
45
|
+
"tsdown": "^0.22.14",
|
|
46
|
+
"typescript": ">5.0.0 <7.0.0",
|
|
47
|
+
"vitest": "^4.1.10",
|
|
48
|
+
"vitest-environment-stencil": "^1.14.0",
|
|
49
|
+
"@stencil/core": "5.0.0-alpha.30"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"@stencil/core": "^5.0.0-
|
|
52
|
+
"@stencil/core": "^5.0.0 || 5.0.0-alpha.30"
|
|
53
53
|
},
|
|
54
54
|
"volta": {
|
|
55
55
|
"extends": "../../package.json"
|
/package/{LICENSE.md → LICENSE}
RENAMED
|
File without changes
|