v-auto-color 1.0.3 → 1.0.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.
@@ -0,0 +1,131 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+
5
+ // src/vite-plugin.ts
6
+ import { createFilter } from "@rollup/pluginutils";
7
+
8
+ // src/core/hash.ts
9
+ function murmurHash3(text) {
10
+ let h1 = 3735928559;
11
+ const c1 = 3432918353;
12
+ const c2 = 461845907;
13
+ const r1 = 15;
14
+ const r2 = 13;
15
+ const m = 5;
16
+ const n = 3864292196;
17
+ let i = 0;
18
+ const length = text.length;
19
+ let k1 = 0;
20
+ while (i < length) {
21
+ const char = text.charCodeAt(i++);
22
+ k1 = k1 << 8 | char;
23
+ }
24
+ k1 = k1 * c1 >>> 0;
25
+ k1 = (k1 << r1 | k1 >>> 32 - r1) >>> 0;
26
+ k1 = k1 * c2 >>> 0;
27
+ h1 ^= k1;
28
+ h1 = (h1 << r2 | h1 >>> 32 - r2) >>> 0;
29
+ h1 = h1 * m + n >>> 0;
30
+ h1 ^= length;
31
+ h1 ^= h1 >>> 16;
32
+ h1 = h1 * 2246822507 >>> 0;
33
+ h1 ^= h1 >>> 13;
34
+ h1 = h1 * 3266489909 >>> 0;
35
+ h1 ^= h1 >>> 16;
36
+ return h1;
37
+ }
38
+ function getTextHash(text) {
39
+ return murmurHash3(text);
40
+ }
41
+
42
+ // src/core/color.ts
43
+ var ColorGenerator = class {
44
+ constructor(config = {}) {
45
+ __publicField(this, "config");
46
+ this.config = {
47
+ category: config.category || "default",
48
+ hue: config.hue || [0, 360],
49
+ saturation: config.saturation || [70, 100],
50
+ lightness: config.lightness || [40, 60]
51
+ };
52
+ }
53
+ // Generate color from hash value
54
+ generateColor(hash) {
55
+ const { hue, saturation, lightness } = this.config;
56
+ const hueRange = hue[1] - hue[0];
57
+ const calculatedHue = hue[0] + hash % hueRange;
58
+ const satRange = saturation[1] - saturation[0];
59
+ const calculatedSat = saturation[0] + (hash >> 8) % satRange;
60
+ const lightRange = lightness[1] - lightness[0];
61
+ const calculatedLight = lightness[0] + (hash >> 16) % lightRange;
62
+ return `hsl(${calculatedHue}, ${calculatedSat}%, ${calculatedLight}%)`;
63
+ }
64
+ // Get color for text (wrapper method)
65
+ getColor(text, hashFn) {
66
+ const hash = hashFn(text);
67
+ return this.generateColor(hash);
68
+ }
69
+ };
70
+
71
+ // src/vite-plugin.ts
72
+ function viteAutoColorPlugin() {
73
+ const filter = createFilter(["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx", "**/*.vue"]);
74
+ return {
75
+ name: "v-auto-color",
76
+ // Analyze code during build and inject precomputed colors
77
+ transform(code, id) {
78
+ if (!filter(id)) return null;
79
+ if (!code.includes("useAutoColor")) return null;
80
+ const useAutoColorRegex = /useAutoColor\s*\(\s*(?:(['"])([^'"]+)\1|\{[^}]*\})\s*\)/g;
81
+ const getColorRegex = /\.getColor\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
82
+ let match;
83
+ const colorSets = /* @__PURE__ */ new Set();
84
+ while ((match = useAutoColorRegex.exec(code)) !== null) {
85
+ let category = "default";
86
+ if (match[2]) {
87
+ category = match[2];
88
+ } else {
89
+ const configMatch = match[0].match(/category\s*:\s*['"]([^'"]+)['"]/);
90
+ if (configMatch) {
91
+ category = configMatch[1];
92
+ }
93
+ }
94
+ colorSets.add(category);
95
+ }
96
+ const texts = /* @__PURE__ */ new Set();
97
+ while ((match = getColorRegex.exec(code)) !== null) {
98
+ texts.add(match[1]);
99
+ }
100
+ const colorUsage = {};
101
+ colorSets.forEach((category) => {
102
+ if (!colorUsage[category]) {
103
+ colorUsage[category] = {};
104
+ }
105
+ texts.forEach((text) => {
106
+ const generator = new ColorGenerator({ category });
107
+ const color = generator.getColor(text, getTextHash);
108
+ colorUsage[category][text] = color;
109
+ });
110
+ });
111
+ if (Object.keys(colorUsage).length > 0) {
112
+ const precomputedCode = `
113
+ // Precomputed colors for v-auto-color
114
+ import { __internal__setPrecomputedColors } from 'v-auto-color';
115
+ __internal__setPrecomputedColors(${JSON.stringify(colorUsage, null, 2)});
116
+ `;
117
+ code = precomputedCode + "\n" + code;
118
+ }
119
+ return code;
120
+ }
121
+ };
122
+ }
123
+ var vite_plugin_default = viteAutoColorPlugin;
124
+
125
+ export {
126
+ __publicField,
127
+ getTextHash,
128
+ ColorGenerator,
129
+ viteAutoColorPlugin,
130
+ vite_plugin_default
131
+ };
package/dist/index.js CHANGED
@@ -96,12 +96,12 @@ var ColorGenerator = class {
96
96
  var import_pluginutils = require("@rollup/pluginutils");
97
97
  function viteAutoColorPlugin() {
98
98
  const filter = (0, import_pluginutils.createFilter)(["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx", "**/*.vue"]);
99
- const colorUsage = {};
100
99
  return {
101
100
  name: "v-auto-color",
102
- // Analyze code during build
101
+ // Analyze code during build and inject precomputed colors
103
102
  transform(code, id) {
104
103
  if (!filter(id)) return null;
104
+ if (!code.includes("useAutoColor")) return null;
105
105
  const useAutoColorRegex = /useAutoColor\s*\(\s*(?:(['"])([^'"]+)\1|\{[^}]*\})\s*\)/g;
106
106
  const getColorRegex = /\.getColor\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
107
107
  let match;
@@ -122,6 +122,7 @@ function viteAutoColorPlugin() {
122
122
  while ((match = getColorRegex.exec(code)) !== null) {
123
123
  texts.add(match[1]);
124
124
  }
125
+ const colorUsage = {};
125
126
  colorSets.forEach((category) => {
126
127
  if (!colorUsage[category]) {
127
128
  colorUsage[category] = {};
@@ -132,25 +133,15 @@ function viteAutoColorPlugin() {
132
133
  colorUsage[category][text] = color;
133
134
  });
134
135
  });
135
- return null;
136
- },
137
- // Generate precomputed colors module
138
- generateBundle() {
139
- const precomputedCode = `
140
- import { __internal__setPrecomputedColors } from 'v-auto-color';
141
- __internal__setPrecomputedColors(${JSON.stringify(colorUsage, null, 2)});
142
- `;
143
- this.emitFile({
144
- type: "asset",
145
- fileName: "v-auto-color-precomputed.js",
146
- source: precomputedCode
147
- });
148
- },
149
- // Inject precomputed colors into HTML
150
- transformIndexHtml(html) {
151
- return html.replace("</head>", `
152
- <script type="module" src="/v-auto-color-precomputed.js"></script>
153
- </head>`);
136
+ if (Object.keys(colorUsage).length > 0) {
137
+ const precomputedCode = `
138
+ // Precomputed colors for v-auto-color
139
+ import { __internal__setPrecomputedColors } from 'v-auto-color';
140
+ __internal__setPrecomputedColors(${JSON.stringify(colorUsage, null, 2)});
141
+ `;
142
+ code = precomputedCode + "\n" + code;
143
+ }
144
+ return code;
154
145
  }
155
146
  };
156
147
  }
package/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  __publicField,
4
4
  getTextHash,
5
5
  viteAutoColorPlugin
6
- } from "./chunk-NJGGDAAU.mjs";
6
+ } from "./chunk-465SVM6G.mjs";
7
7
 
8
8
  // src/index.ts
9
9
  var precomputedColors = {};
@@ -94,12 +94,12 @@ var ColorGenerator = class {
94
94
  // src/vite-plugin.ts
95
95
  function viteAutoColorPlugin() {
96
96
  const filter = (0, import_pluginutils.createFilter)(["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx", "**/*.vue"]);
97
- const colorUsage = {};
98
97
  return {
99
98
  name: "v-auto-color",
100
- // Analyze code during build
99
+ // Analyze code during build and inject precomputed colors
101
100
  transform(code, id) {
102
101
  if (!filter(id)) return null;
102
+ if (!code.includes("useAutoColor")) return null;
103
103
  const useAutoColorRegex = /useAutoColor\s*\(\s*(?:(['"])([^'"]+)\1|\{[^}]*\})\s*\)/g;
104
104
  const getColorRegex = /\.getColor\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
105
105
  let match;
@@ -120,6 +120,7 @@ function viteAutoColorPlugin() {
120
120
  while ((match = getColorRegex.exec(code)) !== null) {
121
121
  texts.add(match[1]);
122
122
  }
123
+ const colorUsage = {};
123
124
  colorSets.forEach((category) => {
124
125
  if (!colorUsage[category]) {
125
126
  colorUsage[category] = {};
@@ -130,25 +131,15 @@ function viteAutoColorPlugin() {
130
131
  colorUsage[category][text] = color;
131
132
  });
132
133
  });
133
- return null;
134
- },
135
- // Generate precomputed colors module
136
- generateBundle() {
137
- const precomputedCode = `
138
- import { __internal__setPrecomputedColors } from 'v-auto-color';
139
- __internal__setPrecomputedColors(${JSON.stringify(colorUsage, null, 2)});
140
- `;
141
- this.emitFile({
142
- type: "asset",
143
- fileName: "v-auto-color-precomputed.js",
144
- source: precomputedCode
145
- });
146
- },
147
- // Inject precomputed colors into HTML
148
- transformIndexHtml(html) {
149
- return html.replace("</head>", `
150
- <script type="module" src="/v-auto-color-precomputed.js"></script>
151
- </head>`);
134
+ if (Object.keys(colorUsage).length > 0) {
135
+ const precomputedCode = `
136
+ // Precomputed colors for v-auto-color
137
+ import { __internal__setPrecomputedColors } from 'v-auto-color';
138
+ __internal__setPrecomputedColors(${JSON.stringify(colorUsage, null, 2)});
139
+ `;
140
+ code = precomputedCode + "\n" + code;
141
+ }
142
+ return code;
152
143
  }
153
144
  };
154
145
  }
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  viteAutoColorPlugin,
3
3
  vite_plugin_default
4
- } from "./chunk-NJGGDAAU.mjs";
4
+ } from "./chunk-465SVM6G.mjs";
5
5
  export {
6
6
  vite_plugin_default as default,
7
7
  viteAutoColorPlugin
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "v-auto-color",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Vite plugin for automatic color generation based on text similarity",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -6,17 +6,17 @@ import { ColorGenerator } from './core/color';
6
6
  // Vite plugin for precomputing colors at build time
7
7
  export function viteAutoColorPlugin(): Plugin {
8
8
  const filter = createFilter(['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx', '**/*.vue']);
9
-
10
- // Collected color usage data
11
- const colorUsage: Record<string, Record<string, string>> = {};
12
9
 
13
10
  return {
14
11
  name: 'v-auto-color',
15
12
 
16
- // Analyze code during build
13
+ // Analyze code during build and inject precomputed colors
17
14
  transform(code, id) {
18
15
  if (!filter(id)) return null;
19
16
 
17
+ // Check if this file imports useAutoColor
18
+ if (!code.includes('useAutoColor')) return null;
19
+
20
20
  // Extract useAutoColor calls and getColor calls
21
21
  const useAutoColorRegex = /useAutoColor\s*\(\s*(?:(['"])([^'"]+)\1|\{[^}]*\})\s*\)/g;
22
22
  const getColorRegex = /\.getColor\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
@@ -47,6 +47,7 @@ export function viteAutoColorPlugin(): Plugin {
47
47
  }
48
48
 
49
49
  // Precompute colors for extracted texts
50
+ const colorUsage: Record<string, Record<string, string>> = {};
50
51
  colorSets.forEach(category => {
51
52
  if (!colorUsage[category]) {
52
53
  colorUsage[category] = {};
@@ -59,30 +60,19 @@ export function viteAutoColorPlugin(): Plugin {
59
60
  });
60
61
  });
61
62
 
62
- return null;
63
- },
64
-
65
- // Generate precomputed colors module
66
- generateBundle() {
67
- // Create precomputed colors code
68
- const precomputedCode = `
69
- import { __internal__setPrecomputedColors } from 'v-auto-color';
70
- __internal__setPrecomputedColors(${JSON.stringify(colorUsage, null, 2)});
71
- `;
63
+ // Inject precomputed colors into the code
64
+ if (Object.keys(colorUsage).length > 0) {
65
+ const precomputedCode = `
66
+ // Precomputed colors for v-auto-color
67
+ import { __internal__setPrecomputedColors } from 'v-auto-color';
68
+ __internal__setPrecomputedColors(${JSON.stringify(colorUsage, null, 2)});
69
+ `;
72
70
 
73
- // Add precomputed colors module to bundle
74
- this.emitFile({
75
- type: 'asset',
76
- fileName: 'v-auto-color-precomputed.js',
77
- source: precomputedCode
78
- });
79
- },
71
+ // Add the precomputed code at the top of the file
72
+ code = precomputedCode + '\n' + code;
73
+ }
80
74
 
81
- // Inject precomputed colors into HTML
82
- transformIndexHtml(html) {
83
- return html.replace('</head>', `
84
- <script type="module" src="/v-auto-color-precomputed.js"></script>
85
- </head>`);
75
+ return code;
86
76
  }
87
77
  };
88
78
  }