-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
feat: Create utils package to share logic across runtime and bundler #345
Open
brijeshb42
wants to merge
7
commits into
mui:master
Choose a base branch
from
brijeshb42:v1/utils-package
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c331ce
feat: Create utils package to share logic across runtime and bundler …
93e0cdd
Fix typescript issue
808fe23
Review fixes
d597937
Remove individual licenses
a7b193d
Add runtime path replacement config
90119d6
Implement evaluateExpression
9f01dd5
Fix test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ docs/public/static/blog/feed/* | |
# vale downloaded config | ||
.github/styles/ | ||
.nx/cache | ||
packages/**/LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,5 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"target": "ES2015", | ||
"allowJs": true, | ||
"lib": ["ES2017", "ES2021.String", "DOM"], | ||
"composite": true, | ||
"noEmit": false, | ||
"resolveJsonModule": true, | ||
"types": ["node", "mocha"], | ||
"jsx": "react-jsx" | ||
}, | ||
"include": [ | ||
"src/**/*.tsx", | ||
"src/**/*.js", | ||
"src/**/*.ts", | ||
"tests/**/*.spec.ts", | ||
"tests/**/*.spec.tsx" | ||
], | ||
"include": ["src/**/*.js", "src/**/*.ts", "tests/**/*.spec.ts"], | ||
"exclude": ["./tsup.config.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"composite": false, | ||
"paths": { | ||
"@babel/core": ["./node_modules/@babel/core"] | ||
} | ||
"composite": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
{ | ||
"name": "@pigment-css/utils", | ||
"version": "0.0.28", | ||
"main": "build/index.js", | ||
"module": "build/index.mjs", | ||
"types": "build/index.d.ts", | ||
"author": "MUI Team", | ||
"description": "Utilities to be used internally across different Pigment CSS packages.", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mui/pigment-css.git", | ||
"directory": "packages/pigment-css-utils" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mui/pigment-css/issues" | ||
}, | ||
"homepage": "https://github.com/mui/pigment-css/tree/master/README.md", | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/mui-org" | ||
}, | ||
"scripts": { | ||
"clean": "rimraf build types processors utils", | ||
"watch": "tsup --watch --clean false", | ||
"copy-license": "node ../../scripts/pigment-license.mjs", | ||
"build": "tsup", | ||
"test": "cd ../../ && cross-env NODE_ENV=test BABEL_ENV=coverage nyc --reporter=text mocha 'packages/pigment-css-utils/**/*.test.{js,ts,tsx}'", | ||
"test:update": "cd ../../ && cross-env NODE_ENV=test UPDATE_FIXTURES=true mocha 'packages/pigment-css-utils/**/*.test.{js,ts,tsx}'", | ||
"test:ci": "cd ../../ && cross-env NODE_ENV=test BABEL_ENV=coverage nyc --reporter=lcov --report-dir=./coverage/pigment-css-utils mocha 'packages/pigment-css-utils/**/*.test.{js,ts,tsx}'", | ||
"typescript": "tsc --noEmit -p ." | ||
}, | ||
"dependencies": { | ||
"@babel/types": "^7.26.3", | ||
"@babel/parser": "^7.26.3", | ||
"@emotion/unitless": "0.10.0", | ||
"@emotion/serialize": "^1.3.3", | ||
"@pigment-css/theme": "workspace:*", | ||
"@wyw-in-js/processor-utils": "^0.5.5", | ||
"@wyw-in-js/shared": "^0.5.5", | ||
"@wyw-in-js/transform": "^0.5.5", | ||
"cssesc": "^3.0.0", | ||
"lodash": "4.17.21" | ||
}, | ||
"devDependencies": { | ||
"@types/cssesc": "3.0.2", | ||
"chai": "^4.4.1" | ||
}, | ||
"sideEffects": false, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"files": [ | ||
"src", | ||
"build", | ||
"package.json", | ||
"LICENSE" | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./build/index.d.ts", | ||
"import": { | ||
"types": "./build/index.d.mts", | ||
"default": "./build/index.mjs" | ||
}, | ||
"require": "./build/index.js", | ||
"default": "./build/index.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"nx": { | ||
"targets": { | ||
"test": { | ||
"cache": false, | ||
"dependsOn": [ | ||
"build" | ||
] | ||
}, | ||
"test:update": { | ||
"cache": false, | ||
"dependsOn": [ | ||
"build" | ||
] | ||
}, | ||
"test:ci": { | ||
"cache": false, | ||
"dependsOn": [ | ||
"build" | ||
] | ||
}, | ||
"build": { | ||
"outputs": [ | ||
"{projectRoot}/build" | ||
], | ||
"dependsOn": [ | ||
"^build" | ||
] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { BaseProcessor as WywBaseProcessor } from '@wyw-in-js/processor-utils'; | ||
|
||
/** | ||
* This is going to be expanded when the react package comes into picture. | ||
* Right now, it only has the bare mimimum. | ||
*/ | ||
export default abstract class BaseProcessor extends WywBaseProcessor {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,102 @@ | ||||||
import { Theme } from '@pigment-css/theme'; | ||||||
import { PluginOptions } from '@wyw-in-js/transform'; | ||||||
|
||||||
export type GenerateClassData<M, E> = { | ||||||
/** | ||||||
* Original classname that would be used if not customized. | ||||||
*/ | ||||||
slug: string; | ||||||
/** | ||||||
* The metadata argument that you passed to the function call, usually as the 2nd argument. | ||||||
* | ||||||
* ```js | ||||||
* const className = css(cssObject, metadata); | ||||||
*/ | ||||||
metadata: M; | ||||||
/** | ||||||
* The variable name that the function call's return value is assigned to. | ||||||
*/ | ||||||
displayName: string; | ||||||
/** | ||||||
* The name of the function that is being called, ie, `css`, `styled`, etc | ||||||
*/ | ||||||
functionName: string; | ||||||
/** | ||||||
* All the extra data specific to the above `functionName` call. | ||||||
*/ | ||||||
extraData: E; | ||||||
}; | ||||||
|
||||||
/** | ||||||
* Feature flags that user can choose to enable/disable to control the output | ||||||
*/ | ||||||
type PigmentFeature = { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nit |
||||||
useLayer?: boolean; | ||||||
}; | ||||||
|
||||||
/** | ||||||
* This is the base Pigment Config that'll be used by bundler package with some extra bundler specific options. | ||||||
*/ | ||||||
export type PigmentConfig = Omit<Partial<PluginOptions>, 'features'> & { | ||||||
wywFeatures?: PluginOptions['features']; | ||||||
features?: PigmentFeature; | ||||||
themeArgs?: { | ||||||
theme: Theme; | ||||||
}; | ||||||
/** | ||||||
* | ||||||
* @param {string} tag The function that was imported | ||||||
* @param {string} source The path that the function was imported from | ||||||
* | ||||||
* If returning a string, this will be used as the import path in the final transform. | ||||||
* | ||||||
* @example | ||||||
* | ||||||
* __Input Code__ | ||||||
* | ||||||
* ```js | ||||||
* import { css } from '@pigment-css/core'; | ||||||
* | ||||||
* const cls1 = css({}); | ||||||
* ``` | ||||||
* __config__ | ||||||
* ```js | ||||||
* { | ||||||
* runtimeReplacementPath(tag, source) { | ||||||
* if (tag === 'css') { | ||||||
* return `@my-lib/runtime/css`; | ||||||
* } | ||||||
* return null; | ||||||
* } | ||||||
* } | ||||||
*``` | ||||||
* __Output__ | ||||||
* | ||||||
* ```js | ||||||
* import { css } from '@my-lib/runtime/css'; | ||||||
* | ||||||
* const cls1 = css({}); | ||||||
* ``` | ||||||
*/ | ||||||
runtimeReplacementPath?: (tag: string, source: string) => string | null; | ||||||
}; | ||||||
|
||||||
/** | ||||||
* @internal | ||||||
*/ | ||||||
export type TransformedInternalConfig = Omit<PigmentConfig, 'wywFeatures' | 'features'> & { | ||||||
feautres?: PluginOptions['features']; | ||||||
pigmentFeatures?: PigmentFeature; | ||||||
}; | ||||||
|
||||||
/** | ||||||
* Internal utility to convert Pigment CSS configuration object to be usable by WyW | ||||||
*/ | ||||||
export function transformPigmentConfig(config?: PigmentConfig): TransformedInternalConfig { | ||||||
const { features, wywFeatures, ...rest } = config ?? {}; | ||||||
return { | ||||||
...rest, | ||||||
pigmentFeatures: features, | ||||||
feautres: wywFeatures, | ||||||
}; | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default as BaseProcessor } from './base-processor'; | ||
export * from './config'; | ||
export * from './utils'; |
11 changes: 11 additions & 0 deletions
11
packages/pigment-css-utils/src/utils/evaluateExpresions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Slightly unsafe but way faster way to evaluate JS code than using babel transforms. | ||
* | ||
* @param expressionString The JS code expression to evaluate | ||
*/ | ||
export function evaluateClassNameArg<T>(expressionString: string): T { | ||
// Create sandbox context | ||
const context = Object.create(null); | ||
const safeEval = new Function('context', `with(context) { return ${expressionString}; }`); | ||
return safeEval(context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './processStyle'; | ||
export * from './valueToLiteral'; | ||
export * from './parseExpressions'; | ||
export * from './evaluateExpresions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { parseExpression, ParserOptions } from '@babel/parser'; | ||
import { ArrayExpression } from '@babel/types'; | ||
|
||
/** | ||
* Parses array expression string to AST for locating sourcemap points. | ||
*/ | ||
export function parseArray( | ||
expressionStr: string, | ||
opts: Pick<ParserOptions, 'startColumn' | 'startIndex' | 'startLine'>, | ||
): ArrayExpression | null { | ||
const expr = parseExpression(expressionStr, opts); | ||
if (expr.type === 'ArrayExpression') { | ||
return expr; | ||
} | ||
return null; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without it, it was typechecking for stuff inside
unplugin
package that does not affect our package.This check can be safely ignored since at the moment we don't care about farm/rolldown/rspack etc.