vite-plugin-components
Advanced tools
Comparing version 0.6.14 to 0.7.1
@@ -61,2 +61,8 @@ import { Plugin } from 'vite'; | ||
customComponentResolvers?: ComponentResolver | ComponentResolver[]; | ||
/** | ||
* Transformer to apply | ||
* | ||
* @default 'vue3' | ||
*/ | ||
transformer?: 'vue3' | 'vue2'; | ||
} | ||
@@ -63,0 +69,0 @@ declare type ResolvedOptions = Omit<Required<Options>, 'customComponentResolvers' | 'libraries' | 'extensions' | 'dirs'> & { |
@@ -66,2 +66,3 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __assign = Object.assign; | ||
extensions: "vue", | ||
transformer: "vue3", | ||
deep: true, | ||
@@ -128,3 +129,3 @@ directoryAsNamespace: false, | ||
} | ||
function resolveOptions(options, viteConfig) { | ||
function resolveOptions(options) { | ||
const resolved = Object.assign({}, defaultOptions, options); | ||
@@ -212,3 +213,3 @@ resolved.libraries = toArray(resolved.libraries).map((i) => typeof i === "string" ? {name: i} : i); | ||
this._searched = false; | ||
this.options = resolveOptions(options, viteConfig); | ||
this.options = resolveOptions(options); | ||
const {globs, dirs} = this.options; | ||
@@ -344,6 +345,6 @@ if (viteConfig.command === "serve") { | ||
// src/transforms/vue.ts | ||
// src/transforms/vue3.ts | ||
var debug4 = _debug2.default.call(void 0, "vite-plugin-components:transform"); | ||
function VueTransformer(ctx) { | ||
var debug4 = _debug2.default.call(void 0, "vite-plugin-components:transform:vue3"); | ||
function Vue3Transformer(ctx) { | ||
return (code, id, path, query) => { | ||
@@ -381,2 +382,39 @@ if (!(path.endsWith(".vue") || ctx.options.customLoaderMatcher(id))) | ||
// src/transforms/vue2.ts | ||
var debug5 = _debug2.default.call(void 0, "vite-plugin-components:transform:vue2"); | ||
function Vue2Transformer(ctx) { | ||
return (code, id, path, query) => { | ||
if (!(path.endsWith(".vue") || ctx.options.customLoaderMatcher(id))) | ||
return code; | ||
ctx.searchGlob(); | ||
const sfcPath = ctx.normalizePath(path); | ||
debug5(sfcPath); | ||
const head = []; | ||
let no = 0; | ||
const componentPaths = []; | ||
let transformed = code.replace(/_c\(['"](.+?)["']([,)])/g, (str, match, append) => { | ||
if (match && !match.startsWith("_")) { | ||
debug5(`| ${match}`); | ||
const name = pascalCase(match); | ||
componentPaths.push(name); | ||
const component = ctx.findComponent(name, [sfcPath]); | ||
if (component) { | ||
const var_name = `__vite_components_${no}`; | ||
head.push(stringifyComponentImport(__assign(__assign({}, component), {name: var_name}))); | ||
no += 1; | ||
return `_c(${var_name}${append}`; | ||
} | ||
} | ||
return str; | ||
}); | ||
debug5(transformed); | ||
debug5(`^ (${no})`); | ||
ctx.updateUsageMap(sfcPath, componentPaths); | ||
transformed = `${head.join("\n")} | ||
${transformed}`; | ||
return transformed; | ||
}; | ||
} | ||
// src/index.ts | ||
@@ -392,3 +430,3 @@ function VitePluginComponents(options = {}) { | ||
transformers = [ | ||
VueTransformer(ctx) | ||
ctx.options.transformer === "vue2" ? Vue2Transformer(ctx) : Vue3Transformer(ctx) | ||
]; | ||
@@ -395,0 +433,0 @@ }, |
{ | ||
"name": "vite-plugin-components", | ||
"description": "Components auto importing for Vite", | ||
"version": "0.6.14", | ||
"version": "0.7.1", | ||
"main": "dist/index.js", | ||
@@ -42,10 +42,10 @@ "module": "dist/index.mjs", | ||
"@types/node": "^14.14.31", | ||
"@typescript-eslint/eslint-plugin": "^4.15.1", | ||
"@typescript-eslint/eslint-plugin": "^4.15.2", | ||
"eslint": "^7.20.0", | ||
"rollup": "^2.39.0", | ||
"rollup": "^2.39.1", | ||
"standard-version": "^9.1.1", | ||
"tsup": "^4.0.0", | ||
"tsup": "^4.2.0", | ||
"typescript": "^4.1.5", | ||
"vite": "^2.0.1" | ||
"vite": "^2.0.2" | ||
} | ||
} |
@@ -87,2 +87,21 @@ <h2 align='center'><samp>vite-plugin-components</samp></h2> | ||
## Vue 2 Support | ||
From `v0.7`, we support Vue 2 with [`vite-plugin-vue2`](https://github.com/underfin/vite-plugin-vue2) | ||
```ts | ||
// vite.config.js | ||
import { createVuePlugin } from 'vite-plugin-vue2' | ||
import ViteComponents from 'vite-plugin-components' | ||
export default { | ||
plugins: [ | ||
createVuePlugin(), | ||
ViteComponents({ | ||
transformer: 'vue2', // <-- | ||
}), | ||
], | ||
} | ||
``` | ||
## Configuration | ||
@@ -89,0 +108,0 @@ |
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
36034
949
139