@miyaokamarina/rollup-plugin-override

1.0.1 • Public • Published

Rollup Plugin Override

Compile-time expression substitution. Similar to @rollup/plugin-replace, but robust and safe.

Usage

yarn add -D @miyaokamarina/rollup-plugin-override
npm install --save-dev @miyaokamarina/rollup-plugin-override
import override from '@miyaokamarina/rollup-plugin-override';

export default {
    plugins: [
        override({
            mappings: {
                'process.env.NODE_ENV': `'production'`,
                'OPT_LEVEL': `3`,
                'BUILD_ID': JSON.stringify(uuid()),
            },
        }),
    ],
};

Options

  • include, exclude — filter patterns to limit files the plugin processes.
  • ignoreScope (default: false) — force enable replacement processing even in scopes that have bindings, shadowing the expression to be rewritten.
  • mappings — a record of key-value mappings; values must be strings containing valid JavaScript expressions.

Why

Unlike the official @rollup/plugin-replace, this plugin is more robust in edge cases:

  • Doesn’t care of the exact code formatting:

    assert(process.env.NODE_ENV === 'production');
    assert(process.env['NODE_ENV'] === 'production');
    assert(process['env'][`NODE_ENV`] === 'production');
    
    assert(
        process
        .env
        .NODE_ENV === 'production'
    );
  • Doesn’t rewrite binding names, assignment LHS expressions, etc.:

    process.env.NODE_ENV = 'production';
    //          ↑ writing the real env variable
    
    import { BUILD_ID } from '#constants';
    
    const { BUILD_ID } = whatever;
    const f = BUILD_ID => g();
    
    class App {
        BUILD_ID;
        OPT_LEVEL() {}
    }
  • Respects the scope:

    const f = BUILD_ID => g(BUILD_ID);
    //                      ↑ “global” `BUILD_ID` is shadowed
    //                        by the function parameter,
    //                        so it will not be rewritten
    
    import process from 'node:process';
    
    console.log(process.env.NODE_ENV);
    //                      ↑ reading the real env variable

Limitations

  • No expressions except plain identifiers and member expressions can be rewritten.
  • Member expression properties must be either plain identifiers, or string/template literal subscripts.
  • Template literals with substitutions are not supported.
  • The value of string or template literal must be a valid JavaScript identifier.

Why Babel

Because it has an easy to use all-in-one API.

Support

If you have any issues, feel free to fork this plugin, or contact me on Telegram https://t.me/miyaokamarina

License

MIT © 2023 Yuri Zemskov

Dependencies (4)

Dev Dependencies (7)

Package Sidebar

Install

npm i @miyaokamarina/rollup-plugin-override

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

39.2 kB

Total Files

8

Last publish

Collaborators

  • miyaokamarina