unplugin svelte components
1.0.0
按需组件自动导入Svelte。
Sveltekit演示Svelte Vite演示
pnpm add -D unplugin-svelte-components // vite.config.ts
import Components from 'unplugin-svelte-components/vite'
export default defineConfig ( {
plugins : [
Components ( { /* options */ } ) ,
] ,
} ) // rollup.config.js
import Components from 'unplugin-svelte-components/rollup'
export default {
plugins : [
Components ( { /* options */ } ) ,
] ,
} // webpack.config.js
module . exports = {
/* ... */
plugins : [
require ( 'unplugin-svelte-components/webpack' ) ( { /* options */ } ) ,
] ,
} // esbuild.config.js
import { build } from 'esbuild'
build ( {
/* ... */
plugins : [
require ( 'unplugin-svelte-components/esbuild' ) ( {
/* options */
} ) ,
] ,
} ) // vite.config.ts
import Components from 'unplugin-svelte-components/vite'
export default defineConfig ( {
plugins : [
Components ( { /* options */ } ) , // before sveltekit plugin
sveltekit ( ) ,
] ,
} ) 按照您平常的方式使用组件,它将按需导入组件,并且不再需要import !
它将自动转动此
< HelloWorld msg =" Hello Svelte " />进入这个
< HelloWorld msg =" Hello Svelte " />
< script >
import HelloWorld from './src/components/HelloWorld.svelte'
</ script > 为了获得对自动成分组件的打字稿支持,您可以将配置更改为以下内容以获得支持。
Components ( {
dts : true , // enabled by default if `typescript` is installed
} )设置完成后,将生成一个components.d.ts 。随意将其纳入您想要的git。
确保您还将
components.d.ts添加到tsconfig.json所包含的includes。
拥有自己的组件已经导入它很酷,但是有时您想进口第三方组件。
因此, unplugin-svelte-components提供了一种导入这些组件的方法。
Components ( {
dts : true ,
external : [
{
from : "agnostic-svelte" , // import from third party
names : [ // import these components
"Alert" ,
"Button as AButton" , // import as `AButton`
] ,
defaultImport : false , // telling `unplugin-svelte-components` to import any component as non-default export
} ,
] ,
} )因此,将使用Alert和AButton 。
在某些情况下,您可能需要在页面中禁用自动象征, unplugin-svelte-components提供了一种方法,只需添加<!-- unplugin-svelte-components disabled -->
<!-- unplugin-svelte-components disabled -->
< HelloWorld msg =" Hello Svelte " /> 以下显示配置的默认值
Components ( {
// relative paths to the directory to search for components.
dirs : [ 'src/components' ] ,
// valid file extensions for components.
extensions : [ 'svelte' ] ,
// search for subdirectories
deep : true ,
// generate `components.d.ts` global declarations,
// also accepts a path for custom filename
// default: `true` if package typescript is installed
dts : false ,
// Allow subdirectories as namespace prefix for components.
directoryAsNamespace : false ,
// Subdirectory paths for ignoring namespace prefixes
// works when `directoryAsNamespace: true`
globalNamespaces : [ ] ,
// Transform path before resolving
importPathTransform : v => v ,
// Allow for components to override other components with the same name
allowOverrides : false ,
// Accept a svelte pre-processor (e.g. svelte-preprocess)
preprocess : null ,
// filters for transforming targets
include : [ / .svelte$ / , / .svelte?svelte / ] ,
exclude : [ / [\/]node_modules[\/] / , / [\/].git[\/] / , / [\/].svelte-kit[\/] / , ] ,
// Generate corresponding .eslintrc-components.json file.
// eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
eslintrc : {
enabled : true , // Default `true`
filepath : './.eslintrc-components.json' , // Default `./.eslintrc-components.json`
globalsPropValue : true , // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
}
} ) 多亏了安东尼·富(Anthony Fu),该项目的灵感来自于普鲁金(Unplugin-Vue)组件。
麻省理工学院许可©2022-PRESENT MOHAMED NESREDIN