vite-plugin-theme-css 0.2.0 → 0.4.0
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 +6 -0
- package/client.d.ts +15 -0
- package/dist/index.cjs +24 -21
- package/dist/index.mjs +25 -20
- package/dist/plugin-dir.d.ts +3 -0
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -161,6 +161,12 @@ getTheme() // 'dark'
|
|
|
161
161
|
|
|
162
162
|
`setTheme` 默认在主题名为 `dark` 时同步给 `<html>` 加上 `dark` class(可用 `syncHtmlDark: false` 关闭)。
|
|
163
163
|
|
|
164
|
+
TypeScript 需引入虚拟模块声明(在 `env.d.ts` / `vite-env.d.ts` 中):
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
/// <reference types="vite-plugin-theme-css/client" />
|
|
168
|
+
```
|
|
169
|
+
|
|
164
170
|
## 插件选项
|
|
165
171
|
|
|
166
172
|
```ts
|
package/client.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 用户侧虚拟模块的 ambient 声明。
|
|
3
|
+
* 在项目 env.d.ts / vite-env.d.ts 中引用:
|
|
4
|
+
*
|
|
5
|
+
* /// <reference types="vite-plugin-theme-css/client" />
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare module '@scope/theme-css' {
|
|
9
|
+
export function theme(...keys: string[]): string
|
|
10
|
+
export function common(...keys: string[]): string
|
|
11
|
+
export function setTheme(name: string): void
|
|
12
|
+
export function getTheme(): string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare module 'virtual:theme-css/css' {}
|
package/dist/index.cjs
CHANGED
|
@@ -36,8 +36,7 @@ __export(src_exports, {
|
|
|
36
36
|
});
|
|
37
37
|
module.exports = __toCommonJS(src_exports);
|
|
38
38
|
var import_node_fs = require("fs");
|
|
39
|
-
var
|
|
40
|
-
var import_node_url = require("url");
|
|
39
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
41
40
|
var import_node_module = require("module");
|
|
42
41
|
var import_jiti = require("jiti");
|
|
43
42
|
|
|
@@ -152,6 +151,11 @@ function createVirtualScssImporter(getContent) {
|
|
|
152
151
|
};
|
|
153
152
|
}
|
|
154
153
|
|
|
154
|
+
// src/plugin-dir.ts
|
|
155
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
156
|
+
var pluginFilename = __filename;
|
|
157
|
+
var pluginDir = import_node_path.default.dirname(__filename);
|
|
158
|
+
|
|
155
159
|
// src/defineTheme.ts
|
|
156
160
|
function defineTheme(config) {
|
|
157
161
|
return config;
|
|
@@ -195,14 +199,12 @@ function createThemeRuntime(config, options = {}) {
|
|
|
195
199
|
}
|
|
196
200
|
|
|
197
201
|
// src/index.ts
|
|
198
|
-
var
|
|
199
|
-
var require2 = (0, import_node_module.createRequire)(import_meta.url);
|
|
202
|
+
var require2 = (0, import_node_module.createRequire)(pluginFilename);
|
|
200
203
|
var babelTransform = (0, import_node_module.createRequire)(require2.resolve("jiti/package.json"))("./dist/babel.cjs");
|
|
201
204
|
function resolveRuntimeEntryPath() {
|
|
202
|
-
const
|
|
203
|
-
const distEntry = import_node_path.default.join(here, "runtime-entry.mjs");
|
|
205
|
+
const distEntry = import_node_path2.default.join(pluginDir, "runtime-entry.mjs");
|
|
204
206
|
if ((0, import_node_fs.existsSync)(distEntry)) return distEntry;
|
|
205
|
-
return
|
|
207
|
+
return import_node_path2.default.join(pluginDir, "runtime", "virtual-entry.ts");
|
|
206
208
|
}
|
|
207
209
|
function themeCss(options = {}) {
|
|
208
210
|
const resolvedOptions = { ...defaultOptions, ...options };
|
|
@@ -211,7 +213,7 @@ function themeCss(options = {}) {
|
|
|
211
213
|
let runtimeDataContent = "";
|
|
212
214
|
const runtimeEntryPath = resolveRuntimeEntryPath();
|
|
213
215
|
async function loadConfigAndGenerate() {
|
|
214
|
-
const jiti = (0, import_jiti.createJiti)(
|
|
216
|
+
const jiti = (0, import_jiti.createJiti)(pluginFilename, {
|
|
215
217
|
interopDefault: true,
|
|
216
218
|
transform: (opts) => {
|
|
217
219
|
let { source } = opts;
|
|
@@ -224,7 +226,7 @@ ${source}`;
|
|
|
224
226
|
return babelTransform({ ...opts, source });
|
|
225
227
|
}
|
|
226
228
|
});
|
|
227
|
-
const mod = await jiti.import(
|
|
229
|
+
const mod = await jiti.import(import_node_path2.default.resolve(configPath));
|
|
228
230
|
const themeConfig = mod.default;
|
|
229
231
|
cssContent = generateThemeCss(themeConfig);
|
|
230
232
|
runtimeDataContent = generateJs(themeConfig, { syncHtmlDark });
|
|
@@ -260,6 +262,18 @@ ${source}`;
|
|
|
260
262
|
return null;
|
|
261
263
|
}
|
|
262
264
|
},
|
|
265
|
+
async transformIndexHtml(html) {
|
|
266
|
+
if (!autoImportCss || html.includes("data-theme-css-vars")) return;
|
|
267
|
+
if (!cssContent) await loadConfigAndGenerate();
|
|
268
|
+
return [
|
|
269
|
+
{
|
|
270
|
+
tag: "style",
|
|
271
|
+
attrs: { "data-theme-css-vars": "" },
|
|
272
|
+
children: cssContent,
|
|
273
|
+
injectTo: "head-prepend"
|
|
274
|
+
}
|
|
275
|
+
];
|
|
276
|
+
},
|
|
263
277
|
config(userConfig) {
|
|
264
278
|
const scssOptions = userConfig.css?.preprocessorOptions?.scss ?? {};
|
|
265
279
|
const userAdditionalData = scssOptions.additionalData;
|
|
@@ -301,19 +315,8 @@ ${source}`;
|
|
|
301
315
|
}
|
|
302
316
|
};
|
|
303
317
|
},
|
|
304
|
-
transformIndexHtml(html) {
|
|
305
|
-
if (!autoImportCss || html.includes(VIRTUAL_CSS_ID)) return;
|
|
306
|
-
return [
|
|
307
|
-
{
|
|
308
|
-
tag: "script",
|
|
309
|
-
attrs: { type: "module" },
|
|
310
|
-
children: `import ${JSON.stringify(VIRTUAL_CSS_ID)};`,
|
|
311
|
-
injectTo: "head-prepend"
|
|
312
|
-
}
|
|
313
|
-
];
|
|
314
|
-
},
|
|
315
318
|
async handleHotUpdate(ctx) {
|
|
316
|
-
const configAbsPath =
|
|
319
|
+
const configAbsPath = import_node_path2.default.resolve(configPath);
|
|
317
320
|
if (ctx.file === configAbsPath) {
|
|
318
321
|
await loadConfigAndGenerate();
|
|
319
322
|
const moduleGraph = ctx.server.moduleGraph;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { existsSync } from "fs";
|
|
3
|
-
import
|
|
4
|
-
import { fileURLToPath } from "url";
|
|
3
|
+
import path2 from "path";
|
|
5
4
|
import { createRequire } from "module";
|
|
6
5
|
import { createJiti } from "jiti";
|
|
7
6
|
|
|
@@ -116,6 +115,12 @@ function createVirtualScssImporter(getContent) {
|
|
|
116
115
|
};
|
|
117
116
|
}
|
|
118
117
|
|
|
118
|
+
// src/plugin-dir.ts
|
|
119
|
+
import path from "path";
|
|
120
|
+
import { fileURLToPath } from "url";
|
|
121
|
+
var pluginFilename = fileURLToPath(import.meta.url);
|
|
122
|
+
var pluginDir = path.dirname(pluginFilename);
|
|
123
|
+
|
|
119
124
|
// src/defineTheme.ts
|
|
120
125
|
function defineTheme(config) {
|
|
121
126
|
return config;
|
|
@@ -159,13 +164,12 @@ function createThemeRuntime(config, options = {}) {
|
|
|
159
164
|
}
|
|
160
165
|
|
|
161
166
|
// src/index.ts
|
|
162
|
-
var require2 = createRequire(
|
|
167
|
+
var require2 = createRequire(pluginFilename);
|
|
163
168
|
var babelTransform = createRequire(require2.resolve("jiti/package.json"))("./dist/babel.cjs");
|
|
164
169
|
function resolveRuntimeEntryPath() {
|
|
165
|
-
const
|
|
166
|
-
const distEntry = path.join(here, "runtime-entry.mjs");
|
|
170
|
+
const distEntry = path2.join(pluginDir, "runtime-entry.mjs");
|
|
167
171
|
if (existsSync(distEntry)) return distEntry;
|
|
168
|
-
return
|
|
172
|
+
return path2.join(pluginDir, "runtime", "virtual-entry.ts");
|
|
169
173
|
}
|
|
170
174
|
function themeCss(options = {}) {
|
|
171
175
|
const resolvedOptions = { ...defaultOptions, ...options };
|
|
@@ -174,7 +178,7 @@ function themeCss(options = {}) {
|
|
|
174
178
|
let runtimeDataContent = "";
|
|
175
179
|
const runtimeEntryPath = resolveRuntimeEntryPath();
|
|
176
180
|
async function loadConfigAndGenerate() {
|
|
177
|
-
const jiti = createJiti(
|
|
181
|
+
const jiti = createJiti(pluginFilename, {
|
|
178
182
|
interopDefault: true,
|
|
179
183
|
transform: (opts) => {
|
|
180
184
|
let { source } = opts;
|
|
@@ -187,7 +191,7 @@ ${source}`;
|
|
|
187
191
|
return babelTransform({ ...opts, source });
|
|
188
192
|
}
|
|
189
193
|
});
|
|
190
|
-
const mod = await jiti.import(
|
|
194
|
+
const mod = await jiti.import(path2.resolve(configPath));
|
|
191
195
|
const themeConfig = mod.default;
|
|
192
196
|
cssContent = generateThemeCss(themeConfig);
|
|
193
197
|
runtimeDataContent = generateJs(themeConfig, { syncHtmlDark });
|
|
@@ -223,6 +227,18 @@ ${source}`;
|
|
|
223
227
|
return null;
|
|
224
228
|
}
|
|
225
229
|
},
|
|
230
|
+
async transformIndexHtml(html) {
|
|
231
|
+
if (!autoImportCss || html.includes("data-theme-css-vars")) return;
|
|
232
|
+
if (!cssContent) await loadConfigAndGenerate();
|
|
233
|
+
return [
|
|
234
|
+
{
|
|
235
|
+
tag: "style",
|
|
236
|
+
attrs: { "data-theme-css-vars": "" },
|
|
237
|
+
children: cssContent,
|
|
238
|
+
injectTo: "head-prepend"
|
|
239
|
+
}
|
|
240
|
+
];
|
|
241
|
+
},
|
|
226
242
|
config(userConfig) {
|
|
227
243
|
const scssOptions = userConfig.css?.preprocessorOptions?.scss ?? {};
|
|
228
244
|
const userAdditionalData = scssOptions.additionalData;
|
|
@@ -264,19 +280,8 @@ ${source}`;
|
|
|
264
280
|
}
|
|
265
281
|
};
|
|
266
282
|
},
|
|
267
|
-
transformIndexHtml(html) {
|
|
268
|
-
if (!autoImportCss || html.includes(VIRTUAL_CSS_ID)) return;
|
|
269
|
-
return [
|
|
270
|
-
{
|
|
271
|
-
tag: "script",
|
|
272
|
-
attrs: { type: "module" },
|
|
273
|
-
children: `import ${JSON.stringify(VIRTUAL_CSS_ID)};`,
|
|
274
|
-
injectTo: "head-prepend"
|
|
275
|
-
}
|
|
276
|
-
];
|
|
277
|
-
},
|
|
278
283
|
async handleHotUpdate(ctx) {
|
|
279
|
-
const configAbsPath =
|
|
284
|
+
const configAbsPath = path2.resolve(configPath);
|
|
280
285
|
if (ctx.file === configAbsPath) {
|
|
281
286
|
await loadConfigAndGenerate();
|
|
282
287
|
const moduleGraph = ctx.server.moduleGraph;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-theme-css",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Vite 插件:用一份主题配置生成 CSS 变量、SCSS helper 与 JS 运行时,支持多主题切换与热更新",
|
|
5
5
|
"author": "yuzinan <1589937631@qq.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,10 +40,14 @@
|
|
|
40
40
|
"types": "./dist/runtime.d.ts",
|
|
41
41
|
"import": "./dist/runtime.mjs",
|
|
42
42
|
"require": "./dist/runtime.cjs"
|
|
43
|
+
},
|
|
44
|
+
"./client": {
|
|
45
|
+
"types": "./client.d.ts"
|
|
43
46
|
}
|
|
44
47
|
},
|
|
45
48
|
"files": [
|
|
46
|
-
"dist"
|
|
49
|
+
"dist",
|
|
50
|
+
"client.d.ts"
|
|
47
51
|
],
|
|
48
52
|
"dependencies": {
|
|
49
53
|
"jiti": "^2.7.0"
|