Bringing rollup into Node.js commands

Now, we could just leave everything alone and run our rollup commands through the command line, but this may make things a bit harder when we bring continuous integration into our process (up next). Also, we may have other developers working on the same system and instead of having them run multiple commands, they can run a single npm command. Instead, we want to integrate rollup into various Node.js scripts.

We looked at this in Chapter 9Practical Example – Building a Static Server, with the microserve package and the start command. But now, we want to integrate two new commands called build and watch.

First, we want the build command to run our rollup configurations. Follow these steps to make this happen:

  1. Let's clean up our main directory and move our rollup configurations to a build directory.
  2. With both of those moved, we will add the following line to our package.json file:
"scripts": {
"start": "node --experimental-modules main.js",
"build": "rollup --config ./build/rollup.config.js && rollup --config ./build/rollup.sass.config.js",
}
  1. With this move, we can run npm run build and see everything built for us in a single command.

Second, we want to add a watch command. This will allow rollup to watch for changes and instantly run that script for us. We can easily add this into our package.json by adding the following line to our scripts section:

"watch": "rollup --config ./build/rollup.config.js --watch"

Now, if we type npm run watch, it will start rollup in watch mode. With that, as we make changes to our JavaScript files, we can see that rollup is automatically rebuilding our distribution file.

One final change that we need to make before we move onto continuous integration is to point our main entry point to our distribution file. To do this, we will change the start section in the package.json file so that it points to dist/build.js:

"start": "node --experimental-modules dist/build.js"

With that, let's go ahead and check to make sure that everything is still working correctly by running npm run start. We will see that some of our files aren't pointing to the correct location. Let's go ahead and fix this by making some changes to the package.json file:

"config": {
"port": 50000,
"key": "../selfsignedkey.pem",
"certificate": "../selfsignedcertificate.pem",
"template": "../template",
"bodyfiles": "../publish",
"development": true
}

With this, we should be good to go! There are plenty of options with Rollup and there are even more when we want to integrate into the Node script system, but this should get us ready for the next section of this chapter, which is integrating into a CI/CD pipeline. Our system of choice is CircleCI.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.172.115