Skip to content

Commit

Permalink
Make sure to define a _jsxFilename for custom components (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot committed Nov 8, 2022
1 parent 5b88975 commit ffba527
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
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;
}

0 comments on commit ffba527

Please sign in to comment.