Bundlers & Developer Setup
Bundlers are one option for serving dev servers & are a wonderful tool for releasing code into production. However, there are now many alternatives that are helpful for speedy local development. Speed comparisons vary depending on what metrics are being used, and like any other framework, there are tradeoffs across any tooling choices.
Vite is currently the leading project for an unbundled development environment. Vite provides a comparison writeup that may be worth checking out when starting a new NodeCG project. The Vue Mastery team also provides a comparison between Vite and Webpack that may be helpful in making your bundling / module choices.
NodeCG usage is currently not limited to any of these options! Make your choice(s) as you will for what you are comfortable with.
Bundlers
Webpack, the most used bundler, has some good documentation for why to use Webpack.
There are several JavaScript bundlers.
- webpack
- By far the most used bundler with a lot of community made extensions.
- rollup
- Primarily used by libraries like React, Vue - utilizes ES Modules for splitting code
- parcel
- Bundler famous for zero-config and super fast building process
- esbuild
- Extremely fast bundler written in Go, however currently the maintainers state that it is unstable for production usage.
These bundlers allows you to
- write modular, organised source code
- treat CSS, images, or any sort of files like JavaScript module
- use npm packages for front-end (dashboard/graphics)
- use JSX/TSX, Vue single file component
- write in other languages like TypeScript
- and many more
Tutorial: Using Parcel
The below tutorial focuses solely on bundler tooling with NodeCG and was last updated in November 2020; functionality may be limited due to updates in underlying tools, particularly Parcel. If you run into issues, feel free to put in a pull request.
Directory Structure
Basically you will have parcel to output the whole dashboard
and graphics
directory. Your project would look like this
foo-layouts
|- extension
|- schemas
|- src
|- package.json
When you run parcel, it will make dashboard
and graphics
directory and
output bundle result into them.
(After running parcel)
foo-layouts
|- extension
|- schemas
|- src
|- package.json
|- dashboard (built)
|- index.html
|- styles.8jx17sx.css
|- main.7x2hdjs.js
|- graphics (built)
|- ds1.html
|- sd1.html
|- styles.03nsh2s.css
|- ds1.rssiahs.js
|- sd1.4jc71nx.js
|- background.d8frsis.png
The random string for each generated files are automatically generated to refresh cache when the files change.
Setup
As I said, parcel is (literally) zero-configuration required. It even installs missing packages for you if there is any.
Add parcel to your bundle
Locally
npm install --save-dev parcel-bundler
# or
yarn add -D parcel-bundler
The parcel
command will be available locally. You can run it either adding
npm scripts, or npx parcel
/yarn parcel
.
Globally
npm install -g parcel-bundler
# or
yarn global add parcel-bundler