<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Serhii Havrylenko on Medium]]></title>
        <description><![CDATA[Stories by Serhii Havrylenko on Medium]]></description>
        <link>https://medium.com/@serhiihavrylenko?source=rss-294219f78e61------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ohsLiJrSCSfjqk4a.</url>
            <title>Stories by Serhii Havrylenko on Medium</title>
            <link>https://medium.com/@serhiihavrylenko?source=rss-294219f78e61------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 22:42:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@serhiihavrylenko/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Monorepo setup with Lerna, TypeScript, Babel 7 and other — Part 4]]></title>
            <link>https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-4-9a468e45c1c3?source=rss-294219f78e61------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a468e45c1c3</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[babel]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[monorepo]]></category>
            <dc:creator><![CDATA[Serhii Havrylenko]]></dc:creator>
            <pubDate>Sun, 28 Oct 2018 15:37:53 GMT</pubDate>
            <atom:updated>2018-10-28T15:56:50.745Z</atom:updated>
            <content:encoded><![CDATA[<h3>Monorepo setup with Lerna, TypeScript, Babel 7 and other — Part 4</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cnUMlHpQOyl7znF7wlNKEQ.jpeg" /></figure><h3>Series</h3><ol><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-1-ac60eeccba5f">Basic monorepo setup with Lerna, Babel and TypeScript</a></li><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-2-8b4801076647">Tslint, Stylelint, Jest, Storybook and Conventional Commit in the monorepo</a></li><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-3-f9943b290fa4">First package development and publishing</a></li><li>Multiple packages and magic in TypeScript monorepo configuration</li></ol><p>Basing on our previous stories we have monorepo ready and one simple component published as separate package. It’s time to create second package which is dependant on our first one and check how integration between them looks like and how it’s simple to write code in the monorepo packages instead of package per repository approach.</p><p>As everyone knows, the main goal of the monorepo is to have multiple packages inside one repository for easily solving dependencies between them and simple release process.</p><p>Let’s create second package which is dependant on our @taxi/input and check how our tools works with it.</p><h3>Development</h3><p>Just create second package in the same way as the first one. In our example it would be @taxi/login-form with next content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/936d036593baa1efaa44bd62fb3d0a17/href">https://medium.com/media/936d036593baa1efaa44bd62fb3d0a17/href</a></iframe><p>Now we could add @taxi/input to the list of dependencies. This could be achieved easily with next command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99425a3b193fce66acf9d05539621622/href">https://medium.com/media/99425a3b193fce66acf9d05539621622/href</a></iframe><p>As soon as we have it, it’s time to run tsc and check what typescript thinks about our code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/759301b53f1b1b0405d55db88ec6a12b/href">https://medium.com/media/759301b53f1b1b0405d55db88ec6a12b/href</a></iframe><p>It happens because in the package.json file for @taxi/input package we have &quot;main&quot;: &quot;dist/index.js&quot; which is pointing to the build version of the package and we have not run build command. So, let&#39;s build our packages and check tsc again:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6e884ae587ec52432eb8894cd814fb9/href">https://medium.com/media/e6e884ae587ec52432eb8894cd814fb9/href</a></iframe><p>Now typescript could find our package, however it doesn&#39;t know anything about typings for that module. It happens because we built our package with babel and it cannot create declaration files.</p><h4>Declarations generation</h4><p>Let’s configure tsc to generate types declarations for our modules. As a first step, we have to move login-form package to the different folder (we would need to have only compilable with tsc packages in the packages directory).</p><p>We would need:</p><p>tsconfig.build.json in the root of monorepo with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b579c7db8e728d2e84fa737458f1604d/href">https://medium.com/media/b579c7db8e728d2e84fa737458f1604d/href</a></iframe><p>Where we specify that we need to emit declarations only and exclude test, stories and dist folder.</p><p>Next we would need tsconfig.build.json inside the each of our packages with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5ad1bf81e931a7ab326a012bcbd5886/href">https://medium.com/media/c5ad1bf81e931a7ab326a012bcbd5886/href</a></iframe><p>Here we would specify from where we would like to take files for generating declarations and where we would like to put them.</p><p>Now we would need script inside the root of monorepo for generating declarations:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3edc85f0cd7293d3c3850dea7c375b1/href">https://medium.com/media/a3edc85f0cd7293d3c3850dea7c375b1/href</a></iframe><p>Time to test it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb6fe7636fce9ba1319d4c2045b1e912/href">https://medium.com/media/cb6fe7636fce9ba1319d4c2045b1e912/href</a></iframe><p>As the result we should have *.d.ts files generated in the dist folder:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d26c57fc75c31d270b193be34f6f5d02/href">https://medium.com/media/d26c57fc75c31d270b193be34f6f5d02/href</a></iframe><p>Now we could check that @taxi/login-form works as well, just return it back to the packages directory and check that tsc works now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eefe41acfc9ada9cb6f20a81dc5138bd/href">https://medium.com/media/eefe41acfc9ada9cb6f20a81dc5138bd/href</a></iframe><h4>Tslint</h4><p>As soon as we generate declarations and run tslint we would have errors like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd2c87315b08639608870ab3e2941107/href">https://medium.com/media/bd2c87315b08639608870ab3e2941107/href</a></iframe><p>It happens because tslint tries to validate *d.ts files as well. Let&#39;s add the to the ignore. With latest version of tslint we could do it in configuration file, so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d50bfb469fe6129ee322e7597979ea6/href">https://medium.com/media/0d50bfb469fe6129ee322e7597979ea6/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d6e2f449478ea9633c1de7b4822fa2f/href">https://medium.com/media/8d6e2f449478ea9633c1de7b4822fa2f/href</a></iframe><p>Now if we run yarn lint:ts we won&#39;t have any errors related to .d.ts files.</p><h4>Drawbacks</h4><p>We are able to generate declarations for all packages, run storybook and tests, however, as soon as we change @taxi/input we would not see any changes in the storybook for @taxi/login-form because we still would use previously built version. To see these changes we would need to rebuild packages one more time. The same applies to the jest runs.</p><p>This approach looks not so cool if each change in one of the packages would require constant rebuild. Lucky we have better option how to fix this issues. More details in the next paragraphs.</p><h3>Typescript</h3><p>Firstly, let’s remove all dist folders inside our packages to have clean code only. On this stage tsc should still fail. Now we could extend tsconfig.json with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf79a3da86ba220736e7fb36c55172a3/href">https://medium.com/media/cf79a3da86ba220736e7fb36c55172a3/href</a></iframe><p>&quot;@taxi/*&quot;: [&quot;packages/*/src&quot;] tells to tsc where to search for @taxi/ packages. In our case we would like to search them in packages/*/src folders as there we have our source code.</p><p>Now we could run tsc and it would finish successfully without any errors like we had earlier.</p><h3>Storybook</h3><p>As storybook is using webpack with awesome-typescript-loader and babel integration, as soon as we start storybook we would get next errors:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f2e5e47361cbbc1856230dbb2f8fc17/href">https://medium.com/media/0f2e5e47361cbbc1856230dbb2f8fc17/href</a></iframe><p>It happens even when tsc could find those modules. The reason of this is simple: tsc checks files, babel transforms them and tries to execute, and it doesn&#39;t know anything about @taxi/input as main still points to the dist folder.</p><p>However, we could override it with aliases for webpack. It could be done in two ways:</p><ol><li>Manual one, in this case we’ll have to define each package which we would like to have in webpack.config.resolve.alias. This approach is simple, but it introduces potential issues in the future when someone could forget about aliases or introduces wrong one.</li><li>Automated one with next info in the storybook/webpack.config.js</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a78be6838da3749d8b6d2f81fbc584d/href">https://medium.com/media/5a78be6838da3749d8b6d2f81fbc584d/href</a></iframe><p>This code is rather simple and self explanatory. The most important part is in reducer for building aliases:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f429fd5f4b2d2299fde97fd9830d30cb/href">https://medium.com/media/f429fd5f4b2d2299fde97fd9830d30cb/href</a></iframe><p>With this reducer we would have automatically generated list of our packages from packages directory.</p><p>Now we could start storybook and check that it works, moreover, if we change @taxi/input and check stories for @taxi/login-form they would have changes immediately without even building packages.</p><h3>Jest</h3><p>We have tsc and storybook working, however, if we run jest it would fail with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eaabdc1b4eb960f057dcf5a4f41726b7/href">https://medium.com/media/eaabdc1b4eb960f057dcf5a4f41726b7/href</a></iframe><p>It happens because jest doesn&#39;t tolerate tsconfig.json, webpack aliases or even babel-webpack-aliases. However, this problem could be solved with simple line in jest.config.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74e2bf7aa65b12dbd605989d08c0d876/href">https://medium.com/media/74e2bf7aa65b12dbd605989d08c0d876/href</a></iframe><p>&#39;@taxi/(.+)$&#39;: &#39;&lt;rootDir&gt;packages/$1/src will tell jest where to find source code for @taxi/ packages and again it points to the src instead of dist.</p><p>Now we could run yarn test and it will work as expected without even building packages.</p><h3>Final words</h3><p>The goal of the article was to configure monorepo with lerna, typescript, babel, tslint, stylelint, jest and semantic-release and it was achieved. We do have monorepo with all those tools in place and it&#39;s ready for real usage. Packages could be created, developed, tested, presented and published easily with one commands in the root of monorepo.</p><p>For those who doesn’t want to use babel, but pure typescript, changes would be extremely simple - just drop babel.config.js, change awesome-typescript-loader for using tsconfig.json instead of babel, ts-jest to use it as well and build command from babel to tsc (just replace emitDeclarationOnly to false in tsconfig.build.json) and that&#39;s it, typescript could be used without babel.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a468e45c1c3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monorepo setup with Lerna, TypeScript, Babel 7 and other — Part 3]]></title>
            <link>https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-3-f9943b290fa4?source=rss-294219f78e61------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9943b290fa4</guid>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[monorepo]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[lerna]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Serhii Havrylenko]]></dc:creator>
            <pubDate>Sun, 28 Oct 2018 15:26:44 GMT</pubDate>
            <atom:updated>2020-04-28T13:51:54.668Z</atom:updated>
            <content:encoded><![CDATA[<h3>Monorepo setup with Lerna, TypeScript, Babel 7 and other — Part 3</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wvZXi5PW_2_00KF1kFWKIw.jpeg" /></figure><h3>Series</h3><ol><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-1-ac60eeccba5f">Basic monorepo setup with Lerna, Babel and TypeScript</a></li><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-2-8b4801076647">Tslint, Stylelint, Jest, Storybook and Conventional Commit in the monorepo</a></li><li>First package development and publishing</li><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-4-9a468e45c1c3">Multiple packages and magic in TypeScript monorepo configuration</a></li></ol><p>In the previous series we have configured monorepo with Lerna, Babel 7, Typescript for building and publishing packages, Jest for testing our code, Tslint and Stylelint for code quality checking and Storybook for components presenting. Now it’s time to create first simple package and publish it to the NPM registry.</p><h3>First package</h3><p>Let’s init first simple package which will be just Input with optional label for it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3a46336b135a022ddabcfc7844c8b74/href">https://medium.com/media/d3a46336b135a022ddabcfc7844c8b74/href</a></iframe><p>and add react and styled-components as peerDependencies:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e51e918e516f62caa31fec629cd69e1/href">https://medium.com/media/3e51e918e516f62caa31fec629cd69e1/href</a></iframe><h3>Development</h3><p>Let’s create simple input with optional label:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d80960ea66020bbb5f1e31b0b7c9c03/href">https://medium.com/media/8d80960ea66020bbb5f1e31b0b7c9c03/href</a></iframe><h3>Storybook</h3><p>storybook is configured, so it&#39;s time to create first story and test first package as well as storybook and typescript integration. Just create Input.story.tsx inside input package.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6095ad103b08655f8460b4bf607a038f/href">https://medium.com/media/6095ad103b08655f8460b4bf607a038f/href</a></iframe><p>and run yarn storybook. When the build process is finished, storybook will be accessible under <a href="http://localhost:6006/">http://localhost:6006</a></p><h3>Testing</h3><p>jest is configured and ready to be used. Let&#39;s create simple snapshot tests for our package:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ed4b56fcc279ec47f758c096b2aa2dc/href">https://medium.com/media/5ed4b56fcc279ec47f758c096b2aa2dc/href</a></iframe><p>All tests could be run with simple command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce38c4e21215b6d0ed04023b96591851/href">https://medium.com/media/ce38c4e21215b6d0ed04023b96591851/href</a></iframe><p>Now we have a chance to check how our integration of jest and jest-styled-components works. Just open created snapshot and check that classnames are replaced with c0, c1, etc:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b8bfe44e1ccfa26255a697aaa0b9085/href">https://medium.com/media/3b8bfe44e1ccfa26255a697aaa0b9085/href</a></iframe><h3>Build</h3><p>It’s time to build our package. Simply run</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4e635c344f7d99f06d1f59906bfc74a/href">https://medium.com/media/b4e635c344f7d99f06d1f59906bfc74a/href</a></iframe><p>and we should have packages/input/dist folder with all compiled files. The most important one is packages/input/dist/Input.js which has:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d50b16c989a099e84395bc7569dee4e/href">https://medium.com/media/7d50b16c989a099e84395bc7569dee4e/href</a></iframe><h3>Publish</h3><p>It’s time to publish our first package, just hit yarn release and answer Yes for publishing question. The result should be like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9688e85c614682da3b3f46c87a843377/href">https://medium.com/media/9688e85c614682da3b3f46c87a843377/href</a></iframe><p>Lerna says that package was published successfully. Now we could check it with yarn info or through web interface for our local npm registry (http://localhost:4873):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/21c2c5ac7de87a359be7ee4ec94c84ed/href">https://medium.com/media/21c2c5ac7de87a359be7ee4ec94c84ed/href</a></iframe><p>Moreover, lerna should create proper tags in our repository associated with released packages and versions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fbb3eb868f3b82b8ee7acf78bfbb121/href">https://medium.com/media/9fbb3eb868f3b82b8ee7acf78bfbb121/href</a></iframe><p>Last, but not least, let’s check that proper Changelog was created for our package:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4037778d307fe971e8075fc25c70dbeb/href">https://medium.com/media/4037778d307fe971e8075fc25c70dbeb/href</a></iframe><p>Now we have first package successfully released, what’s left? Second package which is dependant on our @taxi/input package is left. Integration with lerna would stay the same, simple as it is, however, it would requires some magic for making storybook, jest and tslint works with dependent packages and proper modules resolutions without rebuilding them all the time. Plus, as a bonus we&#39;ll generate types declarations for our packages and include them as a part of final build.</p><p>More details is <a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-4-9a468e45c1c3">HERE</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9943b290fa4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monorepo setup with Lerna, TypeScript, Babel 7 and other — Part 2]]></title>
            <link>https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-2-8b4801076647?source=rss-294219f78e61------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b4801076647</guid>
            <category><![CDATA[tslint]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[storybook]]></category>
            <category><![CDATA[monorepo]]></category>
            <dc:creator><![CDATA[Serhii Havrylenko]]></dc:creator>
            <pubDate>Sun, 28 Oct 2018 15:16:21 GMT</pubDate>
            <atom:updated>2018-12-07T15:07:15.811Z</atom:updated>
            <content:encoded><![CDATA[<h3>Monorepo setup with Lerna, TypeScript, Babel 7 and other — Part 2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I3PZSedrL2o1uut6a9hy4A.png" /></figure><h3>Series</h3><ol><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-1-ac60eeccba5f">Basic monorepo setup with Lerna, Babel and TypeScript</a></li><li>Tslint, Stylelint, Jest, Storybook and Conventional Commit in the monorepo</li><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-3-f9943b290fa4">First package development and publishing</a></li><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-4-9a468e45c1c3">Multiple packages and magic in TypeScript monorepo configuration</a></li></ol><h3>Tslint, Stylelint, Jest, Storybook and Conventional Commit in the monorepo</h3><p>In the first part of the article we configured basic setup of the monorepo with Typescript and Babel for building packages. Let’s proceed with Tslint, Jest, Storybook and others.</p><h3>Static code analyser</h3><p>Having typescript for types checking could protect us from creating plenty of mistakes and save a lot of time for writing basic unit tests related to incorrect input data. However, it won’t protect us from writing over-complicated, unreadable, or even hacky code. Even more, it won’t protect us from using incorrect or unsupported CSS rules, would it be CSS, SCSS or CSS-in-JS.</p><h4>Tslint</h4><p>The easiest solution for automating such checks is using tslint for static code analyzing.</p><p>Let’s install it as devDependency in the root of our monorepo:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6cee24779b0029ca6e6ed9df4cfb8ae7/href">https://medium.com/media/6cee24779b0029ca6e6ed9df4cfb8ae7/href</a></iframe><p>and create simple config file for it tslint.json:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8247205c7aa606f47f4ef50722cf7c13/href">https://medium.com/media/8247205c7aa606f47f4ef50722cf7c13/href</a></iframe><p>and new script in package.json for linting *.ts files:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/147d0171a82d27b203d4acaaee45aaf0/href">https://medium.com/media/147d0171a82d27b203d4acaaee45aaf0/href</a></iframe><p>Now static code analyzer could be run with simple yarn lint:ts command.</p><h4>Stylelint</h4><p>Let’s use stylelint for improving our CSS styles quality and readability. It could be easily used for analyzing CSS-in-JS as well as with simple CSS or SCSS files.</p><p>Installation and configuration process as simple as with tslint:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a0d0d8664be156fe8be1c9133fe94dc/href">https://medium.com/media/4a0d0d8664be156fe8be1c9133fe94dc/href</a></iframe><p>Create simple config file for it - .stylelintrc:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67eae5e8fc28cb06de9d88068bf3ea7b/href">https://medium.com/media/67eae5e8fc28cb06de9d88068bf3ea7b/href</a></iframe><p>and new script in package.json for linting *.ts files:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91e54fd4d2d25ec379f5e562aaaea40a/href">https://medium.com/media/91e54fd4d2d25ec379f5e562aaaea40a/href</a></iframe><p>Now static code analyzer could be run with simple yarn lint:css command.</p><h4>All together</h4><p>For now we have lint:ts for checking typescript code quality and lint:css for CSS, however, they are still separated commands and it would be uncomfortable to run them separately all the time. Let&#39;s group them in one unified lint command and run it with npm-run-al:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0ef1c3f590fc8ed30c6adea5038a7f7/href">https://medium.com/media/a0ef1c3f590fc8ed30c6adea5038a7f7/href</a></iframe><p>and new script in the root package.json:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6e4e629bcea8df26babdea1476fc97b/href">https://medium.com/media/e6e4e629bcea8df26babdea1476fc97b/href</a></iframe><p><strong>Note:</strong> run-p -c allows to run all lint:* commands even if one of them failed. It&#39;s useful in case of separated static code analyzer steps, as after one run we have output from tslint and stylelint, instead of only first failed.</p><h3>Code formatting</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eu_5isN2vzj8tM47.png" /></figure><p>For now we have typescript for static types checking, tslint and stylelint for static code analyzing. Still, we could write unreadable or not well formatted code. We could avoid all issues relate to code formatting with prettier. It will automatically format our code according to predefined standards. Moreover, it will fix some issues reported by tslint.</p><p>As usual, installation and configuration is very simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b649a2fa2899f811004cde4f26e44753/href">https://medium.com/media/b649a2fa2899f811004cde4f26e44753/href</a></iframe><p>Next is needed is .prettierrc:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa21f0c00bbf7b57d1686c2ee3e41457/href">https://medium.com/media/aa21f0c00bbf7b57d1686c2ee3e41457/href</a></iframe><p>and integration with tslint, as both of the tools have common rules (like tabWidth, trailingComma, etc). Next lines should be added to the tslint.json to make it work with prettier:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4355ed94595f6d1ab09255e5619889f4/href">https://medium.com/media/4355ed94595f6d1ab09255e5619889f4/href</a></iframe><p>Important note is quotemark rule. Because of jsx usage we have to override default recommended rules which requires to have single quotemark everywhere.</p><p>Last but not least, let’s add script to the root package.json for automatic code formatting based on defined above rules:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a3f6663b1d0bf4d39e72dd5b7c399c3/href">https://medium.com/media/2a3f6663b1d0bf4d39e72dd5b7c399c3/href</a></iframe><h3>Testing</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*deAZL6wg1o1DZyX3" /></figure><p>For now we have TypeScript for checking types, tslint and stylelint for static code quality analyzing. Last, but not least part is testing. Let&#39;s use jest as test runner and test assertion tool. It has the best support for react, including snapshot testing, extensive mocking library, build-in coverage reporting and ability to run tests in different processes. ts-jest should be used for running typescript code. Installation is also quite simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fcd222299d95025d108f67280ec13ee/href">https://medium.com/media/9fcd222299d95025d108f67280ec13ee/href</a></iframe><p>jest could be configured in two ways:</p><ul><li>yarn jest --init and answer for all questions</li><li>manually create config file with minimum required configuration</li></ul><p>Here is basic setup from jest.config.js in the root of the monorepo</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a91f7c65910a8a5a459c8724bc78850b/href">https://medium.com/media/a91f7c65910a8a5a459c8724bc78850b/href</a></iframe><p>Notes:</p><ul><li>ts-jest could use babel config or pure typescript compiler. In our case we have babel configured, so it could be used with just 1 line in config:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8a395fbbfa6f38d58a6d20ae4277f43/href">https://medium.com/media/b8a395fbbfa6f38d58a6d20ae4277f43/href</a></iframe><ul><li>coverageThreshold is protecting us from writing not enough tests and having poor test coverage.</li></ul><p>As soon as we have basic config, it’s time to install enzyme (and all related libraries) which is extending support for react:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e340c6b4931be16b4d3dbcf47a560b7/href">https://medium.com/media/9e340c6b4931be16b4d3dbcf47a560b7/href</a></iframe><p>Add setupTestFrameworkScriptFile: &#39;&lt;rootDir&gt;jest/setupTests.ts&#39; into jest.config.js file and proper setup for enzyme into jest/setupTests.ts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e511a506c8b37cbc08b1f3191e54a394/href">https://medium.com/media/e511a506c8b37cbc08b1f3191e54a394/href</a></iframe><p>Now jest is able to render react components, however, it will serialize snapshots as pure objects and we want to have it serialized as HTML markup. We could achieve it with proper serializer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5e5a2ad891f34c8c23e42a1a5bf5530/href">https://medium.com/media/a5e5a2ad891f34c8c23e42a1a5bf5530/href</a></iframe><p>and snapshotSerializers: [&#39;enzyme-to-json/serializer&#39;] in jest.config.js.</p><p>We are almost there, we are able to run tests and create proper snapshots. Nevertheless, we still have issues with styled-components - on each change in styles, styled-components is creating different class name. Based on it we&#39;ll have plenty of false negative tests fails just because of class name is changed. Let&#39;s fix it with proper tool</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/030f3ac54074cb4ae7d1d6c93c6abcf4/href">https://medium.com/media/030f3ac54074cb4ae7d1d6c93c6abcf4/href</a></iframe><p>import &#39;jest-styled-components&#39; should be added to the jest/setupTests.ts.</p><p>Just to sum up, jest.config.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/792d2300e38d39d0a8f863a21f1d8bdc/href">https://medium.com/media/792d2300e38d39d0a8f863a21f1d8bdc/href</a></iframe><p>That’s it, jest is configured and could be run. Let&#39;s add test to the root package.json scripts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96824aa70a7e8a270f2cc1213da008c4/href">https://medium.com/media/96824aa70a7e8a270f2cc1213da008c4/href</a></iframe><h3>Storybook</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Nr0BQuhG3DDE95bc" /></figure><p>We already able to write code in monorepo with typescript, analyze it with tslint and stylelint, test it with jest. However, we still cannot see how our components will look like and we cannot even debug it properly.</p><p>There are plenty of ways how to present react component. Let’s go with most famous one — storybook. It allows to present separate components and/or group of them as well as testing it in real browsers, and having documentation close to it.</p><h4>Installation</h4><p>Latest stable version of storybook@3.4.10 works with webpack@3, babel@^6 and typescript@^2.7. As we have latest @babel@^7 and typescript@^3 it&#39;s better to use next version of storybook which has the same set of dependencies as our monorepo, even if it&#39;s bleeding edge version.</p><p>If you do it for the first time, you should have @storybook@cli installed globally on your machine and init:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c09453c0df253975e25552528354892f/href">https://medium.com/media/c09453c0df253975e25552528354892f/href</a></iframe><p>It will automatically detect project type (react), installs all required packages and create basic configuration folder.</p><h4>Typescript</h4><p>Still, as we use typescript, we have to install typings for those packages, loader for wepback and needed peerDependencies:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cef67187db10cd853868fafbcb837d4b/href">https://medium.com/media/cef67187db10cd853868fafbcb837d4b/href</a></iframe><p>Storybook inits application as it is javascript based, as we have typescript everywhere, we have to change path resolution for stories in storybook/config.js file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83511bd9d31c86dc9e43a10bc85afbf9/href">https://medium.com/media/83511bd9d31c86dc9e43a10bc85afbf9/href</a></iframe><p>Last but not least part is webpack.config.js inside storybook folder, just create it with next content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a710200163a97c105a821650342b3bf5/href">https://medium.com/media/a710200163a97c105a821650342b3bf5/href</a></iframe><p>and associated configuration part in tsconfig.json for awesome-typescript-loader:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24f62d685442d125b7913ee3ec96ad66/href">https://medium.com/media/24f62d685442d125b7913ee3ec96ad66/href</a></iframe><p>Configuration part is ready, we could start storybook with yarn storybook command.</p><h4>Tslint</h4><p>As soon as we add storybook to our devDependencies and run yarn lint:ts we will get an error from tslint:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f030cf95554cd476c3a23c7116b74a9b/href">https://medium.com/media/f030cf95554cd476c3a23c7116b74a9b/href</a></iframe><p>The reason is obvious, we use package which is installed as devDependency in our source code (in the story file). Unfortunately there is no options like override for specific path or files. It could be done by splitting lint-ts command into two separated for production code (which will be shipped as packages) and for test code (storybook, tests, etc).</p><p>Let’s create config for tslint for production phase, called tslint.prod.json:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cf24536e310a3c6057adcb650f7dbfa/href">https://medium.com/media/3cf24536e310a3c6057adcb650f7dbfa/href</a></iframe><p>Another config for the test phase called tslint.test.json:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8adeeb0799646cc44e32f9d2542ec433/href">https://medium.com/media/8adeeb0799646cc44e32f9d2542ec433/href</a></iframe><p>&quot;no-implicit-dependencies&quot;: false into tslint.json to disable this rule by default. This one is need to fix issues with IDEs as by default they use tslint.json for all files, whether it test or production code.</p><p>Last, but not least, scripts in the root package.json have to be adjusted:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0819e3e80c05c224a7e7ce6cc8d489ab/href">https://medium.com/media/0819e3e80c05c224a7e7ce6cc8d489ab/href</a></iframe><h4>Addons</h4><p>Storybook allows to pass any props to the react component without rebuilding stories, just through UI interface. To do it, we have to add one more addons — <a href="https://github.com/storybooks/storybook/tree/master/addons/knobs">@storybook/addon-knobs</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be9b1cd92df0ab88d729e0f1d4502577/href">https://medium.com/media/be9b1cd92df0ab88d729e0f1d4502577/href</a></iframe><p><strong>NOTE</strong>: moment has to be installed because of wrong peerDependencies management on storybook and react-datetime level.</p><p>Next step is to add import &#39;@storybook/addon-knobs/register&#39;; to the storybook/addons.js and modify storybook/config.js to have global decorator for each story:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c95ff5269d7a7b2c63e1858be00f1d78/href">https://medium.com/media/c95ff5269d7a7b2c63e1858be00f1d78/href</a></iframe><p>Now knobs could be used in the stories.</p><h3>Final build</h3><p>As we already have build command as well as code formatting and static code analyzing tools in place, it’s time to use them all together in the build process:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa0eac63115915c83bdfc730c8389afc/href">https://medium.com/media/aa0eac63115915c83bdfc730c8389afc/href</a></iframe><p>As soon as we run yarn build command, yarn automatically will run tsc for type checks, tslint for code quality analyzing and test on each of packages in the monorepo.</p><p>If they succeed, build will proceed and prepare all packages for publishing.</p><h3>Committing your work</h3><p>Let’s use conventional commit for committing our work for having consistent commit messages across the monorepo and as a benefit, proper version creation per package basing on <a href="https://github.com/lerna/lerna/tree/master/commands/version#--conventional-commits">conventional-commit</a> approach.</p><p>Let’s install required packages:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7a994db427e5dab72591579eb567e34/href">https://medium.com/media/a7a994db427e5dab72591579eb567e34/href</a></iframe><p><a href="https://github.com/atlassian/cz-lerna-changelog">cz-lerna-changelog</a> should be installed with version ^2.0.0 as it supports latest lerna</p><p>Configuration is quite simple, just add next line to the root package.json file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ff1e8b3a632a0ad7434662633e46d55/href">https://medium.com/media/5ff1e8b3a632a0ad7434662633e46d55/href</a></iframe><p>and simple alias for commit command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cdf914a5bdf54a7c4d7913696cdb7f0/href">https://medium.com/media/3cdf914a5bdf54a7c4d7913696cdb7f0/href</a></iframe><p>Now it’s time to test it, just change something in one of the packages, stage changes with git and run yarn commit:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efd8833f7ca3c81c4755ec77c65dd7af/href">https://medium.com/media/efd8833f7ca3c81c4755ec77c65dd7af/href</a></iframe><h3>Package publishing</h3><p>As described earlier, lerna is used for publishing packages. It could be configured quite easily with next lines in the lerna.json:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba038acdbe91759b4ef9a27f13f1b7e5/href">https://medium.com/media/ba038acdbe91759b4ef9a27f13f1b7e5/href</a></iframe><p>The config is self descriptive, however, the most important parts are:</p><ul><li>registry - specifies where do we want to publish our packages</li><li>conventionalCommits - allows us to use conventional commits for determining new versions</li></ul><p>All other options could be found on the official lerna documentation.</p><p>Now we could add simple alias to the our scripts for having release command there:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e6ea52e67dd81246db9bf3fb7484f35/href">https://medium.com/media/4e6ea52e67dd81246db9bf3fb7484f35/href</a></iframe><p>That’s it. The key part is configured and ready to be used. Time to test it with real packages and we’ll do it in the next story <a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-3-f9943b290fa4">HERE</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b4801076647" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monorepo setup with Lerna, TypeScript, Babel 7 and other — Part 1]]></title>
            <link>https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-1-ac60eeccba5f?source=rss-294219f78e61------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac60eeccba5f</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[babel]]></category>
            <category><![CDATA[lerna]]></category>
            <category><![CDATA[monorepo]]></category>
            <category><![CDATA[storybook]]></category>
            <dc:creator><![CDATA[Serhii Havrylenko]]></dc:creator>
            <pubDate>Sun, 28 Oct 2018 13:42:59 GMT</pubDate>
            <atom:updated>2018-12-20T05:49:37.609Z</atom:updated>
            <content:encoded><![CDATA[<h3>Monorepo setup with Lerna, TypeScript, Babel 7 and other — Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UbI6B3Of2z5tJl5DJnMS0Q.jpeg" /></figure><h3>Series</h3><ol><li>Basic monorepo setup with Lerna, Babel and TypeScript</li><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-2-8b4801076647">Tslint, Stylelint, Jest, Storybook and Conventional Commit in the monorepo</a></li><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-3-f9943b290fa4">First package development and publishing</a></li><li><a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-4-9a468e45c1c3">Multiple packages and magic in TypeScript monorepo configuration</a></li></ol><h3>Basic monorepo setup with Lerna, Babel and TypeScript</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SESN2bGmLvMUQ6_yqwX6_w.jpeg" /></figure><p>Everyone is tired with creating templates for each type of NPM package with pre-configured skeletons, managing different dependencies across own packages, linking packages for local development with instant packages rebuilding which is “wasting developers time”, writing proper configuration for linters, babel, typescript, etc, and adds a lot of repeatable steps for developers. The worst part appears when one of the key dependencies is getting breaking change upgrade, for instance babel - it produces plenty of duplicated changes in each of the package, massive amount of merge request to be reviewed and introduces high change to make a mistake (or even worse - forget about change) in one of the packages. Hopefully, it&#39;s year 2018 and JavaScript world has a solution for managing group of the packages in the one repository. This approach is called monorepo.</p><p>There are many of approaches how to implement monorepo — yarn workspaces, buck, pants, lerna, and others. In this article we&#39;ll cover lerna approach with yarn as package manager. Moreover, lerna integrates so greatly with yarn workspaces as it allows to use workspaces commands directly in the repository even without lerna.</p><h3>The goal</h3><p>The goal of this article is to create monorepo starter with Babel for building packages, TypeScript for having benefits of statically typed languages in the JavaScript world, static code analyzing tools (Tslint, Stylelint), Prettier for having automatically formated code (no more tabs or spaces holly wars), Storybook for developing components, and last, but not least — Lerna for publishing. All components will be written with React and StyledComponents for CSS-in-JS styling.</p><p>Let’s not waste time on long talks about what are those tools and why they are so important, and proceed to the real configuration and will see how everyone will benefit from each in the future.</p><p>For those who are impatient, <a href="https://github.com/serhii-havrylenko/monorepo-babel-ts-lerna-starter">here is the link to the repository</a> which contains whole set of results form this article — configured monorepo, ready to go and use.</p><h3>Pre-requirements</h3><p>Packages:</p><ul><li>NodeJS LTS</li><li><a href="https://yarnpkg.com/">yarn v1.5+</a></li><li><a href="https://lernajs.io/">Lerna v3+</a></li></ul><h3>Local NPM registry</h3><p>Local NPM registry will be used in whole article to avoid publishing to global registry plenty of test packages. There are plenty of ways how to set up private NPM repository. In this example <a href="https://github.com/verdaccio/verdaccio">Verdaccio</a> will be used for such purpose.</p><p>Configuration is simple and trivial:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61622c45e30acdf797e61d5d48ca02a1/href">https://medium.com/media/61622c45e30acdf797e61d5d48ca02a1/href</a></iframe><p>Now we could test how it works:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b815e1b25097a149873ee628d50beb0/href">https://medium.com/media/6b815e1b25097a149873ee628d50beb0/href</a></iframe><p>should still show package details and in console where we started verdaccio we should see incoming request</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa3caf7e5687fd7f911d0abefb38ea96/href">https://medium.com/media/aa3caf7e5687fd7f911d0abefb38ea96/href</a></iframe><p>Moreover, you could open <a href="http://localhost:4873/">http://localhost:4873/</a> for searching published packages.</p><h3>Getting started</h3><p>Time to start our configuration.</p><h3>Repository initialization</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*H2ojYR1E5GPVFFfP.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b6d73eb6ad69b07d30abc2c9f423458/href">https://medium.com/media/1b6d73eb6ad69b07d30abc2c9f423458/href</a></iframe><p>And you have initial structure of the monorepo created by Lerna.</p><p>A little bit of important theory about packages Versioning. This step is the most important one on the repository creation stage as it would impact how do we publish/tags our packages.</p><p>Lerna supports two types of packages versioning:</p><p>When <strong>exact</strong> type is chosen, Lerna will use the same version for all packages in monorepo. In case when <strong>independent</strong> version is selected, Lerna will release each package with independent version. More details about versioning is on <a href="https://github.com/lerna/lerna#how-it-works">official Lerna page</a></p><p>This article will consider only independent versioning for all packages as on the initial stage of packages development of some packages would have much more releases than others, and with independent versioning we would have only required packages released.</p><p>Going back to the real examples:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ae1cb6461508b962964c6e4e9e08f88/href">https://medium.com/media/8ae1cb6461508b962964c6e4e9e08f88/href</a></iframe><p>and new repository for packages with independent versioning is ready.</p><h3>Setting up package manager</h3><p>By default lerna is using NPM, however, it’s quite simple to set Yarn as package manager:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/432151eedd4a22a301cae00ce2d605da/href">https://medium.com/media/432151eedd4a22a301cae00ce2d605da/href</a></iframe><p>Moreover, with &quot;useWorkspaces&quot;: true we allow lerna to support yarn workspaces and with &quot;packages&quot;: [&quot;packages/*&quot;] we specify in which folder(s) we would have all our packages.</p><h3>TypeScript</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*RNig_TSslYZssvKO.png" /></figure><p>TypeScript could be used in two ways:</p><ul><li>native TypeScript with tsc</li><li><a href="https://babeljs.io/docs/en/babel-preset-typescript">@babel/preset-typescript</a></li></ul><p>Implementation from babel does not have all features from latest TypeScript (like const enum), however, in case of usage babel we do not miss all cool plugins and integration with all tools looks much easier.</p><p>In this article we would use only babel with typescript preset.</p><h4>Installation</h4><p>Just hit next command to install all required plugins and presets:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c80be79a86d29c17140f27ca9f6adc5/href">https://medium.com/media/4c80be79a86d29c17140f27ca9f6adc5/href</a></iframe><p>babel-core@7.0.0-bridge.0 should be noticed separately as it&#39;s needed for properly resolving babel-core packages for all libs which requires old version of babel and for avoiding duplicated packages installed and possible misusage of babel configuration. Next line have to be added to the root package.json:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5f5a465c2607ec4310e2e3343f27347/href">https://medium.com/media/b5f5a465c2607ec4310e2e3343f27347/href</a></iframe><h4>Configuration</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AhsI-u5yHZqtUWF1.png" /></figure><p>Configuration is quite simple and trivial — <a href="https://babeljs.io/docs/en/babel-preset-react">@babel/preset-react</a> and <a href="https://babeljs.io/docs/en/babel-preset-typescript">@babel/preset-typescript</a> plus simple config for <a href="https://babeljs.io/docs/en/babel-preset-env">@babel/preset-env</a>.</p><p>In Release candidate version, Babel team has removed support for stage-* and preset-201* packages, so it means that all actually used plugins should be set by users manually.</p><p>Apart from removing stage-* packages, Babel has changed approach for looking up config files - it looks for config in cwd till the first package.json. More details could be found in <a href="https://babeljs.io/docs/en/config-files">official Babel documentation</a></p><p>Let’s configure babel.config.js with next data:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fdc023fd209954e42e891b546b3e471/href">https://medium.com/media/4fdc023fd209954e42e891b546b3e471/href</a></iframe><h4>Building packages</h4><p>Due to changes for config lookup, build command in monorepo should have path to .babelrc or babel.config.js specified, or as <a href="https://babeljs.io/docs/en/options#rootmode">--root-mode option</a>. It could be done directly in build commands:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d250e32e65908ddf255591dec824867f/href">https://medium.com/media/d250e32e65908ddf255591dec824867f/href</a></iframe><p>1.build command in each package. In this case each of packages could be built independently without lerna. In this case build command is a little bit different:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1e3c6e0dba5b10cfe0bd2d7f3bd7500/href">https://medium.com/media/a1e3c6e0dba5b10cfe0bd2d7f3bd7500/href</a></iframe><p>2.Another way how to treat parent babel config is extends option provided by latest babel. The easiest approach for that is setting next lines in package.json on package level:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fbac95396be63a7685aa0430e784036/href">https://medium.com/media/1fbac95396be63a7685aa0430e784036/href</a></iframe><ul><li>In this case build command doesn’t require passing path to babel config and will be simplified:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f665da95a3c5e21d6944c0ff68199aeb/href">https://medium.com/media/f665da95a3c5e21d6944c0ff68199aeb/href</a></iframe><p>3.Last and easiest option is just add --root-mode=upward option to the build command which allows to resolve babel config upward from the current root. In this case build command looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c582f52f7c002769e5c8c725266ab1b9/href">https://medium.com/media/c582f52f7c002769e5c8c725266ab1b9/href</a></iframe><h4>Checking types</h4><p><a href="https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-typescript">@babel/plugin-transform-typescript</a> does not perform type-check for it’s input. However, it could be checked with native typescript compiler (tsc). It should be run with option noEmit for checking types only without emitting any code.</p><p>Let’s create minimal required tsconfig.json in the root of monorepo:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d0329e4bae2e168b283d81218bacd22/href">https://medium.com/media/0d0329e4bae2e168b283d81218bacd22/href</a></iframe><p>and put it in prebuild step to run type checks automatically prior each build</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33c37523ce76a8c0423fe6c17dc52867/href">https://medium.com/media/33c37523ce76a8c0423fe6c17dc52867/href</a></iframe><h3>Peer dependencies</h3><p>Lerna does not have ability to add peer dependency for packages. Nevertheless, it can be done with yarn workspaces — all we need is just add workspaces definition to the root package.json</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b86bbec1feb275f23129a0f552da6b98/href">https://medium.com/media/b86bbec1feb275f23129a0f552da6b98/href</a></iframe><p>In our case react, as well as styled-components, are defined as peerDependencies as all our packages will have them, however we don&#39;t want to have plenty of dependencies on each package installed separately.</p><p>As we decided to set react and styled-components as peer dependency, we still should have them, and associated types definitions for typescript, installed in our node_modules. So let&#39;s add them as devDependency to the root of monorepo:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce24103d684222873090213be71f68fb/href">https://medium.com/media/ce24103d684222873090213be71f68fb/href</a></iframe><p>Stay tuned, in the next post (<a href="https://medium.com/@serhiihavrylenko/monorepo-setup-with-lerna-typescript-babel-7-and-other-part-2-8b4801076647">HERE</a>) we would proceed with configuring tslint, storybook, prettier and other with real packages implementations and publishing them to the NPM registry.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac60eeccba5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>