rollup-plugin-conditional-compilation 1.0.6 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![npm version](https://img.shields.io/npm/v/rollup-plugin-conditional-compilation.svg)](https://www.npmjs.com/package/rollup-plugin-conditional-compilation) [![npm downloads](http://img.shields.io/npm/dm/rollup-plugin-conditional-compilation.svg)](https://npmcharts.com/compare/rollup-plugin-conditional-compilation,token-types?start=1200&interval=30)
4
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/59dd6795e61949fb97066ca52e6097ef)](https://www.codacy.com/app/Borewit/rollup-plugin-conditional-compilation?utm_source=github.com&utm_medium=referral&utm_content=Borewit/rollup-plugin-conditional-compilation&utm_campaign=Badge_Grade)
5
5
 
6
- A simple plugin that allows you to include or exclude code blocks based on compile-time conditions. Same as `#if`, `#else`, `#elif` , `#endif` in C/C++, it looks like this:
6
+ A simple plugin that allows you to include or exclude code blocks based on compile-time conditions. Same as `#if`, `#else`, `#elseif` , `#endif` in C/C++, it looks like this:
7
7
 
8
8
  ```typescript
9
9
  // #if DEBUG
@@ -13,6 +13,14 @@ console.log('user', userData); // when DEBUG is false, this line will be removed
13
13
 
14
14
  > **Note**: You should modify the plugin options to ensure **NOT to strip comments so quickly**, since we work with them. For example, with `@rollup/plugin-typescript`, set `removeComments: false`.
15
15
 
16
+ ## Breaking Changes in v2.0.0
17
+
18
+ Rewrote the plugin from scratch, with the following breaking changes:
19
+
20
+ 1. No longer depends on `Acorn`.
21
+ 2. Faster and more robust parsing for `#if`, `#else`, `#elseif` and `#endif` directives.
22
+ 3. No longer supports `#elif` as an alias for `#elseif` (since it is not a standard directive and may cause confusion).
23
+
16
24
  **More Rollup Plugins** you might be interested in:
17
25
 
18
26
  - [rollup-plugin-conditional-compilation](https://www.npmjs.com/package/rollup-plugin-conditional-compilation): Use directives like `// #if`, `// #else` to do the conditional compilation like C++.
@@ -20,11 +28,6 @@ console.log('user', userData); // when DEBUG is false, this line will be removed
20
28
  - [rollup-plugin-func-macro](https://www.npmjs.com/package/rollup-plugin-func-macro): replace `__func__` by function name of current block, and `__file__` by file name at compile time.
21
29
  For more awesome packages, check out [my homepage💛](https://baendlorel.github.io/?repoType=npm)
22
30
 
23
- ## Fixed Issues in v1.0.6
24
-
25
- 1. Added configurable `ecmaVersion` and `sourceType` options to pass to Acorn. Previously, `sourceType` is fixed in this plugin and prevented parsing ES module files; this is now configurable.
26
- 2. Fixed an issue where `variables` was required; `variables` is now optional.
27
-
28
31
  ## Installation
29
32
 
30
33
  ```bash
@@ -41,7 +44,10 @@ export default {
41
44
  plugins: [
42
45
  // Recommended: run `conditional` before the TypeScript transformer so the
43
46
  // plugin operates on the original source comments.
44
- conditional({ variables: { DEBUG: false, FEATURE_X: true } }),
47
+ conditional({
48
+ variables: { DEBUG: false, FEATURE_X: true },
49
+ expressionCache: true, // default true
50
+ }),
45
51
  typescript({
46
52
  ...,
47
53
  removeComments: false, // !!IMPORTANT!! Don't strip comments so quickly!
@@ -52,10 +58,10 @@ export default {
52
58
 
53
59
  ### Syntax
54
60
 
55
- - Single-line directives only: `// #if <expression>`, `// #else`, `// #elif <expression>` and `// #endif`.
61
+ - Single-line directives only: `// #if <expression>`, `// #else`, `// #elseif <expression>` and `// #endif`.
56
62
  - The `<expression>` is evaluated at build time with the keys from `variables` available as identifiers.
57
63
  - You can write **literally ANY JavaScript expressions** in it, because it is evaluated as an IIFE (Immediately Invoked Function Expression).
58
- - Supported directives: `#if`, `#else`, `#elif`, `#endif` (similar to C/C++ style conditional compilation).
64
+ - Supported directives: `#if`, `#else`, `#elseif`, `#endif` (similar to C/C++ style conditional compilation).
59
65
  - Since it is `if/else` , it follows the syntax of `if/else` statements
60
66
 
61
67
  ### Example
@@ -90,6 +96,7 @@ console.log('always');
90
96
  - Reason 1: block comments can span multiple lines with `*` ahead and may contain nested comments, making parsing more complex and error-prone.
91
97
  - Reason 2: For consistency and simplicity.
92
98
  - **Precise Evaluation**: Expressions are executed using the Function constructor — do not pass untrusted input or rely on side effects.
99
+ - **Expression Cache**: By default, compiled expression functions are cached by expression string (`expressionCache: true`).
93
100
 
94
101
  ## License
95
102
 
package/dist/index.d.ts CHANGED
@@ -1,21 +1,30 @@
1
1
  import { Plugin } from 'rollup';
2
- import * as acorn from 'acorn';
2
+
3
+ declare const ECMA_VERSIONS: (string | number)[];
3
4
 
4
5
  interface RollupConditionalCompilationOptions {
5
- /**
6
- * Variables to be used in the expressions `#if` or `#elif`
7
- */
8
- variables: Record<string, unknown>;
9
- /**
10
- * Will be passed to Acorn
11
- * - default: 'module'
12
- */
13
- sourceType: 'script' | 'module';
14
- /**
15
- * Will be passed to Acorn
16
- * - default: 'latest'
17
- */
18
- ecmaVersion: acorn.ecmaVersion;
6
+ /**
7
+ * Variables to be used in the expressions `#if` or `#elif`
8
+ */
9
+ variables: Record<string, unknown>;
10
+
11
+ /**
12
+ * Will be passed to Acorn
13
+ * - default: 'module'
14
+ */
15
+ sourceType: 'script' | 'module';
16
+
17
+ /**
18
+ * Will be passed to Acorn
19
+ * - default: 'latest'
20
+ */
21
+ ecmaVersion: (typeof ECMA_VERSIONS)[number];
22
+
23
+ /**
24
+ * Cache compiled expression functions by expression string.
25
+ * - default: true
26
+ */
27
+ expressionCache: boolean;
19
28
  }
20
29
 
21
30
  /**
@@ -24,14 +33,14 @@ interface RollupConditionalCompilationOptions {
24
33
  * @param options options of the plugin
25
34
  *
26
35
  * ## About
27
- * @package
36
+ * @package ConditionalCompilation
28
37
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
29
- * @version 1.0.6 (Last Update: 2025.10.09 06:41:12.284)
38
+ * @version 2.0.0 (Last Update: 2026.02.14 20:21:12.439)
30
39
  * @license MIT
31
40
  * @link https://github.com/baendlorel/rollup-plugin-conditional-compilation
32
41
  * @link https://baendlorel.github.io/ Welcome to my site!
33
42
  * @description Conditional Compilation macros like #if #endif in C++.
34
- * @copyright Copyright (c) 2025 Kasukabe Tsumugi. All rights reserved.
43
+ * @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
35
44
  */
36
45
  declare function conditionalCompilation(options?: Partial<RollupConditionalCompilationOptions>): Plugin;
37
46