Skip to content

Commit 7d3f655

Browse files
johnjenkinsJohn Jenkins
andauthored
feat: v4.41 release (new docs for strictPort and jsxImportSource) (#1574)
* feat: add docs for strictPort and jsxImportSource * chore: * chore: --------- Co-authored-by: John Jenkins <john.jenkins@nanoporetech.com>
1 parent 2aa658e commit 7d3f655

File tree

109 files changed

+17133
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+17133
-4
lines changed

cspell-wordlist.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ usingtheplatform
4141
vnode
4242
webapps
4343
wdio
44-
webdriverio
44+
webdriverio
45+
hyperscript

docs/components/api.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ The following primitives can be imported from the `@stencil/core` package and us
119119

120120
### [**h()**](./templating-and-jsx.md):
121121

122-
It's used within the `render()` to turn the JSX into Virtual DOM elements.
122+
Turns JSX syntax into Virtual DOM elements. Read more on the [Templating and JSX](./templating-and-jsx.md#the-h-and-fragment-functions) page.
123123

124-
- **render()**: a utility method to render a virtual DOM created by `h()` into a container.
124+
### **render()**:
125+
126+
A utility method to render a virtual DOM created by `h()` into a container.
125127

126128
__Type:__ `(vnode: VNode, container: Element) => void`
127129
__Example:__

docs/components/templating-and-jsx.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,59 @@ Stencil components are rendered using JSX, a popular, declarative template synta
1111

1212
## Basics
1313

14-
The `render` function is used to output a tree of components that will be drawn to the screen.
14+
### The `h` and `Fragment` functions
15+
16+
The `h` function (short for "hyperscript") is a factory function that creates virtual DOM elements. When you write JSX like `<div>Hello</div>`, it gets transformed into function calls like `h('div', null, 'Hello')`. The `Fragment` function is used to group a list of children without adding extra nodes to the DOM.
17+
18+
#### Set up in Stencil
19+
20+
Within your Stencil project, make sure to add or modify your `tsconfig.json` file, adding:
21+
22+
```json
23+
{
24+
"compilerOptions": {
25+
"jsx": "react",
26+
"jsxFactory": "h",
27+
"jsxFragmentFactory": "Fragment"
28+
}
29+
}
30+
```
31+
32+
You will then need to import `h` (and `Fragment` if you plan to use it) from `@stencil/core` at the top of your component file:
33+
34+
```tsx
35+
import { h, Fragment, Component } from '@stencil/core';
36+
37+
@Component({
38+
tag: 'my-component',
39+
})
40+
class MyComponent { ... }
41+
```
42+
43+
(Read more about using the `Fragment` component in the [Complex Template Content](#complex-template-content) section below.)
44+
45+
### `jsxImportSource` alternative
46+
47+
`jsxImportSource` is a more modern approach that automatically imports the necessary JSX runtime functions instead of manually importing `h`; the compiler automatically imports what it needs from the specified package.
48+
49+
#### Set up in Stencil
50+
51+
To use `jsxImportSource`, modify your `tsconfig.json` file as follows:
52+
53+
```json
54+
{
55+
"compilerOptions": {
56+
"jsx": "react-jsx",
57+
"jsxImportSource": "@stencil/core"
58+
}
59+
}
60+
```
61+
62+
Using this approach means you do not need to manually import `h` or `Fragment` in your component files.
63+
64+
### The `render` function
65+
66+
The `render` function is used within your component to output a tree of elements that will be drawn to the screen.
1567

1668
```tsx
1769
class MyComponent {

docs/config/dev-server.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ If set to `null`, no route will be registered.
130130

131131
Sets the server's port.
132132

133+
### `strictPort`
134+
135+
**Optional**
136+
137+
**Type: `boolean`**
138+
139+
**Default: `false`**
140+
141+
When set to `true`, the dev server will fail to start if the specified `port` is already in use. When set to `false`, the dev server will try the next available port if the specified port is in use.
142+
133143
### `reloadStrategy`
134144

135145
**Optional**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: Build Constants
3+
description: Stencil has a number of add-ons that you can use with the build process.
4+
slug: /build-variables
5+
---
6+
7+
# Build Constants
8+
9+
Build Constants in Stencil allow you to run specific code only when Stencil is running in development mode. This code is stripped from your bundles when doing a production build, therefore keeping your bundles as small as possible.
10+
11+
### Using Build Constants
12+
13+
Lets dive in and look at an example of how to use our build constants:
14+
15+
```tsx
16+
import { Component, Build } from '@stencil/core';
17+
18+
@Component({
19+
tag: 'stencil-app',
20+
styleUrl: 'stencil-app.scss'
21+
})
22+
export class StencilApp {
23+
24+
componentDidLoad() {
25+
if (Build.isDev) {
26+
console.log('im in dev mode');
27+
} else {
28+
console.log('im running in production');
29+
}
30+
31+
if (Build.isBrowser) {
32+
console.log('im in the browser');
33+
} else {
34+
console.log('im in prerendering (server)');
35+
}
36+
}
37+
}
38+
```
39+
40+
As you can see from this example, we just need to import `Build` from `@stencil/core` and then we can use the `isDev` constant to detect when we are running in dev mode or production mode.
41+
42+
### Use Cases
43+
44+
Some use cases we have come up with are:
45+
46+
- Diagnostics code that runs in dev to make sure logic is working like you would expect
47+
- `console.log()`'s that may be useful for debugging in dev mode but that you don't want to ship
48+
- Disabling auth checks when in dev mode
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Components",
3+
"position": 2
4+
}

0 commit comments

Comments
 (0)