@kanso-labs/unplugin-style-dictionary 0.10.14 → 0.10.16

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.js CHANGED
@@ -3,8 +3,9 @@ import { asError, errorMessage } from "./errors.js";
3
3
  import { resolveConfigOption } from "./config.js";
4
4
  import { expandPatterns, realpathOf, watchOptionPatterns, watchPatternsOf } from "./patterns.js";
5
5
  import { runBuilds } from "./compile.js";
6
+ import { checkOptions } from "./options.js";
6
7
  import { createScheduler } from "./scheduler.js";
7
- import { isWatchedSource, registeredSpellingOf } from "./watch-filter.js";
8
+ import { compileWatchPatterns, isWatchedSource, matchesWatchedFile, registeredSpellingOf } from "./watch-filter.js";
8
9
  import fs from "node:fs";
9
10
  import path from "node:path";
10
11
  import { createUnplugin } from "unplugin";
@@ -28,11 +29,14 @@ function discardTemporaryFile(temporary) {
28
29
  function isMessageChannel(value) {
29
30
  return typeof value === "function";
30
31
  }
31
- function nodeModulesNegations(paths) {
32
- const negations = /* @__PURE__ */ new Set();
32
+ function nodeModulesNegations(patterns, paths) {
33
+ const reaching = [...patterns.globs, ...patterns.literals].filter((pattern) => pattern.includes("/node_modules/"));
34
+ const negations = new Set(reaching.map((pattern) => `!${pattern}`));
33
35
  for (const file of paths) {
34
36
  const normalised = file.replace(/\\/g, "/");
35
- if (normalised.includes("/node_modules/")) negations.add(`!${normalised}`);
37
+ if (!normalised.includes("/node_modules/") || isDirectory(file)) continue;
38
+ if (matchesWatchedFile(normalised, reaching)) continue;
39
+ negations.add(`!${normalised}`);
36
40
  }
37
41
  return Array.from(negations);
38
42
  }
@@ -326,6 +330,7 @@ function isDirectory(file) {
326
330
  }
327
331
  const unpluginFactory = (options = {}, meta) => {
328
332
  const isWebpack = meta.framework === "webpack" || meta.framework === "rspack";
333
+ checkOptions(options);
329
334
  const { cache = true, errorOverlay = true, failOnError = "build", logLevel, onBuildEnd, onBuildError, onBuildStart, platforms: platformsOption, report = true, root: rootOption, silent = false } = options;
330
335
  const level = logLevel ?? (silent ? "silent" : void 0);
331
336
  const quiet = level === "silent" || level === "warn";
@@ -346,7 +351,7 @@ const unpluginFactory = (options = {}, meta) => {
346
351
  };
347
352
  let root = rootOption ? path.resolve(process.cwd(), rootOption) : process.cwd();
348
353
  const generatedDestinations = /* @__PURE__ */ new Set();
349
- let cachedPatterns;
354
+ let cachedMatcher;
350
355
  const linkedPaths = /* @__PURE__ */ new Map();
351
356
  const addWatchFiles = (context, paths) => {
352
357
  const contextDependencies = isWebpack ? contextDependenciesOf(context) : void 0;
@@ -411,12 +416,18 @@ const unpluginFactory = (options = {}, meta) => {
411
416
  }
412
417
  for (const line of render(stdoutColour)) console.log(line);
413
418
  };
419
+ const saidAboutConfig = /* @__PURE__ */ new Set();
420
+ const logConfig = (message, type) => {
421
+ if (saidAboutConfig.has(message)) return;
422
+ saidAboutConfig.add(message);
423
+ log(message, type);
424
+ };
414
425
  const resolveConfigs = async () => {
415
426
  const resolved = await resolveConfigOption({
416
427
  config: options.config,
417
428
  configContext,
418
429
  discovery,
419
- log,
430
+ log: logConfig,
420
431
  root
421
432
  });
422
433
  if (!options.watch) return resolved;
@@ -424,14 +435,23 @@ const unpluginFactory = (options = {}, meta) => {
424
435
  for (const item of resolved) if (item.file) item.watched = watched;
425
436
  return resolved;
426
437
  };
427
- const getWatchTargets = async (resolvedConfigs) => {
438
+ const deriveWatchPatterns = async (resolvedConfigs) => {
428
439
  const patterns = await watchPatternsOf({
429
- log,
440
+ log: logConfig,
430
441
  root,
431
442
  watch: options.watch
432
443
  }, resolvedConfigs);
433
- cachedPatterns = patterns;
444
+ const matcher = compileWatchPatterns(patterns);
445
+ cachedMatcher = matcher;
446
+ return {
447
+ matcher,
448
+ patterns
449
+ };
450
+ };
451
+ const getWatchTargets = async (resolvedConfigs) => {
452
+ const { matcher, patterns } = await deriveWatchPatterns(resolvedConfigs);
434
453
  return {
454
+ matcher,
435
455
  paths: await expandPatterns(patterns, log),
436
456
  patterns
437
457
  };
@@ -444,6 +464,7 @@ const unpluginFactory = (options = {}, meta) => {
444
464
  log,
445
465
  logTable,
446
466
  notifyBuildOutcome: (error) => {
467
+ if (!error) saidAboutConfig.clear();
447
468
  notifyBuildOutcome?.(error);
448
469
  },
449
470
  onBuildEnd,
@@ -552,7 +573,7 @@ const unpluginFactory = (options = {}, meta) => {
552
573
  const changed = [...compiler.modifiedFiles ?? [], ...compiler.removedFiles ?? []];
553
574
  for (const file of changed) {
554
575
  const id = registeredSpellingOf(file, linkedPaths);
555
- if (!cachedPatterns || watchedDirectories.has(id.replace(/\\/g, "/")) || isWatchedSource(id, cachedPatterns, generatedDestinations)) return id;
576
+ if (!cachedMatcher || watchedDirectories.has(id.replace(/\\/g, "/")) || isWatchedSource(id, cachedMatcher, generatedDestinations)) return id;
556
577
  }
557
578
  };
558
579
  const rebuildTokens = async () => {
@@ -618,8 +639,8 @@ const unpluginFactory = (options = {}, meta) => {
618
639
  try {
619
640
  const resolved = await resolveConfigs();
620
641
  if (resolved.length > 0) {
621
- const { paths } = await getWatchTargets(resolved);
622
- appendChokidarIgnored(config.build.watch, nodeModulesNegations(paths));
642
+ const { paths, patterns } = await getWatchTargets(resolved);
643
+ appendChokidarIgnored(config.build.watch, nodeModulesNegations(patterns, paths));
623
644
  }
624
645
  } catch (err) {
625
646
  log(`Could not read the configuration while preparing the watch list: ${errorMessage(err)}`, "error");
@@ -632,8 +653,8 @@ const unpluginFactory = (options = {}, meta) => {
632
653
  startupResolved = await resolveConfigs();
633
654
  if (startupResolved.length === 0) return;
634
655
  if (config.server.watch === null) return;
635
- const { paths } = await getWatchTargets(startupResolved);
636
- const negations = nodeModulesNegations(paths);
656
+ const { paths, patterns } = await getWatchTargets(startupResolved);
657
+ const negations = nodeModulesNegations(patterns, paths);
637
658
  if (negations.length === 0) return;
638
659
  const existing = config.server.watch?.ignored;
639
660
  config.server.watch = {
@@ -668,7 +689,7 @@ const unpluginFactory = (options = {}, meta) => {
668
689
  const watcher = fs.watch(directory, (_event, filename) => {
669
690
  if (filename === null) return;
670
691
  const changed = path.posix.join(directory, filename);
671
- if (!isWatchedSource(changed, targets.patterns, generatedDestinations)) return;
692
+ if (!isWatchedSource(changed, targets.matcher, generatedDestinations)) return;
672
693
  schedule(path.basename(changed)).catch(() => {});
673
694
  });
674
695
  watcher.unref();
@@ -682,8 +703,10 @@ const unpluginFactory = (options = {}, meta) => {
682
703
  ownWatchers.clear();
683
704
  };
684
705
  refreshServerWatchList = async (rebuilt) => {
706
+ const previous = new Set(targets.paths);
685
707
  targets = await getWatchTargets(rebuilt);
686
- server.watcher.add(targets.paths);
708
+ const adding = targets.paths.filter((file) => !previous.has(file) || !fs.existsSync(file));
709
+ if (adding.length > 0) server.watcher.add(adding);
687
710
  watchNodeModules(targets.paths);
688
711
  };
689
712
  if (errorOverlay) {
@@ -710,7 +733,7 @@ const unpluginFactory = (options = {}, meta) => {
710
733
  };
711
734
  }
712
735
  server.watcher.on("all", (_event, file) => {
713
- if (!isWatchedSource(file, targets.patterns, generatedDestinations)) return;
736
+ if (!isWatchedSource(file, targets.matcher, generatedDestinations)) return;
714
737
  schedule(path.basename(file)).catch(() => {});
715
738
  });
716
739
  }
@@ -721,12 +744,13 @@ const unpluginFactory = (options = {}, meta) => {
721
744
  if (hostClosed) return;
722
745
  watchRebuild = true;
723
746
  const id = registeredSpellingOf(changed, linkedPaths);
724
- if (cachedPatterns && !isWatchedSource(id, cachedPatterns, generatedDestinations)) return;
747
+ if (cachedMatcher && !isWatchedSource(id, cachedMatcher, generatedDestinations)) return;
725
748
  const resolved = await resolveConfigs();
726
749
  if (resolved.length === 0) return;
727
- const { patterns } = await getWatchTargets(resolved);
728
- if (!isWatchedSource(id, patterns, generatedDestinations)) return;
750
+ const { matcher, patterns } = await deriveWatchPatterns(resolved);
751
+ if (!isWatchedSource(id, matcher, generatedDestinations)) return;
729
752
  if (!isWebpack) await schedule(path.basename(id));
753
+ if (refreshServerWatchList) return;
730
754
  addWatchFiles(this, await expandPatterns(patterns, log));
731
755
  },
732
756
  webpack: adoptCompiler