@mnrendra/rollup-plugin-alias
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@mnrendra/rollup-plugin-alias

🍣 A Rollup plugin to resolve alias paths and automatically read the alias configuration from tsconfig.json.

Install

npm i -D rollup-plugin-esbuild @mnrendra/rollup-plugin-alias

Usage

For ES modules (rollup.config.mjs):

import esbuild from 'rollup-plugin-esbuild' // 'rollup-plugin-esbuild' is required
import alias from '@mnrendra/rollup-plugin-alias'

export default {
  external: (id) => !/^[./]/.test(id),
  input: 'your_input_file.(js|cjs|mjs|jsx|ts|cts|mts|tsx)',
  output: {
    file: 'dist/your_output_file.js',
    format: 'cjs',
    sourcemap: true
  },
  plugins: [
    esbuild({ minify: true }), // <-- Require `esbuild` to be executed immediately before `alias`
    alias() // <-- Execute `alias` immediately after `esbuild`. It will automatically read the alias configuration from `tsconfig.json`.
  ]
}

For CommonJS (rollup.config.js):

const esbuild = require('rollup-plugin-esbuild') // 'rollup-plugin-esbuild' is required
const alias = require('@mnrendra/rollup-plugin-alias')

module.exports = {
  external: (id) => !/^[./]/.test(id),
  input: 'your_input_file.(js|cjs|mjs|jsx|ts|cts|mts|tsx)',
  output: {
    file: 'dist/your_output_file.js',
    format: 'cjs',
    sourcemap: true
  },
  plugins: [
    esbuild({ minify: true }), // <-- Require `esbuild` to be executed immediately before `alias`
    alias() // <-- Execute `alias` immediately after `esbuild`. It will automatically read the alias configuration from `tsconfig.json`.
  ]
}

Options

const alias = require('@mnrendra/rollup-plugin-alias')

module.exports = {
  plugins: [
    alias({
      // List of alias configs to override alias path configurations from `tsconfig.json` or `compilerOptions` option.
      aliases: [{
        alias: '@',
        path: './src'
      }],
      // `tsconfig.json`'s `compilerOptions` contains `baseUrl` and `paths` properties to override alias path configurations from `tsconfig.json`.
      // Note: it will be overridden by the `aliases` option if it exists.
      compilerOptions: {
        baseUrl: './src',
        paths: {
          '@': ['./'],
          '@/*': ['./*']
        }
      }
    })
  ]
}

aliases

type: Alias[]|undefined
default: []
List of alias configs to override alias path configurations from tsconfig.json or compilerOptions option.

compilerOptions

type: CompilerOptions|undefined
default: undefined
tsconfig.json's compilerOptions contains baseUrl and paths properties to override alias path configurations from tsconfig.json.
Note: it will be overridden by the aliases option if it exists.

Types

import type {
  // @mnrendra/types-aliases
  Alias,
  Aliases,
  // @mnrendra/types-tsconfig
  CompilerOptions,
  BaseURL,
  Paths,
  // @mnrendra/rollup-plugin-alias
  Options,
  Plugin
} from '@mnrendra/rollup-plugin-alias'

License

MIT

Author

@mnrendra

Package Sidebar

Install

npm i @mnrendra/rollup-plugin-alias

Weekly Downloads

88

Version

1.1.0

License

MIT

Unpacked Size

13.9 kB

Total Files

7

Last publish

Collaborators

  • mnrendra