<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jesus Perez</title>
    <description>The latest articles on DEV Community by Jesus Perez (@tx2z).</description>
    <link>https://dev.to/tx2z</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F151360%2F58cc8aa7-7887-47b1-807e-4667e4d75766.jpeg</url>
      <title>DEV Community: Jesus Perez</title>
      <link>https://dev.to/tx2z</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tx2z"/>
    <language>en</language>
    <item>
      <title>Create web components with typescript</title>
      <dc:creator>Jesus Perez</dc:creator>
      <pubDate>Thu, 05 Mar 2020 17:25:30 +0000</pubDate>
      <link>https://dev.to/tx2z/create-web-components-with-typescript-2foo</link>
      <guid>https://dev.to/tx2z/create-web-components-with-typescript-2foo</guid>
      <description>&lt;p&gt;In the last months, I've been digging in how to mix two of my favourites "technologies" of recent times. Web components and Typescript.&lt;/p&gt;

&lt;p&gt;I understand web components as they are described in &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components"&gt;their MDN page&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Web Components is a suite of different technologies allowing you to create reusable custom elements — with their functionality encapsulated away from the rest of your code — and utilize them in your web apps.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, in my opinion, they are not something that going to replace frameworks like React or Angular, but a way to create reusable pieces of code that you can use independently of your front-end stack.&lt;/p&gt;

&lt;p&gt;In the other hand, I fell in love with &lt;a href="https://www.typescriptlang.org/"&gt;Typescript&lt;/a&gt; since I started using it. The possibility to use the latest javascript and the security and bug prevention of the typing it's something I miss when I have to work in a project without it.&lt;/p&gt;

&lt;p&gt;So, you will say, it's easy, start to write your components with typescript and then transpile and serve them...&lt;/p&gt;

&lt;p&gt;and that's how my project started...&lt;/p&gt;

&lt;p&gt;Soon in my project I wanted a way to serve my component as a single file, but separate my HTML and CSS in different files when developing, or use &lt;a href="https://lit-html.polymer-project.org/"&gt;lit-html&lt;/a&gt; for the templates, and hot-reload when developing and an option to bundle everything to serve it in production... so I started to experiment with &lt;a href="https://webpack.js.org/"&gt;webpack&lt;/a&gt; (and changed it later with &lt;a href="https://rollupjs.org/"&gt;rollup&lt;/a&gt;).&lt;br&gt;
I wanted as well a way to do tests, so I ended using &lt;a href="https://karma-runner.github.io/"&gt;karma&lt;/a&gt; and &lt;a href="https://jasmine.github.io/"&gt;jasmine&lt;/a&gt;.&lt;br&gt;
I wanted a way to have a component gallery, so I integrated &lt;a href="https://storybook.js.org/"&gt;storybook&lt;/a&gt;.&lt;br&gt;
At the end I wanted to upload the result components to npm, so needed to create a package.json and a README.&lt;/p&gt;

&lt;p&gt;And all that is what included out of the box in what I've called "&lt;a href="https://github.com/tx2z/wc-base-typescript"&gt;wc-base-typescript&lt;/a&gt;" (Original, I know :)).&lt;/p&gt;

&lt;p&gt;This project allows you to start writing you web components with typescript in one minute (depending on your internet connection) and have ready for you all the functionalities I described above.&lt;/p&gt;

&lt;p&gt;Do you want to test it or use it in your next web-component projects? Keep reading or go directly to the &lt;a href="https://github.com/tx2z/wc-base-typescript"&gt;wc-base-typescript github repository&lt;/a&gt; as it's more or less the same but with fewer words.&lt;/p&gt;

&lt;p&gt;Of course, don't forget to give the project an star in github if you think it deserves it!&lt;/p&gt;

&lt;p&gt;And if there is something you think can be done in any other (or better) way, don't hesitate to tell me in the comments or send me a PR, collaboration is more than welcome :)&lt;/p&gt;

&lt;p&gt;Ready? Let's go!&lt;/p&gt;
&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;First thing. It's better if you copy the repository and make your own for your project. You can always download the code but I recommend to use &lt;a href="https://github.com/Rich-Harris/degit/"&gt;degit&lt;/a&gt; or any similar tool for convenience.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; degit &lt;span class="c"&gt;#In case you don't have it installed&lt;/span&gt;
degit tx2z/wc-base-typescript
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once the project is on your computer, enter the folder and install all npm dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And you can start to write your components! I told you 1 minute :)&lt;/p&gt;

&lt;p&gt;All the components you're going to create will be stored in &lt;em&gt;src/components&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I've created in the project three components to show the different options available:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;base-component&lt;/strong&gt; generates a web component with no dependencies or libraries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;lithtml-component&lt;/strong&gt; generate a web component using &lt;a href="https://lit-html.polymer-project.org/"&gt;lit-html&lt;/a&gt; as template library.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;noshadow-component&lt;/strong&gt; generate a custom element (not sure if it can be called a web component) without using the shadow DOM &amp;amp; with no dependencies or libraries.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a quick start, you can copy the content of one of the example components and change what you need.&lt;/p&gt;

&lt;p&gt;It's important as well to include your defined custom element in &lt;em&gt;public/index.html&lt;/em&gt; so you can test it while developing. If you open it you will see that I've included the three example components there.&lt;/p&gt;

&lt;p&gt;Then, to run a server in development mode you only have to type in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Rollup will transpile and pack your components and reload the page every time you save a file.&lt;/p&gt;

&lt;p&gt;That's it. Nothing to configure or to do other than focus in your code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Tests
&lt;/h2&gt;

&lt;p&gt;So maybe you want to add some test to your components to avoid problems when the code-base start to grow... or, even better, you're a super-developer that like to write your code following the Test-driven development (TDD) process.&lt;/p&gt;

&lt;p&gt;I had some hard time trying to add test to my previous web component projects, but, again, now everything is pre-configured and works out of the box. &lt;a href="https://karma-runner.github.io/"&gt;Karma&lt;/a&gt; &amp;amp; &lt;a href="https://jasmine.github.io/"&gt;jasmine&lt;/a&gt; are included as testing environment and framework.&lt;/p&gt;

&lt;p&gt;Inside your component folder include a file ending with "&lt;strong&gt;spec.ts&lt;/strong&gt;" and write your tests (with typescript). Of course, there are some examples in the example components.&lt;/p&gt;

&lt;p&gt;By default, launchers for chrome and firefox are already installed and configured (your test will be launched in both browsers) but you can install and use any other &lt;a href="https://www.npmjs.com/search?q=keywords:karma-launcher"&gt;karma-launcher&lt;/a&gt; if you want.&lt;/p&gt;

&lt;p&gt;To execute the test use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Karma has as well live-reload, so if you change your components or test files the test will be executed again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Storybook as component gallery
&lt;/h2&gt;

&lt;p&gt;You can optionally use &lt;a href="https://storybook.js.org/"&gt;storybook&lt;/a&gt; to develop, document and create a gallery for your web components. You can learn more about storybook in their &lt;a href="https://storybook.js.org/docs/basics/introduction/"&gt;documentation site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Just a little warning here, storybook uses webpack and, when building, rollup will be used... the result should act and look the same but it's not exactly the same.&lt;/p&gt;

&lt;p&gt;Storybook for HTML is used and tuned so no further configuration is needed to use it for your components. The following storybook addons are included:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/storybookjs/storybook/tree/master/addons/knobs"&gt;Knobs&lt;/a&gt;: Allows edit web components' props dynamically using the Storybook UI.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/storybookjs/storybook/tree/master/addons/viewport"&gt;Viewport&lt;/a&gt;: Allows web components to be displayed in different sizes and layouts in Storybook. This helps build responsive web components inside of Storybook.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/storybookjs/storybook/tree/master/addons/a11y"&gt;A11y&lt;/a&gt;: Helps to make your web components more accessible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/storybookjs/storybook/tree/master/addons/notes"&gt;Notes&lt;/a&gt;: Allows to write notes or documentation for your web components.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a standard storybook installation. You can add other &lt;a href="https://github.com/storybookjs/storybook/tree/master/addons/"&gt;addons&lt;/a&gt; or configurations that suit your needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create stories
&lt;/h3&gt;

&lt;p&gt;There are two files inside the example components that are used by storybook.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;@component.stories.ts&lt;/strong&gt;: Is the file to create a story for your component. You can check storybook documentation about how to create stories starting &lt;a href="https://storybook.js.org/docs/guides/guide-html/#step-4-write-your-stories"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;README.md&lt;/strong&gt;: It's used to add documentation for your component using the &lt;a href="https://github.com/storybookjs/storybook/tree/master/addons/notes"&gt;Notes&lt;/a&gt; storybook addon.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Storybook will use these two files to generate the stories and the documentation. Once these two files are present run storybook with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run storybook
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Again, examples of all this can be found in the example components provided :)&lt;/p&gt;

&lt;p&gt;To build a static storybook app ready to be deployed run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run storybook-build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can find your storybook builds in the "dist-storybook" folder.&lt;/p&gt;

&lt;p&gt;If you want to publish your storybook in github pages I recommend you to install &lt;a href="https://github.com/storybookjs/storybook-deployer"&gt;storybook-deployer&lt;/a&gt;. It will do all the work for you.&lt;/p&gt;

&lt;p&gt;Note that storybook is completely optional. If you're not using it you can remove (or not create) the @component.stories.ts files. Even you can remove all the storybook and webpack references in the &lt;a href="//package.json"&gt;package.json&lt;/a&gt; to completely remove storybook from your project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build the components
&lt;/h2&gt;

&lt;p&gt;So it's time to build your components to use them elsewhere. Fear not, just run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can find your builds in the "dist" folder.&lt;/p&gt;

&lt;p&gt;Your components will be compiled as js file (as &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules"&gt;ES module&lt;/a&gt;), to use them as modules in your application, and in a dist.js (as &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/IIFE"&gt;iife&lt;/a&gt;), to use them directly in browsers, with the same name you give to the folder in &lt;em&gt;src/components&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;All modules will include their respective &lt;a href="https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html"&gt;Typescript declaration files&lt;/a&gt; in case you or others want to use them with typescript.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/webcomponents/polyfills"&gt;webcomponents polyfills&lt;/a&gt; will be included as well in the "dist" folder, you can use them or load directly the code from a CDN such as unpkg: &lt;a href="https://unpkg.com/@webcomponents/webcomponentsjs@%5E2/"&gt;https://unpkg.com/@webcomponents/webcomponentsjs@^2/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So that's it take your components and use them whatever you want. Just copy-paste the version (ES module or IIFE) that you need in your project and use it.&lt;/p&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;p&gt;No, that's not how things work nowadays, I know. The wc-base-typescript simplify as well all you need to...&lt;/p&gt;

&lt;h2&gt;
  
  
  Publish component to NPM
&lt;/h2&gt;

&lt;p&gt;You may have seen that a &lt;strong&gt;README&lt;/strong&gt; and a &lt;strong&gt;"package.json"&lt;/strong&gt; are included as well in every example component folder. Rollup will copy these files to the "dist" folder and add some important info to the package.json.&lt;/p&gt;

&lt;p&gt;In other words, you don't need to worry about the &lt;em&gt;main&lt;/em&gt;, &lt;em&gt;module&lt;/em&gt; or &lt;em&gt;type&lt;/em&gt; properties, as they will be filled automatically, but you need to provide the rest of the information in the package.json.&lt;/p&gt;

&lt;p&gt;All you need to do to publish your component in NPM is to enter the component folder inside "dist" your terminal and:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm publish
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Of course, you need to have an account in npm and login into it in your terminal before that. I recommend reading the &lt;a href="https://docs.npmjs.com/packages-and-modules/contributing-packages-to-the-registry"&gt;how-to&lt;/a&gt; in the npm docs before publishing a package.&lt;/p&gt;

&lt;p&gt;Once your package is in npm it can be installed and used as any other npm package. The ES module version will be the default.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing up
&lt;/h2&gt;

&lt;p&gt;So with the &lt;a href="https://github.com/tx2z/wc-base-typescript"&gt;wc-base-typescript&lt;/a&gt; project you can quickly create, test and build for production and publish web components, and optionally create a components gallery to show them up.&lt;/p&gt;

&lt;p&gt;If you want to see one of the uses I give it to it you can check my &lt;a href="https://www.npmjs.com/package/creative-commons-web-component"&gt;Creative Commons license generator as web component&lt;/a&gt; with his &lt;a href="https://tx2z.github.io/creativecommons-webcomponent/"&gt;galery&lt;/a&gt; and &lt;a href="https://github.com/tx2z/creativecommons-webcomponent"&gt;repository&lt;/a&gt;. I created it (all included) in around 6 hours of intermittent work.&lt;/p&gt;

&lt;p&gt;What do you think? Can it be useful for you?&lt;/p&gt;

&lt;p&gt;PD: Don't hesitate to tell me if you use it for any of your projects, I will love to know about what are you creating with it :)&lt;br&gt;
PD2: And again, if there is something you think can be done in any other (or better) way, don't hesitate to tell me in the comments or send me a PR :D&lt;/p&gt;

</description>
      <category>webcomponents</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>rollup</category>
    </item>
  </channel>
</rss>
