@unocss/webpack 0.38.2 → 0.39.2

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
@@ -24,16 +24,16 @@ function resolveId(id) {
24
24
  for (const alias of VIRTUAL_ENTRY_ALIAS) {
25
25
  const match = id.match(alias);
26
26
  if (match) {
27
- return match[1] ? {
28
- id: `/__uno_${match[1]}.css`,
29
- layer: match[1]
30
- } : {
31
- id: "/__uno.css",
32
- layer: LAYER_MARK_ALL
33
- };
27
+ return match[1] ? `/__uno_${match[1]}.css` : "/__uno.css";
34
28
  }
35
29
  }
36
30
  }
31
+ const RESOLVED_ID_RE = /\/__uno(?:(_.*?))?\.css$/;
32
+ function resolveLayer(id) {
33
+ const match = id.match(RESOLVED_ID_RE);
34
+ if (match)
35
+ return match[1] || LAYER_MARK_ALL;
36
+ }
37
37
  const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
38
38
  function getLayerPlaceholder(layer) {
39
39
  return `#--unocss--{layer:${layer}}`;
@@ -54,6 +54,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
54
54
  const uno = core.createGenerator(rawConfig, defaults);
55
55
  let rollupFilter = pluginutils.createFilter(defaultInclude, defaultExclude);
56
56
  const invalidations = [];
57
+ const reloadListeners = [];
57
58
  const modules = new core.BetterMap();
58
59
  const tokens = /* @__PURE__ */ new Set();
59
60
  let ready = reloadConfig();
@@ -67,6 +68,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
67
68
  tokens.clear();
68
69
  await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
69
70
  invalidate();
71
+ dispatchReload();
70
72
  const presets = /* @__PURE__ */ new Set();
71
73
  uno.config.presets.forEach((i) => {
72
74
  if (!i.name)
@@ -88,6 +90,9 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
88
90
  function invalidate() {
89
91
  invalidations.forEach((cb) => cb());
90
92
  }
93
+ function dispatchReload() {
94
+ reloadListeners.forEach((cb) => cb());
95
+ }
91
96
  async function extract(code, id) {
92
97
  if (id)
93
98
  modules.set(id, code);
@@ -117,6 +122,9 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
117
122
  },
118
123
  filter,
119
124
  reloadConfig,
125
+ onReload(fn) {
126
+ reloadListeners.push(fn);
127
+ },
120
128
  uno,
121
129
  extract,
122
130
  getConfig,
@@ -148,7 +156,8 @@ function WebpackPlugin(configOrPath, defaults) {
148
156
  timer = setTimeout(updateModules, UPDATE_DEBOUNCE);
149
157
  });
150
158
  const tasks = [];
151
- const entries = /* @__PURE__ */ new Map();
159
+ const entries = /* @__PURE__ */ new Set();
160
+ const hashs = /* @__PURE__ */ new Map();
152
161
  const plugin = {
153
162
  name: "unocss:webpack",
154
163
  enforce: "pre",
@@ -162,14 +171,18 @@ function WebpackPlugin(configOrPath, defaults) {
162
171
  resolveId(id) {
163
172
  const entry = resolveId(id);
164
173
  if (entry) {
165
- entries.set(entry.id, entry.layer);
166
- entries.set(id, entry.layer);
167
- return entry.id;
174
+ entries.add(entry);
175
+ return entry;
168
176
  }
169
177
  },
170
178
  load(id) {
171
- const layer = entries.get(getPath(id));
172
- const hash = entries.get(`${id}_hash`);
179
+ let layer = resolveLayer(getPath(id));
180
+ if (!layer) {
181
+ const entry = resolveId(id);
182
+ if (entry)
183
+ layer = resolveLayer(entry);
184
+ }
185
+ const hash = hashs.get(id);
173
186
  if (layer)
174
187
  return (hash ? getHashPlaceholder(hash) : "") + getLayerPlaceholder(layer);
175
188
  },
@@ -185,7 +198,7 @@ function WebpackPlugin(configOrPath, defaults) {
185
198
  code = code.replace(HASH_PLACEHOLDER_RE, "");
186
199
  code = code.replace(LAYER_PLACEHOLDER_RE, (_, quote, layer) => {
187
200
  replaced = true;
188
- const css = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries.values())) : result.getLayer(layer) || "";
201
+ const css = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || "";
189
202
  if (!quote)
190
203
  return css;
191
204
  let escaped = JSON.stringify(css).slice(1, -1);
@@ -206,12 +219,12 @@ function WebpackPlugin(configOrPath, defaults) {
206
219
  const result = await uno.generate(tokens);
207
220
  Array.from(plugin.__vfsModules).forEach((id) => {
208
221
  const path = id.slice(plugin.__virtualModulePrefix.length).replace(/\\/g, "/");
209
- const layer = entries.get(path);
222
+ const layer = resolveLayer(path);
210
223
  if (!layer)
211
224
  return;
212
- const code = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries.values())) : result.getLayer(layer) || "";
225
+ const code = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || "";
213
226
  const hash = getHash(code);
214
- entries.set(`${path}_hash`, hash);
227
+ hashs.set(path, hash);
215
228
  plugin.__vfs.writeModule(id, code);
216
229
  });
217
230
  }
package/dist/index.mjs CHANGED
@@ -16,16 +16,16 @@ function resolveId(id) {
16
16
  for (const alias of VIRTUAL_ENTRY_ALIAS) {
17
17
  const match = id.match(alias);
18
18
  if (match) {
19
- return match[1] ? {
20
- id: `/__uno_${match[1]}.css`,
21
- layer: match[1]
22
- } : {
23
- id: "/__uno.css",
24
- layer: LAYER_MARK_ALL
25
- };
19
+ return match[1] ? `/__uno_${match[1]}.css` : "/__uno.css";
26
20
  }
27
21
  }
28
22
  }
23
+ const RESOLVED_ID_RE = /\/__uno(?:(_.*?))?\.css$/;
24
+ function resolveLayer(id) {
25
+ const match = id.match(RESOLVED_ID_RE);
26
+ if (match)
27
+ return match[1] || LAYER_MARK_ALL;
28
+ }
29
29
  const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
30
30
  function getLayerPlaceholder(layer) {
31
31
  return `#--unocss--{layer:${layer}}`;
@@ -46,6 +46,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
46
46
  const uno = createGenerator(rawConfig, defaults);
47
47
  let rollupFilter = createFilter(defaultInclude, defaultExclude);
48
48
  const invalidations = [];
49
+ const reloadListeners = [];
49
50
  const modules = new BetterMap();
50
51
  const tokens = /* @__PURE__ */ new Set();
51
52
  let ready = reloadConfig();
@@ -59,6 +60,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
59
60
  tokens.clear();
60
61
  await Promise.all(modules.map((code, id) => uno.applyExtractors(code, id, tokens)));
61
62
  invalidate();
63
+ dispatchReload();
62
64
  const presets = /* @__PURE__ */ new Set();
63
65
  uno.config.presets.forEach((i) => {
64
66
  if (!i.name)
@@ -80,6 +82,9 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
80
82
  function invalidate() {
81
83
  invalidations.forEach((cb) => cb());
82
84
  }
85
+ function dispatchReload() {
86
+ reloadListeners.forEach((cb) => cb());
87
+ }
83
88
  async function extract(code, id) {
84
89
  if (id)
85
90
  modules.set(id, code);
@@ -109,6 +114,9 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = [], res
109
114
  },
110
115
  filter,
111
116
  reloadConfig,
117
+ onReload(fn) {
118
+ reloadListeners.push(fn);
119
+ },
112
120
  uno,
113
121
  extract,
114
122
  getConfig,
@@ -140,7 +148,8 @@ function WebpackPlugin(configOrPath, defaults) {
140
148
  timer = setTimeout(updateModules, UPDATE_DEBOUNCE);
141
149
  });
142
150
  const tasks = [];
143
- const entries = /* @__PURE__ */ new Map();
151
+ const entries = /* @__PURE__ */ new Set();
152
+ const hashs = /* @__PURE__ */ new Map();
144
153
  const plugin = {
145
154
  name: "unocss:webpack",
146
155
  enforce: "pre",
@@ -154,14 +163,18 @@ function WebpackPlugin(configOrPath, defaults) {
154
163
  resolveId(id) {
155
164
  const entry = resolveId(id);
156
165
  if (entry) {
157
- entries.set(entry.id, entry.layer);
158
- entries.set(id, entry.layer);
159
- return entry.id;
166
+ entries.add(entry);
167
+ return entry;
160
168
  }
161
169
  },
162
170
  load(id) {
163
- const layer = entries.get(getPath(id));
164
- const hash = entries.get(`${id}_hash`);
171
+ let layer = resolveLayer(getPath(id));
172
+ if (!layer) {
173
+ const entry = resolveId(id);
174
+ if (entry)
175
+ layer = resolveLayer(entry);
176
+ }
177
+ const hash = hashs.get(id);
165
178
  if (layer)
166
179
  return (hash ? getHashPlaceholder(hash) : "") + getLayerPlaceholder(layer);
167
180
  },
@@ -177,7 +190,7 @@ function WebpackPlugin(configOrPath, defaults) {
177
190
  code = code.replace(HASH_PLACEHOLDER_RE, "");
178
191
  code = code.replace(LAYER_PLACEHOLDER_RE, (_, quote, layer) => {
179
192
  replaced = true;
180
- const css = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries.values())) : result.getLayer(layer) || "";
193
+ const css = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || "";
181
194
  if (!quote)
182
195
  return css;
183
196
  let escaped = JSON.stringify(css).slice(1, -1);
@@ -198,12 +211,12 @@ function WebpackPlugin(configOrPath, defaults) {
198
211
  const result = await uno.generate(tokens);
199
212
  Array.from(plugin.__vfsModules).forEach((id) => {
200
213
  const path = id.slice(plugin.__virtualModulePrefix.length).replace(/\\/g, "/");
201
- const layer = entries.get(path);
214
+ const layer = resolveLayer(path);
202
215
  if (!layer)
203
216
  return;
204
- const code = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries.values())) : result.getLayer(layer) || "";
217
+ const code = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || "";
205
218
  const hash = getHash(code);
206
- entries.set(`${path}_hash`, hash);
219
+ hashs.set(path, hash);
207
220
  plugin.__vfs.writeModule(id, code);
208
221
  });
209
222
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/webpack",
3
- "version": "0.38.2",
3
+ "version": "0.39.2",
4
4
  "description": "The Webpack plugin for UnoCSS",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -37,8 +37,8 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@rollup/pluginutils": "^4.2.1",
40
- "@unocss/config": "0.38.2",
41
- "@unocss/core": "0.38.2",
40
+ "@unocss/config": "0.39.2",
41
+ "@unocss/core": "0.39.2",
42
42
  "unplugin": "^0.6.3",
43
43
  "webpack-sources": "^3.2.3"
44
44
  },