Monday, September 25, 2017

Sharing React Components Easily with Bit

This is the age of components. Frameworks built for UI components, such as React, enable us to split our UI into individual, reusable pieces that can be worked with in isolation.

In many ways, React components are not that different from other encapsulated code functionalities. They can get defined inputs (usually “props” or ES6 classes) and return UI-oriented elements that will appear as part of your UI.

Individual components are often used across different parts of our UI. The problem is, organizing and sharing our components across teams and applications often presents a real challenge. Sharing components can give us fast and simple access to components written and used by our team, and help make sure our codebase is made of nothing but what we actually need.

Bit is an open-source project that enables us to instantly share components from our existing source code with our team, and use them across our different projects without changing our source code, file structure or the tools we work with. Let’s see how.

Managing code components with Bit

Sharing Components: It’s Not That Simple

Three major issues stand in the way of easily organizing and sharing source-code components: discoverability, maintainability, and the overhead of sharing.

Discoverability is a major issue. A developer working on my team on or a different team has no way of easily discovering and browsing the components available throughout our source code when choosing, upgrading or installing the ones they need with the tools of their choice.

Maintainability is another problem while sharing common components across different parts of our application. Maintainability problems range from simple duplications, to the complexity of maintaining multiple repos and packages and keeping control over the dependency chain. This can quickly get out of hand.

Up until now, sharing our components was a bit of a challenge. It forced us to either duplicate code or invest a lot of time and effort maintaining large packages --- which also weigh down our build and install time.

When trying to find and use an individual React component (Slider, Spinner etc.) we usually ended up installing vast static libraries containing a whole bunch of stuff we didn’t need. It also made it next to impossible to discover or use individual components created by our team or the community.

In some ways, this is very much like using a CD-ROM just to listen to a single song. Using Bit, we can easily share any set of components, making them individually available to anyone on our team.

Sharing Components with Bit

If you haven't heard of Bit’s latest release, it's an open-source project that allows us to share components from our existing source code with our team and across projects.

By decoupling the representation of components from our actual file structure, Bit tracks the components within our source code and enables us to quickly turn any file or subset of files into a reusable component. Using simple glob patterns (see below), the components within an entire library or project can be instantly shared without changing our source code itself or our file structure.

Any component (installed with Bit, and very soon with NPM or Yarn) can be individually shared, discovered and used in any application or project. It can also be modified and updated from any project environment, choosing if and how to let our friends update our components from their own projects (and vice versa).

A Netflix hero banner, showing featured content

Components can be grouped together into “Scopes”, which are collections that can be thought of as “playlists” of individual components sharing common attributes. When using the free Bit community site, each component is presented along with its rendered visuals, test results, semi-automatically generated docs and more.

Regardless of the tools we use to install our components, we can gain full control over our dependency graph and get a clear picture of the components used across our projects. Sharing code can also help keep our UI aligned with our design principles, as we can avoid changes when implementing the same components again and again in different projects.

Let’s try an example.

Continue reading %Sharing React Components Easily with Bit%


by Jonathan Saring via SitePoint

How could a four-day week change your life? [infographic]

So many of us talk of working a four-day week, but is it just wishful thinking, or could it truly be a reality one day? Countries such as Germany, the Netherlands and Denmark are closest to achieving this dream, so what’s the possibility of other countries including the UK following...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World

Writing Server-rendered React Apps with Next.js

The dust has settled a bit as far as the JavaScript front-end ecosystem is considered. React has arguably the biggest mindshare at this point, but has a lot of bells and whistles you need to get comfortable with. Vue offers a considerably simpler alternative. And then there’s Angular/Ember which, while still popular, are not the first recommendations for starting a new project.

So, while React is the most popular option, it still requires lot of tooling to write production-ready apps. Create React App solves many of the pain points of starting, but the jury is still out on how long you can go without ejecting. And when you start looking into the current best practices around front-end, single-page applications (SPAs) --- like server-side rendering, code splitting, and CSS-in-JS --- it’s a lot to find your way through.

That’s where Next comes in.

Why Next?

Next provides a simple yet customizable solution to building production-ready SPAs. Remember how web apps were built in PHP (before “web apps” was even a term)? You create some files in a directory, write your script and you're good to deploy. That's the kind of simplicity Next aims at, for the JavaScript ecosystem.

Next is not a brand new framework per se. It fully embraces React, but provides a framework on top of that for building SPAs while following best practices. You still write React components. Anything you can do with Next, you can do with a combination of React, Webpack, Babel, one of 17 CSS-in-JS alternatives, lazy imports and what not. But while building with Next, you aren't thinking about which CSS-in-JS alternative to use, or how to set up Hot Module Replacement (HMR), or which of many routing options to choose. You're just using Next --- and it just works.

I'd like to think I know a thing or two about JavaScript, but Next.JS saves me an ENORMOUS amount of time. --- Eric Elliott

Getting Started

Next requires minimal setup. This gets you all the dependencies you need for starting:

$ npm install next react react-dom --save

Create a directory for your app, and inside that create a directory called pages. The file system is the API. Every .js file becomes a route that gets automatically processed and rendered.

Create a file ./pages/index.js inside your project with these contents:

export default () => (
  <div>Hello, Next!</div>
)

Populate package.json inside your project with this:

{
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"
  }
}

Then just run npm run dev in the root directory of your project. Go to http://localhost:3000 and you should be able to see your app, running in all its glory!

Just with this much you get:

  • automatic transpilation and bundling (with Webpack and Babel)
  • Hot Module Replacement
  • server-side rendering of ./pages
  • static file serving: ./static/ is mapped to /static/.

Good luck doing that with Vanilla React with this much setup!

Continue reading %Writing Server-rendered React Apps with Next.js%


by Jatin Shridhar via SitePoint

10 Work at Home Jobs That Pay Weekly or More Often

If you work from home, you’ve probably noticed that most work-at-home opportunities pay only once or twice a month. This frequently isn’t enough for someone with immediate financial needs. While you can learn the nitty-gritties of the Internet world, sometimes you need a quicker solution to your...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World

6 Things That Make Yarn the Best JavaScript Package Manager

Yarn is an open-source npm client that was developed at Facebook and improves on many aspects of the standard npm client. In this tutorial, I'll focus on the top six features that make Yarn awesome:

  1. Speed
  2. Robust Installs
  3. License Checks
  4. Compatibility with npm and Bower
  5. Multiple Registries
  6. Emojis

1. Speed

One of Yarn's claims to fame is its speed compared to the standard npm client. But how fast is it? In a recent benchmark, Yarn was two to three times faster than npm. The benchmark timed the installation of React, Angular 2, and Ember. This is a pretty good test for a package manager as each of these frameworks pulls a bunch of dependencies and represents a major share of the dependencies of a real-world web application.  

Let's add another data point and check for ourselves by installing a create-react-app using both yarn and npm. Here is the installation using yarn:

Here is the installation using npm:

Yep. This definitely corroborates other reports about a significant speed advantage to yarn. Yarn installed in 2.59 seconds, while npm took 9.422 seconds. Yarn was 3.63X faster!

2. Robust Installs

Yarn also boasts more robust installs than npm. What makes an install flaky? If subsequent installs fail or produce a different outcome then an install is flaky. There are two main causes:

  1. Transient network issues might cause fetching packages from the registry to fail.
  2. New releases of packages might result in incompatible and breaking changes.

Yarn addresses both concerns. 

Offline Cache

Yarn uses a global offline cache to store packages you've installed once, so new installations use the cached version and avoid flakiness due to intermittent network failures. You can find where your yarn cache is by typing:

Here are the first five packages in my offline cache:

Yarn can go even further and have a full offline mirror that will work across upgrades of the yarn itself.

The yarn.lock File

The yarn.lock file is updated whenever you add or upgrade a version. It essentially pins down the exact version of each package that may be specified in package.json using partial versioning (e.g. just major and minor) and its dependencies. 

Here is the beginning of a typical yarn.lock file. You can see the version as specified in package.json like "abbrev@1" and the pinned version "1.1.0".

But Why?

Yarn also gives you the yarn why command to explain why a particular package is installed in your project:

3. License Checks

Some projects need to abide by certain licensing requirements or just produce a report for internal or external purposes. Yarn makes it really easy with the yarn licenses ls command. It produces a compact report that includes the fully qualified package name, its URL, and the license. Here is an example:

Yarn can even generate a disclaimer for you with yarn licenses generate-disclaimer. The result is some text with a disclaimer message and text for each package in your application. Here is a sample from the disclaimer generated for my test project:

4. Compatibility With npm and Bower

Yarn is fully compatible with npm as it is just a different client that works with npm registries. Very early it supported Bower, but then shortly after a decision was made to drop Bower support. 

The main reason was that the Bower support didn't work very well and emptied the bower_components directory or didn't fetch any bower packages on a fresh project. But another reason is that the Yarn team didn't want to encourage fragmentation in the package management arena and instead preferred for everyone to switch to npm.

If you are invested in Bower and don't want to migrate right now, you can still use Yarn, but add the following snippet to your package.json file:

5. Multiple Registries

Yarn can work with multiple registry types. By default, if you just add a package, it will use its npm registry (which is not the standard npm registry). But it can also add packages from files, remote tarballs, or remote git repositories.

To see the current configured npm registry:

To set a different registry type: yarn config set registry <registry url>

To add packages from different locations, use the following add commands:

6. Emojis FTW!

Some people like emojis, and some people don't. Yarn originally displayed emojis automatically, but only on Mac OS X. It caught fire from both camps: the emoji haters were upset that their console on Mac OS X was cluttered with emojis, and the emoji lovers were upset that they didn't have emojis on Windows and Linux. 

Now, emojis are not displayed on macOS by default, and you can enable emojis with the --emoji flag:

Conclusion

Yarn is the best JavaScript package manager. It is compatible with npm, but much, much faster. It addresses serious problems for large-scale projects with flaky installation, it supports multiple types of registries, and it has emojis to boot. 

JavaScript, though not without its learning curves, has plenty of libraries and frameworks to keep you busy, as you can see. If you’re looking for additional resources to study or to use in your work, check out what we have available in the Envato marketplace.

The JavaScript community is overall very positive, and there is a lot of momentum behind Yarn. It has already addressed some issues such as redundant Bower support and emojis by default. Migrating to Yarn from npm is very easy. I highly recommend that you give a try.


by Gigi Sayfan via Envato Tuts+ Code

TypeScript for Beginners, Part 1: Getting Started

10 Movies Every Social Media Geek Will Love

Movies give us moments of drama, comedy, suspense, horror, and many different feelings in between. They run the emotional gamut — much like our experiences on social media. If you love spending time on the diverse platforms of social media, you’re going to love these films. Here are 10 movies...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World