مكونات عند الطلب تلقائي للاستيراد لـ Svelte.
Sveltekit Demo Svelte Vite Demo
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 > للحصول على دعم TypeScript للمكونات المستخصجة تلقائيًا ، يمكنك تغيير التكوين على النحو التالي للحصول على الدعم.
Components ( {
dts : true , // enabled by default if `typescript` is installed
} ) بمجرد الانتهاء من الإعداد ، سيتم إنشاء components.d.ts . لا تتردد في ارتكابها في غيت أو لا كما تريد.
تأكد أيضًا من
includescomponents.d.tstsconfig.json
إنه لأمر رائع أن تكون مكوناتك الخاصة قد استيرادها ، ولكن في وقت ما تريد استيراد مكونات الطرف الثالث.
وهكذا وفرت 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')
}
} ) بفضل أنتوني فو ، يستوحى هذا المشروع بشكل كبير من أدوات التغلب على غيرها.
رخصة معهد ماساتشوستس للتكنولوجيا © 2022-حامد محمد نيسريدين