Skip to content

Commit c36d784

Browse files
authored
fix(antdx): resolve Think component React #130 in production build (#123)
1 parent 825ec9d commit c36d784

3 files changed

Lines changed: 73 additions & 24 deletions

File tree

.changeset/tame-icons-default.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@modelscope-studio/frontend': patch
3+
'@modelscope-studio/antdx': patch
4+
'modelscope_studio': patch
5+
---
6+
7+
fix(antdx): resolve `Think` component React #130 error in production build
8+
9+
- Fix default import mapping in the Vite plugin so namespace-style globals
10+
(e.g. `@ant-design/icons`) correctly resolve `import X from 'mod'` to
11+
`<global>.default` instead of the namespace object itself.
12+
- Forward `children` to `XThink` so user-provided content renders.

frontend/antdx/think/think.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React from 'react';
44
import { Think as XThink, type ThinkProps } from '@ant-design/x';
55

66
export const Think = sveltify<ThinkProps, ['loading', 'icon', 'title']>(
7-
({ slots, ...props }) => {
7+
({ slots, children, ...props }) => {
88
return (
99
<>
1010
<XThink
@@ -14,7 +14,9 @@ export const Think = sveltify<ThinkProps, ['loading', 'icon', 'title']>(
1414
}
1515
icon={slots.icon ? <ReactSlot slot={slots.icon} /> : props.icon}
1616
title={slots.title ? <ReactSlot slot={slots.title} /> : props.title}
17-
/>
17+
>
18+
{children}
19+
</XThink>
1820
</>
1921
);
2022
}

frontend/plugin.js

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,48 @@ import { parse, transformFromAstSync, traverse, types as t } from '@babel/core';
22
import path from 'node:path';
33
import url from 'node:url';
44

5+
// Each entry mirrors how the global is exposed in
6+
// `frontend/svelte-preprocess-react/inject.ts`:
7+
// - namespace: true -> registered via `import * as X from 'mod'`
8+
// (`window.ms_globals.<key>` is the namespace object)
9+
// - namespace: false -> registered via `import X from 'mod'` (or a single
10+
// named binding stored directly), so
11+
// `window.ms_globals.<key>` already is the value.
512
const baseGlobals = {
6-
react: 'window.ms_globals.React',
7-
'react-dom': 'window.ms_globals.ReactDOM',
8-
'react-dom/client': 'window.ms_globals.ReactDOMClient',
9-
antd: 'window.ms_globals.antd',
10-
antdx: 'window.ms_globals.antdx',
11-
'@ant-design/cssinjs': 'window.ms_globals.antdCssinjs',
12-
'@ant-design/icons': 'window.ms_globals.antdIcons',
13+
react: { ref: 'window.ms_globals.React', namespace: false },
14+
'react-dom': { ref: 'window.ms_globals.ReactDOM', namespace: false },
15+
'react-dom/client': {
16+
ref: 'window.ms_globals.ReactDOMClient',
17+
namespace: false,
18+
},
19+
antd: { ref: 'window.ms_globals.antd', namespace: true },
20+
antdx: { ref: 'window.ms_globals.antdx', namespace: true },
21+
'@ant-design/cssinjs': {
22+
ref: 'window.ms_globals.antdCssinjs',
23+
namespace: true,
24+
},
25+
'@ant-design/icons': {
26+
ref: 'window.ms_globals.antdIcons',
27+
namespace: true,
28+
},
1329

14-
'@svelte-preprocess-react/react-contexts':
15-
'window.ms_globals.internalReactContexts',
16-
dayjs: 'window.ms_globals.dayjs',
17-
'@utils/createItemsContext': 'window.ms_globals.createItemsContext',
18-
'@globals/components': 'window.ms_globals.components',
19-
'@monaco-editor/loader': 'window.ms_globals.monacoLoader',
30+
'@svelte-preprocess-react/react-contexts': {
31+
ref: 'window.ms_globals.internalReactContexts',
32+
namespace: true,
33+
},
34+
dayjs: { ref: 'window.ms_globals.dayjs', namespace: false },
35+
'@utils/createItemsContext': {
36+
ref: 'window.ms_globals.createItemsContext',
37+
namespace: true,
38+
},
39+
'@globals/components': {
40+
ref: 'window.ms_globals.components',
41+
namespace: true,
42+
},
43+
'@monaco-editor/loader': {
44+
ref: 'window.ms_globals.monacoLoader',
45+
namespace: false,
46+
},
2047
};
2148

2249
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
@@ -56,8 +83,8 @@ export const ModelScopeStudioVitePlugin = ({ external = true } = {}) => {
5683
...userConfig.define,
5784
'process.env.NODE_ENV': JSON.stringify('production'),
5885
};
86+
userConfig.build ??= {};
5987
if (external) {
60-
userConfig.build ??= {};
6188
userConfig.build.rollupOptions ??= {};
6289
userConfig.build.rollupOptions.external = [
6390
...(userConfig.build.rollupOptions.external || []),
@@ -90,8 +117,8 @@ export const ModelScopeStudioVitePlugin = ({ external = true } = {}) => {
90117
enterPath.traverse({
91118
ExportNamedDeclaration(nodePath) {
92119
const source = nodePath.node.source?.value;
93-
const variable = globals[source];
94-
if (!variable) {
120+
const entry = globals[source];
121+
if (!entry) {
95122
return;
96123
}
97124
const { specifiers } = nodePath.node;
@@ -100,7 +127,7 @@ export const ModelScopeStudioVitePlugin = ({ external = true } = {}) => {
100127
return t.variableDeclarator(
101128
specifier.local,
102129
t.memberExpression(
103-
t.identifier(variable),
130+
t.identifier(entry.ref),
104131
specifier.local
105132
)
106133
);
@@ -113,9 +140,9 @@ export const ModelScopeStudioVitePlugin = ({ external = true } = {}) => {
113140
},
114141
ImportDeclaration(nodePath) {
115142
const source = nodePath.node.source.value;
116-
const variable = globals[source];
143+
const entry = globals[source];
117144

118-
if (!variable) {
145+
if (!entry) {
119146
return;
120147
}
121148

@@ -128,22 +155,30 @@ export const ModelScopeStudioVitePlugin = ({ external = true } = {}) => {
128155
const decls = specifiers.map((specifier) => {
129156
switch (specifier.type) {
130157
case 'ImportDefaultSpecifier':
158+
// For namespace-style globals, the default export
159+
// lives at `<ref>.default`. For value-style globals,
160+
// `<ref>` itself already is the default value.
131161
return t.variableDeclarator(
132162
specifier.local,
133-
t.identifier(variable)
163+
entry.namespace
164+
? t.memberExpression(
165+
t.identifier(entry.ref),
166+
t.identifier('default')
167+
)
168+
: t.identifier(entry.ref)
134169
);
135170
case 'ImportSpecifier':
136171
return t.variableDeclarator(
137172
specifier.local,
138173
t.memberExpression(
139-
t.identifier(variable),
174+
t.identifier(entry.ref),
140175
specifier.imported
141176
)
142177
);
143178
case 'ImportNamespaceSpecifier':
144179
return t.variableDeclarator(
145180
specifier.local,
146-
t.identifier(variable)
181+
t.identifier(entry.ref)
147182
);
148183
default:
149184
throw new Error(

0 commit comments

Comments
 (0)