Who created this Mantra?

According to Rishi Vashisht Mrityunjay Mahamantra has 33 alphabets, which reflect 33 divine powers of the God. Each letters from Mantra has 33 powers which reflect the devotee’s each part of the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Bundling Your JavaScript Library with Rollup

Though people usually use Rollup to bundle a library. It’s possible to bundle an application too.

To install Rollup globally, run the following command on your terminal:

You can now run the following command to print the Rollup CLI help:

To see Rollup in action, let’s create a simple ES module:

It is just a simple function that will print I eat something on the console or the terminal. Save the file as main.js. On the terminal type the following command to transform our ES module into a CommonJS module:

Along with the main.js file, we should now have the bundle.js file. If we open the bundle.js file, we'll see that our eat function is now using the CommonJS module.

If you’re familiar with Node.js, that’s how you usually export a module.

Rollup offers five output formats for your bundle:

Let’s bundle our main.js module into an AMD module:

Here’s the generated amd.js file looks like:

Now, let’s create the UMD version:

For UMD format, we also need to pass the --name option. It's the identifier that will be used to expose our module. We use the same name as our eat function. We can use a different name though.

If we open up the umd.js file, the generated code will look like this:

It slightly looks more complicated than the previous output formats. It’s an IIFE that will check if the current environment supports a CommonJS or an AMD module format. If the current environment supports a CommonJS module, it will export our eat function.

Or if the current environment supports an AMD module, it will register a factory function for our eat module.

Otherwise, it will add the eat function into the current this object:

For example, if we load our umd.js file in the browser, we can access our eat function from the window object:

Rollup can also output the same exact ES module:

If we check the generated es.js file, we'll find the identical code:

We use this option in case the users of our library want to use a tool that can leverage the ES module, like Webpack 2+.

The last format that we can generate with Rollup is IIFE (Immediately-Invoked Function Expression):

Just like the UMD format, we have to set the --name option. If we check the iife.js file, the eat function is wrapped within the IIFE block:

We can also use a configuration file to configure Rollup. Before continuing, let’s delete the generated files. Then move the main.js file into the src directory.

Create a new file named rollup.config.js and put the following configuration object:

Your current directory should now look like this:

Run the following command to bundle our main.js file:

We should now have the generated CommonJS file stored at: dist/cjs.js.

We can also assign an array to the output option to generate various output formats:

If you run the rollup -c command again, it will generate two bundles with different module format: cjs and umd.

It’s also possible to build bundles from multiple inputs:

One of the most amazing features on Rollup is its tree-shaking ability. RollUp can statically analyze our code and remove unused functions or modules from our bundle.

Within the src directory create a new file named food.js:

In this food module, we export two constants and two functions:

Let’s get back to our main.js file. Rename the eat function to eatFruit. Use the randomFruit function from our food module to generate a random fruit emoji:

Type the following command on the terminal to bundle our main.js module into a CommonJS format.

Let’s inspect the generated bundle.js file:

Even though Rollup is smart enough to exclude any unused functions or modules, it’s a good practice to explicitly export things that we only need. Let’s modify our main.js file to import the randomFruit function only:

Run the command to bundle our main.js file again, we should have the same exact output.

Suppose we use the default keyword to export the four items in the food.js module:

And the main.js file looks like this to accommodate that change:

If we bundle that main.js file through Rollup, the output will look like this:

There’s another three-shaking gotcha explained in the next section that you should be aware of.

We often need to pull a third-party library from NPM. For our example, let’s pull the lodash library. Within the project directory, type the following command to create an empty package.json file:

Then install the lodash-es package, it's the ES modules version for lodash:

Open up the src/food.js file. Let’s replace the getRandomNumberBetween with the lodash’s random function instead.

This time we’re going to use the Rollup configuration file. On your project directory, create a new file named rollup.config.js:

With this configuration file, we’re going to generate a bundle with CommonJS format. Let’s build our main.js file!

You’ll get a warning that it can’t resolve the lodash-es.

If you check the generated dist/cjs.js file, you’ll see that the lodash code is not included. It is simply require the lodash-es module.

The bundle will still work if the user of your library happens to have lodash-es installed on their project. Of course, it's not a reliable approach to build a library. So how can we solve this?

Next, we need to register this plugin within our rollup.config.js file:

Let’s bundle our main.js file once again:

There’s no warning message this time! If you check the generated dist/cjs.js file, you should find the lodash code in the bundle.

But wait, why are there so many lodash codes in our bundle file? Though we only import the random function. 🤔

Performing static analysis in a dynamic programming language like JavaScript is hard. Rollup has to be careful in removing any unused code to guarantee that the final bundle still works correctly.

If an imported module appears to have some side effects, Rollup needs to be conservative and includes those side-effects. Even though it may be a false positive, just like in our lodash case above.

In our lodash case, we can get around this by importing the submodule instead:

The lodash-es package is using the ES module format. So Rollup can process it out of the box. Unfortunately, the majority of packages published on NPM are using the CommonJS format.

Let’s replace the lodash-es package with its CommonJS counterpart: lodash and see if Rollup can handle it.

Don’t forget to update the import statement on the food.js module:

Let’s run the Rollup command to bundle our main.js file:

We’ll end up having an error message like this:

Let’s install this plugin:

Don’t forget to register it on rollup.config.js file:

Try to bundle our main.js file again. There should be no error this time.

Suppose that we decided to list lodash as our library’s peer dependencies. So instead of bundling lodash/random into our bundle, we’d ask the user to install lodash package separately.

We can achieve this by providing the external property to the rollup.config.js file:

The module listed on the external option must match exactly the same as how we imported it in our code. That’s why we use lodash/random instead of just lodash.

Let’s try to bundle our main.js file again:

If we peek at the generated dist/cjs.file, the lodash code is now excluded from our bundle:

If we choose to generate the umd or iife format, we have to specify the global option too. This option tells Rollup how to access that peer dependency.

Before continuing to the next section, let’s revert our change by removing lodash/random module as the peer dependency. Simply remove the external option from rollup.config.js file:

You can use Babel with Rollup to transform your ES 2015+ code into a backward compatible version of JavaScript. This way your library can also be used in some older browsers or Node versions.

First, let’s install @babel/core, @babel/preset-env, and rollup-plugin-babel:

With @babel/preset-env, we can easily target the minimum environments and don't need to choose Babel's plugins manually. The rollup-plugin-babel is the required plugin for Babel integration with Rollup.

Next, let’s create a .babelrc config file. Store it within the src directory instead of the project root directory. This way we can have different babel configurations for other parts, like tests.

We set the modules to false to prevent Babel from transforming any ES modules. Rollup already handles this for us. We also set the useBuiltIns to usage to import polyfills for the features that we only use.

We also set the minimum Node version to 4. If we don't set the targets option, it will work exactly the same as using @babel/preset-es2015, @babel/preset-es2016, and @babel/preset-es2017 together.

Lastly, we need to register the rollup-plugin-babel plugin within the rollup.config.js file:

If you check the generated dist/cjs.js file, you'll find that our ES 2015 code is transformed into the ES5 standard:

Now run the Rollup command to bundle our main.js file:

If we check our code in the generated bundle file, it should now back to the ES 2015 syntax again:

It’s also possible for us to use the .browserslist configuration file. First, let’s remove the targets option from the src/.babelrc file:

Next, create a new .browserslistrc file in the project root directory:

We will get a similar output if we run the Rollup command again.

Suppose that we want to generate a umd format. So it can be used in a browser too. Let's update our rollup.config.js file:

Let’s update our .browserslistrc file too:

The above query means that we want to support:

Run the Rollup command again:

So how can we use a different browserslist query for cjs and umd format? For the CommonJS format we want to use:

While for the UMD format, we’d like to use:

Note that on the cjs section, we pass babelrc: false to the babel plugin. This way we can override the configurations that are set on the src/.babelrc file.

Don’t forget to update the .browerslistrc too:

The umd format will still use the query set on the .browerslistrc. But the cjs format will now use the maintained node versions query instead. Run the bundle command again:

You should now find that the umd format are using ES 2015 syntax again.

We’ll keep the non-minified version of the umd bundle. So we just need to add a similar umd config but with the rollup-plugin-terser addition.

Once it’s added, let’s bundle our main.js file again:

We should now get a new bundle named umd.min.js. If you check this bundle, you'll see that the code is minified.

Let’s wrap this up by publishing our bundles to NPM. First, let’s install rollup as our dev dependency:

All this time we use the globally installed rollup to bundle our library. It's not the right approach. Other people might contribute to our library. It's possible that they have a different Rollup version, or even worse they might not have Rollup installed at all.

We might also use a CI pipeline to automate package publishing. And the server might not have Rollup installed. This way we can make sure every contributor or the CI server uses the same version of Rollup.

Let’s update our package.json file too:

Note that our dependencies’ version might be different, and that’s okay. Also notice the main, module, and browser properties:

We also add some scripts:

Let’s update our Rollup config file to generate the es module too. We also use the package.json file to specify the output path for each format.

Let’s try to bundle our library:

We should now have four build files within the dist directory:

We only want to distribute the generated bundle files. We don’t need to include the src directory or the Rollup configuration file. We can create .npmignore file to exclude files or directories that we don't want to distribute to our users.

Next, log in to NPM using your terminal:

Type the following command to check whether you’re logged in or not:

Before publishing your package, make sure that your bundle can be installed and working properly. You can install your package locally. Create a directory somewhere outside of your project directory.

Now, within that test directory, create an empty package.json file:

You can then install your package by passing the path to your package project directory:

You can verify further by opening the node-repl and try to require your package:

If all goes well, you’re ready to publish your package! Run the following command to:

Congratulation 🎉 You’ve made it until the end! Let’s recap what you’ve learned so far:

Add a comment

Related posts:

Keyscape Vst Free Download Windows

41K views 1 year ago #FreeVst #keyscape COMO INSTALAR KEYSCAPE FREE VST Spectrasonics Keyscape es un nuevo y extraordinario instrumento virtual con la mayor colección de teclados del mundo,.. Apr 9…

The Secret to Successful Fintech Startups.

Fintech is a rapidly growing industry causing disruption in the financial world. Fintech startups have come to dominate traditional banks and other financial institutions with innovative…

Training mind to be calm

Training your mind to remain calm in the face of problems takes practice and patience, but it can be done. Here are some techniques you can try: Practice mindfulness: Mindfulness is the practice of…