From e6cb4a16ad455b59299aabb0a0290e3bcc3df870 Mon Sep 17 00:00:00 2001 From: siriwatknp Date: Tue, 28 May 2024 11:29:41 -0400 Subject: [PATCH] add createExtendSxProp --- .../exports/createExtendSxProp.js | 5 +++ packages/pigment-css-react/package.json | 3 +- .../src/createExtendSxProp.d.ts | 1 + .../src/createExtendSxProp.js | 12 +++++++ packages/pigment-css-react/src/index.ts | 1 + .../src/processors/createExtendSxProp.ts | 35 +++++++++++++++++++ packages/pigment-css-react/tsup.config.ts | 10 +++++- 7 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 packages/pigment-css-react/exports/createExtendSxProp.js create mode 100644 packages/pigment-css-react/src/createExtendSxProp.d.ts create mode 100644 packages/pigment-css-react/src/createExtendSxProp.js create mode 100644 packages/pigment-css-react/src/processors/createExtendSxProp.ts diff --git a/packages/pigment-css-react/exports/createExtendSxProp.js b/packages/pigment-css-react/exports/createExtendSxProp.js new file mode 100644 index 00000000..b459d9d7 --- /dev/null +++ b/packages/pigment-css-react/exports/createExtendSxProp.js @@ -0,0 +1,5 @@ +Object.defineProperty(exports, '__esModule', { + value: true, +}); + +exports.default = require('../processors/createExtendSxProp').CreateExtendSxPropProcessor; diff --git a/packages/pigment-css-react/package.json b/packages/pigment-css-react/package.json index 4c5b47ed..f6dde337 100644 --- a/packages/pigment-css-react/package.json +++ b/packages/pigment-css-react/package.json @@ -85,7 +85,8 @@ "keyframes": "./exports/keyframes.js", "generateAtomics": "./exports/generateAtomics.js", "css": "./exports/css.js", - "createUseThemeProps": "./exports/createUseThemeProps.js" + "createUseThemeProps": "./exports/createUseThemeProps.js", + "createExtendSxProp": "./exports/createExtendSxProp.js" } }, "files": [ diff --git a/packages/pigment-css-react/src/createExtendSxProp.d.ts b/packages/pigment-css-react/src/createExtendSxProp.d.ts new file mode 100644 index 00000000..11c2b7ff --- /dev/null +++ b/packages/pigment-css-react/src/createExtendSxProp.d.ts @@ -0,0 +1 @@ +export default function createExtendSxProp(): >(props: T) => T; diff --git a/packages/pigment-css-react/src/createExtendSxProp.js b/packages/pigment-css-react/src/createExtendSxProp.js new file mode 100644 index 00000000..eefca5b3 --- /dev/null +++ b/packages/pigment-css-react/src/createExtendSxProp.js @@ -0,0 +1,12 @@ +/** + * Runtime function for creating `extendSxProp`. + * + * This is a placeholder for the runtime function for replacing the `extendSxProp` in Material UI + * to reduce bundle size only. DO NOT use it in your codebase. + */ +function extendSxProp(props) { + return props; +} +export default function createExtendSxProp() { + return extendSxProp; +} diff --git a/packages/pigment-css-react/src/index.ts b/packages/pigment-css-react/src/index.ts index 2b27d96f..b2766401 100644 --- a/packages/pigment-css-react/src/index.ts +++ b/packages/pigment-css-react/src/index.ts @@ -4,4 +4,5 @@ export { default as keyframes } from './keyframes'; export { generateAtomics, atomics } from './generateAtomics'; export { default as css } from './css'; export { default as createUseThemeProps } from './createUseThemeProps'; +export { default as createExtendSxProp } from './createExtendSxProp'; export { default as Box } from './Box'; diff --git a/packages/pigment-css-react/src/processors/createExtendSxProp.ts b/packages/pigment-css-react/src/processors/createExtendSxProp.ts new file mode 100644 index 00000000..9767bf12 --- /dev/null +++ b/packages/pigment-css-react/src/processors/createExtendSxProp.ts @@ -0,0 +1,35 @@ +import type { Expression, Params, TailProcessorParams } from '@wyw-in-js/processor-utils'; +import BaseProcessor from './base-processor'; + +export class CreateExtendSxPropProcessor extends BaseProcessor { + constructor(params: Params, ...args: TailProcessorParams) { + super([params[0]], ...args); + } + + // eslint-disable-next-line class-methods-use-this + build(): void {} + + doEvaltimeReplacement(): void { + this.replacer(this.value, false); + } + + get value(): Expression { + return this.astService.nullLiteral(); + } + + doRuntimeReplacement(): void { + const t = this.astService; + + const extendSxPropImportIdentifier = t.addNamedImport( + this.tagSource.imported, + process.env.PACKAGE_NAME as string, + ); + + this.replacer(t.callExpression(extendSxPropImportIdentifier, []), true); + } + + public override get asSelector(): string { + // For completeness, this is not intended to be used. + return `.${this.className}`; + } +} diff --git a/packages/pigment-css-react/tsup.config.ts b/packages/pigment-css-react/tsup.config.ts index 5fd52716..272fbc45 100644 --- a/packages/pigment-css-react/tsup.config.ts +++ b/packages/pigment-css-react/tsup.config.ts @@ -1,7 +1,15 @@ import { Options, defineConfig } from 'tsup'; import config from '../../tsup.config'; -const processors = ['styled', 'sx', 'keyframes', 'generateAtomics', 'css', 'createUseThemeProps']; +const processors = [ + 'styled', + 'sx', + 'keyframes', + 'generateAtomics', + 'css', + 'createUseThemeProps', + 'createExtendSxProp', +]; const external = ['react', 'react-is', 'prop-types']; const baseConfig: Options = {