feat-flag-drupal 0.0.4 → 0.1.1

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
@@ -4,10 +4,12 @@ This project is a simple versioning system for web assets. It uses Vite to bundl
4
4
 
5
5
  ## Development vs. Production
6
6
 
7
- This project has two main modes:
7
+ This project has multiple build modes:
8
8
 
9
- * **Development (`npm run dev`):** Uses a standard `index.html` file for a fast and reliable development experience with Hot Module Replacement (HMR).
10
- * **Production Build (`npm run build`):** Uses `index.twig` as the source, compiles it to HTML, and generates versioned assets in the `build/` directory.
9
+ * **Development with HMR (`npm run dev`):** Uses a standard `index.html` file for a fast development experience with Hot Module Replacement.
10
+ * **Development Build (`npm run dev:build`):** Rebuilds the current version without incrementing - useful for testing changes to the latest version.
11
+ * **Production Build (`npm run build`):** Rebuilds the current version (same as `dev:build`).
12
+ * **Create New Version (`npm run build:version`):** Increments the version and creates new versioned assets.
11
13
 
12
14
  ## How to Use
13
15
 
@@ -17,33 +19,47 @@ This project has two main modes:
17
19
  ```
18
20
 
19
21
  2. **Develop your application:**
20
- * Run `npm run dev`.
22
+ * Run `npm run dev` for live development with HMR.
21
23
  * Make changes to your assets in the `src/` directory.
22
24
  * The browser will automatically update as you save files.
23
25
 
24
- 3. **Prepare for production:**
25
- * Modify `index.twig` to structure your final production HTML. You can use Twig syntax here.
26
+ 3. **Test your changes:**
27
+ * Run `npm run dev:build` or `npm run build` to rebuild the current version.
28
+ * This lets you test the production build without creating a new version.
26
29
 
27
- 4. **Build a new version:**
28
- * When you are ready to create a new version of your assets, run the following command:
30
+ 4. **Create a new version:**
31
+ * When you are ready to release a new version, run:
29
32
  ```bash
30
- npm run build
33
+ npm run build:version
31
34
  ```
35
+ * This will increment the patch version and create new versioned assets.
32
36
 
33
37
  ## How the Build Process Works
34
38
 
35
- The `npm run build` command does the following:
39
+ ### Rebuilding Current Version (`npm run build` or `npm run dev:build`)
36
40
 
37
- 1. **Determines New Version:** It checks for a `build/manifest.json`. If it exists, it increments the `latest_version` number to create a new version. If not, it uses the version from `package.json`. The `package.json` file itself is not modified.
38
- 2. **Builds Assets:** It runs Vite to build and bundle your assets, compiling your `index.twig` file into a final `index.html`.
39
- 3. **Outputs Versioned Files:** The output files are placed in the `build/` directory. The filenames will include the new version number, for example:
41
+ These commands rebuild the current version without incrementing:
42
+
43
+ 1. **Uses Current Version:** Reads the `latest_version` from `build/manifest.json` (or `package.json` if no manifest exists).
44
+ 2. **Builds Assets:** Runs Vite to build and bundle your assets.
45
+ 3. **Outputs Versioned Files:** Overwrites the existing versioned files in the `build/` directory.
46
+ 4. **Updates Manifest:** Updates the manifest entry for the current version.
47
+
48
+ Use this during development when you want to iterate on the current version without creating new version numbers.
49
+
50
+ ### Creating New Version (`npm run build:version`)
51
+
52
+ This command creates a new version:
53
+
54
+ 1. **Increments Version:** Reads `latest_version` from `build/manifest.json` and increments the patch number (e.g., 0.0.1 → 0.0.2).
55
+ 2. **Builds Assets:** Runs Vite to build and bundle your assets.
56
+ 3. **Outputs Versioned Files:** Creates new files with the incremented version number:
40
57
  * `main-0.0.2.js`
41
58
  * `style-0.0.2.css`
42
- 4. **Generates a Manifest:** It creates or updates a `build/manifest.json` file.
59
+ 4. **Updates Manifest:** Adds the new version to `build/manifest.json` and updates `latest_version`.
43
60
 
44
- ## Twig Support
61
+ Use this when you're ready to release a new version to production.
45
62
 
46
- This project uses `vite-plugin-twig-drupal` to compile `.twig` files during the **build process only**. You can write your final production markup in `index.twig` and it will be compiled to `index.html` in the `build` directory.
47
63
 
48
64
  ## Feature Flagging and the Manifest
49
65
 
@@ -69,8 +85,126 @@ Here is an example of the manifest structure:
69
85
 
70
86
  Your feature flagging system can consume this file to dynamically load the correct assets for a given version.
71
87
 
72
- ## Publishing as an NPM Package
73
88
 
74
- This project is set up to be published as an NPM package. The `files` property in `package.json` is configured to only include the `build` directory when you publish the package.
89
+ # Integrating Version Builder with a Drupal Theme
90
+
91
+ This guide explains how to integrate the `version-builder` project into a Drupal theme to manage versioned CSS and JavaScript assets.
92
+
93
+ ### 1. Place Your Project Inside Your Drupal Theme
94
+
95
+ Move your entire `version-builder` project into a subdirectory within your Drupal theme. For example:
96
+
97
+ ```
98
+ /themes/custom/my_theme/
99
+ ├── my_theme.info.yml
100
+ ├── my_theme.libraries.yml
101
+ ├── my_theme.theme
102
+ ├── css/
103
+ ├── js/
104
+ ├── templates/
105
+ └── version-builder/ <-- Your project goes here
106
+ ├── src/
107
+ ├── build/
108
+ ├── scripts/
109
+ ├── package.json
110
+ └── ...
111
+ ```
112
+
113
+ ### 2. The Build Process
114
+
115
+ Your build process remains the same. You will navigate into the `version-builder` directory and run the build command:
116
+
117
+ ```bash
118
+ cd /path/to/your/theme/version-builder
119
+ npm run build
120
+ ```
121
+
122
+ This will generate the versioned assets and the `manifest.json` inside `/themes/custom/my_theme/version-builder/build/`.
123
+
124
+ ### 3. Integrate with Drupal's Library System
125
+
126
+ This is the most critical part. You need to make Drupal aware of your versioned files. You'll do this by dynamically reading the `manifest.json` and telling Drupal which files to load.
127
+
128
+ **A. Define a placeholder library in `my_theme.libraries.yml`:**
129
+
130
+ Create a library entry that you will override dynamically.
131
+
132
+ ```yaml
133
+ # my_theme.libraries.yml
134
+ versioned-assets:
135
+ js:
136
+ # This is just a placeholder. It will be replaced.
137
+ js/placeholder.js: {}
138
+ css:
139
+ # This is just a placeholder. It will be replaced.
140
+ theme:
141
+ css/placeholder.css: {}
142
+ ```
143
+
144
+ **B. Read the manifest and alter the library in `my_theme.theme`:**
145
+
146
+ You'll use a hook, like `hook_page_attachments_alter`, to read your `manifest.json` and swap out the placeholder assets with the real, versioned ones.
147
+
148
+ ```php
149
+ <?php
150
+ // my_theme.theme
151
+
152
+ /**
153
+ * Implements hook_page_attachments_alter().
154
+ */
155
+ function my_theme_page_attachments_alter(array &$attachments) {
156
+ // 1. Decide which version to load.
157
+ // This is where your feature flagging logic goes. For example, you could use a query
158
+ // parameter, a config setting, or check the user's role.
159
+ // For this example, we'll just load the 'latest_version'.
160
+ $version_to_load = NULL;
161
+
162
+ // 2. Read and parse the manifest.json.
163
+ $manifest_path = \Drupal::service('extension.path.resolver')->getPath('theme', 'my_theme') . '/version-builder/build/manifest.json';
164
+ if (file_exists($manifest_path)) {
165
+ $manifest = json_decode(file_get_contents($manifest_path), TRUE);
166
+
167
+ // Get the version key from your logic.
168
+ $version_to_load = $manifest['latest_version']; // Or get from a feature flag service.
169
+
170
+ if (isset($manifest['versions'][$version_to_load])) {
171
+ $assets = $manifest['versions'][$version_to_load];
172
+
173
+ // 3. Dynamically override the library definition.
174
+ // The key 'my_theme/versioned-assets' matches the library defined in your .libraries.yml file.
175
+ $attachments['#attached']['library']['my_theme/versioned-assets'] = [
176
+ 'js' => [
177
+ // The path is relative to your Drupal root.
178
+ 'themes/custom/my_theme/version-builder/build' . $assets['main.js'] => [],
179
+ ],
180
+ 'css' => [
181
+ 'theme' => [
182
+ 'themes/custom/my_theme/version-builder/build' . $assets['style.css'] => [],
183
+ ],
184
+ ],
185
+ ];
186
+ }
187
+ }
188
+ }
189
+ ```
190
+
191
+ ### 4. Attach the Library in Your Twig Templates
192
+
193
+ Finally, in your Drupal theme's Twig templates (e.g., `page.html.twig`), you attach the library as you normally would.
194
+
195
+ ```twig
196
+ {# templates/page.html.twig #}
197
+
198
+ {{ attach_library('my_theme/versioned-assets') }}
199
+
200
+ <div class="page-content">
201
+ ...
202
+ </div>
203
+ ```
204
+
205
+ ### Summary of the Workflow
75
206
 
76
- The `main` entry in `package.json` points to `build/main.js`. After running a build, the actual filename will be versioned (e.g., `build/main-0.0.2.js`). You may need to adjust your package consumption logic to account for the versioned filenames.
207
+ 1. You work on your assets inside the `version-builder/src` directory.
208
+ 2. You run `npm run build` to create a new, versioned set of assets and update the `manifest.json`.
209
+ 3. When a user visits your Drupal site, the `my_theme.theme` file reads the manifest, determines which version to show based on your feature flagging logic, and tells Drupal the exact CSS and JS files to load for that version.
210
+ 4. Drupal attaches those specific, versioned files to the page.
@@ -0,0 +1 @@
1
+ document.addEventListener("DOMContentLoaded",()=>{const e=document.getElementById("app");e.textContent="Hello from version-builder!"});
@@ -1,17 +1,13 @@
1
1
  {
2
2
  "versions": {
3
- "0.0.4": {
4
- "main.js": "/main-0.0.4.js",
5
- "style.css": "/style-0.0.4.css"
3
+ "0.1.0": {
4
+ "main.js": "/main-0.1.0.js",
5
+ "style.css": "/style-0.1.0.css"
6
6
  },
7
- "0.0.5": {
8
- "main.js": "/main-0.0.5.js",
9
- "style.css": "/style-0.0.5.css"
10
- },
11
- "0.0.6": {
12
- "main.js": "/main-0.0.6.js",
13
- "style.css": "/style-0.0.6.css"
7
+ "0.1.1": {
8
+ "main.js": "/main-0.1.1.js",
9
+ "style.css": "/style-0.1.1.css"
14
10
  }
15
11
  },
16
- "latest_version": "0.0.6"
12
+ "latest_version": "0.1.1"
17
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feat-flag-drupal",
3
- "version": "0.0.4",
3
+ "version": "0.1.1",
4
4
  "private": false,
5
5
  "main": "build/main.js",
6
6
  "files": [
@@ -9,11 +9,15 @@
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "dev": "vite",
12
+ "dev:build": "node scripts/dev-build.js",
12
13
  "build": "node scripts/build.js",
14
+ "build:version": "node scripts/version.js",
13
15
  "preview": "vite preview"
14
16
  },
15
17
  "devDependencies": {
16
18
  "sass-embedded": "^1.98.0",
17
19
  "vite": "^7.0.0"
18
- }
20
+ },
21
+ "license": "MIT",
22
+ "description": "A tool to build and manage versioned assets for Drupal themes, enabling feature flagging and A/B testing."
19
23
  }
@@ -1,66 +0,0 @@
1
- /* empty css */document.querySelector("#app").innerHTML=`
2
- <pre>
3
- <code>
4
- # Version Builder
5
-
6
- This project is a simple versioning system for web assets. It uses Vite to bundle your code and a custom script to automatically increment the version number with each build.
7
-
8
- ## How to Use
9
-
10
- 1. **Place your code:** Put your HTML, CSS, JavaScript, and other asset files in the root directory of the project. The main entry point for the application is \`index.html\`.
11
-
12
- 2. **Install dependencies:**
13
- \`\`\`bash
14
- npm install
15
- \`\`\`
16
-
17
- 3. **Modify your code:** Make any changes you need to your source files.
18
-
19
- 4. **Build a new version:** When you are ready to create a new version of your assets, run the following command:
20
- \`\`\`bash
21
- npm run build
22
- \`\`\`
23
-
24
- ## How it Works
25
-
26
- The \`npm run build\` command does the following:
27
-
28
- 1. **Increments Version:** It automatically increments the \`patch\` number (the last digit) of the version in \`package.json\`. For example, \`0.0.1\` becomes \`0.0.2\`.
29
- 2. **Builds Assets:** It runs Vite to build and bundle your assets.
30
- 3. **Outputs Versioned Files:** The output files are placed in the \`build/\` directory. The filenames will include the new version number, for example:
31
- * \`main-0.0.2.js\`
32
- * \`style-0.0.2.css\`
33
- 4. **Generates a Manifest:** It creates or updates a \`build/manifest.json\` file.
34
-
35
- ## Feature Flagging and the Manifest
36
-
37
- To support feature flagging systems, the build process generates a \`manifest.json\` file. This file acts as a single source of truth for your system to look up which assets belong to which version.
38
-
39
- Here is an example of the manifest structure:
40
-
41
- \`\`\`json
42
- {
43
- "versions": {
44
- "0.0.1": {
45
- "main.js": "/main-0.0.1.js",
46
- "style.css": "/style-0.0.1.css"
47
- },
48
- "0.0.2": {
49
- "main.js": "/main-0.0.2.js",
50
- "style.css": "/style-0.0.2.css"
51
- }
52
- },
53
- "latest_version": "0.0.2"
54
- }
55
- \`\`\`
56
-
57
- Your feature flagging system can consume this file to dynamically load the correct assets for a given version.
58
-
59
- ## Publishing as an NPM Package
60
-
61
- This project is set up to be published as an NPM package. The \`files\` property in \`package.json\` is configured to only include the \`build\` directory when you publish the package.
62
-
63
- The \`main\` entry in \`package.json\` points to \`build/main.js\`. After running a build, the actual filename will be versioned (e.g., \`build/main-0.0.2.js\`). You may need to adjust your package consumption logic to account for the versioned filenames.
64
- </code>
65
- </pre>
66
- `;
@@ -1,66 +0,0 @@
1
- document.querySelector("#app").innerHTML=`
2
- <pre>
3
- <code>
4
- # Version Builder
5
-
6
- This project is a simple versioning system for web assets. It uses Vite to bundle your code and a custom script to automatically increment the version number with each build.
7
-
8
- ## How to Use
9
-
10
- 1. **Place your code:** Put your HTML, CSS, JavaScript, and other asset files in the root directory of the project. The main entry point for the application is \`index.html\`.
11
-
12
- 2. **Install dependencies:**
13
- \`\`\`bash
14
- npm install
15
- \`\`\`
16
-
17
- 3. **Modify your code:** Make any changes you need to your source files.
18
-
19
- 4. **Build a new version:** When you are ready to create a new version of your assets, run the following command:
20
- \`\`\`bash
21
- npm run build
22
- \`\`\`
23
-
24
- ## How it Works
25
-
26
- The \`npm run build\` command does the following:
27
-
28
- 1. **Increments Version:** It automatically increments the \`patch\` number (the last digit) of the version in \`package.json\`. For example, \`0.0.1\` becomes \`0.0.2\`.
29
- 2. **Builds Assets:** It runs Vite to build and bundle your assets.
30
- 3. **Outputs Versioned Files:** The output files are placed in the \`build/\` directory. The filenames will include the new version number, for example:
31
- * \`main-0.0.2.js\`
32
- * \`style-0.0.2.css\`
33
- 4. **Generates a Manifest:** It creates or updates a \`build/manifest.json\` file.
34
-
35
- ## Feature Flagging and the Manifest
36
-
37
- To support feature flagging systems, the build process generates a \`manifest.json\` file. This file acts as a single source of truth for your system to look up which assets belong to which version.
38
-
39
- Here is an example of the manifest structure:
40
-
41
- \`\`\`json
42
- {
43
- "versions": {
44
- "0.0.1": {
45
- "main.js": "/main-0.0.1.js",
46
- "style.css": "/style-0.0.1.css"
47
- },
48
- "0.0.2": {
49
- "main.js": "/main-0.0.2.js",
50
- "style.css": "/style-0.0.2.css"
51
- }
52
- },
53
- "latest_version": "0.0.2"
54
- }
55
- \`\`\`
56
-
57
- Your feature flagging system can consume this file to dynamically load the correct assets for a given version.
58
-
59
- ## Publishing as an NPM Package
60
-
61
- This project is set up to be published as an NPM package. The \`files\` property in \`package.json\` is configured to only include the \`build\` directory when you publish the package.
62
-
63
- The \`main\` entry in \`package.json\` points to \`build/main.js\`. After running a build, the actual filename will be versioned (e.g., \`build/main-0.0.2.js\`). You may need to adjust your package consumption logic to account for the versioned filenames.
64
- </code>
65
- </pre>
66
- `;
@@ -1 +0,0 @@
1
- :root{--text: #6b6375;--text-h: #08060d;--bg: #fff;--border: #e5e4e7;--code-bg: #f4f3ec;--accent: #aa3bff;--accent-bg: rgba(170, 59, 255, .1);--accent-border: rgba(170, 59, 255, .5);--social-bg: rgba(244, 243, 236, .5);--shadow: rgba(0, 0, 0, .1) 0 10px 15px -3px, rgba(0, 0, 0, .05) 0 4px 6px -2px;--sans: system-ui, "Segoe UI", Roboto, sans-serif;--heading: system-ui, "Segoe UI", Roboto, sans-serif;--mono: ui-monospace, Consolas, monospace;font:18px/145% var(--sans);letter-spacing:.18px;color-scheme:light dark;color:var(--text);background:var(--bg);font-synthesis:none;text-rendering:optimizeLegibility;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}@media(max-width:1024px){:root{font-size:16px}}@media(prefers-color-scheme:dark){:root{--text: #9ca3af;--text-h: #f3f4f6;--bg: #16171d;--border: #2e303a;--code-bg: #1f2028;--accent: #c084fc;--accent-bg: rgba(192, 132, 252, .15);--accent-border: rgba(192, 132, 252, .5);--social-bg: rgba(47, 48, 58, .5);--shadow: rgba(0, 0, 0, .4) 0 10px 15px -3px, rgba(0, 0, 0, .25) 0 4px 6px -2px}#social .button-icon{filter:invert(1) brightness(2)}}body{margin:0}h1,h2{font-family:var(--heading);font-weight:500;color:var(--text-h)}h1{font-size:56px;letter-spacing:-1.68px;margin:32px 0}@media(max-width:1024px){h1{font-size:36px;margin:20px 0}}h2{font-size:24px;line-height:118%;letter-spacing:-.24px;margin:0 0 8px}@media(max-width:1024px){h2{font-size:20px}}p{margin:0}code{font-family:var(--mono);display:flex;flex-wrap:wrap;border-radius:4px;max-width:80vw;color:var(--text-h);overflow-x:auto}code{font-size:15px;line-height:135%;padding:4px 8px;background:var(--code-bg)}.counter{font-size:16px;padding:5px 10px;border-radius:5px;color:var(--accent);background:var(--accent-bg);border:2px solid transparent;transition:border-color .3s;margin-bottom:24px}.counter:hover{border-color:var(--accent-border)}.counter:focus-visible{outline:2px solid var(--accent);outline-offset:2px}.hero{position:relative}.hero .base,.hero .framework,.hero .vite{inset-inline:0;margin:0 auto}.hero .base{width:170px;position:relative;z-index:0}.hero .framework,.hero .vite{position:absolute}.hero .framework{z-index:1;top:34px;height:28px;transform:perspective(2000px) rotate(300deg) rotateX(44deg) rotateY(39deg) scale(1.4)}.hero .vite{z-index:0;top:107px;height:26px;width:auto;transform:perspective(2000px) rotate(300deg) rotateX(40deg) rotateY(39deg) scale(.8)}#app{max-width:100%;margin:0 auto;border-inline:1px solid var(--border);min-height:100svh;display:flex;flex-direction:column;box-sizing:border-box}#center{display:flex;flex-direction:column;gap:25px;place-content:center;place-items:center;flex-grow:1}@media(max-width:1024px){#center{padding:32px 20px 24px;gap:18px}}#next-steps{display:flex;border-top:1px solid var(--border);text-align:left}#next-steps>div{flex:1 1 0;padding:32px}@media(max-width:1024px){#next-steps>div{padding:24px 20px}}#next-steps .icon{margin-bottom:16px;width:22px;height:22px}@media(max-width:1024px){#next-steps{flex-direction:column;text-align:center}}#docs{border-right:1px solid var(--border)}@media(max-width:1024px){#docs{border-right:none;border-bottom:1px solid var(--border)}}#next-steps ul{list-style:none;padding:0;display:flex;gap:8px;margin:32px 0 0}#next-steps ul .logo{height:18px}#next-steps ul a{color:var(--text-h);font-size:16px;border-radius:6px;background:var(--social-bg);display:flex;padding:6px 12px;align-items:center;gap:8px;text-decoration:none;transition:box-shadow .3s}#next-steps ul a:hover{box-shadow:var(--shadow)}#next-steps ul a .button-icon{height:18px;width:18px}@media(max-width:1024px){#next-steps ul{margin-top:20px;flex-wrap:wrap;justify-content:center}#next-steps ul li{flex:1 1 calc(50% - 8px)}#next-steps ul a{width:100%;justify-content:center;box-sizing:border-box}}#spacer{height:88px;border-top:1px solid var(--border)}@media(max-width:1024px){#spacer{height:48px}}.ticks{position:relative;width:100%}.ticks:before,.ticks:after{content:"";position:absolute;top:-4.5px;border:5px solid transparent}.ticks:before{left:0;border-left-color:var(--border)}.ticks:after{right:0;border-right-color:var(--border)}
File without changes
File without changes
File without changes