@moritzloewenstein/vite-plugin-sass-glob-import 5.0.1 → 5.0.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/README.md CHANGED
@@ -12,14 +12,14 @@ Fork of [vite-plugin-sass-glob-import](https://github.com/cmalven/vite-plugin-sa
12
12
  ## Install
13
13
 
14
14
  ```shell
15
- npm i -D vite-plugin-sass-glob-import
15
+ npm i -D @moritzloewenstein/vite-plugin-sass-glob-import
16
16
  ```
17
17
 
18
18
  ```js
19
19
  // In vite.config.js
20
20
 
21
21
  import { defineConfig } from "vite";
22
- import sassGlobImports from "vite-plugin-sass-glob-import";
22
+ import sassGlobImports from "@moritzloewenstein/vite-plugin-sass-glob-import";
23
23
 
24
24
  export default defineConfig({
25
25
  plugins: [sassGlobImports()],
package/dist/index.d.mts CHANGED
@@ -2,6 +2,7 @@ import { Plugin } from 'vite';
2
2
 
3
3
  interface PluginOptions {
4
4
  ignorePaths?: string[];
5
+ autoInvalidation?: boolean;
5
6
  }
6
7
 
7
8
  declare function sassGlobImports(_options?: PluginOptions): Plugin;
package/dist/index.mjs CHANGED
@@ -4,29 +4,83 @@ import path from "path";
4
4
  import c from "ansi-colors";
5
5
  import { globSync } from "glob";
6
6
  import { minimatch } from "minimatch";
7
+ import {
8
+ normalizePath
9
+ } from "vite";
7
10
  var IMPORT_REGEX;
8
11
  var options;
12
+ var globToModuleIds;
13
+ var projectRoot;
14
+ var server;
9
15
  function sassGlobImports(_options = {}) {
10
16
  IMPORT_REGEX = /^([ \t]*(?:\/\*.*)?)@(import|use)\s+["']([^"']+\*[^"']*(?:\.scss|\.sass)?)["'];?([ \t]*(?:\/[/*].*)?)$/gm;
11
17
  options = _options;
18
+ globToModuleIds = /* @__PURE__ */ new Map();
12
19
  return {
13
20
  name: "sass-glob-import",
14
21
  enforce: "pre",
22
+ configResolved(config) {
23
+ projectRoot = normalizePath(config.root);
24
+ if (options.autoInvalidation && !config.server.watch) {
25
+ config.logger.warn(
26
+ "vite-plugin-sass-glob-import: set server.watch: true for automatic glob module invalidation"
27
+ );
28
+ }
29
+ },
30
+ configureServer(_server) {
31
+ if (!options.autoInvalidation) {
32
+ return;
33
+ }
34
+ server = _server;
35
+ server.watcher.on("all", async (_event, pathName) => {
36
+ if (!path.extname(pathName).match(/\.sass|\.scss/i)) {
37
+ return;
38
+ }
39
+ const relPathName = path.relative(projectRoot, pathName);
40
+ const modsToInvalidate = /* @__PURE__ */ new Set();
41
+ for (const [glob, modSet] of globToModuleIds) {
42
+ if (minimatch(relPathName, glob)) {
43
+ for (const mod of modSet) {
44
+ modsToInvalidate.add(mod);
45
+ }
46
+ }
47
+ }
48
+ const modsByFile = /* @__PURE__ */ new Set();
49
+ for (const mod of modsToInvalidate) {
50
+ const modules = server.moduleGraph.getModulesByFile(mod);
51
+ if (!modules) continue;
52
+ for (const m of modules) {
53
+ modsByFile.add(m);
54
+ }
55
+ }
56
+ invalidateModules(server, modsByFile, Date.now());
57
+ await Promise.all(
58
+ Array.from(modsByFile).map((mod) => server.reloadModule(mod))
59
+ );
60
+ });
61
+ },
15
62
  transform(src, id) {
16
- const fileName = path.basename(id);
17
- const filePath = path.dirname(id);
18
63
  return {
19
- code: transform(src, fileName, filePath),
64
+ code: transform(src, id),
20
65
  map: null
21
66
  // provide source map if available
22
67
  };
23
68
  }
24
69
  };
25
70
  }
71
+ function invalidateModules(server2, modules, timestamp) {
72
+ const seen = /* @__PURE__ */ new Set();
73
+ for (const mod of modules) {
74
+ server2.moduleGraph.invalidateModule(mod, seen, timestamp, true);
75
+ }
76
+ return modules;
77
+ }
26
78
  function isSassOrScss(filename) {
27
79
  return !fs.statSync(filename).isDirectory() && path.extname(filename).match(/\.sass|\.scss/i);
28
80
  }
29
- function transform(src, fileName, filePath) {
81
+ function transform(src, id) {
82
+ const fileName = path.basename(id);
83
+ const filePath = path.dirname(id);
30
84
  const isSass = path.extname(fileName).match(/\.sass/i);
31
85
  const searchBases = [filePath];
32
86
  const ignorePaths = options.ignorePaths || [];
@@ -35,6 +89,22 @@ function transform(src, fileName, filePath) {
35
89
  const result = [...src.matchAll(IMPORT_REGEX)];
36
90
  if (result.length === 0) continue;
37
91
  const [importRule, startComment, importType, globPattern, endComment] = result[0];
92
+ if (options.autoInvalidation) {
93
+ const projectGlob = path.relative(
94
+ projectRoot,
95
+ path.resolve(path.join(filePath, globPattern))
96
+ );
97
+ const hasGlob = globToModuleIds.has(projectGlob);
98
+ if (!globToModuleIds.get(projectGlob)?.has(id)) {
99
+ const modSet = globToModuleIds.get(projectGlob) ?? /* @__PURE__ */ new Set();
100
+ modSet.add(id);
101
+ globToModuleIds.set(projectGlob, modSet);
102
+ if (!hasGlob) {
103
+ const globDir = projectGlob.split("*")[0];
104
+ server.watcher.add(globDir);
105
+ }
106
+ }
107
+ }
38
108
  let files = [];
39
109
  let basePath = "";
40
110
  for (let i2 = 0; i2 < searchBases.length; i2++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moritzloewenstein/vite-plugin-sass-glob-import",
3
- "version": "5.0.1",
3
+ "version": "5.0.2",
4
4
  "description": "Use glob syntax for imports in your main Sass or SCSS file.",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.mts",