الفرق بين المراجعتين ل"Next.js/module path aliases"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
ط (مراجعة)
سطر 1: سطر 1:
 
<noinclude>{{DISPLAYTITLE:المسارات المطلقة البديلة للمُدرجات و الوحدات البرمجية في Next.js}}</noinclude>
 
<noinclude>{{DISPLAYTITLE:المسارات المطلقة البديلة للمُدرجات و الوحدات البرمجية في Next.js}}</noinclude>
دعمت Next.js تلقائيًا الخياران <code>"paths"</code> و <code>"baseUrl"</code> في ملفي التهيئة <code>tsconfig.json</code> و <code>jsconfig.json</code> ابتداءً من النسخة 9.4. ويتيح لك هذان الخياران تهيئة مسارات بديلة aliases للوحدات البرمجية (أكثرها شيوعًا تبديل مجلدات معينة لاستخدام المسارات المطلقة). ولهذه الخيارات ميزة مفيدة هي تكاملها التلقائي مع بعض محررات الشيفرة مثل vscode. <blockquote>'''ملاحظة''': يمكنك استخدام الملف <code>jsconfig.json</code> عندما لا تستخدم TypeScript.
+
دعمت Next.js تلقائيًا الخياران <code>"paths"</code> و <code>"baseUrl"</code> في ملفي التهيئة <code>tsconfig.json</code> و <code>jsconfig.json</code> ابتداءً من النسخة 9.4. ويتيح لك هذان الخياران تهيئة مسارات بديلة aliases للوحدات البرمجية (أكثرها شيوعًا اعتماد مسار مجلدات معينة في مجلد المشروع لاستخدام المسارات المطلقة). ولهذه الخيارات ميزة مفيدة هي تكاملها التلقائي مع بعض محررات الشيفرة مثل vscode. <blockquote>'''ملاحظة''': يمكنك استخدام الملف <code>jsconfig.json</code> عندما لا تستخدم [[TypeScript]].
  
'''ملاحظة''': لا بد من إعادة إقلاع خادم Next.js لإظهار التعديلاتت التي تجريها على <code>tsconfig.json</code> أو <code>jsconfig.json</code>.</blockquote>يتيح لك الخيار <code>baseUrl</code> إدراج الموارد مباشرة من جذر المشروع. إليك مثالًا:<syntaxhighlight lang="javascript">
+
'''ملاحظة''': لا بد من إعادة إقلاع خادم Next.js لإظهار التعديلات التي تجريها على <code>tsconfig.json</code> أو <code>jsconfig.json</code>.</blockquote>يتيح لك الخيار <code>baseUrl</code> إدراج الموارد مباشرة من جذر المشروع. إليك مثالًا:<syntaxhighlight lang="javascript">
 
// tsconfig.json or jsconfig.json
 
// tsconfig.json or jsconfig.json
 
{
 
{
سطر 9: سطر 9:
 
   }
 
   }
 
}
 
}
 +
</syntaxhighlight><syntaxhighlight lang="javascript">
 
// components/button.js
 
// components/button.js
 
export default function Button() {
 
export default function Button() {
 
   return <button>Click me</button>
 
   return <button>Click me</button>
 
}
 
}
 +
</syntaxhighlight><syntaxhighlight lang="javascript">
 
// pages/index.js
 
// pages/index.js
 
import Button from 'components/button'
 
import Button from 'components/button'
سطر 34: سطر 36:
 
   }
 
   }
 
}
 
}
 +
</syntaxhighlight><syntaxhighlight lang="javascript">
 
// components/button.js
 
// components/button.js
 
export default function Button() {
 
export default function Button() {
 
   return <button>Click me</button>
 
   return <button>Click me</button>
 
}
 
}
 +
</syntaxhighlight><syntaxhighlight lang="javascript">
 
// pages/index.js
 
// pages/index.js
 
import Button from '@/components/button'
 
import Button from '@/components/button'
سطر 50: سطر 54:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
== أمثلة ==
 +
 +
* [https://github.com/vercel/next.js/tree/canary/examples/with-absolute-imports Absolute Imports and Aliases]
  
 
== المصادر ==
 
== المصادر ==
  
 
* الصفحة [https://nextjs.org/docs/advanced-features/module-path-aliases Absolute imports and modules path aliases] من توثيق Next.js الرسمي.
 
* الصفحة [https://nextjs.org/docs/advanced-features/module-path-aliases Absolute imports and modules path aliases] من توثيق Next.js الرسمي.

مراجعة 11:42، 18 ديسمبر 2022

دعمت Next.js تلقائيًا الخياران "paths" و "baseUrl" في ملفي التهيئة tsconfig.json و jsconfig.json ابتداءً من النسخة 9.4. ويتيح لك هذان الخياران تهيئة مسارات بديلة aliases للوحدات البرمجية (أكثرها شيوعًا اعتماد مسار مجلدات معينة في مجلد المشروع لاستخدام المسارات المطلقة). ولهذه الخيارات ميزة مفيدة هي تكاملها التلقائي مع بعض محررات الشيفرة مثل vscode.

ملاحظة: يمكنك استخدام الملف jsconfig.json عندما لا تستخدم TypeScript. ملاحظة: لا بد من إعادة إقلاع خادم Next.js لإظهار التعديلات التي تجريها على tsconfig.json أو jsconfig.json.

يتيح لك الخيار baseUrl إدراج الموارد مباشرة من جذر المشروع. إليك مثالًا:

// tsconfig.json or jsconfig.json
{
  "compilerOptions": {
    "baseUrl": "."
  }
}
// components/button.js
export default function Button() {
  return <button>Click me</button>
}
// pages/index.js
import Button from 'components/button'

export default function HomePage() {
  return (
    <>
      <h1>Hello World</h1>
      <Button />
    </>
  )
}

وعلى الرغم من فائدة baseUrl، فقد ترغب في إضافة بدائل أخرى لا تربط بالضرورة كل مسار ببديل alias، لهذا السبب وجد الخيار "paths" في TypeScript. إذ يتيح استخدام "paths" تهيئة بدائل لمسارات الوحدات البرمجية، كأن يُستبدل المسار */components/@ مثلًا بالمسار */components. إليك مثالًا عن طريقة التهيئة:

// tsconfig.json or jsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/components/*": ["components/*"]
    }
  }
}
// components/button.js
export default function Button() {
  return <button>Click me</button>
}
// pages/index.js
import Button from '@/components/button'

export default function HomePage() {
  return (
    <>
      <h1>Hello World</h1>
      <Button />
    </>
  )
}

أمثلة

المصادر