Skip to content

Commit 225ab04

Browse files
authored
Merge branch 'main' into add-testing-docs
2 parents b9b0ce9 + 34721a3 commit 225ab04

1 file changed

Lines changed: 58 additions & 1 deletion

File tree

docs/_guide/you-will-need.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,61 @@ Please note this list may increase over time. Catalyst will never ship with poly
2323

2424
When using build tools, some JavaScript minifiers modify the class name that Catalyst relies on. You know you have an issue if you encounter the error `"c" is not a valid custom element name`.
2525

26-
A best practice is to allow class names that end with `Element`. For instance, for Terser, you can use the following config: `{ keep_classnames: /Element$/ }`
26+
The preferred way to handle this is to disable renaming class names in your build tools.
27+
28+
#### ESBuild
29+
30+
When using ESBuild you can turn off all class and function name minification with the [`keep_names`](https://esbuild.github.io/api/#keep-names) option. Setting this to `true` in your build will opt-out all classes and all functions from minification.
31+
32+
33+
```ts
34+
{ keep_names: true }
35+
// Or --keep-names on the CLI
36+
```
37+
38+
#### Terser
39+
40+
When using Terser you have a bit more control, and can explicitly opt just classes, or just certain class names out of minification. For example to opt-out class names that end with `Element` you can set the following config:
41+
42+
```ts
43+
{ keep_classnames: /Element$/ }
44+
```
45+
46+
It is also possible to set `keep_classnames` to `true` (or pass `--keep-classnames` to the CLI tool), which will opt-out all class names. [You can read more about the minification options on Terser's docs](https://terser.org/docs/api-reference#minify-options)
47+
48+
#### SWC
49+
50+
When using SWC you can use the `keep_classnames` option just like Terser. As SWC also handles Transpilation, you should be sure to enable native class syntax by specifiying `target` to at least `es2016`. [Take a look at the SWC docs for more about compression options](https://swc.rs/docs/configuration/minification#jscminifycompress).
51+
52+
```json
53+
{
54+
"jsc": {
55+
"target": "es2016",
56+
"minify": {
57+
"compress": {
58+
"keep_classnames": true
59+
}
60+
}
61+
}
62+
}
63+
```
64+
65+
#### Other alternatives
66+
67+
If your tool chain does not support opting out of minification, or if you would prefer to keep name minification on, you can instead selectively re-assign the `name` field to Catalyst controllers:
68+
69+
```ts
70+
@controller
71+
class UserList extends HTMLElement {
72+
static name = 'UserList'
73+
}
74+
```
75+
76+
TypeScript decorators only support _class declarations_ which means you will still need to keep the class name between `class` and `extends`. For example the following will be a SyntaxError:
77+
78+
```ts
79+
@controller
80+
class extends HTMLElement {
81+
static name = 'UserList'
82+
}
83+
```

0 commit comments

Comments
 (0)