@variant-forge/unplugin 0.1.2 → 0.1.4
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 +115 -0
- package/dist/chunk-CUXQPZR7.js +60 -0
- package/dist/esbuild.d.ts +6 -3
- package/dist/esbuild.js +9 -2
- package/dist/index.d.ts +6 -4
- package/dist/index.js +8 -55
- package/dist/rollup.d.ts +6 -3
- package/dist/rollup.js +9 -2
- package/dist/vite.d.ts +6 -3
- package/dist/vite.js +9 -2
- package/dist/webpack.d.ts +6 -3
- package/dist/webpack.js +9 -2
- package/package.json +6 -5
- package/dist/esbuild.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/rollup.d.ts.map +0 -1
- package/dist/vite.d.ts.map +0 -1
- package/dist/webpack.d.ts.map +0 -1
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._
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createUnplugin } from "unplugin";
|
|
3
|
+
import { VariantNodeClient } from "@variant-forge/sdk-node";
|
|
4
|
+
var virtualModuleId = "virtual:variant-forge";
|
|
5
|
+
var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
6
|
+
var unplugin = createUnplugin((options) => {
|
|
7
|
+
if (!options || !options.sdkKey || !options.apiUrl) {
|
|
8
|
+
throw new Error("Variant Forge unplugin requires sdkKey and apiUrl");
|
|
9
|
+
}
|
|
10
|
+
const fetchedConfigs = {};
|
|
11
|
+
return {
|
|
12
|
+
name: "variant-forge-unplugin",
|
|
13
|
+
async buildStart() {
|
|
14
|
+
const client = new VariantNodeClient({
|
|
15
|
+
sdkKey: options.sdkKey,
|
|
16
|
+
apiUrl: options.apiUrl
|
|
17
|
+
});
|
|
18
|
+
await client.sync();
|
|
19
|
+
const context = options.context || {};
|
|
20
|
+
for (const key of options.configs || []) {
|
|
21
|
+
fetchedConfigs[key] = client.getConfig(key, context);
|
|
22
|
+
if (fetchedConfigs[key] === void 0) {
|
|
23
|
+
fetchedConfigs[key] = client.isFeatureEnabled(key, context);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
client.close();
|
|
27
|
+
},
|
|
28
|
+
resolveId(id) {
|
|
29
|
+
if (id === virtualModuleId) {
|
|
30
|
+
return resolvedVirtualModuleId;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
},
|
|
34
|
+
load(id) {
|
|
35
|
+
if (id === resolvedVirtualModuleId) {
|
|
36
|
+
let code = "";
|
|
37
|
+
for (const [key, value] of Object.entries(fetchedConfigs)) {
|
|
38
|
+
code += `export const ${key} = ${JSON.stringify(value)};
|
|
39
|
+
`;
|
|
40
|
+
}
|
|
41
|
+
code += `export default {
|
|
42
|
+
`;
|
|
43
|
+
for (const key of Object.keys(fetchedConfigs)) {
|
|
44
|
+
code += ` ${key},
|
|
45
|
+
`;
|
|
46
|
+
}
|
|
47
|
+
code += `};
|
|
48
|
+
`;
|
|
49
|
+
return code;
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
var index_default = unplugin;
|
|
56
|
+
|
|
57
|
+
export {
|
|
58
|
+
unplugin,
|
|
59
|
+
index_default
|
|
60
|
+
};
|
package/dist/esbuild.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
import { VariantUnpluginOptions } from './index.js';
|
|
3
|
+
|
|
4
|
+
declare const _default: (options: VariantUnpluginOptions) => unplugin.EsbuildPlugin;
|
|
5
|
+
|
|
6
|
+
export { _default as default };
|
package/dist/esbuild.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import * as unplugin$1 from 'unplugin';
|
|
2
|
+
|
|
3
|
+
interface VariantUnpluginOptions {
|
|
2
4
|
/**
|
|
3
5
|
* The Server SDK Key for your environment.
|
|
4
6
|
*/
|
|
@@ -13,6 +15,6 @@ export interface VariantUnpluginOptions {
|
|
|
13
15
|
*/
|
|
14
16
|
context?: Record<string, string | number | boolean | null>;
|
|
15
17
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
declare const unplugin: unplugin$1.UnpluginInstance<VariantUnpluginOptions, boolean>;
|
|
19
|
+
|
|
20
|
+
export { type VariantUnpluginOptions, unplugin as default, unplugin };
|
package/dist/index.js
CHANGED
|
@@ -1,55 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const fetchedConfigs = {};
|
|
10
|
-
return {
|
|
11
|
-
name: 'variant-forge-unplugin',
|
|
12
|
-
async buildStart() {
|
|
13
|
-
const client = new VariantNodeClient({
|
|
14
|
-
sdkKey: options.sdkKey,
|
|
15
|
-
apiUrl: options.apiUrl,
|
|
16
|
-
});
|
|
17
|
-
// Use sync to fetch once without starting the background polling interval
|
|
18
|
-
await client.sync();
|
|
19
|
-
const context = options.context || {};
|
|
20
|
-
for (const key of options.configs || []) {
|
|
21
|
-
fetchedConfigs[key] = client.getConfig(key, context);
|
|
22
|
-
// Also check if it's a feature flag if getConfig returned undefined, or assume it's merged
|
|
23
|
-
if (fetchedConfigs[key] === undefined) {
|
|
24
|
-
fetchedConfigs[key] = client.isFeatureEnabled(key, context);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
// Cleanup to make sure process can exit
|
|
28
|
-
client.close();
|
|
29
|
-
},
|
|
30
|
-
resolveId(id) {
|
|
31
|
-
if (id === virtualModuleId) {
|
|
32
|
-
return resolvedVirtualModuleId;
|
|
33
|
-
}
|
|
34
|
-
return null;
|
|
35
|
-
},
|
|
36
|
-
load(id) {
|
|
37
|
-
if (id === resolvedVirtualModuleId) {
|
|
38
|
-
// Generate the JS module exporting the fetched configs
|
|
39
|
-
let code = '';
|
|
40
|
-
for (const [key, value] of Object.entries(fetchedConfigs)) {
|
|
41
|
-
code += `export const ${key} = ${JSON.stringify(value)};\n`;
|
|
42
|
-
}
|
|
43
|
-
// Also provide a default export containing all keys
|
|
44
|
-
code += `export default {\n`;
|
|
45
|
-
for (const key of Object.keys(fetchedConfigs)) {
|
|
46
|
-
code += ` ${key},\n`;
|
|
47
|
-
}
|
|
48
|
-
code += `};\n`;
|
|
49
|
-
return code;
|
|
50
|
-
}
|
|
51
|
-
return null;
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
});
|
|
55
|
-
export default unplugin;
|
|
1
|
+
import {
|
|
2
|
+
index_default,
|
|
3
|
+
unplugin
|
|
4
|
+
} from "./chunk-CUXQPZR7.js";
|
|
5
|
+
export {
|
|
6
|
+
index_default as default,
|
|
7
|
+
unplugin
|
|
8
|
+
};
|
package/dist/rollup.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
import { VariantUnpluginOptions } from './index.js';
|
|
3
|
+
|
|
4
|
+
declare const _default: (options: VariantUnpluginOptions) => unplugin.RollupPlugin<any> | unplugin.RollupPlugin<any>[];
|
|
5
|
+
|
|
6
|
+
export { _default as default };
|
package/dist/rollup.js
CHANGED
package/dist/vite.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import * as unplugin from 'unplugin';
|
|
2
|
+
import { VariantUnpluginOptions } from './index.js';
|
|
3
|
+
|
|
4
|
+
declare const _default: (options: VariantUnpluginOptions) => unplugin.VitePlugin<any> | unplugin.VitePlugin<any>[];
|
|
5
|
+
|
|
6
|
+
export { _default as default };
|
package/dist/vite.js
CHANGED
package/dist/webpack.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { VariantUnpluginOptions } from './index.js';
|
|
2
|
+
import 'unplugin';
|
|
3
|
+
|
|
4
|
+
declare const _default: (options: VariantUnpluginOptions) => WebpackPluginInstance;
|
|
5
|
+
|
|
6
|
+
export { _default as default };
|
package/dist/webpack.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@variant-forge/unplugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -10,17 +10,18 @@
|
|
|
10
10
|
],
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"unplugin": "^3.4.0",
|
|
13
|
-
"@variant-forge/sdk-node": "0.1.
|
|
13
|
+
"@variant-forge/sdk-node": "0.1.3"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/node": "^22.15.3",
|
|
17
17
|
"eslint": "^9.39.1",
|
|
18
|
+
"tsup": "^8.4.0",
|
|
18
19
|
"typescript": "5.9.2",
|
|
19
|
-
"@package/
|
|
20
|
-
"@package/
|
|
20
|
+
"@package/eslint-config": "0.0.0",
|
|
21
|
+
"@package/typescript-config": "0.0.0"
|
|
21
22
|
},
|
|
22
23
|
"scripts": {
|
|
23
|
-
"build": "
|
|
24
|
+
"build": "tsup src/index.ts src/vite.ts src/webpack.ts src/rollup.ts src/esbuild.ts --format esm --dts --clean",
|
|
24
25
|
"lint": "eslint . --max-warnings 0",
|
|
25
26
|
"check-types": "tsc --noEmit",
|
|
26
27
|
"release": "pnpm publish --access public"
|
package/dist/esbuild.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"esbuild.d.ts","sourceRoot":"","sources":["../src/esbuild.ts"],"names":[],"mappings":";AACA,wBAAgC"}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf;;OAEG;IACH,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,CAAC;CAC5D;AAKD,eAAO,MAAM,QAAQ,sEA6DnB,CAAC;AAEH,eAAe,QAAQ,CAAC"}
|
package/dist/rollup.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rollup.d.ts","sourceRoot":"","sources":["../src/rollup.ts"],"names":[],"mappings":";AACA,wBAA+B"}
|
package/dist/vite.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../src/vite.ts"],"names":[],"mappings":";AACA,wBAA6B"}
|
package/dist/webpack.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"webpack.d.ts","sourceRoot":"","sources":["../src/webpack.ts"],"names":[],"mappings":";AACA,wBAAgC"}
|