<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Lit Me In]]></title><description><![CDATA[Lit Me In]]></description><link>https://lit.me.in</link><generator>RSS for Node</generator><lastBuildDate>Mon, 07 Sep 2026 00:20:35 GMT</lastBuildDate><atom:link href="https://lit.me.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Getting Started with lit]]></title><description><![CDATA[So I've been wanted to try for sometime but what put me off was the lack of a comprehensive documentation from scratch assuming a little knowledge of TypeScript and build tools by the learner. Because that's where I was coming from. Had it been JavaS...]]></description><link>https://lit.me.in/getting-started-with-lit</link><guid isPermaLink="true">https://lit.me.in/getting-started-with-lit</guid><category><![CDATA[lit]]></category><category><![CDATA[TypeScript]]></category><category><![CDATA[getting started]]></category><category><![CDATA[rollup]]></category><dc:creator><![CDATA[Anjanesh Lekshminarayanan]]></dc:creator><pubDate>Wed, 07 Aug 2024 00:20:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1722965260164/ca74d6c0-1650-4be5-bb06-466efb2d25f5.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>So I've been wanted to try for sometime but what put me off was the lack of a comprehensive documentation from scratch assuming a little knowledge of TypeScript and build tools by the learner. Because that's where I was coming from. Had it been JavaScript it would've been easier but I want to start using TypeScript since it has been starting to be more appealing for development.</p>
<p>Let's look at this example : <a target="_blank" href="https://lit.dev/tutorials/intro-to-lit/">https://lit.dev/tutorials/intro-to-lit/</a> in TypeScript. The option to choose TypeScript is the toggle button on the right hand side.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1722961454550/fa390a5b-068b-4e23-8b00-610849a1e3a6.png" alt class="image--center mx-auto" /></p>
<p>There are 2 files included in the example - you can see the 2 tabs - <code>my-element.ts</code> and <code>index.html</code>. Create these 2 files with ditto code in some directory, say, <code>getting-started</code>. Added here incase the tutorial has the code changed later on.</p>
<p><code>my-element.ts</code></p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> {LitElement, html} <span class="hljs-keyword">from</span> <span class="hljs-string">'lit'</span>;
<span class="hljs-keyword">import</span> {customElement, property} <span class="hljs-keyword">from</span> <span class="hljs-string">'lit/decorators.js'</span>;

<span class="hljs-meta">@customElement</span>(<span class="hljs-string">'my-element'</span>)
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> MyElement <span class="hljs-keyword">extends</span> LitElement {
  <span class="hljs-meta">@property</span>()
  version = <span class="hljs-string">'STARTING'</span>;

  render() {
    <span class="hljs-keyword">return</span> html`<span class="xml">
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Welcome to the Lit tutorial!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>This is the </span><span class="hljs-subst">${<span class="hljs-built_in">this</span>.version}</span><span class="xml"> code.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    `</span>;
  }
}
</code></pre>
<p><code>index.html</code></p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"module"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"my-elements.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
      <span class="hljs-selector-tag">body</span> {
        <span class="hljs-attribute">font-family</span>: <span class="hljs-string">'Open Sans'</span>, sans-serif;
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.5em</span>;
        <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">0.5em</span>;
      }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">my-element</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">my-element</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>The tutorial example assumes you know TypeScript configuration if working on TypeScript and some knowledge about build tools - obviously - since it is a <strong>lit</strong> tutorial exclusively and not the entire web stack.</p>
<p><a target="_blank" href="https://lit.dev/docs/tools/requirements/">https://lit.dev/docs/tools/requirements/</a> says "Lit is published as ES2021." but let's specify ES2022 in <code>tsconfig.json</code> in the same <code>getting-started</code> directory, which is as below :</p>
<pre><code class="lang-json">{
    <span class="hljs-attr">"compilerOptions"</span>: {
      <span class="hljs-attr">"module"</span>: <span class="hljs-string">"ES2022"</span>,
      <span class="hljs-attr">"moduleResolution"</span>: <span class="hljs-string">"node"</span>,
      <span class="hljs-attr">"experimentalDecorators"</span>: <span class="hljs-literal">true</span>,
      <span class="hljs-attr">"useDefineForClassFields"</span>: <span class="hljs-literal">false</span>,
      <span class="hljs-attr">"lib"</span>: [<span class="hljs-string">"ES2022"</span>, <span class="hljs-string">"dom"</span>],
      <span class="hljs-attr">"target"</span>: <span class="hljs-string">"ES2022"</span>,
      <span class="hljs-attr">"outDir"</span>: <span class="hljs-string">"./dist"</span>,
      <span class="hljs-attr">"baseUrl"</span>: <span class="hljs-string">"./"</span>,
      <span class="hljs-attr">"declaration"</span>: <span class="hljs-literal">true</span>,
      <span class="hljs-attr">"declarationMap"</span>: <span class="hljs-literal">true</span>,
      <span class="hljs-attr">"sourceMap"</span>: <span class="hljs-literal">true</span>,
    }
  }
</code></pre>
<p>There are some npm packages to be installed (assuming you have NodeJS, I have 21.7.3) - instead of installing them one by one - just add this <code>package.json</code> file in <code>getting-started</code> directory.</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"dependencies"</span>: {
    <span class="hljs-attr">"lit"</span>: <span class="hljs-string">"^3.1.4"</span>,
    <span class="hljs-attr">"rollup"</span>: <span class="hljs-string">"^4.20.0"</span>,
    <span class="hljs-attr">"typescript"</span>: <span class="hljs-string">"^5.5.4"</span>
  },
  <span class="hljs-attr">"devDependencies"</span>: {
    <span class="hljs-attr">"@rollup/plugin-node-resolve"</span>: <span class="hljs-string">"^15.2.3"</span>,
    <span class="hljs-attr">"@rollup/plugin-terser"</span>: <span class="hljs-string">"^0.4.4"</span>,
    <span class="hljs-attr">"@web/dev-server"</span>: <span class="hljs-string">"^0.4.6"</span>,
    <span class="hljs-attr">"@web/rollup-plugin-copy"</span>: <span class="hljs-string">"^0.5.1"</span>,
    <span class="hljs-attr">"@web/rollup-plugin-html"</span>: <span class="hljs-string">"^2.3.0"</span>,
    <span class="hljs-attr">"rollup-plugin-summary"</span>: <span class="hljs-string">"^2.0.1"</span>
  },
  <span class="hljs-attr">"type"</span>: <span class="hljs-string">"module"</span>
}
</code></pre>
<p>lit is the main package for lit, typescript is for the language used and rollup is used for building for production which will be explained at the end. The devDependencies are for mainly for rollup except for a web dev server (line having version 0.4.6) and terser for minifying the code (terser replaces the once widely used <a target="_blank" href="https://www.npmjs.com/package/uglify-js">uglify-js</a> during it's time)</p>
<p><code>npm i</code> should install everything without any errors creating a <code>node_modules</code> sub-directory in the directory <code>getting-started</code>.</p>
<p>Now in the terminal in the <code>getting-started</code> directory, run <code>tsc</code> which will convert <code>my-elements.ts</code> to <code>my-elements.js</code>. In case tsc doesn't work enter <code>sudo npm i -g typescript</code> in terminal. This installed TypeScript 5.5.4 on my macOS. Or on Windows, open PowerShell in Administrator mode and run <code>npm i -g typescript</code> and close the administrator PowerShell terminal and open a new non-admin PowerShell terminal and cd into the directory <code>getting-started</code>.</p>
<p><code>tsc</code> will compile all .ts files to .js files into a folder called <code>dist</code> (which we specified in <code>tsconfg.json</code> as <code>"outDir": "./dist",</code>). <code>tsc my-element.ts</code> will compile to JavaScript with but will neglet the <code>tsconfig.json</code> configuration file which is of no good because then it'll spit out all sorts of TypeScript compilation.</p>
<p>Now run <code>npx web-dev-server --node-resolve --watch --open</code> - this will open localhost:8000 but will throw an error saying my-elements.js is not found (404).</p>
<p>If we edit the index.html file to specify the my-elements.js file in dist folder it'll work :</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"module"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"dist/my-elements.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>Ideally this shouldn't be done while development and there should be a way to specify the target folder containing the js files. I'll update this portion when I get some clarity on this.</p>
<p>Now your localhost:8000 should show this :</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1722964210940/7d2ef1ea-f0ee-4187-b1f6-54b4d9ca9a13.png" alt class="image--center mx-auto" /></p>
<p>Now to build it for production. <code>lit</code> recommends and explains the use of <code>rollup</code> which is new to me too. But there are several mismatches in their documentation at <a target="_blank" href="https://lit.dev/docs/tools/production/">https://lit.dev/docs/tools/production/</a> for the <code>rollup.config.js</code> file. Here's my version :</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Import rollup plugins</span>

<span class="hljs-keyword">import</span> { rollupPluginHTML <span class="hljs-keyword">as</span> html } <span class="hljs-keyword">from</span> <span class="hljs-string">"@web/rollup-plugin-html"</span>;
<span class="hljs-keyword">import</span> {copy} <span class="hljs-keyword">from</span> <span class="hljs-string">'@web/rollup-plugin-copy'</span>;
<span class="hljs-keyword">import</span> resolve <span class="hljs-keyword">from</span> <span class="hljs-string">'@rollup/plugin-node-resolve'</span>;
<span class="hljs-keyword">import</span> terser <span class="hljs-keyword">from</span> <span class="hljs-string">'@rollup/plugin-terser'</span>;
<span class="hljs-comment">// import minifyHTML from 'rollup-plugin-minify-html-literals';</span>
<span class="hljs-keyword">import</span> summary <span class="hljs-keyword">from</span> <span class="hljs-string">'rollup-plugin-summary'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {

  <span class="hljs-attr">plugins</span>: [
    <span class="hljs-comment">// Entry point for application build; can specify a glob to build multiple</span>
    <span class="hljs-comment">// HTML files for non-SPA app</span>
    html({
      <span class="hljs-attr">input</span>: <span class="hljs-string">'index.html'</span>,
    }),

    <span class="hljs-comment">// Resolve bare module specifiers to relative paths</span>
    resolve(),
    <span class="hljs-comment">// Minify HTML template literals</span>
    <span class="hljs-comment">// minifyHTML(),</span>
    <span class="hljs-comment">// Minify JS</span>
    terser({
      <span class="hljs-attr">ecma</span>: <span class="hljs-number">2021</span>,
      <span class="hljs-attr">module</span>: <span class="hljs-literal">true</span>,
      <span class="hljs-attr">warnings</span>: <span class="hljs-literal">true</span>,
    }),

    <span class="hljs-comment">// Print bundle summary</span>

    summary(),
    <span class="hljs-comment">// Optional: copy any static assets to build directory</span>
    copy({
      <span class="hljs-attr">patterns</span>: [<span class="hljs-string">'images/**/*'</span>],
    }),
  ],

  <span class="hljs-attr">output</span>: {
    <span class="hljs-attr">dir</span>: <span class="hljs-string">'build'</span>,
  },

  <span class="hljs-attr">preserveEntrySignatures</span>: <span class="hljs-string">'strict'</span>,

};
</code></pre>
<p>I couldn't install <code>rollup-plugin-minify-html-literals</code> on my macOS and Windows machines.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1722964582204/a47d0b9e-39c6-4764-9ca5-20f723a056b8.png" alt class="image--center mx-auto" /></p>
<p>But I realised that <code>rollup-plugin-minify-html-literals</code> was used only for minifying HTML here. So I commented out the <code>minifyHTML()</code> call in <code>rollup.config.js</code> file - and also it's import.</p>
<p>Now run <code>rollup -c</code> - I had to do <code>sudo npm i -g rollup</code> before running <code>rollup -c</code> again. It would've created a build in the <code>build</code> folder.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1722964899829/535a7688-aa6e-4332-a55c-10b9d178aa91.png" alt class="image--center mx-auto" /></p>
<p>Now goto the <code>build</code> folder and you'll there are 2 files - <code>index.html</code> and <code>my-elements.js</code> - to run index.html run a simple webserver in the build folder like</p>
<p><code>npx web-dev-server --node-resolve --watch --open</code></p>
<p>or</p>
<p><code>php -S localhost:8000</code> if you have PHP installed.</p>
<p>and you'll be able to see the text on your localhost:8000 webpage.</p>
<p>Happy litting.</p>
]]></content:encoded></item></channel></rss>