@sdata/web-vue-auto-import-resolver 3.21.2 → 3.22.0

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.
Files changed (3) hide show
  1. package/index.d.ts +20 -23
  2. package/index.js +179 -164
  3. package/package.json +2 -1
package/index.d.ts CHANGED
@@ -1,23 +1,21 @@
1
- import * as url from 'url';
2
- import { ComponentResolver } from 'unplugin-vue-components/types';
3
- import { Plugin } from 'vite';
4
-
5
- interface SdDesignResolverOptions {
6
- /** @default 'Sd' */
7
- prefix?: string;
1
+ import { ComponentResolver } from "unplugin-vue-components/types";
2
+ import { Plugin } from "vite";
3
+ //#region index.d.ts
4
+ export interface SdDesignResolverOptions {
5
+ /** @default 'Sd' */
6
+ prefix?: string;
8
7
  }
9
- interface SdDesignScssImporterOptions {
10
- /** @default '@sdata/web-vue' */
11
- packageName?: string;
12
- }
13
- interface SdDesignVitePluginOptions extends SdDesignScssImporterOptions {
8
+ export interface SdDesignScssImporterOptions {
9
+ /** @default '@sdata/web-vue' */
10
+ packageName?: string;
14
11
  }
12
+ export interface SdDesignVitePluginOptions extends SdDesignScssImporterOptions {}
15
13
  /**
16
14
  * Resolver for SD Design Vue
17
15
  *
18
16
  * @link https://www.npmjs.com/package/@sdata/web-vue
19
17
  */
20
- declare function SdDesignResolver(options?: SdDesignResolverOptions): ComponentResolver[];
18
+ export declare function SdDesignResolver(options?: SdDesignResolverOptions): ComponentResolver[];
21
19
  /**
22
20
  * Create a custom Sass importer that resolves `@style/` and `@components/`
23
21
  * aliases used in `@sdata/web-vue` SCSS files.
@@ -40,13 +38,13 @@ declare function SdDesignResolver(options?: SdDesignResolverOptions): ComponentR
40
38
  * });
41
39
  * ```
42
40
  */
43
- declare function createSdDesignScssImporter(options?: SdDesignScssImporterOptions): {
44
- canonicalize(url: string): url.URL | null;
45
- load(canonicalUrl: URL): Promise<{
46
- contents: string;
47
- syntax: "css" | "scss" | "indented";
48
- sourceMapUrl: URL;
49
- } | null>;
41
+ export declare function createSdDesignScssImporter(options?: SdDesignScssImporterOptions): {
42
+ canonicalize(url: string): import("node:url").URL | null;
43
+ load(canonicalUrl: URL): Promise<{
44
+ contents: string;
45
+ syntax: "scss" | "indented" | "css";
46
+ sourceMapUrl: URL;
47
+ } | null>;
50
48
  };
51
49
  /**
52
50
  * Create a Vite plugin that automatically configures a custom Sass importer
@@ -66,6 +64,5 @@ declare function createSdDesignScssImporter(options?: SdDesignScssImporterOption
66
64
  * });
67
65
  * ```
68
66
  */
69
- declare function createSdDesignVitePlugin(options?: SdDesignVitePluginOptions): Plugin;
70
-
71
- export { SdDesignResolver, type SdDesignResolverOptions, type SdDesignScssImporterOptions, type SdDesignVitePluginOptions, createSdDesignScssImporter, createSdDesignVitePlugin };
67
+ export declare function createSdDesignVitePlugin(options?: SdDesignVitePluginOptions): Plugin;
68
+ //#endregion
package/index.js CHANGED
@@ -1,179 +1,194 @@
1
- // index.ts
2
- import { existsSync, readFileSync } from "fs";
3
- import { readFile } from "fs/promises";
4
- import { createRequire } from "module";
5
- import path from "path";
6
- import { fileURLToPath, pathToFileURL } from "url";
7
- var DEFAULT_PREFIX = "Sd";
8
- var PACKAGE_NAME = "@sdata/web-vue";
9
- var SASS_EXTENSIONS = [".scss", ".sass", ".css"];
10
- var cachedComponentSet;
11
- var parseImportClause = (clause, modulePath, importPathMap) => {
12
- const trimmedClause = clause.trim();
13
- if (!trimmedClause) {
14
- return;
15
- }
16
- const namedOnlyMatch = /^\{([\s\S]*)\}$/.exec(trimmedClause);
17
- if (namedOnlyMatch) {
18
- const namedImports = namedOnlyMatch[1]?.split(",").map((item) => item.trim()).filter(Boolean) ?? [];
19
- for (const namedImport of namedImports) {
20
- const [sourceName, localName] = namedImport.split(/\s+as\s+/i).map((item) => item.trim());
21
- importPathMap.set(localName ?? sourceName, modulePath);
22
- }
23
- return;
24
- }
25
- const [defaultImport, namedImportsClause] = trimmedClause.split(/,\s*(?=\{)/).map((item) => item.trim());
26
- if (defaultImport) {
27
- importPathMap.set(defaultImport, modulePath);
28
- }
29
- if (!namedImportsClause) {
30
- return;
31
- }
32
- parseImportClause(namedImportsClause, modulePath, importPathMap);
1
+ import { createRequire } from "node:module";
2
+ import { existsSync, readFileSync } from "node:fs";
3
+ import { readFile } from "node:fs/promises";
4
+ import path from "node:path";
5
+ import { fileURLToPath, pathToFileURL } from "node:url";
6
+ //#region index.ts
7
+ const DEFAULT_PREFIX = "Sd";
8
+ const PACKAGE_NAME = "@sdata/web-vue";
9
+ const SASS_EXTENSIONS = [
10
+ ".scss",
11
+ ".sass",
12
+ ".css"
13
+ ];
14
+ let cachedComponentSet;
15
+ const parseImportClause = (clause, modulePath, importPathMap) => {
16
+ const trimmedClause = clause.trim();
17
+ if (!trimmedClause) return;
18
+ const namedOnlyMatch = /^\{([\s\S]*)\}$/.exec(trimmedClause);
19
+ if (namedOnlyMatch) {
20
+ const namedImports = namedOnlyMatch[1]?.split(",").map((item) => item.trim()).filter(Boolean) ?? [];
21
+ for (const namedImport of namedImports) {
22
+ const [sourceName, localName] = namedImport.split(/\s+as\s+/i).map((item) => item.trim());
23
+ importPathMap.set(localName ?? sourceName, modulePath);
24
+ }
25
+ return;
26
+ }
27
+ const [defaultImport, namedImportsClause] = trimmedClause.split(/,\s*(?=\{)/).map((item) => item.trim());
28
+ if (defaultImport) importPathMap.set(defaultImport, modulePath);
29
+ if (!namedImportsClause) return;
30
+ parseImportClause(namedImportsClause, modulePath, importPathMap);
33
31
  };
34
- var getPackageRoot = () => {
35
- const require2 = createRequire(import.meta.url);
36
- return path.dirname(require2.resolve(`${PACKAGE_NAME}/package.json`));
32
+ const getPackageRoot = () => {
33
+ const require = createRequire(import.meta.url);
34
+ return path.dirname(require.resolve(`${PACKAGE_NAME}/package.json`));
37
35
  };
38
- var getComponentExportSet = () => {
39
- if (cachedComponentSet) {
40
- return cachedComponentSet;
41
- }
42
- const packageRoot = getPackageRoot();
43
- const entryPath = path.resolve(packageRoot, "es/index.js");
44
- const source = readFileSync(entryPath, "utf8");
45
- const importPathMap = /* @__PURE__ */ new Map();
46
- for (const match of source.matchAll(/import\s+([\s\S]*?)\s+from\s+["'](\.[^"']+)["'];/g)) {
47
- const clause = match[1];
48
- const modulePath = match[2];
49
- if (!clause || !modulePath) {
50
- continue;
51
- }
52
- parseImportClause(clause, modulePath, importPathMap);
53
- }
54
- const exportBlockMatch = /export\s*\{([\s\S]*?)\};?\s*$/.exec(source);
55
- if (!exportBlockMatch?.[1]) {
56
- cachedComponentSet = /* @__PURE__ */ new Set();
57
- return cachedComponentSet;
58
- }
59
- const exportSpecifiers = exportBlockMatch[1].split(",").map((item) => item.trim()).filter(Boolean);
60
- const componentSet = /* @__PURE__ */ new Set();
61
- for (const exportSpecifier of exportSpecifiers) {
62
- const [localName, exportedName] = exportSpecifier.split(/\s+as\s+/i).map((item) => item.trim());
63
- const importName = exportedName ?? localName;
64
- if (!localName || !importName || importName === "default") {
65
- continue;
66
- }
67
- const modulePath = importPathMap.get(localName);
68
- if (!modulePath || modulePath.startsWith("./_") || modulePath === "./sd-vue.js") {
69
- continue;
70
- }
71
- componentSet.add(importName);
72
- }
73
- cachedComponentSet = componentSet;
74
- return componentSet;
36
+ const getComponentExportSet = () => {
37
+ if (cachedComponentSet) return cachedComponentSet;
38
+ const packageRoot = getPackageRoot();
39
+ const entryPath = path.resolve(packageRoot, "es/index.js");
40
+ const source = readFileSync(entryPath, "utf8");
41
+ const importPathMap = /* @__PURE__ */ new Map();
42
+ for (const match of source.matchAll(/import\s+([\s\S]*?)\s+from\s+["'](\.[^"']+)["'];/g)) {
43
+ const clause = match[1];
44
+ const modulePath = match[2];
45
+ if (!clause || !modulePath) continue;
46
+ parseImportClause(clause, modulePath, importPathMap);
47
+ }
48
+ const exportBlockMatch = /export\s*\{([\s\S]*?)\};?\s*$/.exec(source);
49
+ if (!exportBlockMatch?.[1]) {
50
+ cachedComponentSet = /* @__PURE__ */ new Set();
51
+ return cachedComponentSet;
52
+ }
53
+ const exportSpecifiers = exportBlockMatch[1].split(",").map((item) => item.trim()).filter(Boolean);
54
+ const componentSet = /* @__PURE__ */ new Set();
55
+ for (const exportSpecifier of exportSpecifiers) {
56
+ const [localName, exportedName] = exportSpecifier.split(/\s+as\s+/i).map((item) => item.trim());
57
+ const importName = exportedName ?? localName;
58
+ if (!localName || !importName || importName === "default") continue;
59
+ const modulePath = importPathMap.get(localName);
60
+ if (!modulePath || modulePath.startsWith("./_") || modulePath === "./sd-vue.js") continue;
61
+ componentSet.add(importName);
62
+ }
63
+ cachedComponentSet = componentSet;
64
+ return componentSet;
75
65
  };
66
+ /**
67
+ * Resolver for SD Design Vue
68
+ *
69
+ * @link https://www.npmjs.com/package/@sdata/web-vue
70
+ */
76
71
  function SdDesignResolver(options = {}) {
77
- const prefix = options.prefix ?? DEFAULT_PREFIX;
78
- return [
79
- {
80
- type: "component",
81
- resolve: (name) => {
82
- if (!name.startsWith(prefix)) {
83
- return;
84
- }
85
- const componentName = name.substring(prefix.length);
86
- if (!componentName || !getComponentExportSet().has(componentName)) {
87
- return;
88
- }
89
- return { name: componentName, as: name, from: PACKAGE_NAME };
90
- }
91
- }
92
- ];
72
+ const prefix = options.prefix ?? DEFAULT_PREFIX;
73
+ return [{
74
+ type: "component",
75
+ resolve: (name) => {
76
+ if (!name.startsWith(prefix)) return;
77
+ const componentName = name.substring(prefix.length);
78
+ if (!componentName || !getComponentExportSet().has(componentName)) return;
79
+ return {
80
+ name: componentName,
81
+ as: name,
82
+ from: PACKAGE_NAME
83
+ };
84
+ }
85
+ }];
93
86
  }
94
87
  function resolveAliasedPath(url, componentsDir) {
95
- const normalized = url.replaceAll("\\", "/");
96
- if (/^(?:sass:|https?:|file:)/.test(normalized)) {
97
- return null;
98
- }
99
- let basePath;
100
- if (normalized.startsWith("@style/")) {
101
- basePath = path.resolve(componentsDir, "style", normalized.slice("@style/".length));
102
- } else if (normalized.startsWith("@components/")) {
103
- basePath = path.resolve(componentsDir, normalized.slice("@components/".length));
104
- } else {
105
- return null;
106
- }
107
- const ext = path.extname(basePath);
108
- const dir = path.dirname(basePath);
109
- const base = path.basename(basePath);
110
- const candidates = ext ? [basePath] : [
111
- ...SASS_EXTENSIONS.map((e) => `${basePath}${e}`),
112
- ...SASS_EXTENSIONS.map((e) => path.join(dir, `_${base}${e}`)),
113
- ...SASS_EXTENSIONS.map((e) => path.join(basePath, `index${e}`)),
114
- ...SASS_EXTENSIONS.map((e) => path.join(basePath, `_index${e}`))
115
- ];
116
- return candidates.find((c) => existsSync(c)) ?? null;
88
+ const normalized = url.replaceAll("\\", "/");
89
+ if (/^(?:sass:|https?:|file:)/.test(normalized)) return null;
90
+ let basePath;
91
+ if (normalized.startsWith("@style/")) basePath = path.resolve(componentsDir, "style", normalized.slice(7));
92
+ else if (normalized.startsWith("@components/")) basePath = path.resolve(componentsDir, normalized.slice(12));
93
+ else return null;
94
+ const ext = path.extname(basePath);
95
+ const dir = path.dirname(basePath);
96
+ const base = path.basename(basePath);
97
+ return (ext ? [basePath] : [
98
+ ...SASS_EXTENSIONS.map((e) => `${basePath}${e}`),
99
+ ...SASS_EXTENSIONS.map((e) => path.join(dir, `_${base}${e}`)),
100
+ ...SASS_EXTENSIONS.map((e) => path.join(basePath, `index${e}`)),
101
+ ...SASS_EXTENSIONS.map((e) => path.join(basePath, `_index${e}`))
102
+ ]).find((c) => existsSync(c)) ?? null;
117
103
  }
118
104
  function getComponentsDir(packageName) {
119
- const packageRoot = path.dirname(createRequire(import.meta.url)(`${packageName}/package.json`));
120
- return path.resolve(packageRoot, "es");
105
+ const packageRoot = path.dirname(createRequire(import.meta.url)(`${packageName}/package.json`));
106
+ return path.resolve(packageRoot, "es");
121
107
  }
122
108
  function createImporter(componentsDir) {
123
- return {
124
- canonicalize(url) {
125
- const resolved = resolveAliasedPath(url, componentsDir);
126
- return resolved ? pathToFileURL(resolved) : null;
127
- },
128
- async load(canonicalUrl) {
129
- if (canonicalUrl.protocol !== "file:") {
130
- return null;
131
- }
132
- const filePath = fileURLToPath(canonicalUrl);
133
- const ext = path.extname(filePath);
134
- let syntax = "scss";
135
- if (ext === ".sass") {
136
- syntax = "indented";
137
- } else if (ext === ".css") {
138
- syntax = "css";
139
- }
140
- return {
141
- contents: await readFile(filePath, "utf8"),
142
- syntax,
143
- sourceMapUrl: canonicalUrl
144
- };
145
- }
146
- };
109
+ return {
110
+ canonicalize(url) {
111
+ const resolved = resolveAliasedPath(url, componentsDir);
112
+ return resolved ? pathToFileURL(resolved) : null;
113
+ },
114
+ async load(canonicalUrl) {
115
+ if (canonicalUrl.protocol !== "file:") return null;
116
+ const filePath = fileURLToPath(canonicalUrl);
117
+ const ext = path.extname(filePath);
118
+ let syntax = "scss";
119
+ if (ext === ".sass") syntax = "indented";
120
+ else if (ext === ".css") syntax = "css";
121
+ return {
122
+ contents: await readFile(filePath, "utf8"),
123
+ syntax,
124
+ sourceMapUrl: canonicalUrl
125
+ };
126
+ }
127
+ };
147
128
  }
129
+ /**
130
+ * Create a custom Sass importer that resolves `@style/` and `@components/`
131
+ * aliases used in `@sdata/web-vue` SCSS files.
132
+ *
133
+ * Use this when you need to configure `css.preprocessorOptions.scss.importers`
134
+ * manually instead of using the Vite plugin.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * import { createSdDesignScssImporter } from '@sdata/web-vue-auto-import-resolver';
139
+ *
140
+ * export default defineConfig({
141
+ * css: {
142
+ * preprocessorOptions: {
143
+ * scss: {
144
+ * importers: [createSdDesignScssImporter()],
145
+ * },
146
+ * },
147
+ * },
148
+ * });
149
+ * ```
150
+ */
148
151
  function createSdDesignScssImporter(options = {}) {
149
- const packageName = options.packageName ?? PACKAGE_NAME;
150
- return createImporter(getComponentsDir(packageName));
152
+ return createImporter(getComponentsDir(options.packageName ?? PACKAGE_NAME));
151
153
  }
154
+ /**
155
+ * Create a Vite plugin that automatically configures a custom Sass importer
156
+ * to resolve `@style/` and `@components/` aliases used in `@sdata/web-vue`
157
+ * SCSS files. Equivalent to calling `createSdDesignScssImporter()` and
158
+ * adding it to `css.preprocessorOptions.scss.importers` manually.
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * import { createSdDesignVitePlugin, SdDesignResolver } from '@sdata/web-vue-auto-import-resolver';
163
+ *
164
+ * export default defineConfig({
165
+ * plugins: [
166
+ * createSdDesignVitePlugin(),
167
+ * Components({ resolvers: [SdDesignResolver()] }),
168
+ * ],
169
+ * });
170
+ * ```
171
+ */
152
172
  function createSdDesignVitePlugin(options = {}) {
153
- const packageName = options.packageName ?? PACKAGE_NAME;
154
- return {
155
- name: "sd-design",
156
- config(config) {
157
- const importer = createImporter(getComponentsDir(packageName));
158
- const existingScss = config.css?.preprocessorOptions?.scss ?? {};
159
- const existingImporters = Array.isArray(existingScss.importers) ? existingScss.importers : [];
160
- return {
161
- css: {
162
- ...config.css,
163
- preprocessorOptions: {
164
- ...config.css?.preprocessorOptions,
165
- scss: {
166
- ...existingScss,
167
- importers: [importer, ...existingImporters]
168
- }
169
- }
170
- }
171
- };
172
- }
173
- };
173
+ const packageName = options.packageName ?? PACKAGE_NAME;
174
+ return {
175
+ name: "sd-design",
176
+ config(config) {
177
+ const importer = createImporter(getComponentsDir(packageName));
178
+ const existingScss = config.css?.preprocessorOptions?.scss ?? {};
179
+ const existingImporters = Array.isArray(existingScss.importers) ? existingScss.importers : [];
180
+ return { css: {
181
+ ...config.css,
182
+ preprocessorOptions: {
183
+ ...config.css?.preprocessorOptions,
184
+ scss: {
185
+ ...existingScss,
186
+ importers: [importer, ...existingImporters]
187
+ }
188
+ }
189
+ } };
190
+ }
191
+ };
174
192
  }
175
- export {
176
- SdDesignResolver,
177
- createSdDesignScssImporter,
178
- createSdDesignVitePlugin
179
- };
193
+ //#endregion
194
+ export { SdDesignResolver, createSdDesignScssImporter, createSdDesignVitePlugin };
package/package.json CHANGED
@@ -21,6 +21,7 @@
21
21
  },
22
22
  "peerDependencies": {
23
23
  "@sdata/web-vue": "*",
24
+ "unplugin-vue-components": "*",
24
25
  "vite": "*"
25
26
  },
26
27
  "exports": {
@@ -30,5 +31,5 @@
30
31
  "default": "./index.js"
31
32
  }
32
33
  },
33
- "version": "3.21.2"
34
+ "version": "3.22.0"
34
35
  }