Skip to content

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

  1. NPM Modules
    NPM packages can be imported normally without any special configuration:

    import _ from 'lodash';
    import axios from 'axios';
    

  2. Relative and Absolute Imports
    Files in the js and components directories can be imported using relative or absolute paths:

    import Button from './components/Button.vue';  // Relative
    import Navbar from '/view/frontend/web/components/Navbar.vue';  // Absolute
    

  3. Module-Based Imports
    The most powerful feature is the ability to import files from other Magento modules using the following notation:

    import Demo from 'Vendor_ModuleA::components/demo.vue';
    import Main from 'Vendor_ModuleF::js/main.js';
    

  4. If the root directory (e.g., components or js) is omitted, it defaults to components:

    import Demo from 'Vendor_ModuleA::demo.vue';  // Same as Vendor_ModuleA::components/demo.vue
    

  5. Theme-Based Imports
    Files from the theme can be imported using a similar notation:

    import Navbar from 'Theme::nav.vue';
    

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:

import Bar from '../../bar.vue';
If the bar.vue component is overridden in the theme, this relative path will still reference the original module component, ignoring the theme's overwrite.

Conversely:

import Bar from 'Vendor_Module::bar.vue';
When importing via the 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

  1. Always Use Module-Based Imports
    Use the Vendor_Module:: notation for importing resources from Magento modules. This ensures:
  2. Proper resolution of overridden components or scripts in themes.
  3. A modular and maintainable codebase.

Example:

import Button from 'Vendor_Module::button.vue';

  1. Avoid Relative or Absolute Imports
    Unless specifically required, do not use relative or absolute paths for module resources in js or components. These bypass Vite's resolution mechanism, causing unexpected behavior if resources are overridden.

  2. Use Theme Imports for Overrides
    If you want to explicitly use a resource from the theme, use the Theme:: notation:

    import Navbar from 'Theme::navbar.vue';
    

  3. Consistency in Import Notation
    Maintain consistent use of the Vendor_Module:: and Theme:: notations throughout your project to prevent confusion and ensure compatibility.


Examples

Importing from a Module

import Card from 'Vendor_ModuleA::components/Card.vue';
import ApiService from 'Vendor_ModuleB::js/api-service.js';

Importing from the Theme

import Header from 'Theme::header.vue';

Improper Import

// Avoid this
import Footer from './Footer.vue'; // This will not reflect theme overrides

Benefits of Using Module-Based and Theme Imports

  1. Seamless Theme Overrides
    Ensures the correct component or script is loaded, respecting the hierarchy of overwrites.

  2. Modularity
    Encourages a modular approach, making your codebase more maintainable and easier to scale.

  3. Flexibility
    Supports customizations without breaking the original structure of modules.

  4. Error Prevention
    Prevents unexpected behavior caused by hardcoded relative or absolute paths.


Key Notes

  • This system applies only to files in the js and components 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.