← Back to TypeScript Course | Chapter 25: Build Tools | Lesson 3 of 6

TypeScript के साथ Rollup

Rollup एक module bundler है जो commonly libraries build करने के लिए उपयोग होता है। TypeScript type checking प्रदान कर सकता है जबकि Rollup optimized JavaScript bundles produce करता है।
Syntax
typescript
// rollup.config.js
import typescript from "@rollup/plugin-typescript";

export default {
    input: "src/index.ts",
    output: { file: "dist/bundle.js", format: "esm" },
    plugins: [typescript()],
};

Core Concept

Rollup एक bundler है जो small, tree-shaken output produce करने पर focused है — खासकर libraries के लिए — और ES modules natively समझता है, bundle step के दौरान .ts source files compile करने के लिए @rollup/plugin-typescript के साथ pairing करते हुए।

उदाहरण: Core Concept

typescript
// rollup.config.js uses @rollup/plugin-typescript to compile .ts sources
console.log("Rollup focuses on small, tree-shaken output for libraries");

Basic Setup

एक basic setup rollup.config.js (या .mjs) में plugins array में @rollup/plugin-typescript add करता है, इसे आपके tsconfig.json पर point करते हुए ताकि Rollup आपके project के existing compiler options उपयोग करके TypeScript compile करे।

उदाहरण: Basic Setup

typescript
// import typescript from "@rollup/plugin-typescript";
// export default { plugins: [typescript()] };
console.log("plugin-typescript compiles using your existing tsconfig.json");

Typed Example

Rollup एक single config में एक typed source tree से कई typed output formats (ESM, CJS, UMD) emit कर सकता है, उपयोगी जब एक library publish कर रहे हों जिसे modern bundlers और older CommonJS consumers दोनों support करना हो।

उदाहरण: Typed Example

typescript
// output: [{ format: "esm" }, { format: "cjs" }, { format: "umd", name: "MyLib" }]
console.log("Rollup can emit multiple typed output formats from one source tree");

Project Usage

एक असली project में, Rollup की aggressive tree-shaking मतलब है कि सिर्फ एक consumer द्वारा असल में imported typed exports उनके bundle में आते हैं, जो यही कारण है कि यह typed npm libraries publish करने के लिए standard choice है।

उदाहरण: Project Usage

typescript
// Only imported, used exports end up in the consumer's final bundle
console.log("Aggressive tree-shaking makes Rollup common for npm libraries");

Best Practices

अपने bundle के साथ .d.ts declaration files emit करें (tsc --emitDeclarationOnly या rollup-plugin-dts के through) इसलिए आपकी published library के consumers को पूरी type information मिलती है, सिर्फ compiled JavaScript नहीं।

उदाहरण: Best Practices

typescript
// tsc --emitDeclarationOnly  (or rollup-plugin-dts)
console.log("Emit .d.ts files alongside the bundle for library consumers");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. @rollup/plugin-node-resolve भूल जाना, इसलिए node_modules से imports resolve नहीं होते।
  2. tsconfig में declaration set न करना, इसलिए एक library .d.ts files के बिना publish होती है।
  3. एक tsconfig module setting उपयोग करना जो ESNext नहीं है, जो Rollup को tree shaking से रोकता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.