include

const defaultInclude = [
  {
    and: [rootPath, { not: /[\\/]node_modules[\\/]/ }],
  },
  /\.(?:ts|tsx|jsx|mts|cts)$/,
];

The source.include is used to specify additional JavaScript files that need to be compiled.

To avoid redundant compilation, by default, Rsbuild only compiles JavaScript files in the current directory and TypeScript and JSX files in all directories. It does not compile JavaScript files under node_modules.

Through the source.include config, you can specify directories or modules that need to be compiled by Rsbuild. The usage of source.include is consistent with Rule.include in Rspack, which supports passing in strings or regular expressions to match the module path.

For example:

import path from 'node:path';

export default {
  source: {
    include: [path.resolve(__dirname, '../other-dir')],
  },
};

Compile Npm Packages

A typical usage scenario is to compile npm packages under node_modules, because some third-party dependencies have ESNext syntax, which may cause them to fail to run on low-version browsers. You can solve the problem by using this config to specify the dependencies that need to be compiled.

Take query-string as an example, you can add the following config:

import path from 'node:path';

export default {
  source: {
    include: [
      // Method 1:
      // First get the path of the module by require.resolve
      // Then pass path.dirname to point to the corresponding directory
      path.dirname(require.resolve('query-string')),
      // Method 2:
      // Match by regular expression
      // All paths containing `/node_modules/query-string/` will be matched
      /[\\/]node_modules[\\/]query-string[\\/]/,
    ],
  },
};

The above two methods match the absolute paths of files using "path prefixes" and "regular expressions" respectively. It is worth noting that all referenced modules in the project will be matched. Therefore, you should avoid using overly loose values for matching to prevent compilation performance issues or compilation errors.

TIP

In the regular expression example above, we use [\\/] to match the path separator because different operating systems use different path separators. Using [\\/] ensures that the paths will match in both MacOS and Windows.

Compile Sub Dependencies

When you compile an npm package via source.include, Rsbuild will only compile the matching module by default, not the Sub Dependencies of the module.

Take query-string for example, it depends on the decode-uri-component package, which also has ESNext code, so you need to add the decode-uri-component package to source.include as well.

export default {
  source: {
    include: [
      /[\\/]node_modules[\\/]query-string[\\/]/,
      /[\\/]node_modules[\\/]decode-uri-component[\\/]/,
    ],
  },
};

Compile Libraries in Monorepo

When developing in Monorepo, if you need to refer to the source code of other libraries in Monorepo, you can add the corresponding library to source.include:

import path from 'node:path';

const packagesDir = path.resolve(__dirname, '../../packages');

export default {
  source: {
    include: [
      // Compile all files in Monorepo's package directory
      // It is recommended to exclude the node_modules
      {
        and: [packagesDir, { not: /[\\/]node_modules[\\/]/ }],
      },
    ],
  },
};

If you match a module that is symlinked to the current project, then you need to match the real path of the module, not the symlinked path.

For example, if you symlink the packages/foo path in Monorepo to the node_modules/foo path of the current project, you need to match the packages/foo path, not the node_modules/foo path.

Compile node_modules

In general, source.include should not be used to compile the entire node_modules directory. For example, the following configuration is not recommended:

export default {
  source: {
    include: [/[\\/]node_modules[\\/]/],
  },
};

This is because most of the npm packages in node_modules are already compiled, and it is usually unnecessary to recompile them. Compiling the entire node_modules will increase compilation time and may cause unexpected errors in certain npm packages, such as core-js, which may result in runtime exceptions after compilation.

If you are willing to accept the increase in compilation time, you can use the following configuration to compile all JavaScript files but exclude `core-js':

export default {
  source: {
    include: [{ not: /[\\/]core-js[\\/]/ }],
  },
};