For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt, and this page is available as Markdown at /next/api/rspeedy/rspeedy.resolve.alias.md.
Rspeedy logo
Rspeedy
  • English
  • Home > @lynx-js/rspeedy > Resolve > alias

    Resolve.alias property

    Create aliases to import or require certain modules more easily.

    Signature:

    alias?: Record<string, string | false | string[]> | undefined;

    Default Value

    undefined

    Example 1

    A trailing $ can also be added to the given object's keys to signify an exact match:

    import { defineConfig } from '@lynx-js/rspeedy'
    export default defineConfig({
      resolve: {
        alias: {
          xyz$: 'path/to/file.js',
        },
      },
    })

    which would yield these results:

    import Test1 from 'xyz'; // Exact match, so path/to/file.js is resolved and imported
    import Test2 from 'xyz/file.js'; // Not an exact match, normal resolution takes place

    Example 2

    resolve.alias is useful to control how a npm package is resolved.

    • Change react to @lynx-js/react:
    import { defineConfig } from '@lynx-js/rspeedy'
    import { createRequire } from 'module'
    const require = createRequire(import.meta.url)
    export default defineConfig({
      resolve: {
        alias: {
          react: require.resolve('@lynx-js/react'),
        },
      },
    })

    This allows you to use some third-party libraries that directly uses react as dependencies in ReactLynx.

    • Force using the same version of dayjs:
    import { defineConfig } from '@lynx-js/rspeedy'
    import { createRequire } from 'module'
    const require = createRequire(import.meta.url)
    export default defineConfig({
      resolve: {
        alias: {
          dayjs: require.resolve('dayjs'),
        },
      },
    })

    Please note that this is dangerous, since all the dayjs(including the dependencies of a dependencies) is resolved to the version in the project. It may cause both compile-time and runtime errors due to version mismatch.

    Example 3

    Setting resolve.alias to false will ignore a module.

    import { defineConfig } from '@lynx-js/rspeedy'
    export default defineConfig({
      resolve: {
        alias: {
          'ignored-module': false,
          './ignored-module': false,
        },
      },
    })
    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.