To bundle multiple JavaScript files into a single file using Parcel, follow these steps:
-
Initialize a New Node.js Project (if you haven't already):
Open your terminal and run:
npm init -y
This will create a
package.json
file in your project directory. -
Install Parcel:
Run the following command to install Parcel as a development dependency:
npm install --save-dev parcel
-
Create the Source Directory and Files:
Create a directory named
src
and add your JavaScript files. For example:-
src/index.js
// src/index.js import { greet } from "./greet.js"; console.log(greet("World"));
-
src/greet.js
// src/greet.js export function greet(name) { return `Hello, ${name}!`; }
-
-
Create an HTML File to Include the JavaScript Bundle:
Create a file named
index.html
in the root of your project:<!-- index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Parcel Example</title> </head> <body> <script src="./src/index.js"></script> </body> </html>
-
Add a Build Script to
package.json
:Open your
package.json
file and add abuild
script:{ "scripts": { "start": "parcel index.html", "build": "parcel build index.html" } }
parcel build
will bundle your files for production and output them to thedist
directory.parcel start
will start a development server and serve your files with live reloading.
-
Run the Build Script:
To build your project and bundle the files, run:
npm run build
To start a development server and see your project in action, run:
npm start
-
After running the build script, you should find the bundled files in the
dist
directory. -
To test the development server, open your browser and navigate to
http://localhost:1234
(or whatever port Parcel indicates in the terminal).
-
Parcel Configuration: Parcel does not require a configuration file by default. It automatically handles module bundling based on your project structure.
-
JavaScript Files:
index.js
imports thegreet
function fromgreet.js
, and Parcel bundles these files together. -
HTML File: Includes the bundled JavaScript file and serves as the entry point for Parcel.
-
Build Script: Uses Parcel to bundle files for production, outputting to the
dist
directory.
This setup demonstrates how to use Parcel to bundle multiple JavaScript files into a single file and optionally serve them using a development server. If you have further questions or need additional help, feel free to ask!