rollup-plugin-strict-alias

raw JSON →
1.0.0 verified Mon Apr 27 auth: no javascript

A Rollup plugin for defining import aliases with exact string matching (===) rather than prefix matching (startsWith). Version 1.0.0 is the current stable release. This plugin emulates Webpack's resolve.alias behavior, avoiding false positives that occur with substring replacement (e.g., 'react' won't accidentally match 'react-router'). It supports local file resolution with configurable extensions. Unlike rollup-plugin-alias, this plugin provides strict alias substitution and handles submodule imports (e.g., 'lodash/map'). The package is authored by adriantoine and has minimal maintenance commitment; last release is final. Suitable for projects using Rollup that need precise alias mapping.

error Error: Cannot find module 'rollup-plugin-strict-alias'
cause The package is not installed or not in node_modules.
fix
Run npm install rollup-plugin-strict-alias --save-dev or add to package.json dependencies.
error TypeError: alias is not a function
cause Using named import { alias } instead of default import.
fix
Change to import alias from 'rollup-plugin-strict-alias'.
error (!) Plugin strict-alias: Could not resolve './some/path' from 'CWD'
cause The aliased local path does not exist or the file extension is missing.
fix
Check the path and ensure the file exists with an extension included in the resolve option. Use absolute paths if needed.
gotcha Aliases are matched exactly (===) not via startsWith. A path like 'lodash/map' will NOT be replaced if the alias is 'lodash'.
fix Define aliases for full import paths if submodule mapping is needed, or use a plugin that supports wildcard/regex patterns.
deprecated The package has not been updated since 2016. No security patches or Rollup API compatibility updates.
fix Consider using alternative plugins like @rollup/plugin-alias (maintained by the Rollup team).
gotcha Local aliases (starting with './') are resolved relative to the current working directory, not the importing file.
fix Use absolute paths or ensure the alias path is correct relative to process.cwd().
gotcha If the `resolve` option is not provided, local aliases implicitly append '.js' extension. Other extensions like '.jsx' or '.ts' are not tried.
fix Provide an explicit `resolve` array with the extensions you want (e.g., ['.js', '.jsx']).
gotcha The plugin replaces import specifiers before other plugins run. If another plugin later modifies the specifier, the alias may not apply.
fix Ensure this plugin is placed early in the plugins array, before any plugin that transforms imports.
npm install rollup-plugin-strict-alias
yarn add rollup-plugin-strict-alias
pnpm add rollup-plugin-strict-alias

Configures Rollup with strict aliases for 'react' and 'lodash', then bundles an ES module.

import { rollup } from 'rollup';
import alias from 'rollup-plugin-strict-alias';

rollup({
  input: './src/index.js',
  plugins: [
    alias({
      'react': './vendor/react.js',
      'lodash': './node_modules/lodash-es'
    })
  ]
}).then(bundle => bundle.write({ file: 'dist/bundle.js', format: 'esm' }));