Getting started
Parcel bundler is a new bundler that can work without any configuration. Here is a quick look at this awesome tool. First, start an NPM package and install the bundler as a dev dependency.
1 | npm init -y |
Let us make an HTML page that includes a JavaScript file.
1 | <html> |
As you can see, it is including src/main.js
file. We hope that file is a single bundle prepared for the browser - before ES6 modules land, the browser cannot usually load additional files. But our file does import code from other local files!
1 | import {name} from './utils' |
The second file will export the name constant
1 | export const name = 'World' |
Let us load the HTML page in the browser. Here is a command to server the file using Parcel
1 | { |
When we run npm start
we get a server watching our files and serving the page at port :1234
1 | npm start |
We can open any browser and not only we see the Hello World
message, we also can get source maps and hot module reload! Even though the source panel complains about the import
syntax, the break points are working.
The served page is interesting - it is NOT the input index.html
file. Instead Parcel serves a generated file dist/index.html
. That file refers to bundles JavaScript internally, even uses hashes to bust the browser cache.
1 | <html> |
The JavaScript file dist/9d1eab3205c8799f59b49243d0d1e069.js
is your typical single file produced from src/main.js
by tracing all imports and bringing them together into single output file.
Production
When you are happy with your application you can produce the "production" bundle that is minified and can be served from any static server. Here is the command
1 | { |
Now you can either open local file dist/index.html
or use any static server on folder dist
.
CSS
What if our JavaScript code includes CSS files? Let's try it
1 | import './app.css' |
My design skills are sick, so here is my CSS
1 | body { |
Same npm start
command - and parcel now makes a separate CSS file in the dist
folder and includes a link in the dist/index.html
file!
1 | <html> |
Ohh, and my page is classy.
Nice!
Wait, did I say "CSS"? I meant - I want to use Less!
1 | import './app.less' |
1 | @dominant: antiquewhite; |
Nothing else needed to be done - Parcel does CSS bundling as if by magic....
TypeScript
Advantages of static types are obvious. So let's use TypeScript files! Point index.html
at src/main.ts
, rename our source files and sprinkle some type love there.
1 | <html> |
1 | import './app.less' |
1 | export const name: string = 'World' |
Here is where the Parcel magic really arrives: the bundler detects missing preprocessor and installs the necessary dependencies automatically!
1 | $ npm start |
All installed, and the application is working. Talk about great dev experience!
API
If you want to use the bundler's NPM module API to do your own bundling, the docs are not there yet. But luckily, the code is well organized and you should be able to figure out what to call by looking at how Parcel CLI uses its main module. Here is a quick example
1 | const ParcelBundler = require('parcel-bundler') |
If you execute this Node script with node ./out.js
you will get a description of the produced bundle
1 | $ node ./out.js |
The result describes the main output file dist/index.html
and all linked resources (JS and CSS bundles). You can just grab the files in the dist
folder and use them.
You can also bundle files in the watch
mode and get notified whenever the source files are changed - Parcel will watch all source files for you.
1 | const ParcelBundler = require('parcel-bundler') |
Start this script, and once it is running change one of the source files, for example src/utils.ts
- notice that the bundle has been automatically rebuilt.
1 | $ node ./out.js |
Great job, Parcel team