Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to define a _jsxFilename for custom components #1306

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/toolpad-app/src/runtime/AppModulesProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function AppModulesProvider({ dom, children }: AppModulesProviderProps) {

if (content) {
if (!fromCache) {
const createPromise = loadModule(content).then(
const createPromise = loadModule(content, id).then(
(module: unknown) => {
cache.set(cacheId, {
state: 'loaded',
Expand Down
7 changes: 5 additions & 2 deletions packages/toolpad-app/src/runtime/loadCodeComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export function ensureToolpadComponent<P>(Component: unknown): ToolpadComponent<
return createComponent(Component);
}

export default async function loadCodeComponent(src: string): Promise<ToolpadComponent> {
const { default: Component } = await loadModule(src);
export default async function loadCodeComponent(
src: string,
filename: string,
): Promise<ToolpadComponent> {
const { default: Component } = await loadModule(src, filename);

if (!ReactIs.isValidElementType(Component) || typeof Component === 'string') {
throw new Error(`No React Component exported.`);
Expand Down
12 changes: 7 additions & 5 deletions packages/toolpad-app/src/runtime/loadModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ async function createRequire(urlImports: string[]) {
return require;
}

export default async function loadModule(src: string): Promise<any> {
export default async function loadModule(src: string, filename: string): Promise<any> {
const imports = findImports(src).filter((maybeUrl) => isAbsoluteUrl(maybeUrl));

let compiled: TransformResult;

try {
compiled = transform(src, {
transforms: ['jsx', 'typescript', 'imports'],
jsxRuntime: 'classic',
});
} catch (rawError) {
const err = errorFrom(rawError);
Expand All @@ -77,10 +78,11 @@ export default async function loadModule(src: string): Promise<any> {
};

const instantiateModuleCode = `
(${Object.keys(globals).join(', ')}) => {
${compiled.code}
}
`;
const _jsxFileName = ${JSON.stringify(filename)};
(${Object.keys(globals).join(', ')}) => {
${compiled.code}
}
`;

// eslint-disable-next-line no-eval
const instantiateModule = (0, eval)(instantiateModuleCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ function CodeComponentEditorContent({ codeComponentNode }: CodeComponentEditorCo
const frameDocument = frameRef.current?.contentDocument;

const debouncedInput = useDebounced(input.attributes.code.value, 250);
const { Component: GeneratedComponent, error: compileError } = useCodeComponent(debouncedInput);
const { Component: GeneratedComponent, error: compileError } = useCodeComponent(
debouncedInput,
`/components/${codeComponentNode.name}`,
);
const CodeComponent: ToolpadComponent<any> = useLatest(GeneratedComponent) || Noop;

const { argTypes = {} } = CodeComponent[TOOLPAD_COMPONENT];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type UseCodeComponent =
error: Error;
};

export default function useCodeComponent(src: string | null): UseCodeComponent {
export default function useCodeComponent(src: string | null, filename: string): UseCodeComponent {
const [state, setState] = React.useState<UseCodeComponent>({});

React.useEffect(() => {
Expand All @@ -25,7 +25,7 @@ export default function useCodeComponent(src: string | null): UseCodeComponent {
}

const startSrc = src;
loadCodeComponent(startSrc)
loadCodeComponent(startSrc, filename)
.then((Component) => {
if (startSrc === src) {
setState({ Component });
Expand All @@ -36,7 +36,7 @@ export default function useCodeComponent(src: string | null): UseCodeComponent {
setState({ error });
}
});
}, [src]);
}, [src, filename]);

return state;
}