|
1
|
--- |
|
2
|
sidebar_label: "loader-must-be-exported-in-module-definition" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `loader-must-be-exported-in-module-definition - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule requires that loader files under a module's `loaders` directory are registered in the module's `loaders` array. |
|
12
|
|
|
13
|
## Severity |
|
14
|
|
|
15
|
`warn`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
16
|
|
|
17
|
## What it Targets |
|
18
|
|
|
19
|
This rule targets loader files under `src/modules/<module>/loaders/`. It reports a loader file that isn't referenced in the `loaders` array of the module's definition in `index.ts`. |
|
20
|
|
|
21
|
The following loader is reported by the rule when it isn't registered in the module definition in `index.ts`: |
|
22
|
|
|
23
|
```ts title="src/modules/blog/loaders/my-loader.ts" |
|
24
|
import { LoaderOptions } from "@medusajs/framework/types" |
|
25
|
|
|
26
|
export default async function myLoader({ |
|
27
|
container, |
|
28
|
}: LoaderOptions) { |
|
29
|
// ... |
|
30
|
} |
|
31
|
``` |
|
32
|
|
|
33
|
Instead, register the loader in the module's `loaders` array in `index.ts`: |
|
34
|
|
|
35
|
```ts title="src/modules/blog/index.ts" |
|
36
|
import { Module } from "@medusajs/framework/utils" |
|
37
|
import BlogModuleService from "./service" |
|
38
|
import myLoader from "./loaders/my-loader" |
|
39
|
|
|
40
|
export default Module("blog", { |
|
41
|
service: BlogModuleService, |
|
42
|
loaders: [myLoader], |
|
43
|
}) |
|
44
|
``` |
|
45
|
|
|
46
|
## Why it's Important |
|
47
|
|
|
48
|
A loader only runs when it's registered in the module's `loaders` array. An unregistered loader file is dead code that never executes, which often points to a forgotten registration. |
|
49
|
|
|
50
|
Learn more in the [Modules documentation](!docs!/learn/fundamentals/modules). |
|
51
|
|
|
52
|
## Fixable |
|
53
|
|
|
54
|
This rule isn't auto-fixable. |
|
55
|
|
|
56
|
## Turn it Off |
|
57
|
|
|
58
|
To turn off this rule, set it to `off` in your ESLint configuration: |
|
59
|
|
|
60
|
```ts title="eslint.config.ts" |
|
61
|
import { defineConfig } from "eslint/config" |
|
62
|
import medusa from "@medusajs/eslint-plugin" |
|
63
|
|
|
64
|
export default defineConfig([ |
|
65
|
...medusa.configs.recommended, |
|
66
|
{ |
|
67
|
rules: { |
|
68
|
"@medusajs/loader-must-be-exported-in-module-definition": "off", |
|
69
|
}, |
|
70
|
}, |
|
71
|
]) |
|
72
|
``` |
|
73
|
|
|
74
|
Or disable it for a single line using an inline comment: |
|
75
|
|
|
76
|
```ts |
|
77
|
// eslint-disable-next-line @medusajs/loader-must-be-exported-in-module-definition |
|
78
|
export default async function myLoader() {} |
|
79
|
``` |