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