CC 4.0 License

The content of this section is derived from the content of the following links and is subject to the CC BY 4.0 license.

The following contents can be assumed to be the result of modifications and deletions based on the original contents if not specifically stated.

Resolve

Used to configure the Rspack module resolution logic.

  • Type: Object

resolve.alias

  • Type: Record<string, false | string | (string | false)[]>
  • Default: {}

Path alias, e.g.

{ "@": path.resolve(__dirname, './src'), "abc$": path.resolve(__dirname, './node_modules/abc/index.js'), }

At this point:

  • require("@/a") will attempt to resolve <root>/src/a.
  • require("abc") will attempt to resolve <root>/src/abc.
  • require("abc/file.js") will not match, and it will attempt to resolve node_modules/abc/file.js.

resolve.aliasFields

  • Type: string[]
  • Default: ['browser']

Define a field, such as browser, that should be parsed in accordance with this specification.

resolve.conditionNames

  • Type: string[]
  • Default: []

Same as node's conditionNames for the exports and imports fields in package.json.

resolve.descriptionFiles

  • Type: string[]
  • Default: ['package.json']

The JSON files to use for descriptions.

rspack.config.js
module.exports = {
  resolve: {
    descriptionFiles: ['package.json'],
  },
};

resolve.enforceExtension

  • Type: boolean

By default, It changes to true if resolve.extensions contains an empty string; otherwise, this value changes to false.

If true, it will not allow extension-less files. So by default require('./foo') works if ./foo has a .js extension, but with this enabled only require('./foo.js') will work.

rspack.config.js
module.exports = {
  resolve: {
    enforceExtension: false,
  },
};

resolve.exportsFields

  • Type: string[]
  • Default: ["exports"]

Customize the exports field in package.json. e.g.

lib/package.json
{
  "name": "lib",
  "testExports": {
    ".": "./test.js"
  },
  "exports": {
    ".": "./index.js"
  }
}

When this configuration is ["testExports", "exports"], the result of import value from 'lib' is lib/test.js.

resolve.extensions

  • Type: string[]
  • Default: [".js", ".json", ".wasm"]

Parse modules in order, e.g. require('. /index'), will try to parse '. /index.js', '. /index.json'...

resolve.extensionAlias

  • Type: Record<string, string[] | string>
  • Default: {}

Define alias for the extension. e.g.

rspack.config.js
module.exports = {
  resolve: {
    extensionAlias: {
      '.js': ['.ts', '.js'],
    },
  },
};

This is particularly useful for TypeScript projects, as TypeScript recommends using the .js extension to reference TypeScript files.

index.ts
import { foo } from './foo.js'; // actually refers to `foo.ts`

Rspack will try to resolve './foo.ts' and ./foo.js' sequentially when resolving import './foo.js'.

resolve.fallback

  • Type: Record<string, false | string>
  • Default: {}

Redirect module requests when normal resolving fails.

rspack.config.js
module.exports = {
  //...
  resolve: {
    fallback: {
      abc: false, // do not include a polyfill for abc
      xyz: path.resolve(__dirname, 'path/to/file.js'), // include a polyfill for xyz
    },
  },
};

Rspack does not polyfills Node.js core modules automatically which means if you use them in your code running in browsers or alike, you will have to install compatible modules from NPM and include them yourself.

You could use node-polyfill-webpack-plugin to polyfill Node.js core API automatically.

rspack.config.js
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');

module.exports = {
  // ...
  plugins: [new NodePolyfillPlugin()],
};

Or refer to the list of Node.js polyfills used by webpack 4:

rspack.config.js
module.exports = {
  //...
  resolve: {
    fallback: {
      assert: require.resolve('assert'),
      buffer: require.resolve('buffer'),
      console: require.resolve('console-browserify'),
      constants: require.resolve('constants-browserify'),
      crypto: require.resolve('crypto-browserify'),
      domain: require.resolve('domain-browser'),
      events: require.resolve('events'),
      http: require.resolve('stream-http'),
      https: require.resolve('https-browserify'),
      os: require.resolve('os-browserify/browser'),
      path: require.resolve('path-browserify'),
      punycode: require.resolve('punycode'),
      process: require.resolve('process/browser'),
      querystring: require.resolve('querystring-es3'),
      stream: require.resolve('stream-browserify'),
      string_decoder: require.resolve('string_decoder'),
      sys: require.resolve('util'),
      timers: require.resolve('timers-browserify'),
      tty: require.resolve('tty-browserify'),
      url: require.resolve('url'),
      util: require.resolve('util'),
      vm: require.resolve('vm-browserify'),
      zlib: require.resolve('browserify-zlib'),
    },
  },
};

resolve.importsFields

  • Type: string[]
  • Default: ["imports"]

Customize the imports field in package.json which are used to provide the internal requests of a package (requests starting with # are considered internal).

e.g.

package.json
{
  "name": "lib",
  "imports": {
    "#foo": "./src/foo.js",
    "#common/*": "./src/common/*.js"
  },
  "testImports": {
    "#foo": "./src/test/foo.js"
  }
}

When this configuration is ["testImports", "imports"], the result of import value from '#foo' in current package is src/test/foo.js.

resolve.mainFields

  • Type: string[]
  • Default: Based on the target option

When importing from an npm package, for example import React from 'react', resolve.mainFields is used to determine which fields in package.json are resolved.

If target is 'web', 'webworker', or not specified, the default value is ["browser", "module", "main"].

rspack.config.js
module.exports = {
  resolve: {
    mainFields: ['browser', 'module', 'main'],
  },
};

For any other target (including 'node'), the default value is ["module", "main"].

rspack.config.js
module.exports = {
  resolve: {
    mainFields: ['module', 'main'],
  },
};

For example, consider an arbitrary library called foo with a package.json that contains the following fields:

package.json
{
  "name": "foo",
  "browser": "./dist/browser.js",
  "module": "./dist/module.js"
}

When import foo from 'foo', Rspack resolves to the module in the browser field, because the browser field has the highest priority in mainFields array.

resolve.mainFiles

  • Type: string[]
  • default: ["index"]

The filename suffix when resolving directories, e.g. require('. /dir/') will try to resolve '. /dir/index'.

Can configure multiple filename suffixes:

rspack.config.js
module.exports = {
  resolve: {
    mainFiles: ['index', 'main'],
  },
};

resolve.modules

  • Type: string[]
  • Default: ["node_modules"]

The name of the directory to use when resolving dependencies.

resolve.preferRelative

  • Type: boolean
  • Default: false

When enabled, require('file') will first look for the . /file file in the current directory, not <modules>/file.

resolve.preferAbsolute

  • Type: boolean
  • Default: false

Opt for absolute paths when resolving, in relation to resolve.roots.

resolve.tsConfig

  • Type: string | object | undefined
  • Default: undefined

The replacement of tsconfig-paths-webpack-plugin in Rspack.

rspack.config.js
module.exports = {
  resolve: {
    // string
    tsConfig: path.resolve(__dirname, './tsconfig.json'),
    // or object
    tsConfig: {
      configFile: path.resolve(__dirname, './tsconfig.json'),
      references: 'auto',
    },
  },
};

Click to see the example.

resolve.tsConfig.configFile

  • Type: string

If you pass the path of tsconfig.json via the option, Rspack will try to resolve modules based on the paths and baseUrl of tsconfig.json, functionally equivalent to tsconfig-paths-webpack-plugin.

resolve.tsConfig.references

  • Type: string[] | "auto" | undefined
  • Default: undefined

Supports tsconfig project references defined in tsconfig-paths-webpack-plugin.

The list of tsconfig paths can be provided manually, or you may specify auto to read the paths list from tsconfig.references automatically.

This feature is disabled when the value is undefined.

resolve.fullySpecified

  • Type: boolean
  • Default: false

No longer resolve extensions, no longer resolve mainFiles in package.json (but does not affect requests from mainFiles, browser, alias).

resolve.restrictions

  • Type: string[]
  • Default: []

A list of resolve restrictions to restrict the paths that a request can be resolved on.

resolve.roots

  • Type: string[]
  • Default: []

A list of directories where server-relative URLs (beginning with '/') are resolved. It defaults to the context configuration option. On systems other than Windows, these requests are initially resolved as an absolute path.

  • Type: boolean
  • Default: true

Whether to resolve symlinks to their symlinked location.

When enabled, symlinked resources are resolved to their real path, not their symlinked location. Note that this may cause module resolution to fail when using tools that symlink packages (like npm link).

resolve.byDependency

  • Type: Record<string, Resolve>.

Customize the Resolve configuration based on the module type.