Let's Write a JavaScript Library in ES6 using Webpack and Babel

Let's Write a JavaScript Library in ES6 using Webpack and Babel

Writing your own webpack configuration for a JavaScript library in ES6 and learn webpack along the way. In this blog we will learn how to create our own webpack configuration to bundle a small JavaScript utility library using webpack and babel. Let's create the source code for our library. For that we will be create two utility functions into two separate files in our source folder.

In this blog we will learn how to create our own webpack configuration to bundle a small JavaScript utility library using webpack and babel.

Let's create the source code for our library. For that we will be create two utility functions into two separate files in our source folder.

Step 1 : Create a directory demo and run following command in it.

  $ npm init -y

The above command will create a package.json in your project root. I am using a --y to initialize it with default options.

Directory Structure

demo
  |-- src/
  |-- package.json

Step 2: Adding our source code.

Let's add our source code into src directory:

src
 |--index.js
 |--capital.js
 |--addDOMContent.js

Our utility library contains two functions capital, to capitalize a string and addDOMContent, to add content to a web page, each in it's own module.

capital.js

function capital(string) {
  return (capitalizedString =
    string.substring(0, 1).toUpperCase() + string.substring(1))
}

export default capital

addDOMContent.js

function addDOMContent(content) {
  const node = document.createElement("h1")
  node.innerText = content
  document.body.appendChild(node)
}

export default addDOMContent

Inside our index.js, we will import these two functions.

index.js

import capital from "./capital"
import addDOMContent from "./addDOMContent"

export { capital, addDOMContent }

So far we got the source code ready but we still need to bundle it so that the browsers can understand and oh boy!, we need to support some older browsers too. Anyway, being responsible developers we are going to do that.

javascript es6 programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Taking functional programming to JavaScript objects

Functional programming seems to be where the hype is nowadays — And for good reason; It can drastically reduce the amount of boilerplate code that you have to write in comparison to imperative programming which allows you to improve the readability of your code and consequently, maintainability.

JavaScript: Template Literals (ES6)

Beginners Guide to Template Literals in JavaScript. JavaScript: Template Literals (ES6). Inthis article, I will discuss another powerful feature of modern JavaScript; template literals, which was released in the 2015 edition of the ECMAScript specification and was referred to as template strings prior to ES6. Similar to string interpolation features in other languages like Ruby and Phyton, template literals provide syntactic sugar that allows us to construct strings more quickly. Before template literals were added to the JavaScript language in ES6, single quotes (‘ ’) and double quotes (“ ”) were the only ways to make a string. Template literals offer another way to declare strings that makes use of backtick (` `) character.

Learn Javascript | Intro to programming

Learn Javascipt programming language. This is introduction to programming and Javascript. For more programming videos: ♥ Subscribe: https://www.youtube.com/c...

ES6 JavaScript— “this” keyword

Every function created with the function keyword or with the method shorthand has its own “this”, and it generally binds to “who” called it. Arrow functions don’t bind “this” to anything, the “this” inside depends on the surrounding context

Object Oriented Programming JavaScript - ES6

Learning OOP JavaScript-ES6 will significantly help you become a better developer. Concepts such as the class declaration, constructors, getter and setter, methods, static properties, static method, and inheritance can definitely help you leverage those concepts when working with other JavaScript frameworks or libraries.