[フレーム]

Chapter 21: Introduction to JavaScript Tooling and Build Systems

Modern JavaScript development relies heavily on external tools to manage dependencies, enable the use of modern syntax, and optimize code for production. Understanding the tooling ecosystem is crucial for joining any professional team.

1. Node Package Manager (NPM)

NPM (or Yarn/PNPM) is the standard package manager for JavaScript. It allows you to download and manage code libraries (packages/dependencies) written by others.

  • package.json: The core configuration file for any JS project. It lists project metadata, scripts, and dependencies.

  • node_modules: The folder where all downloaded packages are stored.

  • npm install [package-name]: Downloads and installs a package into node_modules.

  • npm install --save-dev [package-name]: Installs a package needed only during development (e.g., a testing framework or a bundler).

2. Transpilers: Babel

A Transpiler converts modern JavaScript code (like ES6, ES7+) into older, more widely supported JavaScript (ES5) so it can run reliably in all browsers.

  • Problem: You want to use features like const/let, arrow functions, and async/await in your source code.

Solution (Babel): Babel reads your modern source code and outputs equivalent code that runs even in older environments.

JavaScript

// Source Code:
const sum = (a, b) => a + b;
// Babel Output (ES5):
var sum = function(a, b) {
 return a + b;
};

3. Bundlers (Module Bundling)

A Bundler takes all your JavaScript files (and often CSS/images), resolves their import statements, and combines them into one or a few optimized files (bundles) that the browser can load efficiently.

Key Bundler Tasks

  • Module Resolution: Figure out the order of files based on import/export.

  • Minification: Remove unnecessary characters (whitespace, comments) to shrink file size.

  • Tree Shaking: Eliminate unused code from imported libraries.

Popular Bundlers

  • Webpack: The long-standing, powerful, and highly configurable industry standard (though complex).

  • Vite: A newer, faster tool that uses native browser ES Modules during development for instant starting times.

4. Linting and Formatting

These tools enforce a consistent code style, which is crucial for team development. They usually run before the code is committed.

  • Linter (ESLint): Analyzes code for potential errors (e.g., unused variables) and stylistic issues (e.g., semi-colon usage).

  • Formatter (Prettier): Automatically enforces consistent code style (spaces, line breaks, indentation) without reporting errors.

Workflow Summary

  1. Write modern JS code.

  2. ESLint/Prettier check and format the code.

  3. Babel transpiles the code to ES5.

  4. Webpack/Vite bundle the resulting JS files into an optimized bundle file (e.g., dist/bundle.js).

  5. The browser loads the optimized bundle.

People also reading
Membership not found

AltStyle によって変換されたページ (->オリジナル) /