vite-plugin-components
Advanced tools
Comparing version 0.6.6 to 0.6.7
@@ -69,2 +69,3 @@ import { Plugin } from 'vite'; | ||
path: string; | ||
absolute?: string; | ||
importName?: string; | ||
@@ -71,0 +72,0 @@ } |
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __assign = Object.assign; | ||
// src/context.ts | ||
var _path = require('path'); var _path2 = _interopRequireDefault(_path); | ||
var _path = require('path'); | ||
var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); | ||
var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_chokidar); | ||
@@ -15,3 +16,3 @@ // src/utils.ts | ||
var debug2 = _debug2.default.call(void 0, "vite-plugin-components:helper:library"); | ||
var debug = _debug2.default.call(void 0, "vite-plugin-components:helper:library"); | ||
function tryLoadVeturTags(name) { | ||
@@ -44,3 +45,3 @@ var _a; | ||
} | ||
debug2(entries); | ||
debug(entries); | ||
const prefixKebab = kebabCase(prefix); | ||
@@ -63,2 +64,14 @@ const kebabEntries = entries.map((name) => ({name, kebab: kebabCase(name)})); | ||
// src/constants.ts | ||
var defaultOptions = { | ||
dirs: "src/components", | ||
extensions: "vue", | ||
deep: true, | ||
directoryAsNamespace: false, | ||
globalNamespaces: [], | ||
libraries: [], | ||
customLoaderMatcher: () => false, | ||
customComponentResolvers: [] | ||
}; | ||
// src/utils.ts | ||
@@ -101,13 +114,21 @@ function pascalCase(str) { | ||
} | ||
function stringifyComponentImport({name, path: path5, importName}) { | ||
function matchGlobs(filepath, globs) { | ||
for (const glob of globs) { | ||
if (_minimatch2.default.call(void 0, filepath, glob)) | ||
return true; | ||
} | ||
return false; | ||
} | ||
function stringifyComponentImport({name, path, importName}) { | ||
if (importName) | ||
return `import { ${importName} as ${name} } from '${path5}'`; | ||
return `import { ${importName} as ${name} } from '${path}'`; | ||
else | ||
return `import ${name} from '${path5}'`; | ||
return `import ${name} from '${path}'`; | ||
} | ||
function resolveOptions(options, defaultOptions2) { | ||
const resolvedOptions = Object.assign({}, defaultOptions2, options); | ||
function resolveOptions(options, viteConfig) { | ||
const resolvedOptions = Object.assign({}, defaultOptions, options); | ||
resolvedOptions.libraries = toArray(resolvedOptions.libraries).map((i) => typeof i === "string" ? {name: i} : i); | ||
resolvedOptions.customComponentResolvers = toArray(resolvedOptions.customComponentResolvers); | ||
resolvedOptions.customComponentResolvers.push(...resolvedOptions.libraries.map((lib) => LibraryResolver(lib))); | ||
resolvedOptions.dirs = toArray(resolvedOptions.dirs).map((i) => _path.resolve.call(void 0, viteConfig.root, i)); | ||
return resolvedOptions; | ||
@@ -117,3 +138,3 @@ } | ||
const {dirs, directoryAsNamespace, globalNamespaces} = options; | ||
const parsedFilePath = _path2.default.parse(filePath); | ||
const parsedFilePath = _path.parse.call(void 0, filePath); | ||
let strippedPath = ""; | ||
@@ -145,3 +166,3 @@ for (const dir of toArray(dirs)) { | ||
function resolveAlias(filepath, alias = []) { | ||
let result = filepath; | ||
const result = filepath; | ||
if (Array.isArray(alias)) { | ||
@@ -157,14 +178,15 @@ for (const {find, replacement} of alias) | ||
var debug4 = _debug2.default.call(void 0, "vite-plugin-components:glob"); | ||
var debug2 = _debug2.default.call(void 0, "vite-plugin-components:glob"); | ||
function searchComponents(ctx) { | ||
var _a, _b; | ||
debug4(`started with: [${ctx.globs.join(", ")}]`); | ||
var _a; | ||
debug2(`started with: [${ctx.globs.join(", ")}]`); | ||
const root = ctx.root; | ||
const files = _fastglob2.default.sync(ctx.globs, { | ||
ignore: ["node_modules"], | ||
onlyFiles: true, | ||
cwd: ((_a = ctx.viteConfig) == null ? void 0 : _a.root) || process.cwd() | ||
cwd: root | ||
}); | ||
if (!files.length && !((_b = ctx.options.customComponentResolvers) == null ? void 0 : _b.length)) | ||
if (!files.length && !((_a = ctx.options.customComponentResolvers) == null ? void 0 : _a.length)) | ||
console.warn("[vite-plugin-components] no components found"); | ||
debug4(`${files.length} components found.`); | ||
debug2(`${files.length} components found.`); | ||
ctx.addComponents(files); | ||
@@ -174,12 +196,16 @@ } | ||
// src/context.ts | ||
var debug6 = { | ||
components: _debug2.default.call(void 0, "vite-plugin-components:context:components") | ||
var debug3 = { | ||
components: _debug2.default.call(void 0, "vite-plugin-components:context:components"), | ||
search: _debug2.default.call(void 0, "vite-plugin-components:context:search"), | ||
hmr: _debug2.default.call(void 0, "vite-plugin-components:context:hmr") | ||
}; | ||
var Context = class { | ||
constructor(options) { | ||
this.options = options; | ||
constructor(options, viteConfig) { | ||
this.viteConfig = viteConfig; | ||
this._componentPaths = new Set(); | ||
this._componentNameMap = {}; | ||
this._searched = 0; | ||
const {extensions, dirs, deep} = options; | ||
this._componentUsageMap = {}; | ||
this._searched = false; | ||
this.options = resolveOptions(options, viteConfig); | ||
const {extensions, dirs, deep} = this.options; | ||
const exts = toArray(extensions); | ||
@@ -190,8 +216,31 @@ if (!exts.length) | ||
this.globs = toArray(dirs).map((i) => deep ? `${i}/**/*.${extsGlob}` : `${i}/*.${extsGlob}`); | ||
if (viteConfig.command === "serve") { | ||
_chokidar2.default.watch(dirs, {ignoreInitial: true}).on("unlink", (path) => { | ||
if (matchGlobs(path, this.globs)) { | ||
this.removeComponents(path); | ||
this.onUpdate(path); | ||
} | ||
}).on("add", (path) => { | ||
if (matchGlobs(path, this.globs)) { | ||
this.addComponents(path); | ||
this.onUpdate(path); | ||
} | ||
}); | ||
} | ||
} | ||
get root() { | ||
var _a; | ||
return ((_a = this.viteConfig) == null ? void 0 : _a.root) || process.cwd(); | ||
return this.viteConfig.root; | ||
} | ||
setServer(server) { | ||
this._server = server; | ||
} | ||
updateUsageMap(path, paths) { | ||
if (!this._componentUsageMap[path]) | ||
this._componentUsageMap[path] = new Set(); | ||
paths.forEach((p) => { | ||
this._componentUsageMap[path].add(p); | ||
}); | ||
} | ||
addComponents(paths) { | ||
debug3.components("add", paths); | ||
const size = this._componentPaths.size; | ||
@@ -206,2 +255,3 @@ toArray(paths).forEach((p) => this._componentPaths.add(p)); | ||
removeComponents(paths) { | ||
debug3.components("remove", paths); | ||
const size = this._componentPaths.size; | ||
@@ -215,12 +265,38 @@ toArray(paths).forEach((p) => this._componentPaths.delete(p)); | ||
} | ||
onUpdate(path) { | ||
if (!this._server) | ||
return; | ||
const payload = { | ||
type: "update", | ||
updates: [] | ||
}; | ||
const timestamp = +new Date(); | ||
const name = pascalCase(getNameFromFilePath(path, this.options)); | ||
Object.entries(this._componentUsageMap).forEach(([key, values]) => { | ||
if (values.has(name)) { | ||
const r = `/${_path.relative.call(void 0, this.viteConfig.root, key)}`; | ||
payload.updates.push({ | ||
acceptedPath: r, | ||
path: r, | ||
timestamp, | ||
type: "js-update" | ||
}); | ||
} | ||
}); | ||
if (payload.updates.length) | ||
this._server.ws.send(payload); | ||
} | ||
updateComponentNameMap() { | ||
this._componentNameMap = {}; | ||
debug6.components(this._componentPaths); | ||
Array.from(this._componentPaths).forEach((path5) => { | ||
const name = pascalCase(getNameFromFilePath(path5, this.options)); | ||
Array.from(this._componentPaths).forEach((path) => { | ||
const name = pascalCase(getNameFromFilePath(path, this.options)); | ||
if (this._componentNameMap[name]) { | ||
console.warn(`[vite-plugin-components] component "${name}"(${path5}) has naming conflicts with other components, ignored.`); | ||
console.warn(`[vite-plugin-components] component "${name}"(${path}) has naming conflicts with other components, ignored.`); | ||
return; | ||
} | ||
this._componentNameMap[name] = {name, path: `/${path5}`}; | ||
this._componentNameMap[name] = { | ||
name, | ||
absolute: path, | ||
path: `/${this.relative(path)}` | ||
}; | ||
}); | ||
@@ -236,3 +312,6 @@ } | ||
if (typeof result === "string") { | ||
return {name, path: result}; | ||
return { | ||
name, | ||
path: result | ||
}; | ||
} else { | ||
@@ -252,22 +331,17 @@ return { | ||
} | ||
normalizePath(path5) { | ||
return this.relative(this.resolveAlias(path5)); | ||
} | ||
resolveAlias(path5) { | ||
normalizePath(path) { | ||
var _a; | ||
return resolveAlias(path5, ((_a = this.viteConfig) == null ? void 0 : _a.alias) || []); | ||
return resolveAlias(path, ((_a = this.viteConfig) == null ? void 0 : _a.resolve.alias) || []); | ||
} | ||
relative(path5) { | ||
if (path5.startsWith("/") && !path5.startsWith(this.root)) | ||
return path5.slice(1).replace(/\\/g, "/"); | ||
return _path.relative.call(void 0, this.root, path5).replace(/\\/g, "/"); | ||
relative(path) { | ||
if (path.startsWith("/") && !path.startsWith(this.root)) | ||
return path.slice(1).replace(/\\/g, "/"); | ||
return _path.relative.call(void 0, this.root, path).replace(/\\/g, "/"); | ||
} | ||
searchGlob(forceMs = -1) { | ||
if (this._searched && forceMs < 0) | ||
searchGlob() { | ||
if (this._searched) | ||
return; | ||
const now = +new Date(); | ||
if (now - this._searched > forceMs) { | ||
searchComponents(this); | ||
this._searched = now; | ||
} | ||
searchComponents(this); | ||
debug3.search(this._componentNameMap); | ||
this._searched = true; | ||
} | ||
@@ -278,22 +352,21 @@ }; | ||
var debug8 = _debug2.default.call(void 0, "vite-plugin-components:transform"); | ||
var debug4 = _debug2.default.call(void 0, "vite-plugin-components:transform"); | ||
function VueTransformer(ctx) { | ||
return (code, id, path5, query) => { | ||
var _a; | ||
if (!(path5.endsWith(".vue") || ctx.options.customLoaderMatcher(id))) | ||
return (code, id, path, query) => { | ||
if (!(path.endsWith(".vue") || ctx.options.customLoaderMatcher(id))) | ||
return code; | ||
if (((_a = ctx.viteConfig) == null ? void 0 : _a.command) === "serve") | ||
ctx.searchGlob(500); | ||
else | ||
ctx.searchGlob(); | ||
const sfcPath = ctx.normalizePath(path5); | ||
debug8(sfcPath); | ||
ctx.searchGlob(); | ||
const sfcPath = ctx.normalizePath(path); | ||
debug4(sfcPath); | ||
const head = []; | ||
let no = 0; | ||
const componentPaths = []; | ||
let transformed = code.replace(/_resolveComponent\("(.+?)"\)/g, (str, match) => { | ||
if (match) { | ||
debug8(`| ${match}`); | ||
const component = ctx.findComponent(pascalCase(match), [sfcPath]); | ||
if (match && !match.startsWith("_")) { | ||
debug4(`| ${match}`); | ||
const name = pascalCase(match); | ||
componentPaths.push(name); | ||
const component = ctx.findComponent(name, [sfcPath]); | ||
if (component) { | ||
const var_name = `__vite_component_${no}`; | ||
const var_name = `__vite_components_${no}`; | ||
head.push(stringifyComponentImport(__assign(__assign({}, component), {name: var_name}))); | ||
@@ -306,3 +379,4 @@ no += 1; | ||
}); | ||
debug8(`^ (${no})`); | ||
debug4(`^ (${no})`); | ||
ctx.updateUsageMap(sfcPath, componentPaths); | ||
transformed = `${head.join("\n")} | ||
@@ -315,17 +389,5 @@ ${transformed}`; | ||
// src/index.ts | ||
var defaultOptions = { | ||
dirs: "src/components", | ||
extensions: "vue", | ||
deep: true, | ||
directoryAsNamespace: false, | ||
globalNamespaces: [], | ||
libraries: [], | ||
customLoaderMatcher: () => false, | ||
customComponentResolvers: [] | ||
}; | ||
function VitePluginComponents(options = {}) { | ||
const ctx = new Context(resolveOptions(options, defaultOptions)); | ||
const transformer = [ | ||
VueTransformer(ctx) | ||
]; | ||
let ctx; | ||
let transformers; | ||
return { | ||
@@ -335,8 +397,14 @@ name: "vite-plugin-components", | ||
configResolved(config) { | ||
ctx.viteConfig = config; | ||
ctx = new Context(options, config); | ||
transformers = [ | ||
VueTransformer(ctx) | ||
]; | ||
}, | ||
configureServer(server) { | ||
ctx.setServer(server); | ||
}, | ||
transform(code, id) { | ||
const {path: path5, query} = parseId(id); | ||
for (const trans of transformer) | ||
code = trans(code, id, path5, query); | ||
const {path, query} = parseId(id); | ||
for (const trans of transformers) | ||
code = trans(code, id, path, query); | ||
return code; | ||
@@ -343,0 +411,0 @@ } |
{ | ||
"name": "vite-plugin-components", | ||
"description": "Components auto importing for Vite", | ||
"version": "0.6.6", | ||
"version": "0.6.7", | ||
"main": "dist/index.js", | ||
@@ -28,6 +28,10 @@ "module": "dist/index.mjs", | ||
"dependencies": { | ||
"chokidar": "^3.5.1", | ||
"debug": "^4.3.2", | ||
"fast-glob": "^3.2.4", | ||
"fast-glob": "npm:@knightly/fast-glob@master", | ||
"minimatch": "^3.0.4" | ||
}, | ||
"peerDependencies": { | ||
"vite": "^2.0.0-beta.69" | ||
}, | ||
"devDependencies": { | ||
@@ -37,11 +41,11 @@ "@antfu/eslint-config": "^0.4.3", | ||
"@types/minimatch": "^3.0.3", | ||
"@types/node": "^14.14.20", | ||
"@typescript-eslint/eslint-plugin": "^4.12.0", | ||
"eslint": "^7.17.0", | ||
"rollup": "^2.36.1", | ||
"@types/node": "^14.14.27", | ||
"@typescript-eslint/eslint-plugin": "^4.15.0", | ||
"eslint": "^7.19.0", | ||
"rollup": "^2.38.5", | ||
"standard-version": "^9.1.0", | ||
"tsup": "^3.11.0", | ||
"typescript": "^4.1.3", | ||
"vite": "^2.0.0-beta.19" | ||
"tsup": "^3.12.1", | ||
"typescript": "^4.1.5", | ||
"vite": "^2.0.0-beta.69" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32552
859
5
+ Addedchokidar@^3.5.1
+ Added@esbuild/linux-loong64@0.14.54(transitive)
+ Added@knightly/fast-glob@3.2.5-knightly-master.202101180131(transitive)
+ Addedanymatch@3.1.3(transitive)
+ Addedbinary-extensions@2.3.0(transitive)
+ Addedchokidar@3.6.0(transitive)
+ Addedesbuild@0.14.54(transitive)
+ Addedesbuild-android-64@0.14.54(transitive)
+ Addedesbuild-android-arm64@0.14.54(transitive)
+ Addedesbuild-darwin-64@0.14.54(transitive)
+ Addedesbuild-darwin-arm64@0.14.54(transitive)
+ Addedesbuild-freebsd-64@0.14.54(transitive)
+ Addedesbuild-freebsd-arm64@0.14.54(transitive)
+ Addedesbuild-linux-32@0.14.54(transitive)
+ Addedesbuild-linux-64@0.14.54(transitive)
+ Addedesbuild-linux-arm@0.14.54(transitive)
+ Addedesbuild-linux-arm64@0.14.54(transitive)
+ Addedesbuild-linux-mips64le@0.14.54(transitive)
+ Addedesbuild-linux-ppc64le@0.14.54(transitive)
+ Addedesbuild-linux-riscv64@0.14.54(transitive)
+ Addedesbuild-linux-s390x@0.14.54(transitive)
+ Addedesbuild-netbsd-64@0.14.54(transitive)
+ Addedesbuild-openbsd-64@0.14.54(transitive)
+ Addedesbuild-sunos-64@0.14.54(transitive)
+ Addedesbuild-windows-32@0.14.54(transitive)
+ Addedesbuild-windows-64@0.14.54(transitive)
+ Addedesbuild-windows-arm64@0.14.54(transitive)
+ Addedfsevents@2.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-binary-path@2.1.0(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addednanoid@3.3.7(transitive)
+ Addednormalize-path@3.0.0(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedpostcss@8.4.49(transitive)
+ Addedreaddirp@3.6.0(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedrollup@2.77.3(transitive)
+ Addedsource-map-js@1.2.1(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedvite@2.9.18(transitive)
- Removedfast-glob@3.3.2(transitive)