@unocss/vite 0.12.16 → 0.13.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/dist/index.d.ts CHANGED
@@ -1,20 +1,9 @@
1
1
  import * as vite from 'vite';
2
2
  import { Plugin } from 'vite';
3
3
  import { UserConfig, UnoGenerator, BetterMap, UserConfigDefaults } from '@unocss/core';
4
+ import { LoadConfigResult } from '@unocss/config';
4
5
 
5
- declare type FilterPattern = ReadonlyArray<string | RegExp> | string | RegExp | null;
6
- interface PluginConfig<Theme extends {} = {}> extends UserConfig<Theme> {
7
- /**
8
- * Patterns that filter the files being extracted.
9
- */
10
- include?: FilterPattern;
11
- /**
12
- * Patterns that filter the files NOT being extracted.
13
- */
14
- exclude?: FilterPattern;
15
- }
16
-
17
- interface VitePluginConfig<Theme extends {} = {}> extends PluginConfig<Theme> {
6
+ interface VitePluginConfig<Theme extends {} = {}> extends UserConfig<Theme> {
18
7
  /**
19
8
  * Enable UnoCSS inspector
20
9
  *
@@ -34,15 +23,15 @@ interface VitePluginConfig<Theme extends {} = {}> extends PluginConfig<Theme> {
34
23
  mode?: 'global' | 'per-module' | 'vue-scoped' | 'dist-chunk';
35
24
  }
36
25
 
37
- interface UnocssPluginContext<Config extends PluginConfig = PluginConfig> {
26
+ interface UnocssPluginContext<Config extends UserConfig = UserConfig> {
27
+ ready: Promise<LoadConfigResult<Config>>;
38
28
  uno: UnoGenerator;
39
- config: Config;
40
29
  tokens: Set<string>;
41
30
  modules: BetterMap<string, string>;
42
31
  filter: (code: string, id: string) => boolean;
43
- reloadConfig: () => Promise<void>;
44
32
  extract: (code: string, id?: string) => Promise<void>;
45
- configFilepath?: string;
33
+ reloadConfig: () => Promise<LoadConfigResult<Config>>;
34
+ getConfig: () => Promise<Config>;
46
35
  invalidate: () => void;
47
36
  onInvalidate: (fn: () => void) => void;
48
37
  }
@@ -51,13 +40,13 @@ declare function ChunkModeBuildPlugin({ uno, filter }: UnocssPluginContext): Plu
51
40
 
52
41
  declare function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }: UnocssPluginContext): Plugin[];
53
42
 
54
- declare function GlobalModeBuildPlugin({ uno, extract, tokens, modules, filter }: UnocssPluginContext): Plugin[];
43
+ declare function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter }: UnocssPluginContext): Plugin[];
55
44
 
56
45
  declare function GlobalModePlugin(ctx: UnocssPluginContext): vite.Plugin[];
57
46
 
58
47
  declare function PerModuleModePlugin({ uno, filter }: UnocssPluginContext): Plugin;
59
48
 
60
- declare function VueScopedPlugin({ uno, config }: UnocssPluginContext): Plugin;
49
+ declare function VueScopedPlugin({ uno, ready }: UnocssPluginContext): Plugin;
61
50
 
62
51
  declare function defineConfig<Theme extends {}>(config: VitePluginConfig<Theme>): VitePluginConfig<Theme>;
63
52
  declare function UnocssPlugin(configOrPath?: VitePluginConfig | string, defaults?: UserConfigDefaults): Plugin[];
package/dist/index.js CHANGED
@@ -74,14 +74,26 @@ var INCLUDE_COMMENT = "@unocss-include";
74
74
  var import_pluginutils = __toModule(require("@rollup/pluginutils"));
75
75
  var import_config = __toModule(require("@unocss/config"));
76
76
  var import_core = __toModule(require("@unocss/core"));
77
- function createContext(configOrPath, defaults = {}) {
78
- const { config = {}, filepath } = (0, import_config.loadConfig)(configOrPath);
79
- let rawConfig = config;
80
- const uno = (0, import_core.createGenerator)(config, defaults);
77
+ function createContext(configOrPath, defaults = {}, extraConfigSources = []) {
78
+ const loadConfig = (0, import_config.createConfigLoader)(configOrPath, extraConfigSources);
79
+ let rawConfig = {};
80
+ const uno = (0, import_core.createGenerator)(rawConfig, defaults);
81
+ let rollupFilter = (0, import_pluginutils.createFilter)(defaultInclude, defaultExclude);
81
82
  const invalidations = [];
82
83
  const modules = new import_core.BetterMap();
83
84
  const tokens = new Set();
84
- let rollupFilter = (0, import_pluginutils.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
85
+ const ready = reloadConfig();
86
+ async function reloadConfig() {
87
+ const result = await loadConfig();
88
+ rawConfig = result.config;
89
+ uno.setConfig(rawConfig);
90
+ uno.config.envMode = "dev";
91
+ rollupFilter = (0, import_pluginutils.createFilter)(rawConfig.include || defaultInclude, rawConfig.exclude || defaultExclude);
92
+ tokens.clear();
93
+ await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
94
+ invalidate();
95
+ return result;
96
+ }
85
97
  function invalidate() {
86
98
  invalidations.forEach((cb) => cb());
87
99
  }
@@ -93,21 +105,15 @@ function createContext(configOrPath, defaults = {}) {
93
105
  if (tokens.size > len)
94
106
  invalidate();
95
107
  }
96
- async function reloadConfig() {
97
- if (!filepath)
98
- return;
99
- rawConfig = (0, import_config.loadConfig)(filepath).config;
100
- uno.setConfig(rawConfig);
101
- uno.config.envMode = "dev";
102
- rollupFilter = (0, import_pluginutils.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
103
- tokens.clear();
104
- await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
105
- invalidate();
106
- }
107
108
  const filter = (code, id) => {
108
109
  return code.includes(INCLUDE_COMMENT) || rollupFilter(id);
109
110
  };
111
+ async function getConfig() {
112
+ await ready;
113
+ return rawConfig;
114
+ }
110
115
  return {
116
+ ready,
111
117
  tokens,
112
118
  modules,
113
119
  invalidate,
@@ -118,17 +124,14 @@ function createContext(configOrPath, defaults = {}) {
118
124
  reloadConfig,
119
125
  uno,
120
126
  extract,
121
- get config() {
122
- return rawConfig;
123
- },
124
- configFilepath: filepath
127
+ getConfig
125
128
  };
126
129
  }
127
130
 
128
131
  // ../plugins-common/utils.ts
129
132
  var import_crypto = __toModule(require("crypto"));
130
133
  function getHash(input, length = 8) {
131
- return (0, import_crypto.createHash)("sha256").update(input).digest("hex").substr(0, length);
134
+ return (0, import_crypto.createHash)("sha256").update(input).digest("hex").slice(0, length);
132
135
  }
133
136
  function getPath(id) {
134
137
  return id.replace(/\?.*$/, "");
@@ -178,7 +181,7 @@ function ChunkModeBuildPlugin({ uno, filter }) {
178
181
  }
179
182
 
180
183
  // src/modes/global/build.ts
181
- function GlobalModeBuildPlugin({ uno, extract, tokens, modules, filter }) {
184
+ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter }) {
182
185
  const vfsLayerMap = new Map();
183
186
  let tasks = [];
184
187
  let cssPlugin;
@@ -214,8 +217,9 @@ function GlobalModeBuildPlugin({ uno, extract, tokens, modules, filter }) {
214
217
  if (layer)
215
218
  return getLayerPlaceholder(layer);
216
219
  },
217
- configResolved(config) {
220
+ async configResolved(config) {
218
221
  cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
222
+ await ready;
219
223
  },
220
224
  async renderChunk(_, chunk) {
221
225
  if (!cssPlugin)
@@ -332,7 +336,7 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
332
336
  name: "unocss:global",
333
337
  apply: "serve",
334
338
  enforce: "pre",
335
- configureServer(_server) {
339
+ async configureServer(_server) {
336
340
  servers.push(_server);
337
341
  _server.middlewares.use(async (req, res, next) => {
338
342
  var _a;
@@ -467,8 +471,8 @@ ${css}`;
467
471
 
468
472
  // src/modes/vue-scoped.ts
469
473
  var import_pluginutils2 = __toModule(require("@rollup/pluginutils"));
470
- function VueScopedPlugin({ uno, config }) {
471
- const filter = (0, import_pluginutils2.createFilter)(config.include || [/\.vue$/], config.exclude || defaultExclude);
474
+ function VueScopedPlugin({ uno, ready }) {
475
+ let filter = (0, import_pluginutils2.createFilter)([/\.vue$/], defaultExclude);
472
476
  async function transformSFC(code) {
473
477
  const { css } = await uno.generate(code);
474
478
  if (!css)
@@ -479,6 +483,10 @@ function VueScopedPlugin({ uno, config }) {
479
483
  return {
480
484
  name: "unocss:vue-scoped",
481
485
  enforce: "pre",
486
+ async configResolved() {
487
+ const { config } = await ready;
488
+ filter = (0, import_pluginutils2.createFilter)(config.include || [/\.vue$/], config.exclude || defaultExclude);
489
+ },
482
490
  transform(code, id) {
483
491
  if (!filter(id))
484
492
  return;
@@ -498,24 +506,22 @@ function VueScopedPlugin({ uno, config }) {
498
506
 
499
507
  // src/config-hmr.ts
500
508
  function ConfigHMRPlugin(ctx) {
501
- const { uno, configFilepath, reloadConfig } = ctx;
509
+ const { ready, uno } = ctx;
502
510
  return {
503
511
  name: "unocss:config",
504
- api: {
505
- get config() {
506
- if (!configFilepath)
507
- return ctx.config;
508
- }
512
+ async configResolved() {
513
+ await ready;
509
514
  },
510
- configureServer(server) {
515
+ async configureServer(server) {
511
516
  uno.config.envMode = "dev";
512
- if (!configFilepath)
517
+ const { sources } = await ready;
518
+ if (!sources.length)
513
519
  return;
514
- server.watcher.add(configFilepath);
520
+ server.watcher.add(sources);
515
521
  server.watcher.on("change", async (p) => {
516
- if (p !== configFilepath)
522
+ if (!sources.includes(p))
517
523
  return;
518
- reloadConfig();
524
+ await ctx.reloadConfig();
519
525
  server.ws.send({
520
526
  type: "custom",
521
527
  event: "unocss:config-changed"
@@ -531,12 +537,12 @@ function defineConfig(config) {
531
537
  }
532
538
  function UnocssPlugin(configOrPath, defaults = {}) {
533
539
  const ctx = createContext(configOrPath, defaults);
534
- const { config } = ctx;
535
- const mode = config.mode ?? "global";
540
+ const inlineConfig = configOrPath && typeof configOrPath !== "string" ? configOrPath : {};
541
+ const mode = inlineConfig.mode ?? "global";
536
542
  const plugins = [
537
543
  ConfigHMRPlugin(ctx)
538
544
  ];
539
- if (config.inspector !== false)
545
+ if (inlineConfig.inspector !== false)
540
546
  plugins.push((0, import_inspector.default)(ctx));
541
547
  if (mode === "per-module") {
542
548
  plugins.push(PerModuleModePlugin(ctx));
package/dist/index.mjs CHANGED
@@ -38,16 +38,28 @@ var INCLUDE_COMMENT = "@unocss-include";
38
38
 
39
39
  // ../plugins-common/context.ts
40
40
  import { createFilter } from "@rollup/pluginutils";
41
- import { loadConfig } from "@unocss/config";
41
+ import { createConfigLoader } from "@unocss/config";
42
42
  import { BetterMap, createGenerator } from "@unocss/core";
43
- function createContext(configOrPath, defaults = {}) {
44
- const { config = {}, filepath } = loadConfig(configOrPath);
45
- let rawConfig = config;
46
- const uno = createGenerator(config, defaults);
43
+ function createContext(configOrPath, defaults = {}, extraConfigSources = []) {
44
+ const loadConfig = createConfigLoader(configOrPath, extraConfigSources);
45
+ let rawConfig = {};
46
+ const uno = createGenerator(rawConfig, defaults);
47
+ let rollupFilter = createFilter(defaultInclude, defaultExclude);
47
48
  const invalidations = [];
48
49
  const modules = new BetterMap();
49
50
  const tokens = new Set();
50
- let rollupFilter = createFilter(config.include || defaultInclude, config.exclude || defaultExclude);
51
+ const ready = reloadConfig();
52
+ async function reloadConfig() {
53
+ const result = await loadConfig();
54
+ rawConfig = result.config;
55
+ uno.setConfig(rawConfig);
56
+ uno.config.envMode = "dev";
57
+ rollupFilter = createFilter(rawConfig.include || defaultInclude, rawConfig.exclude || defaultExclude);
58
+ tokens.clear();
59
+ await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
60
+ invalidate();
61
+ return result;
62
+ }
51
63
  function invalidate() {
52
64
  invalidations.forEach((cb) => cb());
53
65
  }
@@ -59,21 +71,15 @@ function createContext(configOrPath, defaults = {}) {
59
71
  if (tokens.size > len)
60
72
  invalidate();
61
73
  }
62
- async function reloadConfig() {
63
- if (!filepath)
64
- return;
65
- rawConfig = loadConfig(filepath).config;
66
- uno.setConfig(rawConfig);
67
- uno.config.envMode = "dev";
68
- rollupFilter = createFilter(config.include || defaultInclude, config.exclude || defaultExclude);
69
- tokens.clear();
70
- await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
71
- invalidate();
72
- }
73
74
  const filter = (code, id) => {
74
75
  return code.includes(INCLUDE_COMMENT) || rollupFilter(id);
75
76
  };
77
+ async function getConfig() {
78
+ await ready;
79
+ return rawConfig;
80
+ }
76
81
  return {
82
+ ready,
77
83
  tokens,
78
84
  modules,
79
85
  invalidate,
@@ -84,17 +90,14 @@ function createContext(configOrPath, defaults = {}) {
84
90
  reloadConfig,
85
91
  uno,
86
92
  extract,
87
- get config() {
88
- return rawConfig;
89
- },
90
- configFilepath: filepath
93
+ getConfig
91
94
  };
92
95
  }
93
96
 
94
97
  // ../plugins-common/utils.ts
95
98
  import { createHash } from "crypto";
96
99
  function getHash(input, length = 8) {
97
- return createHash("sha256").update(input).digest("hex").substr(0, length);
100
+ return createHash("sha256").update(input).digest("hex").slice(0, length);
98
101
  }
99
102
  function getPath(id) {
100
103
  return id.replace(/\?.*$/, "");
@@ -144,7 +147,7 @@ function ChunkModeBuildPlugin({ uno, filter }) {
144
147
  }
145
148
 
146
149
  // src/modes/global/build.ts
147
- function GlobalModeBuildPlugin({ uno, extract, tokens, modules, filter }) {
150
+ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter }) {
148
151
  const vfsLayerMap = new Map();
149
152
  let tasks = [];
150
153
  let cssPlugin;
@@ -180,8 +183,9 @@ function GlobalModeBuildPlugin({ uno, extract, tokens, modules, filter }) {
180
183
  if (layer)
181
184
  return getLayerPlaceholder(layer);
182
185
  },
183
- configResolved(config) {
186
+ async configResolved(config) {
184
187
  cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
188
+ await ready;
185
189
  },
186
190
  async renderChunk(_, chunk) {
187
191
  if (!cssPlugin)
@@ -298,7 +302,7 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
298
302
  name: "unocss:global",
299
303
  apply: "serve",
300
304
  enforce: "pre",
301
- configureServer(_server) {
305
+ async configureServer(_server) {
302
306
  servers.push(_server);
303
307
  _server.middlewares.use(async (req, res, next) => {
304
308
  var _a;
@@ -433,8 +437,8 @@ ${css}`;
433
437
 
434
438
  // src/modes/vue-scoped.ts
435
439
  import { createFilter as createFilter2 } from "@rollup/pluginutils";
436
- function VueScopedPlugin({ uno, config }) {
437
- const filter = createFilter2(config.include || [/\.vue$/], config.exclude || defaultExclude);
440
+ function VueScopedPlugin({ uno, ready }) {
441
+ let filter = createFilter2([/\.vue$/], defaultExclude);
438
442
  async function transformSFC(code) {
439
443
  const { css } = await uno.generate(code);
440
444
  if (!css)
@@ -445,6 +449,10 @@ function VueScopedPlugin({ uno, config }) {
445
449
  return {
446
450
  name: "unocss:vue-scoped",
447
451
  enforce: "pre",
452
+ async configResolved() {
453
+ const { config } = await ready;
454
+ filter = createFilter2(config.include || [/\.vue$/], config.exclude || defaultExclude);
455
+ },
448
456
  transform(code, id) {
449
457
  if (!filter(id))
450
458
  return;
@@ -464,24 +472,22 @@ function VueScopedPlugin({ uno, config }) {
464
472
 
465
473
  // src/config-hmr.ts
466
474
  function ConfigHMRPlugin(ctx) {
467
- const { uno, configFilepath, reloadConfig } = ctx;
475
+ const { ready, uno } = ctx;
468
476
  return {
469
477
  name: "unocss:config",
470
- api: {
471
- get config() {
472
- if (!configFilepath)
473
- return ctx.config;
474
- }
478
+ async configResolved() {
479
+ await ready;
475
480
  },
476
- configureServer(server) {
481
+ async configureServer(server) {
477
482
  uno.config.envMode = "dev";
478
- if (!configFilepath)
483
+ const { sources } = await ready;
484
+ if (!sources.length)
479
485
  return;
480
- server.watcher.add(configFilepath);
486
+ server.watcher.add(sources);
481
487
  server.watcher.on("change", async (p) => {
482
- if (p !== configFilepath)
488
+ if (!sources.includes(p))
483
489
  return;
484
- reloadConfig();
490
+ await ctx.reloadConfig();
485
491
  server.ws.send({
486
492
  type: "custom",
487
493
  event: "unocss:config-changed"
@@ -497,12 +503,12 @@ function defineConfig(config) {
497
503
  }
498
504
  function UnocssPlugin(configOrPath, defaults = {}) {
499
505
  const ctx = createContext(configOrPath, defaults);
500
- const { config } = ctx;
501
- const mode = config.mode ?? "global";
506
+ const inlineConfig = configOrPath && typeof configOrPath !== "string" ? configOrPath : {};
507
+ const mode = inlineConfig.mode ?? "global";
502
508
  const plugins = [
503
509
  ConfigHMRPlugin(ctx)
504
510
  ];
505
- if (config.inspector !== false)
511
+ if (inlineConfig.inspector !== false)
506
512
  plugins.push(UnocssInspector(ctx));
507
513
  if (mode === "per-module") {
508
514
  plugins.push(PerModuleModePlugin(ctx));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/vite",
3
- "version": "0.12.16",
3
+ "version": "0.13.1",
4
4
  "description": "The Vite plugin for UnoCSS",
5
5
  "keywords": [
6
6
  "unocss",
@@ -35,10 +35,10 @@
35
35
  ],
36
36
  "dependencies": {
37
37
  "@rollup/pluginutils": "^4.1.1",
38
- "@unocss/config": "0.12.16",
39
- "@unocss/core": "0.12.16",
40
- "@unocss/inspector": "0.12.16",
41
- "@unocss/scope": "0.12.16"
38
+ "@unocss/config": "0.13.1",
39
+ "@unocss/core": "0.13.1",
40
+ "@unocss/inspector": "0.13.1",
41
+ "@unocss/scope": "0.13.1"
42
42
  },
43
43
  "devDependencies": {
44
44
  "vite": "^2.6.14"