@variant-forge/unplugin 0.1.3 → 0.1.5

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 ADDED
@@ -0,0 +1,115 @@
1
+ # Variant Forge Unplugin
2
+
3
+ This package provides a unified build plugin (for Vite, Webpack, Rollup, and esbuild) that evaluates feature flags and configurations from Variant Forge **at build time**.
4
+
5
+ It uses the Node SDK to fetch and evaluate rules during the build process, then injects the evaluated values into your application as a virtual module (`virtual:variant-forge`). This allows you to bundle specific static configurations or feature flags directly into your client bundle, avoiding the need for an initial network request on the client.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @variant-forge/unplugin -D
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### 1. Configure the Plugin
16
+
17
+ Add the plugin to your bundler configuration. Below is an example for **Vite**:
18
+
19
+ ```typescript
20
+ // vite.config.ts
21
+ import { defineConfig } from 'vite';
22
+ import variantForge from '@variant-forge/unplugin/vite';
23
+
24
+ export default defineConfig({
25
+ plugins: [
26
+ variantForge({
27
+ // Your Server SDK key
28
+ sdkKey: process.env.VARIANT_SERVER_KEY,
29
+ apiUrl: 'http://localhost:3000',
30
+
31
+ // The keys of configs/flags to fetch and bundle
32
+ configs: ['max_retries', 'new_ui_v2'],
33
+
34
+ // Optional: A static context to use for evaluation at build time
35
+ context: { platform: 'web', env: 'production' },
36
+ }),
37
+ ],
38
+ });
39
+ ```
40
+
41
+ _Note: You can import from `@variant-forge/unplugin/webpack`, `/rollup`, or `/esbuild` depending on your bundler._
42
+
43
+ ### 2. Add TypeScript Definitions
44
+
45
+ If you are using TypeScript, you need to tell it about the virtual module. You can add the provided definitions to your `tsconfig.json` or `vite-env.d.ts` (or similar file):
46
+
47
+ ```typescript
48
+ /// <reference types="@variant-forge/unplugin/client" />
49
+ ```
50
+
51
+ ### 3. Consume in your Application
52
+
53
+ You can now import the virtual module directly in your application code. The module will contain the evaluated configurations and flags exactly as they were resolved during the build.
54
+
55
+ ```typescript
56
+ // Import all configurations as a default export
57
+ import variantConfig from 'virtual:variant-forge';
58
+
59
+ console.log(variantConfig.max_retries);
60
+ console.log(variantConfig.new_ui_v2);
61
+
62
+ // Or import specific keys as named exports
63
+ import { max_retries, new_ui_v2 } from 'virtual:variant-forge';
64
+
65
+ if (new_ui_v2) {
66
+ console.log('Running the new UI!');
67
+ }
68
+ ```
69
+
70
+ ### Tree-Shaking Support
71
+
72
+ Because the virtual module exports static constants, modern bundlers (Vite, Rollup, Webpack) will automatically perform **tree-shaking (dead code elimination)** on unreachable branches.
73
+
74
+ ```typescript
75
+ import { NEW_DASHBOARD } from 'virtual:variant-forge';
76
+
77
+ if (NEW_DASHBOARD) {
78
+ // If NEW_DASHBOARD is evaluated as false during build time,
79
+ // this code is stripped out completely from your bundle!
80
+ initNewDashboard();
81
+ }
82
+ ```
83
+
84
+ ### Global Environment Variables (Optional)
85
+
86
+ If you prefer to use global variables instead of importing a virtual module, you can enable `injectEnv`. This will inject the fetched configurations directly into the bundler's global definitions.
87
+
88
+ ```typescript
89
+ variantForge({
90
+ sdkKey: process.env.VARIANT_SERVER_KEY,
91
+ apiUrl: 'http://localhost:3000',
92
+ configs: ['NEW_DASHBOARD'],
93
+
94
+ // Inject into process.env / import.meta.env
95
+ injectEnv: true,
96
+ // Optional prefix (defaults to 'VARIANT_')
97
+ envPrefix: 'APP_',
98
+ });
99
+ ```
100
+
101
+ Once enabled, you can access the variables directly in your code without any imports:
102
+
103
+ ```typescript
104
+ // With Vite:
105
+ if (import.meta.env.APP_NEW_DASHBOARD) {
106
+ console.log('New dashboard enabled!');
107
+ }
108
+
109
+ // With Webpack or Vite:
110
+ if (process.env.APP_NEW_DASHBOARD) {
111
+ console.log('New dashboard enabled!');
112
+ }
113
+ ```
114
+
115
+ _Note: Tree-shaking applies to global variables as well._
@@ -19,7 +19,7 @@ var unplugin = createUnplugin((options) => {
19
19
  const context = options.context || {};
20
20
  for (const key of options.configs || []) {
21
21
  fetchedConfigs[key] = client.getConfig(key, context);
22
- if (fetchedConfigs[key] === void 0) {
22
+ if (fetchedConfigs[key] === null) {
23
23
  fetchedConfigs[key] = client.isFeatureEnabled(key, context);
24
24
  }
25
25
  }
@@ -49,6 +49,29 @@ var unplugin = createUnplugin((options) => {
49
49
  return code;
50
50
  }
51
51
  return null;
52
+ },
53
+ vite: {
54
+ config() {
55
+ if (!options.injectEnv) return;
56
+ const prefix = options.envPrefix ?? "VARIANT_";
57
+ const defines = {};
58
+ for (const [key, value] of Object.entries(fetchedConfigs)) {
59
+ defines[`import.meta.env.${prefix}${key}`] = JSON.stringify(value);
60
+ defines[`process.env.${prefix}${key}`] = JSON.stringify(value);
61
+ }
62
+ return { define: defines };
63
+ }
64
+ },
65
+ webpack(compiler) {
66
+ if (!options.injectEnv) return;
67
+ const prefix = options.envPrefix ?? "VARIANT_";
68
+ const defines = {};
69
+ for (const [key, value] of Object.entries(fetchedConfigs)) {
70
+ defines[`process.env.${prefix}${key}`] = JSON.stringify(value);
71
+ }
72
+ if (compiler.webpack && compiler.webpack.DefinePlugin) {
73
+ compiler.options.plugins.push(new compiler.webpack.DefinePlugin(defines));
74
+ }
52
75
  }
53
76
  };
54
77
  });
package/dist/esbuild.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unplugin
3
- } from "./chunk-CUXQPZR7.js";
3
+ } from "./chunk-VDS3W3XK.js";
4
4
 
5
5
  // src/esbuild.ts
6
6
  var esbuild_default = unplugin.esbuild;
package/dist/index.d.ts CHANGED
@@ -14,6 +14,15 @@ interface VariantUnpluginOptions {
14
14
  * Optional context for targeting evaluation.
15
15
  */
16
16
  context?: Record<string, string | number | boolean | null>;
17
+ /**
18
+ * If true, also injects the fetched configs as global environment variables.
19
+ * By default, variables are prefixed with 'VARIANT_' (e.g. process.env.VARIANT_FEATURE).
20
+ */
21
+ injectEnv?: boolean;
22
+ /**
23
+ * Prefix used for injected environment variables. Defaults to 'VARIANT_'.
24
+ */
25
+ envPrefix?: string;
17
26
  }
18
27
  declare const unplugin: unplugin$1.UnpluginInstance<VariantUnpluginOptions, boolean>;
19
28
 
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  index_default,
3
3
  unplugin
4
- } from "./chunk-CUXQPZR7.js";
4
+ } from "./chunk-VDS3W3XK.js";
5
5
  export {
6
6
  index_default as default,
7
7
  unplugin
package/dist/rollup.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unplugin
3
- } from "./chunk-CUXQPZR7.js";
3
+ } from "./chunk-VDS3W3XK.js";
4
4
 
5
5
  // src/rollup.ts
6
6
  var rollup_default = unplugin.rollup;
package/dist/vite.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unplugin
3
- } from "./chunk-CUXQPZR7.js";
3
+ } from "./chunk-VDS3W3XK.js";
4
4
 
5
5
  // src/vite.ts
6
6
  var vite_default = unplugin.vite;
package/dist/webpack.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unplugin
3
- } from "./chunk-CUXQPZR7.js";
3
+ } from "./chunk-VDS3W3XK.js";
4
4
 
5
5
  // src/webpack.ts
6
6
  var webpack_default = unplugin.webpack;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@variant-forge/unplugin",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -10,7 +10,7 @@
10
10
  ],
11
11
  "dependencies": {
12
12
  "unplugin": "^3.4.0",
13
- "@variant-forge/sdk-node": "0.1.2"
13
+ "@variant-forge/sdk-node": "0.1.4"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@types/node": "^22.15.3",