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