'Conditional compilation in rollup

How can I exclude some part of code in file (not the file itself but only few lines) from compiling into bundle? In C# it looks like

#ifdef _DEBUG
...
#else
...
#endif

Is it possible for js files compiling with rollup?



Solution 1:[1]

rollup-plugin-jscc

Possibly you need rollup-plugin-jscc, but I'm not sure about it's state in 2022 :(

In usage section of linked README.md file you can find example:

/*#if _DEBUG
import mylib from 'mylib-debug';
//#else */
import mylib from 'mylib'
//#endif

mylib.log('Starting $_APPNAME v$_VERSION...')

@rollup/plugin-alias

Another solution is a little bit different:

/*START.DEBUG_ONLY*/
import 'robot3/debug';
/*END.DEBUG_ONLY*/
import {...} from 'robot3';

Also you can use official @rollup/plugin-alias to mock import 'robot3/debug', but this approach needs empty mock-file for replacement, what is not so clean.

rollup.config.js

import alias from '@rollup/plugin-alias';
module.exports = {
  input: ...,
  output: ...,
  plugins: [
    ...,
    alias({
      entries: [
        { find: 'robot3/debug', replacement: resolve(__dirname, './src/mocks/empty.js') },
      ]
    })
  ]
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1