@sdata/web-vue-auto-import-resolver 3.21.1 → 3.21.3
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/index.d.ts +16 -18
- package/index.js +179 -164
- package/package.json +2 -1
package/index.d.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { ComponentResolver } from "unplugin-vue-components/types";
|
|
2
|
+
import { Plugin } from "vite";
|
|
3
|
+
//#region index.d.ts
|
|
5
4
|
interface SdDesignResolverOptions {
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
/** @default 'Sd' */
|
|
6
|
+
prefix?: string;
|
|
8
7
|
}
|
|
9
8
|
interface SdDesignScssImporterOptions {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
interface SdDesignVitePluginOptions extends SdDesignScssImporterOptions {
|
|
9
|
+
/** @default '@sdata/web-vue' */
|
|
10
|
+
packageName?: string;
|
|
14
11
|
}
|
|
12
|
+
interface SdDesignVitePluginOptions extends SdDesignScssImporterOptions {}
|
|
15
13
|
/**
|
|
16
14
|
* Resolver for SD Design Vue
|
|
17
15
|
*
|
|
@@ -41,12 +39,12 @@ declare function SdDesignResolver(options?: SdDesignResolverOptions): ComponentR
|
|
|
41
39
|
* ```
|
|
42
40
|
*/
|
|
43
41
|
declare function createSdDesignScssImporter(options?: SdDesignScssImporterOptions): {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
42
|
+
canonicalize(url: string): import("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
|
|
@@ -67,5 +65,5 @@ declare function createSdDesignScssImporter(options?: SdDesignScssImporterOption
|
|
|
67
65
|
* ```
|
|
68
66
|
*/
|
|
69
67
|
declare function createSdDesignVitePlugin(options?: SdDesignVitePluginOptions): Plugin;
|
|
70
|
-
|
|
71
|
-
export { SdDesignResolver,
|
|
68
|
+
//#endregion
|
|
69
|
+
export { SdDesignResolver, SdDesignResolverOptions, SdDesignScssImporterOptions, SdDesignVitePluginOptions, createSdDesignScssImporter, createSdDesignVitePlugin };
|
package/index.js
CHANGED
|
@@ -1,179 +1,194 @@
|
|
|
1
|
-
|
|
2
|
-
import { existsSync, readFileSync } from "fs";
|
|
3
|
-
import { readFile } from "fs/promises";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
const getPackageRoot = () => {
|
|
33
|
+
const require = createRequire(import.meta.url);
|
|
34
|
+
return path.dirname(require.resolve(`${PACKAGE_NAME}/package.json`));
|
|
37
35
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
120
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
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
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
createSdDesignScssImporter,
|
|
178
|
-
createSdDesignVitePlugin
|
|
179
|
-
};
|
|
193
|
+
//#endregion
|
|
194
|
+
export { SdDesignResolver, createSdDesignScssImporter, createSdDesignVitePlugin };
|
package/package.json
CHANGED