vite-plugin-theme-css 0.2.0 → 0.3.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/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 import_node_path = __toESM(require("path"), 1);
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 import_meta = {};
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 here = import_node_path.default.dirname((0, import_node_url.fileURLToPath)(import_meta.url));
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 import_node_path.default.join(here, "runtime", "virtual-entry.ts");
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)(import_meta.url, {
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(import_node_path.default.resolve(configPath));
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 = import_node_path.default.resolve(configPath);
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 path from "path";
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(import.meta.url);
167
+ var require2 = createRequire(pluginFilename);
163
168
  var babelTransform = createRequire(require2.resolve("jiti/package.json"))("./dist/babel.cjs");
164
169
  function resolveRuntimeEntryPath() {
165
- const here = path.dirname(fileURLToPath(import.meta.url));
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 path.join(here, "runtime", "virtual-entry.ts");
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(import.meta.url, {
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(path.resolve(configPath));
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 = path.resolve(configPath);
284
+ const configAbsPath = path2.resolve(configPath);
280
285
  if (ctx.file === configAbsPath) {
281
286
  await loadConfigAndGenerate();
282
287
  const moduleGraph = ctx.server.moduleGraph;
@@ -0,0 +1,3 @@
1
+ /** 插件包根目录(须与 index.ts 同级,dev 与 dist 均能正确定位 runtime-entry) */
2
+ export declare const pluginFilename: string;
3
+ export declare const pluginDir: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-theme-css",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Vite 插件:用一份主题配置生成 CSS 变量、SCSS helper 与 JS 运行时,支持多主题切换与热更新",
5
5
  "author": "yuzinan <1589937631@qq.com>",
6
6
  "license": "MIT",