@sesamy/sesamy-components 0.0.1

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 ADDED
@@ -0,0 +1,124 @@
1
+ # 🌐 svelte-web-components-template
2
+
3
+ > A base template for building a shareable web components library using [Vite](https://vitejs.dev), [Svelte](https://svelte.dev) and [TypeScript](https://www.typescriptlang.org).
4
+
5
+ This templates generates vanilla [web components](https://developer.mozilla.org/en-US/docs/Web/Web_Components) than can be used with [plain HTML](https://www.webcomponents.org/introduction#how-do-i-use-a-web-component-) or within any major frameworks, such as React, Angular, Vue or Svelte (see [compatibility](https://custom-elements-everywhere.com/)).
6
+
7
+ ## How to use this template
8
+
9
+ You can directly create a new GitHub repo from this template by selecting the **Use this template** button on GitHub.
10
+
11
+ You can also clone it locally with the following commands:
12
+
13
+ ```bash
14
+ npx degit sinedied/svelte-web-components-template#main my-component-lib
15
+ cd my-component-lib
16
+ npm install
17
+ ```
18
+
19
+ Your components source code lives in `lib/` folder. Only components with the `.wc.svelte` extension will be exported as web components and available in your library. This means that you can also use regular Svelte components with the `.svelte` extension as child components for your implementation details.
20
+
21
+ You can add additional components by adding them to the `lib` folder and editing `lib/index.js`.
22
+
23
+ ## Testing your components
24
+
25
+ You can start a development server with:
26
+
27
+ ```bash
28
+ npm start
29
+ ```
30
+
31
+ Then open your browser to [localhost:5173](http://localhost:5173).
32
+
33
+ This will build the demo application located in the `demo/` folder, in which you can use and test your web components during development.
34
+
35
+ If you need unit tests, you can take a look at [Jest](https://jestjs.io) and [Jest testing library](https://github.com/testing-library/svelte-testing-library).
36
+
37
+ ### Using the built web components with the demo app
38
+
39
+ The demo application is provided for development and testing of your components, that's why it imports the `.svelte` files from the `lib/` folder directly by default.
40
+
41
+ If you prefer, you can import the built web components from the `dist/` folder instead, by editing `demo/src/App.svelte` and replacing the `import '../../lib';` statement with `import '../../../dist/lib';` if you have the `bundleComponents` option enabled, or individually import your components with `import import '../../dist/MyComponent.wc.js';` otherwise.
42
+
43
+ You'll also have to make sure to run the `npm run build` script to generate the `dist/lib/` folder first.
44
+
45
+ ## Building the library
46
+
47
+ The command `npm run build` will create the web components library in the `dist/lib/` folder. It creates both an ES module (`dist/lib/<your-lib>.js`) suitable for bundler (non-minified), a minified ES module (`dist/lib/<your-lib>.min.js`) and a regular UMD script (`dist/lib/<your-lib>.umd.js`).
48
+
49
+ The build is automatically called when executing `npm publish` to distribute your library, thanks to the `prepublishOnly` script entry in `package.json`.
50
+
51
+ ## Notes and limitations
52
+
53
+ This template does not provide any web components polyfills for older browsers support. It's usually best to leave that task to the host application, hence why they're left out.
54
+
55
+ ### Props
56
+
57
+ Any props accepted by your web component are automatically transformed to element attributes. Since camelCase or PascalCase does not work in HTML, you have to make sure to name your props in lowercase.
58
+
59
+ ```html
60
+ <script>
61
+ export let myvalue = "Default";
62
+ </script>
63
+ ```
64
+
65
+ ### Events
66
+
67
+ The Svelte syntax event for listening to events like `on:myevent` doesn't work with events dispatched from a Svelte web component ([#3119](https://github.com/sveltejs/svelte/issues/3119)).
68
+
69
+ You need to use a workaround for that, by creating a `CustomEvent` and dispatching it.
70
+
71
+ Here's an example:
72
+
73
+ ```html
74
+ // MyComponent.wc.svelte
75
+ <svelte:options tag="my-component" />
76
+ <script>
77
+ import { get_current_component } from "svelte/internal";
78
+ const component = get_current_component();
79
+
80
+ // example function for dispatching events
81
+ const dispatchEvent = (name, detail) =>
82
+ component.dispatchEvent(new CustomEvent(name, { detail }));
83
+ </script>
84
+ <button on:click={() => dispatchEvent("test", "Hello!")}>
85
+ Click to dispatch event
86
+ </button>
87
+ ```
88
+
89
+ ## Building each component into its own module
90
+
91
+ By default this template will build all components into a single module.
92
+
93
+ If you prefer to build each component into its own module, you can do so by setting the environment variable `BUNDLE_COMPONENTS` to `false`, or editing `vite.config.js` and setting the `bundleComponents` option to `false`.
94
+
95
+ Then you also need to replace the content of `packages/lib/index.ts` with:
96
+
97
+ ```js
98
+ export default () => {
99
+ import('./MyComponent.wc.svelte');
100
+ // Import each of your component this way
101
+ };
102
+ ```
103
+
104
+ This will enable code-splitting and will generate an ES module for each component in the `dist/lib/` folder.
105
+
106
+ As you changed the way components are exported, you also need to replace the `import '../../lib';` statement in `demo/src/App.svelte` to `import '../../lib/MyComponent.wc.svelte';`.
107
+
108
+ ## Why enable `allowJs` in the TS template?
109
+
110
+ While `allowJs: false` would indeed prevent the use of `.js` files in the project, it does not prevent the use of JavaScript syntax in `.svelte` files. In addition, it would force `checkJs: false`, bringing the worst of both worlds: not being able to guarantee the entire codebase is TypeScript, and also having worse typechecking for the existing JavaScript. In addition, there are valid use cases in which a mixed codebase may be relevant.
111
+
112
+
113
+ ## Why is HMR not preserving my local component state?
114
+
115
+ HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/sveltejs/svelte-hmr/tree/master/packages/svelte-hmr#svelte-hmr).
116
+
117
+ If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.
118
+
119
+ ```ts
120
+ // store.ts
121
+ // An extremely simple external store
122
+ import { writable } from 'svelte/store'
123
+ export default writable(0)
124
+ ```