Home > @lynx-js/rspeedy > Source > include

Source.include property

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

Signature:

include?: Rspack.RuleSetCondition[] | undefined;

Remarks

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.

Example 1

  • 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 not be supported in Lynx. You can solve the problem by using this config to specify the dependencies that need to be compiled.

import { createRequire } from 'node:module'
import path from 'node:path'

import { defineConfig } from '@lynx-js/rspeedy'

const require = createRequire(import.meta.url)

export default defineConfig({
  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[\\/]/,
    ],
  },
})

Example 2

  • Compile Libraries in Monorepo
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { defineConfig } from '@lynx-js/rspeedy'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

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

export default defineConfig({
  source: {
    include: [
      // Compile all files in Monorepo's package directory
      // It is recommended to exclude the node_modules
      {
        and: [packagesDir, { not: /[\\/]node_modules[\\/]/ }],
      },
    ],
  },
})
Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the Apache License 2.0.