rollup-plugin-path-rewrite
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Node.js CI NPM version

rollup-plugin-path-rewrite

Rollup plugin for rewriting the output file path. In some specific cases you may need to change/rewrite the path of a file or directory generated by Rollup. Usually you need it when using Rollup to build an ES modules library with:

{
    format: "es",
    preserveModules: true
}

In such cases, the following plugin can be of assistance.

Install

npm install rollup-plugin-path-rewrite --save-dev

Usage

Create a rollup.config.js configuration file and import the plugin:

import pathRewrite from "rollup-plugin-path-rewrite";

export default {
    input: "src/index.js",
    output: {
        dir: "dist",
        format: "es",
        preserveModules: true,
    },
    plugins: [
        // ...other plugins...
        pathRewrite({
            test: /.d.ts$/i,
            replaceTo: (path) => {
                return `types/${path}`;
            },
        }),
    ],
};

Then call rollup either via the CLI or the API.

The configuration above will check all paths in the output result, and if a path corresponds to options.test, it will apply options.replaceTo to this path, thereby modifying the resulting output path.

It will produce smth like (according your file structure):

dist
├── types
│   ├── first.d.ts
│   ├── second
│   │   └── second.d.ts
├── ...other files/dirs

Options

test

Type: RegExp

A RegExp to match the target paths.

replaceTo

Type: RegExp

Type: string | (path: string) => string

A string or a function that returns a new path to replace the matched (by options.test) paths.

Package Sidebar

Install

npm i rollup-plugin-path-rewrite

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

8.74 kB

Total Files

7

Last publish

Collaborators

  • grumaks