Importing Between Magento Modules
In MageObsidian Components, importing JavaScript files and Vue components follows flexible conventions that apply specifically to files in the js
and components
directories. This approach ensures compatibility, modularity, and seamless overwriting of components by themes.
Supported Import Methods
-
NPM Modules
NPM packages can be imported normally without any special configuration: -
Relative and Absolute Imports
Files in thejs
andcomponents
directories can be imported using relative or absolute paths: -
Module-Based Imports
The most powerful feature is the ability to import files from other Magento modules using the following notation: -
If the root directory (e.g.,
components
orjs
) is omitted, it defaults tocomponents
: -
Theme-Based Imports
Files from the theme can be imported using a similar notation:
This follows the same resolution rules as module-based imports, allowing themes to override components or scripts seamlessly.
Application in js
and components
This import system is specifically designed for files located in the js
and components
directories:
- JavaScript files: These are tracked in view/frontend/web/js
.
- Vue components: These are tracked in view/frontend/web/components
.
For example:
- Vendor_ModuleF::js/main.js
references a file in the js
directory.
- Vendor_ModuleA::components/demo.vue
references a file in the components
directory.
Any imports outside these directories must use standard relative or absolute paths.
Why This Matters
Resolving Overwritten Components
When files are overridden in themes, relative or absolute imports fail to reflect these changes, leading to unexpected behavior.
For example:
bar.vue
component is overridden in the theme, this relative path will still reference the original module component, ignoring the theme's overwrite.
Conversely:
Vendor_Module::
notation, Vite automatically resolves the appropriate file, respecting the overwrite hierarchy (module -> theme). This ensures the correct resource is always loaded.
Best Practices for Imports
- Always Use Module-Based Imports
Use theVendor_Module::
notation for importing resources from Magento modules. This ensures: - Proper resolution of overridden components or scripts in themes.
- A modular and maintainable codebase.
Example:
-
Avoid Relative or Absolute Imports
Unless specifically required, do not use relative or absolute paths for module resources injs
orcomponents
. These bypass Vite's resolution mechanism, causing unexpected behavior if resources are overridden. -
Use Theme Imports for Overrides
If you want to explicitly use a resource from the theme, use theTheme::
notation: -
Consistency in Import Notation
Maintain consistent use of theVendor_Module::
andTheme::
notations throughout your project to prevent confusion and ensure compatibility.
Examples
Importing from a Module
Importing from the Theme
Improper Import
Benefits of Using Module-Based and Theme Imports
-
Seamless Theme Overrides
Ensures the correct component or script is loaded, respecting the hierarchy of overwrites. -
Modularity
Encourages a modular approach, making your codebase more maintainable and easier to scale. -
Flexibility
Supports customizations without breaking the original structure of modules. -
Error Prevention
Prevents unexpected behavior caused by hardcoded relative or absolute paths.
Key Notes
- This system applies only to files in the
js
andcomponents
directories. - Use the
Vendor_Module::
notation for all imports within Magento modules. - Use the
Theme::
notation for resources explicitly from themes. - Avoid relative or absolute paths unless necessary.
- Vite handles resolution and ensures that overridden components or scripts in themes are correctly loaded.
By following these guidelines, MageObsidian Components enables a clean, flexible, and reliable workflow for managing JavaScript and Vue components across modules and themes.