|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
|
|
4
|
type MessageIds = "nodeOnlyImportInAdmin" |
|
5
|
|
|
6
|
/** |
|
7
|
* Framework entry points that pull Node-only code into the bundle. The admin |
|
8
|
* dashboard is a browser bundle, so importing any of these breaks the admin |
|
9
|
* build (or ships server-only code to the client). |
|
10
|
*/ |
|
11
|
const FORBIDDEN_ADMIN_IMPORTS = new Set([ |
|
12
|
"@medusajs/framework/utils", |
|
13
|
"@medusajs/framework/http", |
|
14
|
"@medusajs/framework/workflows-sdk", |
|
15
|
]) |
|
16
|
|
|
17
|
function check( |
|
18
|
context: Parameters<Parameters<typeof createRule>[0]["create"]>[0], |
|
19
|
source: TSESTree.StringLiteral | null | undefined |
|
20
|
) { |
|
21
|
if (!source) { |
|
22
|
return |
|
23
|
} |
|
24
|
const value = source.value |
|
25
|
if (typeof value !== "string") { |
|
26
|
return |
|
27
|
} |
|
28
|
|
|
29
|
if (FORBIDDEN_ADMIN_IMPORTS.has(value)) { |
|
30
|
context.report({ |
|
31
|
node: source, |
|
32
|
messageId: "nodeOnlyImportInAdmin", |
|
33
|
data: { source: value }, |
|
34
|
}) |
|
35
|
} |
|
36
|
} |
|
37
|
|
|
38
|
export const rule = createRule<[], MessageIds>({ |
|
39
|
name: "admin-no-medusa-utils-import", |
|
40
|
meta: { |
|
41
|
type: "problem", |
|
42
|
docs: { |
|
43
|
description: |
|
44
|
"Don't import Node-only framework modules (`@medusajs/framework/utils`, `@medusajs/framework/http`, `@medusajs/framework/workflows-sdk`) in admin code — they break the browser bundle.", |
|
45
|
}, |
|
46
|
messages: { |
|
47
|
nodeOnlyImportInAdmin: |
|
48
|
"`{{ source }}` is a Node-only module and must not be imported in admin code — it breaks the browser bundle. Move this logic to the server (a workflow, API route, or module service) and call it from the admin via the JS SDK.", |
|
49
|
}, |
|
50
|
schema: [], |
|
51
|
}, |
|
52
|
defaultOptions: [], |
|
53
|
create(context) { |
|
54
|
return { |
|
55
|
ImportDeclaration(node) { |
|
56
|
check(context, node.source) |
|
57
|
}, |
|
58
|
ExportNamedDeclaration(node) { |
|
59
|
if (node.source && node.source.type === AST_NODE_TYPES.Literal) { |
|
60
|
check(context, node.source as TSESTree.StringLiteral) |
|
61
|
} |
|
62
|
}, |
|
63
|
ExportAllDeclaration(node) { |
|
64
|
check(context, node.source) |
|
65
|
}, |
|
66
|
} |
|
67
|
}, |
|
68
|
}) |
|
69
|
|
|
70
|
export default rule |