Skip to content

Support serialization of exotic react compoonent #1490

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-hotels-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/integration': major
---

Support serialisation of exotic react components
24 changes: 24 additions & 0 deletions packages/integration/src/processVanillaFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ describe('serializeVanillaModule', () => {
`);
});

test('should handle function serialization', () => {
const component = { render: () => null };

Object.defineProperty(component, '__function_serializer__', {
value: {
importPath: 'my-package',
importName: 'myFunction',
args: ['arg1', 'arg2'],
},
writable: false,
});

const exports = {
component,
};

expect(serializeVanillaModule(['import "./styles.css"'], exports, null))
.toMatchInlineSnapshot(`
"import "./styles.css"
import { myFunction as _86bce } from 'my-package';
export var component = _86bce('arg1','arg2');"
`);
});

test('should re-use exports in handle serialized function args', () => {
const complexExport = {
my: {
Expand Down
62 changes: 36 additions & 26 deletions packages/integration/src/processVanillaFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { IdentifierOption } from './types';
const originalNodeEnv = process.env.NODE_ENV;

// Copied from https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore/blob/51f83bd3db728fd7ee177de1ffc253fdb99c537f/README.md#_isplainobject
function isPlainObject(value: unknown) {
function isPlainObject(value: unknown): value is object {
if (typeof value !== 'object' || value === null) {
return false;
}
Expand All @@ -36,6 +36,16 @@ function isPlainObject(value: unknown) {
);
}

function isExoticReactComponent(
value: unknown,
): value is React.ExoticComponent {
return (
isPlainObject(value) &&
'render' in value &&
typeof value.render === 'function'
);
}

export function stringifyFileScope({
packageName,
filePath,
Expand Down Expand Up @@ -186,7 +196,7 @@ export async function processVanillaFile({

function stringifyExports(
functionSerializationImports: Set<string>,
value: any,
value: unknown,
unusedCompositionRegex: RegExp | null,
key: string,
exportLookup: Map<any, string>,
Expand All @@ -206,31 +216,8 @@ function stringifyExports(
return next(value);
}

if (Array.isArray(value) || isPlainObject(value)) {
const reusedExport = exportLookup.get(value);

if (reusedExport && reusedExport !== key) {
exportDependencyGraph.addDependency(key, reusedExport);
return reusedExport;
}
return next(value);
}

if (Symbol.toStringTag in Object(value)) {
const { [Symbol.toStringTag]: _tag, ...valueWithoutTag } = value;
return next(valueWithoutTag);
}

if (valueType === 'string') {
return next(
unusedCompositionRegex
? value.replace(unusedCompositionRegex, '')
: value,
);
}

if (
valueType === 'function' &&
(valueType === 'function' || isExoticReactComponent(value)) &&
(value.__function_serializer__ || value.__recipe__)
) {
const { importPath, importName, args } =
Expand Down Expand Up @@ -273,6 +260,29 @@ function stringifyExports(
}
}

if (Array.isArray(value) || isPlainObject(value)) {
const reusedExport = exportLookup.get(value);

if (reusedExport && reusedExport !== key) {
exportDependencyGraph.addDependency(key, reusedExport);
return reusedExport;
}
return next(value);
}

if (Symbol.toStringTag in Object(value)) {
const { [Symbol.toStringTag]: _tag, ...valueWithoutTag } = value;
return next(valueWithoutTag);
}

if (valueType === 'string') {
return next(
unusedCompositionRegex
? value.replace(unusedCompositionRegex, '')
: value,
);
}

throw new Error(dedent`
Invalid exports.

Expand Down