1.5.9 β€’ Published 10 months ago

nuxt-module-structure v1.5.9

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Nuxt Module architecture

The Nuxt Module Structure package allows you to create a modular architecture for your Nuxt applications, organizing your code into layers. This helps in maintaining a clean structure, especially for large applications. Nuxt Layers Documentation

You can find the official documentation for Nuxt Layers here: πŸ”— Nuxt Layers

Installation

To initialize the main modules, run:

npm init-modules ./ -i18n

Replace ./ with your desired path for the main modules. If you need to configure i18n, include it as shown.

Usage

When you run the initialization command, you will be prompted for:

  1. Name of the main module: The default name is app.
  2. Authentication module: The default is to create an authentication module.

Directory Structure

Upon execution, the following directory structure will be created:

modules/
  β”œβ”€β”€ app/
  β”‚   β”œβ”€β”€ pages/
  β”‚   β”œβ”€β”€ layouts/
  β”‚   β”œβ”€β”€ components/
  β”‚   └── store/
  └── auth/
      β”œβ”€β”€ pages/
      β”œβ”€β”€ layouts/
      β”œβ”€β”€ components/
      └── store/

Each module will automatically have its path added to the nuxtModuleConfig:

nuxtModuleConfig: {
  dir: {
        pages: "./pages",         // Custom pages directory
        layouts: "./layouts",     // Custom layouts directory
    //  plugins:"./plugins",      // Custom plugins directory  
    //  assets: "./assets",       // Custom assets directory
    //  middleware: "./middleware", // Custom middleware directory
  },
  extends: ["<layer-path>"]
}

Next step

Adding Separate Modules

To add a separate module in the future, run:

npm run add-module ./modules/app about-us
  • ./modules/app :
  • about-us:

You can specify additional configurations such as:

  • i18n (if you need internationalization)
  • store (if you need a pinia store)
  • layout (if you need a layout)
npm run add-module ./modules/app about-us -i18n -store -layout

Outbut

modules/
  β”œβ”€β”€ app/
  β”‚   β”œβ”€β”€ pages/
  β”‚   β”œβ”€β”€ layouts/
  β”‚   β”œβ”€β”€ components/
  β”‚   β”œβ”€β”€ store/
  β”‚   └── about-us/   # Add the about-us module here
  β”‚       β”œβ”€β”€ pages/
  β”‚       β”œβ”€β”€ layouts/
  β”‚       β”œβ”€β”€ components/
  β”‚       β”œβ”€β”€ i18n/
          └── store/
  └── auth/
      β”œβ”€β”€ pages/
      β”œβ”€β”€ layouts/
      β”œβ”€β”€ components/
      └── store/

⚠️ Warning: Proper Module Structure

When adding a child module inside a parent module, always create a modules/ directory inside the parent module and place the child module inside it.

Correct Structure:

modules/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ pages/
β”‚   β”œβ”€β”€ layouts/
β”‚   β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ store/
β”‚   └── modules/   # Create this directory inside the parent module
β”‚       β”œβ”€β”€ profile/  # Add child modules inside β€œmodules/”
β”‚       β”‚   β”œβ”€β”€ pages/
β”‚       β”‚   β”œβ”€β”€ layouts/
β”‚       β”‚   β”œβ”€β”€ components/
β”‚       β”‚   └── store/
└── auth/
β”œβ”€β”€ pages/
β”œβ”€β”€ layouts/
β”œβ”€β”€ components/
└── store/

❌ Incorrect Structure:

modules/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ pages/
β”‚   β”œβ”€β”€ layouts/
β”‚   β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ store/
β”‚   β”œβ”€β”€ profile/  # ❌ Do NOT add child modules directly here
└── auth/

Why is this important?

  • Keeps the module hierarchy clean and organized.
  • Prevents conflicts and confusion when adding new modules.
  • Ensures consistency across the project

Module Auto-Generation

Page Creation

When adding a module, a page is automatically created inside the pages directory:

β”œβ”€β”€ home/
β”‚   β”œβ”€β”€ pages/
β”‚         β”œβ”€β”€β”€β”€/home.vue
β”‚
│────────────────────

home.vue

  <template>
    <div class="">${filename} page</div>
  </template>
  <script lang="ts" setup>
  definePageMeta({
   // layout: "module-lay",
  });
  </script>

###homeStore.ts

  import { defineStore } from "pinia";
  const useModuleNameStore = defineStore("useModuleNameStore", () => {});
  export default useuseModuleNameStoreStore;

Is Store

###Store Setup

β”œβ”€β”€ module-name/
β”‚   β”œβ”€β”€ store/
β”‚         β”œβ”€β”€β”€β”€/moduleNameStore.ts
β”‚
│────────────────────

###moduleNameStore.ts

  import { defineStore } from "pinia";
  const useModuleNameStore = defineStore("useModuleNameStore", () => {});
  export default useuseModuleNameStoreStore;

Layout Setup

Each module includes a layout file:

β”œβ”€β”€ module-name/
β”‚   β”œβ”€β”€ Layouts/
β”‚         β”œβ”€β”€β”€β”€/module-name-lay.vue
β”‚
│────────────────────

###module-name-layout.vue

  <script setup lang="ts">
  // import parent layout what you want and wrap solt inside it to make sited layout 
  // import ParentLayout from "../../layouts/*****.vue" 
  </script>
  
  <template>
  <!-- <pearnt-layout>  -->
      <slot />
      <!--  <pearnt-layout> -->
  </template>

Is i18n

###If i18n is not initialized when creating modules, running add-module --i18n will automatically generate i18n.config.ts in the root project.

β”œβ”€β”€ root/
β”‚   β”œβ”€β”€ i18n/
β”‚         β”œβ”€β”€β”€β”€/global
β”‚                 β”œβ”€β”€β”€β”€/en.ts
β”‚                  β”œβ”€β”€β”€β”€/ar.ts
β”‚         β”œβ”€β”€β”€β”€/i18n.config.ts
β”‚
│────────────────────

i18n.config.ts

export default {
}
import en from "./global/en"
import ar from "./global/ar"

export default defineI18nConfig(() => ({
  legacy: false,
  locale: "ar",
  fallbackLocale: "",
  messages: {
    en: {
      ...en,
    },
    ar: {
      ...ar,
    },
  },
}));

Global Translation Files (en.ts / ar.ts)

export default {
}

##Using i18n as a Composable in components

 import ar from "../i18n/ar";
 import en from "../i18n/en";
 import { useI18n } from "vue-i18n";
 const { t } = useI18n({
   messages: {
     en: en,
     ar: ar,
   },
 });
1.5.9

10 months ago

1.5.8

10 months ago

1.5.7

10 months ago

1.5.6

10 months ago

1.5.5

10 months ago

1.5.4

10 months ago

1.5.3

10 months ago

1.5.2

10 months ago

1.5.1

10 months ago

1.4.1

10 months ago

1.4.0

10 months ago

1.3.10

10 months ago

1.2.10

10 months ago

1.2.9

10 months ago

1.2.8

10 months ago

1.2.7

10 months ago

1.2.6

10 months ago

1.2.5

10 months ago

1.2.4

10 months ago

1.2.3

10 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.2.19

10 months ago

1.1.19

10 months ago

1.1.18

10 months ago

1.0.18

10 months ago

1.0.17

10 months ago

1.0.16

10 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago