@unocss/postcss 0.50.1 → 0.50.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
@@ -16,7 +16,7 @@ const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
16
16
 
17
17
  const defaultIncludeGlobs = ["**/*.{html,js,ts,jsx,tsx,vue,svelte,astro,elm,php,phtml,mdx,md}"];
18
18
 
19
- function parseApply(root, uno, directiveName) {
19
+ async function parseApply(root, uno, directiveName) {
20
20
  root.walkAtRules(directiveName, async (rule) => {
21
21
  if (!rule.parent)
22
22
  return;
@@ -77,10 +77,10 @@ function parseApply(root, uno, directiveName) {
77
77
  });
78
78
  }
79
79
 
80
- function parseTheme(root, uno, directiveName) {
81
- const themeFnRE = new RegExp(`${directiveName}\\((.*?)\\)`, "g");
80
+ const themeFnRE = (directiveName) => new RegExp(`${directiveName}\\((.*?)\\)`, "g");
81
+ async function parseTheme(root, uno, directiveName) {
82
82
  root.walkDecls((decl) => {
83
- const matches = Array.from(decl.value.matchAll(themeFnRE));
83
+ const matches = Array.from(decl.value.matchAll(themeFnRE(directiveName)));
84
84
  if (!matches.length)
85
85
  return;
86
86
  for (const match of matches) {
@@ -111,7 +111,7 @@ function parseTheme(root, uno, directiveName) {
111
111
  });
112
112
  }
113
113
 
114
- function parseScreen(root, uno, directiveName) {
114
+ async function parseScreen(root, uno, directiveName) {
115
115
  root.walkAtRules(directiveName, async (rule) => {
116
116
  let breakpointName = "";
117
117
  let prefix = "";
@@ -158,105 +158,151 @@ function calcMaxWidthBySize(size) {
158
158
  return Number.isNaN(maxWidth) ? size : `${maxWidth}${unit}`;
159
159
  }
160
160
 
161
- function unocss({ content, directiveMap, cwd, configOrPath } = {
162
- cwd: process.cwd()
163
- }) {
161
+ function unocss(options = {}) {
162
+ core.warnOnce(
163
+ "`@unocss/postcss` package is in an experimental state right now. It doesn't follow semver, and may introduce breaking changes in patch versions."
164
+ );
165
+ const {
166
+ cwd = process.cwd(),
167
+ directiveMap = {
168
+ apply: "apply",
169
+ theme: "theme",
170
+ screen: "screen",
171
+ unocss: "unocss"
172
+ },
173
+ content,
174
+ configOrPath
175
+ } = options;
164
176
  const fileMap = /* @__PURE__ */ new Map();
165
177
  const fileClassMap = /* @__PURE__ */ new Map();
166
178
  const classes = /* @__PURE__ */ new Set();
179
+ const targetCache = /* @__PURE__ */ new Set();
167
180
  const config$1 = config.loadConfig(cwd, configOrPath);
168
181
  let uno;
169
- core.warnOnce(
170
- "`@unocss/postcss` package is in an experimental state right now. It doesn't follow semver, and may introduce breaking changes in patch versions."
171
- );
182
+ let promises$1 = [];
183
+ let last_config_mtime = 0;
184
+ const targetRE = new RegExp(Object.values(directiveMap).join("|"));
172
185
  return {
173
- postcssPlugin: "unocss",
186
+ postcssPlugin: directiveMap.unocss,
174
187
  plugins: [
175
188
  async function(root, result) {
176
- const cfg = await config$1;
177
- if (!Object.keys(!cfg.config))
178
- throw new Error("UnoCSS config file not found.");
179
- if (!uno)
180
- uno = core.createGenerator(cfg.config);
181
- await parseApply(root, uno, directiveMap?.apply || "apply");
182
- parseTheme(root, uno, directiveMap?.theme || "theme");
183
- await parseScreen(root, uno, directiveMap?.screen || "screen");
189
+ if (!result.opts.from?.split("?")[0].endsWith(".css"))
190
+ return;
191
+ let isTarget = false;
192
+ if (targetRE.test(root.toString())) {
193
+ if (!targetCache.has(result.opts.from)) {
194
+ root.walkAtRules((rule) => {
195
+ if (rule.name === directiveMap.unocss || rule.name === directiveMap.apply || rule.name === directiveMap.theme || rule.name === directiveMap.screen)
196
+ isTarget = true;
197
+ });
198
+ if (!isTarget) {
199
+ const themeFn = themeFnRE(directiveMap.theme);
200
+ root.walkDecls((decl) => {
201
+ if (themeFn.test(decl.value))
202
+ isTarget = true;
203
+ });
204
+ }
205
+ } else {
206
+ isTarget = true;
207
+ }
208
+ } else if (targetCache.has(result.opts.from)) {
209
+ targetCache.delete(result.opts.from);
210
+ }
211
+ if (!isTarget)
212
+ return;
213
+ else
214
+ targetCache.add(result.opts.from);
215
+ try {
216
+ const cfg = await config$1;
217
+ if (!uno) {
218
+ uno = core.createGenerator(cfg.config);
219
+ } else if (cfg.sources.length) {
220
+ const config_mtime = (await promises.stat(cfg.sources[0])).mtimeMs;
221
+ if (config_mtime > last_config_mtime) {
222
+ uno = core.createGenerator((await config.loadConfig(cwd, configOrPath)).config);
223
+ last_config_mtime = config_mtime;
224
+ }
225
+ }
226
+ } catch (error) {
227
+ throw new Error(`UnoCSS config not found: ${error.message}`);
228
+ }
184
229
  const globs = content?.filter((v) => typeof v === "string") ?? defaultIncludeGlobs;
185
230
  const rawContent = content?.filter((v) => typeof v === "object") ?? [];
186
231
  const entries = await fg__default(globs, {
187
232
  cwd,
188
233
  dot: true,
189
234
  absolute: true,
190
- ignore: ["**/{.git,node_modules}/**"]
235
+ ignore: ["**/{.git,node_modules}/**"],
236
+ stats: true
191
237
  });
192
- if (result.opts.from?.split("?")[0].endsWith(".css")) {
193
- result.messages.push({
194
- type: "dependency",
195
- plugin: "unocss",
196
- file: result.opts.from,
197
- parent: result.opts.from
198
- });
199
- await Promise.all(
200
- [
201
- ...rawContent.map(async (v) => {
202
- const { matched } = await uno.generate(v.raw, {
203
- id: `unocss${v.extension}`
204
- });
205
- for (const candidate of matched)
206
- classes.add(candidate);
207
- }),
208
- ...entries.map(async (file) => {
209
- result.messages.push({
210
- type: "dependency",
211
- plugin: "unocss",
212
- file,
213
- parent: result.opts.from
214
- });
215
- const { mtimeMs } = await promises.stat(file);
216
- if (fileMap.has(file) && mtimeMs <= fileMap.get(file))
217
- return;
218
- else
219
- fileMap.set(file, mtimeMs);
220
- const content2 = await promises.readFile(file, "utf8");
221
- const { matched } = await uno.generate(content2, {
222
- id: file
223
- });
224
- fileClassMap.set(file, matched);
225
- })
226
- ]
227
- );
228
- for (const set of fileClassMap.values()) {
229
- for (const candidate of set)
238
+ result.messages.push({
239
+ type: "dependency",
240
+ plugin: directiveMap.unocss,
241
+ file: result.opts.from,
242
+ parent: result.opts.from
243
+ });
244
+ await parseApply(root, uno, directiveMap.apply);
245
+ await parseTheme(root, uno, directiveMap.theme);
246
+ await parseScreen(root, uno, directiveMap.screen);
247
+ promises$1.push(
248
+ ...rawContent.map(async (v) => {
249
+ const { matched } = await uno.generate(v.raw, {
250
+ id: `unocss.${v.extension}`
251
+ });
252
+ for (const candidate of matched)
230
253
  classes.add(candidate);
231
- }
232
- const c = await uno.generate(classes);
233
- classes.clear();
234
- const excludes = [];
235
- root.walkAtRules("unocss", (rule) => {
236
- if (rule.params) {
237
- const source = rule.source;
238
- const layers = rule.params.split(",").map((v) => v.trim());
239
- const css = postcss__default.parse(
240
- layers.map((i) => (i === "all" ? c.getLayers() : c.getLayer(i)) || "").filter(Boolean).join("\n")
241
- );
242
- css.walkDecls((declaration) => {
243
- declaration.source = source;
244
- });
245
- rule.replaceWith(css);
246
- excludes.push(rule.params);
247
- }
248
- });
249
- root.walkAtRules("unocss", (rule) => {
250
- if (!rule.params) {
251
- const source = rule.source;
252
- const css = postcss__default.parse(c.getLayers(void 0, excludes) || "");
253
- css.walkDecls((declaration) => {
254
- declaration.source = source;
255
- });
256
- rule.replaceWith(css);
257
- }
258
- });
254
+ }),
255
+ ...entries.map(async ({ path: file, mtimeMs }) => {
256
+ result.messages.push({
257
+ type: "dependency",
258
+ plugin: directiveMap.unocss,
259
+ file,
260
+ parent: result.opts.from
261
+ });
262
+ if (fileMap.has(file) && mtimeMs <= fileMap.get(file))
263
+ return;
264
+ else
265
+ fileMap.set(file, mtimeMs);
266
+ const content2 = await promises.readFile(file, "utf8");
267
+ const { matched } = await uno.generate(content2, {
268
+ id: file
269
+ });
270
+ fileClassMap.set(file, matched);
271
+ })
272
+ );
273
+ await Promise.all(promises$1);
274
+ promises$1 = [];
275
+ for (const set of fileClassMap.values()) {
276
+ for (const candidate of set)
277
+ classes.add(candidate);
259
278
  }
279
+ const c = await uno.generate(classes);
280
+ classes.clear();
281
+ const excludes = [];
282
+ root.walkAtRules(directiveMap.unocss, (rule) => {
283
+ if (rule.params) {
284
+ const source = rule.source;
285
+ const layers = rule.params.split(",").map((v) => v.trim());
286
+ const css = postcss__default.parse(
287
+ layers.map((i) => (i === "all" ? c.getLayers() : c.getLayer(i)) || "").filter(Boolean).join("\n")
288
+ );
289
+ css.walkDecls((declaration) => {
290
+ declaration.source = source;
291
+ });
292
+ rule.replaceWith(css);
293
+ excludes.push(rule.params);
294
+ }
295
+ });
296
+ root.walkAtRules(directiveMap.unocss, (rule) => {
297
+ if (!rule.params) {
298
+ const source = rule.source;
299
+ const css = postcss__default.parse(c.getLayers(void 0, excludes) || "");
300
+ css.walkDecls((declaration) => {
301
+ declaration.source = source;
302
+ });
303
+ rule.replaceWith(css);
304
+ }
305
+ });
260
306
  }
261
307
  ]
262
308
  };
package/dist/index.d.ts CHANGED
@@ -10,12 +10,13 @@ interface UnoPostcssPluginOptions {
10
10
  apply: string;
11
11
  screen: string;
12
12
  theme: string;
13
+ unocss: string;
13
14
  };
14
15
  cwd?: string;
15
16
  configOrPath?: string | UserConfig;
16
17
  }
17
18
 
18
- declare function unocss({ content, directiveMap, cwd, configOrPath }?: UnoPostcssPluginOptions): {
19
+ declare function unocss(options?: UnoPostcssPluginOptions): {
19
20
  postcssPlugin: string;
20
21
  plugins: ((root: Root, result: Result) => Promise<void>)[];
21
22
  };
package/dist/index.mjs CHANGED
@@ -8,7 +8,7 @@ import MagicString from 'magic-string';
8
8
 
9
9
  const defaultIncludeGlobs = ["**/*.{html,js,ts,jsx,tsx,vue,svelte,astro,elm,php,phtml,mdx,md}"];
10
10
 
11
- function parseApply(root, uno, directiveName) {
11
+ async function parseApply(root, uno, directiveName) {
12
12
  root.walkAtRules(directiveName, async (rule) => {
13
13
  if (!rule.parent)
14
14
  return;
@@ -69,10 +69,10 @@ function parseApply(root, uno, directiveName) {
69
69
  });
70
70
  }
71
71
 
72
- function parseTheme(root, uno, directiveName) {
73
- const themeFnRE = new RegExp(`${directiveName}\\((.*?)\\)`, "g");
72
+ const themeFnRE = (directiveName) => new RegExp(`${directiveName}\\((.*?)\\)`, "g");
73
+ async function parseTheme(root, uno, directiveName) {
74
74
  root.walkDecls((decl) => {
75
- const matches = Array.from(decl.value.matchAll(themeFnRE));
75
+ const matches = Array.from(decl.value.matchAll(themeFnRE(directiveName)));
76
76
  if (!matches.length)
77
77
  return;
78
78
  for (const match of matches) {
@@ -103,7 +103,7 @@ function parseTheme(root, uno, directiveName) {
103
103
  });
104
104
  }
105
105
 
106
- function parseScreen(root, uno, directiveName) {
106
+ async function parseScreen(root, uno, directiveName) {
107
107
  root.walkAtRules(directiveName, async (rule) => {
108
108
  let breakpointName = "";
109
109
  let prefix = "";
@@ -150,105 +150,151 @@ function calcMaxWidthBySize(size) {
150
150
  return Number.isNaN(maxWidth) ? size : `${maxWidth}${unit}`;
151
151
  }
152
152
 
153
- function unocss({ content, directiveMap, cwd, configOrPath } = {
154
- cwd: process.cwd()
155
- }) {
153
+ function unocss(options = {}) {
154
+ warnOnce(
155
+ "`@unocss/postcss` package is in an experimental state right now. It doesn't follow semver, and may introduce breaking changes in patch versions."
156
+ );
157
+ const {
158
+ cwd = process.cwd(),
159
+ directiveMap = {
160
+ apply: "apply",
161
+ theme: "theme",
162
+ screen: "screen",
163
+ unocss: "unocss"
164
+ },
165
+ content,
166
+ configOrPath
167
+ } = options;
156
168
  const fileMap = /* @__PURE__ */ new Map();
157
169
  const fileClassMap = /* @__PURE__ */ new Map();
158
170
  const classes = /* @__PURE__ */ new Set();
171
+ const targetCache = /* @__PURE__ */ new Set();
159
172
  const config = loadConfig(cwd, configOrPath);
160
173
  let uno;
161
- warnOnce(
162
- "`@unocss/postcss` package is in an experimental state right now. It doesn't follow semver, and may introduce breaking changes in patch versions."
163
- );
174
+ let promises = [];
175
+ let last_config_mtime = 0;
176
+ const targetRE = new RegExp(Object.values(directiveMap).join("|"));
164
177
  return {
165
- postcssPlugin: "unocss",
178
+ postcssPlugin: directiveMap.unocss,
166
179
  plugins: [
167
180
  async function(root, result) {
168
- const cfg = await config;
169
- if (!Object.keys(!cfg.config))
170
- throw new Error("UnoCSS config file not found.");
171
- if (!uno)
172
- uno = createGenerator(cfg.config);
173
- await parseApply(root, uno, directiveMap?.apply || "apply");
174
- parseTheme(root, uno, directiveMap?.theme || "theme");
175
- await parseScreen(root, uno, directiveMap?.screen || "screen");
181
+ if (!result.opts.from?.split("?")[0].endsWith(".css"))
182
+ return;
183
+ let isTarget = false;
184
+ if (targetRE.test(root.toString())) {
185
+ if (!targetCache.has(result.opts.from)) {
186
+ root.walkAtRules((rule) => {
187
+ if (rule.name === directiveMap.unocss || rule.name === directiveMap.apply || rule.name === directiveMap.theme || rule.name === directiveMap.screen)
188
+ isTarget = true;
189
+ });
190
+ if (!isTarget) {
191
+ const themeFn = themeFnRE(directiveMap.theme);
192
+ root.walkDecls((decl) => {
193
+ if (themeFn.test(decl.value))
194
+ isTarget = true;
195
+ });
196
+ }
197
+ } else {
198
+ isTarget = true;
199
+ }
200
+ } else if (targetCache.has(result.opts.from)) {
201
+ targetCache.delete(result.opts.from);
202
+ }
203
+ if (!isTarget)
204
+ return;
205
+ else
206
+ targetCache.add(result.opts.from);
207
+ try {
208
+ const cfg = await config;
209
+ if (!uno) {
210
+ uno = createGenerator(cfg.config);
211
+ } else if (cfg.sources.length) {
212
+ const config_mtime = (await stat(cfg.sources[0])).mtimeMs;
213
+ if (config_mtime > last_config_mtime) {
214
+ uno = createGenerator((await loadConfig(cwd, configOrPath)).config);
215
+ last_config_mtime = config_mtime;
216
+ }
217
+ }
218
+ } catch (error) {
219
+ throw new Error(`UnoCSS config not found: ${error.message}`);
220
+ }
176
221
  const globs = content?.filter((v) => typeof v === "string") ?? defaultIncludeGlobs;
177
222
  const rawContent = content?.filter((v) => typeof v === "object") ?? [];
178
223
  const entries = await fg(globs, {
179
224
  cwd,
180
225
  dot: true,
181
226
  absolute: true,
182
- ignore: ["**/{.git,node_modules}/**"]
227
+ ignore: ["**/{.git,node_modules}/**"],
228
+ stats: true
183
229
  });
184
- if (result.opts.from?.split("?")[0].endsWith(".css")) {
185
- result.messages.push({
186
- type: "dependency",
187
- plugin: "unocss",
188
- file: result.opts.from,
189
- parent: result.opts.from
190
- });
191
- await Promise.all(
192
- [
193
- ...rawContent.map(async (v) => {
194
- const { matched } = await uno.generate(v.raw, {
195
- id: `unocss${v.extension}`
196
- });
197
- for (const candidate of matched)
198
- classes.add(candidate);
199
- }),
200
- ...entries.map(async (file) => {
201
- result.messages.push({
202
- type: "dependency",
203
- plugin: "unocss",
204
- file,
205
- parent: result.opts.from
206
- });
207
- const { mtimeMs } = await stat(file);
208
- if (fileMap.has(file) && mtimeMs <= fileMap.get(file))
209
- return;
210
- else
211
- fileMap.set(file, mtimeMs);
212
- const content2 = await readFile(file, "utf8");
213
- const { matched } = await uno.generate(content2, {
214
- id: file
215
- });
216
- fileClassMap.set(file, matched);
217
- })
218
- ]
219
- );
220
- for (const set of fileClassMap.values()) {
221
- for (const candidate of set)
230
+ result.messages.push({
231
+ type: "dependency",
232
+ plugin: directiveMap.unocss,
233
+ file: result.opts.from,
234
+ parent: result.opts.from
235
+ });
236
+ await parseApply(root, uno, directiveMap.apply);
237
+ await parseTheme(root, uno, directiveMap.theme);
238
+ await parseScreen(root, uno, directiveMap.screen);
239
+ promises.push(
240
+ ...rawContent.map(async (v) => {
241
+ const { matched } = await uno.generate(v.raw, {
242
+ id: `unocss.${v.extension}`
243
+ });
244
+ for (const candidate of matched)
222
245
  classes.add(candidate);
223
- }
224
- const c = await uno.generate(classes);
225
- classes.clear();
226
- const excludes = [];
227
- root.walkAtRules("unocss", (rule) => {
228
- if (rule.params) {
229
- const source = rule.source;
230
- const layers = rule.params.split(",").map((v) => v.trim());
231
- const css = postcss.parse(
232
- layers.map((i) => (i === "all" ? c.getLayers() : c.getLayer(i)) || "").filter(Boolean).join("\n")
233
- );
234
- css.walkDecls((declaration) => {
235
- declaration.source = source;
236
- });
237
- rule.replaceWith(css);
238
- excludes.push(rule.params);
239
- }
240
- });
241
- root.walkAtRules("unocss", (rule) => {
242
- if (!rule.params) {
243
- const source = rule.source;
244
- const css = postcss.parse(c.getLayers(void 0, excludes) || "");
245
- css.walkDecls((declaration) => {
246
- declaration.source = source;
247
- });
248
- rule.replaceWith(css);
249
- }
250
- });
246
+ }),
247
+ ...entries.map(async ({ path: file, mtimeMs }) => {
248
+ result.messages.push({
249
+ type: "dependency",
250
+ plugin: directiveMap.unocss,
251
+ file,
252
+ parent: result.opts.from
253
+ });
254
+ if (fileMap.has(file) && mtimeMs <= fileMap.get(file))
255
+ return;
256
+ else
257
+ fileMap.set(file, mtimeMs);
258
+ const content2 = await readFile(file, "utf8");
259
+ const { matched } = await uno.generate(content2, {
260
+ id: file
261
+ });
262
+ fileClassMap.set(file, matched);
263
+ })
264
+ );
265
+ await Promise.all(promises);
266
+ promises = [];
267
+ for (const set of fileClassMap.values()) {
268
+ for (const candidate of set)
269
+ classes.add(candidate);
251
270
  }
271
+ const c = await uno.generate(classes);
272
+ classes.clear();
273
+ const excludes = [];
274
+ root.walkAtRules(directiveMap.unocss, (rule) => {
275
+ if (rule.params) {
276
+ const source = rule.source;
277
+ const layers = rule.params.split(",").map((v) => v.trim());
278
+ const css = postcss.parse(
279
+ layers.map((i) => (i === "all" ? c.getLayers() : c.getLayer(i)) || "").filter(Boolean).join("\n")
280
+ );
281
+ css.walkDecls((declaration) => {
282
+ declaration.source = source;
283
+ });
284
+ rule.replaceWith(css);
285
+ excludes.push(rule.params);
286
+ }
287
+ });
288
+ root.walkAtRules(directiveMap.unocss, (rule) => {
289
+ if (!rule.params) {
290
+ const source = rule.source;
291
+ const css = postcss.parse(c.getLayers(void 0, excludes) || "");
292
+ css.walkDecls((declaration) => {
293
+ declaration.source = source;
294
+ });
295
+ rule.replaceWith(css);
296
+ }
297
+ });
252
298
  }
253
299
  ]
254
300
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/postcss",
3
- "version": "0.50.1",
3
+ "version": "0.50.2",
4
4
  "description": "PostCSS plugin for UnoCSS",
5
5
  "author": "sibbng <sibbngheid@gmail.com>",
6
6
  "license": "MIT",
@@ -36,8 +36,8 @@
36
36
  "postcss": "^8.4.21"
37
37
  },
38
38
  "dependencies": {
39
- "@unocss/config": "0.50.1",
40
- "@unocss/core": "0.50.1",
39
+ "@unocss/config": "0.50.2",
40
+ "@unocss/core": "0.50.2",
41
41
  "css-tree": "^2.3.1",
42
42
  "fast-glob": "^3.2.12",
43
43
  "magic-string": "^0.30.0",