How to use the watch function from rollup

Find comprehensive JavaScript rollup.watch code examples handpicked from public code repositorys.

rollup.watch is a function that watches a Rollup configuration and automatically rebuilds the project when changes are detected.

162
163
164
165
166
167
168
169
170
171
			aditional[fileOrFolder] = true
		}
	}
}

let watcher = rollup.watch({
	input: build.input,
	/*experimentalCacheExpiry: 0,
	cache: true,*/
	treeshake: build.minified || !__IS_LOCALHOST__ ? true : false,
fork icon0
star icon0
watch icon2

+ 3 other calls in file

32
33
34
35
36
37
38
39
40
41
console.log(`Rollup build currently have ${warnings.count} warnings`);
warnings.flush();
options.map(async option => {
    const bundle = await rollup.rollup(option)
    await Promise.all(option.output.map(bundle.write))
    rollup.watch(option).on('event', event => {
        if (event.code === 'BUNDLE_START') {
            console.log(`Building ${event.input}`);
        } else if (event.code === 'BUNDLE_END') {
            console.log(`${event.input} has been built successfully!`)
fork icon0
star icon0
watch icon2

+ 3 other calls in file

How does rollup.watch work?

rollup.watch is a function provided by the Rollup library for building JavaScript bundles. It takes a Rollup configuration object as input and sets up a file watcher that detects changes in the input files, rebuilds the bundle, and writes the output to the specified location. The rollup.watch function returns an object with methods to control and stop the watcher. These methods can be used to manually start and stop the watch process, or to perform other actions such as reloading the browser when changes are made. When a file change is detected, Rollup re-runs the build process and produces a new output bundle. By default, Rollup will only rebuild the files that have changed and any dependencies that have been affected. This can result in faster build times compared to other bundlers that rebuild the entire project from scratch on each change. In addition to the configuration options provided to rollup.watch, there are several plugins available for Rollup that can extend its functionality, such as adding support for different file types or integrating with other build tools.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const rollup = require("rollup");
const rollupConfig = require("./rollup.config.js");

const watcher = rollup.watch(rollupConfig);

watcher.on("event", (event) => {
  switch (event.code) {
    case "START":
      console.log("Building bundle...");
      break;
    case "BUNDLE_START":
      console.log("Creating bundle...");
      break;
    case "BUNDLE_END":
      console.log("Bundle created successfully!");
      break;
    case "END":
      console.log("Build complete!");
      break;
    case "ERROR":
      console.error("Error during build:", event.error);
      break;
  }
});

This example creates a watcher using rollup.watch, passing in a Rollup configuration object. It then listens for events emitted by the watcher, such as 'START', 'BUNDLE_START', 'BUNDLE_END', 'END', and 'ERROR'. Based on the event code, it logs messages to the console to provide feedback on the build process.