You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
643 lines
22 KiB
JavaScript
643 lines
22 KiB
JavaScript
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var _chunkFD6BQCQ7js = require('./chunk-FD6BQCQ7.js');
|
|
|
|
|
|
|
|
|
|
var _chunkBTQOTIPQjs = require('./chunk-BTQOTIPQ.js');
|
|
|
|
// src/core/unplugin.ts
|
|
var _unplugin = require('unplugin');
|
|
var _pluginutils = require('@rollup/pluginutils');
|
|
var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_chokidar);
|
|
|
|
// src/core/context.ts
|
|
var _path = require('path');
|
|
var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
|
|
var _utils = require('@antfu/utils');
|
|
|
|
// src/core/options.ts
|
|
|
|
|
|
var _localpkg = require('local-pkg');
|
|
|
|
// src/core/type-imports/detect.ts
|
|
|
|
|
|
|
|
// src/core/type-imports/index.ts
|
|
var TypeImportPresets = [
|
|
{
|
|
from: "vue-router",
|
|
names: [
|
|
"RouterView",
|
|
"RouterLink"
|
|
]
|
|
},
|
|
{
|
|
from: "vue-starport",
|
|
names: [
|
|
"Starport",
|
|
"StarportCarrier"
|
|
]
|
|
}
|
|
];
|
|
|
|
// src/core/type-imports/detect.ts
|
|
function detectTypeImports() {
|
|
return TypeImportPresets.map((i) => _localpkg.isPackageExists.call(void 0, i.from) ? i : void 0).filter(_utils.notNullish);
|
|
}
|
|
function resolveTypeImports(imports) {
|
|
return imports.flatMap((i) => i.names.map((n) => ({ from: i.from, name: n, as: n })));
|
|
}
|
|
|
|
// src/core/options.ts
|
|
var defaultOptions = {
|
|
dirs: "src/components",
|
|
extensions: "vue",
|
|
deep: true,
|
|
dts: _localpkg.isPackageExists.call(void 0, "typescript"),
|
|
directoryAsNamespace: false,
|
|
globalNamespaces: [],
|
|
resolvers: [],
|
|
importPathTransform: (v) => v,
|
|
allowOverrides: false
|
|
};
|
|
function normalizeResolvers(resolvers) {
|
|
return _utils.toArray.call(void 0, resolvers).flat().map((r) => typeof r === "function" ? { resolve: r, type: "component" } : r);
|
|
}
|
|
function resolveOptions(options, root) {
|
|
const resolved = Object.assign({}, defaultOptions, options);
|
|
resolved.resolvers = normalizeResolvers(resolved.resolvers);
|
|
resolved.extensions = _utils.toArray.call(void 0, resolved.extensions);
|
|
if (resolved.globs) {
|
|
resolved.globs = _utils.toArray.call(void 0, resolved.globs).map((glob) => _utils.slash.call(void 0, _path.resolve.call(void 0, root, glob)));
|
|
resolved.resolvedDirs = [];
|
|
} else {
|
|
const extsGlob = resolved.extensions.length === 1 ? resolved.extensions : `{${resolved.extensions.join(",")}}`;
|
|
resolved.dirs = _utils.toArray.call(void 0, resolved.dirs);
|
|
resolved.resolvedDirs = resolved.dirs.map((i) => _utils.slash.call(void 0, _path.resolve.call(void 0, root, i)));
|
|
resolved.globs = resolved.resolvedDirs.map((i) => resolved.deep ? _utils.slash.call(void 0, _path.join.call(void 0, i, `**/*.${extsGlob}`)) : _utils.slash.call(void 0, _path.join.call(void 0, i, `*.${extsGlob}`)));
|
|
if (!resolved.extensions.length)
|
|
throw new Error("[unplugin-vue-components] `extensions` option is required to search for components");
|
|
}
|
|
resolved.dts = !resolved.dts ? false : _path.resolve.call(void 0, root, typeof resolved.dts === "string" ? resolved.dts : "components.d.ts");
|
|
if (!resolved.types && resolved.dts)
|
|
resolved.types = detectTypeImports();
|
|
resolved.types = resolved.types || [];
|
|
resolved.root = root;
|
|
resolved.transformer = options.transformer || getVueVersion() || "vue3";
|
|
resolved.directives = typeof options.directives === "boolean" ? options.directives : !resolved.resolvers.some((i) => i.type === "directive") ? false : getVueVersion() === "vue3";
|
|
return resolved;
|
|
}
|
|
function getVueVersion() {
|
|
var _a;
|
|
try {
|
|
const vue = _chunkBTQOTIPQjs.__require.call(void 0, "vue");
|
|
const version = ((_a = vue == null ? void 0 : vue.default) == null ? void 0 : _a.version) || (vue == null ? void 0 : vue.version) || "3";
|
|
return version.startsWith("2.") ? "vue2" : "vue3";
|
|
} catch (e2) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// src/core/fs/glob.ts
|
|
var _fastglob = require('fast-glob'); var _fastglob2 = _interopRequireDefault(_fastglob);
|
|
|
|
var debug = _debug2.default.call(void 0, "unplugin-vue-components:glob");
|
|
function searchComponents(ctx) {
|
|
var _a;
|
|
debug(`started with: [${ctx.options.globs.join(", ")}]`);
|
|
const root = ctx.root;
|
|
const files = _fastglob2.default.sync(ctx.options.globs, {
|
|
ignore: ["node_modules"],
|
|
onlyFiles: true,
|
|
cwd: root,
|
|
absolute: true
|
|
});
|
|
if (!files.length && !((_a = ctx.options.resolvers) == null ? void 0 : _a.length))
|
|
console.warn("[unplugin-vue-components] no components found");
|
|
debug(`${files.length} components found.`);
|
|
ctx.addComponents(files);
|
|
}
|
|
|
|
// src/core/declaration.ts
|
|
|
|
var _fs = require('fs');
|
|
|
|
function parseDeclaration(code) {
|
|
if (!code)
|
|
return {};
|
|
return Object.fromEntries(Array.from(code.matchAll(/(?<!\/\/)\s+\s+['"]?(.+?)['"]?:\s(.+?)\n/g)).map((i) => [i[1], i[2]]));
|
|
}
|
|
async function generateDeclaration(ctx, root, filepath, removeUnused = false) {
|
|
const items = [
|
|
...Object.values(_chunkBTQOTIPQjs.__spreadValues.call(void 0, _chunkBTQOTIPQjs.__spreadValues.call(void 0, {}, ctx.componentNameMap), ctx.componentCustomMap)),
|
|
...resolveTypeImports(ctx.options.types)
|
|
];
|
|
const imports = Object.fromEntries(items.map(({ from: path, as: name, name: importName }) => {
|
|
if (!name)
|
|
return void 0;
|
|
path = _chunkFD6BQCQ7js.getTransformedPath.call(void 0, path, ctx);
|
|
const related = _path.isAbsolute.call(void 0, path) ? `./${_path.relative.call(void 0, _path.dirname.call(void 0, filepath), path)}` : path;
|
|
let entry = `typeof import('${_utils.slash.call(void 0, related)}')`;
|
|
if (importName)
|
|
entry += `['${importName}']`;
|
|
else
|
|
entry += "['default']";
|
|
return [name, entry];
|
|
}).filter(_utils.notNullish));
|
|
if (!Object.keys(imports).length)
|
|
return;
|
|
const originalContent = _fs.existsSync.call(void 0, filepath) ? await _fs.promises.readFile(filepath, "utf-8") : "";
|
|
const originalImports = parseDeclaration(originalContent);
|
|
const lines = Object.entries(_chunkBTQOTIPQjs.__spreadValues.call(void 0, _chunkBTQOTIPQjs.__spreadValues.call(void 0, {}, originalImports), imports)).sort((a, b) => a[0].localeCompare(b[0])).filter(([name]) => removeUnused ? items.find((i) => i.as === name) : true).map(([name, v]) => {
|
|
if (!/^\w+$/.test(name))
|
|
name = `'${name}'`;
|
|
return `${name}: ${v}`;
|
|
});
|
|
const code = `// generated by unplugin-vue-components
|
|
// We suggest you to commit this file into source control
|
|
// Read more: https://github.com/vuejs/core/pull/3399
|
|
import '@vue/runtime-core'
|
|
|
|
declare module '@vue/runtime-core' {
|
|
export interface GlobalComponents {
|
|
${lines.join("\n ")}
|
|
}
|
|
}
|
|
|
|
export {}
|
|
`;
|
|
if (code !== originalContent)
|
|
await _fs.promises.writeFile(filepath, code, "utf-8");
|
|
}
|
|
|
|
// src/core/transformer.ts
|
|
|
|
var _magicstring = require('magic-string'); var _magicstring2 = _interopRequireDefault(_magicstring);
|
|
|
|
// src/core/transforms/component.ts
|
|
|
|
var debug2 = _debug2.default.call(void 0, "unplugin-vue-components:transform:component");
|
|
var resolveVue2 = (code, s) => {
|
|
const results = [];
|
|
for (const match of code.matchAll(/_c\([\s\n\t]*['"](.+?)["']([,)])/g)) {
|
|
const [full, matchedName, append] = match;
|
|
if (match.index != null && matchedName && !matchedName.startsWith("_")) {
|
|
const start = match.index;
|
|
const end = start + full.length;
|
|
results.push({
|
|
rawName: matchedName,
|
|
replace: (resolved) => s.overwrite(start, end, `_c(${resolved}${append}`)
|
|
});
|
|
}
|
|
}
|
|
return results;
|
|
};
|
|
var resolveVue3 = (code, s) => {
|
|
const results = [];
|
|
for (const match of code.matchAll(/_resolveComponent\("(.+?)"\)/g)) {
|
|
const matchedName = match[1];
|
|
if (match.index != null && matchedName && !matchedName.startsWith("_")) {
|
|
const start = match.index;
|
|
const end = start + match[0].length;
|
|
results.push({
|
|
rawName: matchedName,
|
|
replace: (resolved) => s.overwrite(start, end, resolved)
|
|
});
|
|
}
|
|
}
|
|
return results;
|
|
};
|
|
async function transformComponent(code, transformer2, s, ctx, sfcPath) {
|
|
let no = 0;
|
|
const results = transformer2 === "vue2" ? resolveVue2(code, s) : resolveVue3(code, s);
|
|
for (const { rawName, replace } of results) {
|
|
debug2(`| ${rawName}`);
|
|
const name = _chunkFD6BQCQ7js.pascalCase.call(void 0, rawName);
|
|
ctx.updateUsageMap(sfcPath, [name]);
|
|
const component = await ctx.findComponent(name, "component", [sfcPath]);
|
|
if (component) {
|
|
const varName = `__unplugin_components_${no}`;
|
|
s.prepend(`${_chunkFD6BQCQ7js.stringifyComponentImport.call(void 0, _chunkBTQOTIPQjs.__spreadProps.call(void 0, _chunkBTQOTIPQjs.__spreadValues.call(void 0, {}, component), { as: varName }), ctx)};
|
|
`);
|
|
no += 1;
|
|
replace(varName);
|
|
}
|
|
}
|
|
debug2(`^ (${no})`);
|
|
}
|
|
|
|
// src/core/transforms/directive/index.ts
|
|
|
|
|
|
// src/core/transforms/directive/vue2.ts
|
|
|
|
var getRenderFnStart = (ast) => {
|
|
var _a, _b;
|
|
const renderFn = ast.program.body.find((node) => node.type === "VariableDeclaration" && node.declarations[0].id.type === "Identifier" && node.declarations[0].id.name === "render");
|
|
const start = (_b = (_a = renderFn == null ? void 0 : renderFn.declarations[0].init) == null ? void 0 : _a.body) == null ? void 0 : _b.start;
|
|
if (start === null || start === void 0)
|
|
throw new Error("[unplugin-vue-components:directive] Cannot find render function position.");
|
|
return start + 1;
|
|
};
|
|
async function resolveVue22(code, s) {
|
|
var _a, _b, _c;
|
|
if (!_localpkg.isPackageExists.call(void 0, "@babel/parser"))
|
|
throw new Error('[unplugin-vue-components:directive] To use Vue 2 directive you will need to install Babel first: "npm install -D @babel/parser"');
|
|
const { parse } = await _localpkg.importModule.call(void 0, "@babel/parser");
|
|
const ast = parse(code, {
|
|
sourceType: "module"
|
|
});
|
|
const nodes = [];
|
|
const { walk } = await Promise.resolve().then(() => require("./src-4OAWGTJE.js"));
|
|
walk(ast.program, {
|
|
enter(node) {
|
|
if (node.type === "CallExpression")
|
|
nodes.push(node);
|
|
}
|
|
});
|
|
const results = [];
|
|
for (const node of nodes) {
|
|
const { callee, arguments: args } = node;
|
|
if (callee.type !== "Identifier" || callee.name !== "_c" || ((_a = args[1]) == null ? void 0 : _a.type) !== "ObjectExpression")
|
|
continue;
|
|
const directives = (_b = args[1].properties.find((property) => property.type === "ObjectProperty" && property.key.type === "Identifier" && property.key.name === "directives")) == null ? void 0 : _b.value;
|
|
if (!directives || directives.type !== "ArrayExpression")
|
|
continue;
|
|
const renderStart = getRenderFnStart(ast);
|
|
for (const directive of directives.elements) {
|
|
if ((directive == null ? void 0 : directive.type) !== "ObjectExpression")
|
|
continue;
|
|
const nameNode = (_c = directive.properties.find((p) => p.type === "ObjectProperty" && p.key.type === "Identifier" && p.key.name === "name")) == null ? void 0 : _c.value;
|
|
if ((nameNode == null ? void 0 : nameNode.type) !== "StringLiteral")
|
|
continue;
|
|
const name = nameNode.value;
|
|
if (!name || name.startsWith("_"))
|
|
continue;
|
|
results.push({
|
|
rawName: name,
|
|
replace: (resolved) => {
|
|
s.prependLeft(renderStart, `
|
|
this.$options.directives["${name}"] = ${resolved};`);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
// src/core/transforms/directive/vue3.ts
|
|
function resolveVue32(code, s) {
|
|
const results = [];
|
|
for (const match of code.matchAll(/_resolveDirective\("(.+?)"\)/g)) {
|
|
const matchedName = match[1];
|
|
if (match.index != null && matchedName && !matchedName.startsWith("_")) {
|
|
const start = match.index;
|
|
const end = start + match[0].length;
|
|
results.push({
|
|
rawName: matchedName,
|
|
replace: (resolved) => s.overwrite(start, end, resolved)
|
|
});
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
// src/core/transforms/directive/index.ts
|
|
var debug3 = _debug2.default.call(void 0, "unplugin-vue-components:transform:directive");
|
|
async function transformDirective(code, transformer2, s, ctx, sfcPath) {
|
|
let no = 0;
|
|
const results = await (transformer2 === "vue2" ? resolveVue22(code, s) : resolveVue32(code, s));
|
|
for (const { rawName, replace } of results) {
|
|
debug3(`| ${rawName}`);
|
|
const name = _chunkFD6BQCQ7js.pascalCase.call(void 0, rawName);
|
|
ctx.updateUsageMap(sfcPath, [name]);
|
|
const directive = await ctx.findComponent(name, "directive", [sfcPath]);
|
|
if (!directive)
|
|
continue;
|
|
const varName = `__unplugin_directives_${no}`;
|
|
s.prepend(`${_chunkFD6BQCQ7js.stringifyComponentImport.call(void 0, _chunkBTQOTIPQjs.__spreadProps.call(void 0, _chunkBTQOTIPQjs.__spreadValues.call(void 0, {}, directive), { as: varName }), ctx)};
|
|
`);
|
|
no += 1;
|
|
replace(varName);
|
|
}
|
|
debug3(`^ (${no})`);
|
|
}
|
|
|
|
// src/core/transformer.ts
|
|
var debug4 = _debug2.default.call(void 0, "unplugin-vue-components:transformer");
|
|
function transformer(ctx, transformer2) {
|
|
return async (code, id, path) => {
|
|
ctx.searchGlob();
|
|
const sfcPath = ctx.normalizePath(path);
|
|
debug4(sfcPath);
|
|
const s = new (0, _magicstring2.default)(code);
|
|
await transformComponent(code, transformer2, s, ctx, sfcPath);
|
|
if (ctx.options.directives)
|
|
await transformDirective(code, transformer2, s, ctx, sfcPath);
|
|
s.prepend(_chunkFD6BQCQ7js.DISABLE_COMMENT);
|
|
const result = { code: s.toString() };
|
|
if (ctx.sourcemap)
|
|
result.map = s.generateMap({ source: id, includeContent: true });
|
|
return result;
|
|
};
|
|
}
|
|
|
|
// src/core/context.ts
|
|
var debug5 = {
|
|
components: _debug2.default.call(void 0, "unplugin-vue-components:context:components"),
|
|
search: _debug2.default.call(void 0, "unplugin-vue-components:context:search"),
|
|
hmr: _debug2.default.call(void 0, "unplugin-vue-components:context:hmr"),
|
|
decleration: _debug2.default.call(void 0, "unplugin-vue-components:decleration"),
|
|
env: _debug2.default.call(void 0, "unplugin-vue-components:env")
|
|
};
|
|
var Context = class {
|
|
constructor(rawOptions) {
|
|
this.rawOptions = rawOptions;
|
|
this.transformer = void 0;
|
|
this._componentPaths = /* @__PURE__ */ new Set();
|
|
this._componentNameMap = {};
|
|
this._componentUsageMap = {};
|
|
this._componentCustomMap = {};
|
|
this.root = process.cwd();
|
|
this.sourcemap = true;
|
|
this.alias = {};
|
|
this._searched = false;
|
|
this.options = resolveOptions(rawOptions, this.root);
|
|
this.generateDeclaration = _utils.throttle.call(void 0, 500, false, this.generateDeclaration.bind(this));
|
|
this.setTransformer(this.options.transformer);
|
|
}
|
|
setRoot(root) {
|
|
if (this.root === root)
|
|
return;
|
|
debug5.env("root", root);
|
|
this.root = root;
|
|
this.options = resolveOptions(this.rawOptions, this.root);
|
|
}
|
|
setTransformer(name) {
|
|
debug5.env("transformer", name);
|
|
this.transformer = transformer(this, name || "vue3");
|
|
}
|
|
transform(code, id) {
|
|
const { path, query } = _chunkFD6BQCQ7js.parseId.call(void 0, id);
|
|
return this.transformer(code, id, path, query);
|
|
}
|
|
setupViteServer(server) {
|
|
if (this._server === server)
|
|
return;
|
|
this._server = server;
|
|
this.setupWatcher(server.watcher);
|
|
}
|
|
setupWatcher(watcher) {
|
|
const { globs } = this.options;
|
|
watcher.on("unlink", (path) => {
|
|
if (!_chunkFD6BQCQ7js.matchGlobs.call(void 0, path, globs))
|
|
return;
|
|
path = _utils.slash.call(void 0, path);
|
|
this.removeComponents(path);
|
|
this.onUpdate(path);
|
|
});
|
|
watcher.on("add", (path) => {
|
|
if (!_chunkFD6BQCQ7js.matchGlobs.call(void 0, path, globs))
|
|
return;
|
|
path = _utils.slash.call(void 0, path);
|
|
this.addComponents(path);
|
|
this.onUpdate(path);
|
|
});
|
|
}
|
|
setupWatcherWebpack(watcher, emitUpdate) {
|
|
const { globs } = this.options;
|
|
watcher.on("unlink", (path) => {
|
|
if (!_chunkFD6BQCQ7js.matchGlobs.call(void 0, path, globs))
|
|
return;
|
|
path = _utils.slash.call(void 0, path);
|
|
this.removeComponents(path);
|
|
emitUpdate(path, "unlink");
|
|
});
|
|
watcher.on("add", (path) => {
|
|
if (!_chunkFD6BQCQ7js.matchGlobs.call(void 0, path, globs))
|
|
return;
|
|
path = _utils.slash.call(void 0, path);
|
|
this.addComponents(path);
|
|
emitUpdate(path, "add");
|
|
});
|
|
}
|
|
updateUsageMap(path, paths) {
|
|
if (!this._componentUsageMap[path])
|
|
this._componentUsageMap[path] = /* @__PURE__ */ new Set();
|
|
paths.forEach((p) => {
|
|
this._componentUsageMap[path].add(p);
|
|
});
|
|
}
|
|
addComponents(paths) {
|
|
debug5.components("add", paths);
|
|
const size = this._componentPaths.size;
|
|
_utils.toArray.call(void 0, paths).forEach((p) => this._componentPaths.add(p));
|
|
if (this._componentPaths.size !== size) {
|
|
this.updateComponentNameMap();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
addCustomComponents(info) {
|
|
if (info.as)
|
|
this._componentCustomMap[info.as] = info;
|
|
}
|
|
removeComponents(paths) {
|
|
debug5.components("remove", paths);
|
|
const size = this._componentPaths.size;
|
|
_utils.toArray.call(void 0, paths).forEach((p) => this._componentPaths.delete(p));
|
|
if (this._componentPaths.size !== size) {
|
|
this.updateComponentNameMap();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
onUpdate(path) {
|
|
this.generateDeclaration();
|
|
if (!this._server)
|
|
return;
|
|
const payload = {
|
|
type: "update",
|
|
updates: []
|
|
};
|
|
const timestamp = +new Date();
|
|
const name = _chunkFD6BQCQ7js.pascalCase.call(void 0, _chunkFD6BQCQ7js.getNameFromFilePath.call(void 0, path, this.options));
|
|
Object.entries(this._componentUsageMap).forEach(([key, values]) => {
|
|
if (values.has(name)) {
|
|
const r = `/${_utils.slash.call(void 0, _path.relative.call(void 0, this.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 = {};
|
|
Array.from(this._componentPaths).forEach((path) => {
|
|
const name = _chunkFD6BQCQ7js.pascalCase.call(void 0, _chunkFD6BQCQ7js.getNameFromFilePath.call(void 0, path, this.options));
|
|
if (this._componentNameMap[name] && !this.options.allowOverrides) {
|
|
console.warn(`[unplugin-vue-components] component "${name}"(${path}) has naming conflicts with other components, ignored.`);
|
|
return;
|
|
}
|
|
this._componentNameMap[name] = {
|
|
as: name,
|
|
from: path
|
|
};
|
|
});
|
|
}
|
|
async findComponent(name, type, excludePaths = []) {
|
|
let info = this._componentNameMap[name];
|
|
if (info && !excludePaths.includes(info.from) && !excludePaths.includes(info.from.slice(1)))
|
|
return info;
|
|
for (const resolver of this.options.resolvers) {
|
|
if (resolver.type !== type)
|
|
continue;
|
|
const result = await resolver.resolve(name);
|
|
if (result) {
|
|
if (typeof result === "string") {
|
|
info = {
|
|
as: name,
|
|
from: result
|
|
};
|
|
this.addCustomComponents(info);
|
|
return info;
|
|
} else {
|
|
info = _chunkBTQOTIPQjs.__spreadValues.call(void 0, {
|
|
as: name
|
|
}, _chunkFD6BQCQ7js.normalizeComponetInfo.call(void 0, result));
|
|
this.addCustomComponents(info);
|
|
return info;
|
|
}
|
|
}
|
|
}
|
|
return void 0;
|
|
}
|
|
normalizePath(path) {
|
|
var _a, _b, _c;
|
|
return _chunkFD6BQCQ7js.resolveAlias.call(void 0, path, ((_b = (_a = this.viteConfig) == null ? void 0 : _a.resolve) == null ? void 0 : _b.alias) || ((_c = this.viteConfig) == null ? void 0 : _c.alias) || []);
|
|
}
|
|
relative(path) {
|
|
if (path.startsWith("/") && !path.startsWith(this.root))
|
|
return _utils.slash.call(void 0, path.slice(1));
|
|
return _utils.slash.call(void 0, _path.relative.call(void 0, this.root, path));
|
|
}
|
|
searchGlob() {
|
|
if (this._searched)
|
|
return;
|
|
searchComponents(this);
|
|
debug5.search(this._componentNameMap);
|
|
this._searched = true;
|
|
}
|
|
generateDeclaration() {
|
|
if (!this.options.dts)
|
|
return;
|
|
debug5.decleration("generating");
|
|
generateDeclaration(this, this.options.root, this.options.dts, !this._server);
|
|
}
|
|
get componentNameMap() {
|
|
return this._componentNameMap;
|
|
}
|
|
get componentCustomMap() {
|
|
return this._componentCustomMap;
|
|
}
|
|
};
|
|
|
|
// src/core/unplugin.ts
|
|
var PLUGIN_NAME = "unplugin:webpack";
|
|
var unplugin_default = _unplugin.createUnplugin.call(void 0, (options = {}) => {
|
|
const filter = _pluginutils.createFilter.call(void 0, options.include || [/\.vue$/, /\.vue\?vue/], options.exclude || [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/]);
|
|
const ctx = new Context(options);
|
|
const api = {
|
|
async findComponent(name, filename) {
|
|
return await ctx.findComponent(name, "component", filename ? [filename] : []);
|
|
},
|
|
stringifyImport(info) {
|
|
return _chunkFD6BQCQ7js.stringifyComponentImport.call(void 0, info, ctx);
|
|
}
|
|
};
|
|
return {
|
|
name: "unplugin-vue-components",
|
|
enforce: "post",
|
|
api,
|
|
transformInclude(id) {
|
|
return filter(id);
|
|
},
|
|
async transform(code, id) {
|
|
if (!_chunkFD6BQCQ7js.shouldTransform.call(void 0, code))
|
|
return null;
|
|
try {
|
|
const result = await ctx.transform(code, id);
|
|
ctx.generateDeclaration();
|
|
return result;
|
|
} catch (e) {
|
|
this.error(e);
|
|
}
|
|
},
|
|
vite: {
|
|
configResolved(config) {
|
|
ctx.setRoot(config.root);
|
|
ctx.sourcemap = true;
|
|
if (config.plugins.find((i) => i.name === "vite-plugin-vue2"))
|
|
ctx.setTransformer("vue2");
|
|
if (options.dts) {
|
|
ctx.searchGlob();
|
|
ctx.generateDeclaration();
|
|
}
|
|
if (config.build.watch && config.command === "build")
|
|
ctx.setupWatcher(_chokidar2.default.watch(ctx.options.globs));
|
|
},
|
|
configureServer(server) {
|
|
ctx.setupViteServer(server);
|
|
}
|
|
},
|
|
webpack(compiler) {
|
|
let watcher;
|
|
let fileDepQueue = [];
|
|
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => {
|
|
if (!watcher && compiler.watching) {
|
|
watcher = compiler.watching;
|
|
ctx.setupWatcherWebpack(_chokidar2.default.watch(ctx.options.globs), (path, type) => {
|
|
fileDepQueue.push({ path, type });
|
|
process.nextTick(() => {
|
|
watcher.invalidate();
|
|
});
|
|
});
|
|
}
|
|
});
|
|
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|
if (fileDepQueue.length) {
|
|
fileDepQueue.forEach(({ path, type }) => {
|
|
if (type === "unlink")
|
|
compilation.fileDependencies.delete(path);
|
|
else
|
|
compilation.fileDependencies.add(path);
|
|
});
|
|
fileDepQueue = [];
|
|
}
|
|
});
|
|
}
|
|
};
|
|
});
|
|
|
|
|
|
|
|
exports.unplugin_default = unplugin_default;
|