Skip to content

Commit 65364ca

Browse files
brijeshb42Brijesh Bittu
and
Brijesh Bittu
authored
feat: Create utils package to share logic across runtime and bundler (#345)
Co-authored-by: Brijesh Bittu <[email protected]>
1 parent 46f2088 commit 65364ca

31 files changed

+1120
-130
lines changed

.codesandbox/ci.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
"installCommand": "install:codesandbox",
44
"node": "18",
55
"packages": [
6+
"packages/pigment-css-nextjs-plugin",
67
"packages/pigment-css-react",
8+
"packages/pigment-css-theme",
79
"packages/pigment-css-unplugin",
8-
"packages/pigment-css-nextjs-plugin",
10+
"packages/pigment-css-utils",
911
"packages/pigment-css-vite-plugin"
1012
],
1113
"sandboxes": [

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ docs/public/static/blog/feed/*
3030
# vale downloaded config
3131
.github/styles/
3232
.nx/cache
33+
packages/**/LICENSE

packages/eslint-plugin-material-ui/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@
99
"@typescript-eslint/experimental-utils": "^5.62.0",
1010
"@typescript-eslint/parser": "^7.5.0"
1111
},
12-
"scripts": {
13-
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/eslint-plugin-material-ui/**/*.test.js' --timeout 3000"
14-
},
1512
"license": "MIT"
1613
}

packages/pigment-css-nextjs-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"watch": "tsup --watch --tsconfig tsconfig.build.json",
2626
"copy-license": "node ../../scripts/pigment-license.mjs",
2727
"build": "tsup --tsconfig tsconfig.build.json",
28-
"typecheck": "tsc --noEmit -p ."
28+
"typescript": "tsc --noEmit -p ."
2929
},
3030
"dependencies": {
3131
"@pigment-css/unplugin": "workspace:^"

packages/pigment-css-nextjs-plugin/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
4-
"resolveJsonModule": true,
5-
"target": "ES2015"
4+
"skipLibCheck": true
65
},
76
"include": ["src/**/*.ts"],
87
"exclude": ["./tsup.config.ts"]

packages/pigment-css-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@babel/helper-module-imports": "^7.25.9",
3636
"@babel/helper-plugin-utils": "^7.25.9",
3737
"@babel/parser": "^7.26.2",
38-
"@babel/types": "^7.26.0",
38+
"@babel/types": "^7.26.5",
3939
"@emotion/css": "^11.13.4",
4040
"@emotion/is-prop-valid": "^1.3.1",
4141
"@emotion/react": "^11.13.3",

packages/pigment-css-theme/LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
{
22
"extends": "../../tsconfig.json",
3-
"compilerOptions": {
4-
"target": "ES2015",
5-
"allowJs": true,
6-
"lib": ["ES2017", "ES2021.String", "DOM"],
7-
"composite": true,
8-
"noEmit": false,
9-
"resolveJsonModule": true,
10-
"types": ["node", "mocha"],
11-
"jsx": "react-jsx"
12-
},
13-
"include": [
14-
"src/**/*.tsx",
15-
"src/**/*.js",
16-
"src/**/*.ts",
17-
"tests/**/*.spec.ts",
18-
"tests/**/*.spec.tsx"
19-
],
3+
"include": ["src/**/*.js", "src/**/*.ts", "tests/**/*.spec.ts"],
204
"exclude": ["./tsup.config.ts"]
215
}

packages/pigment-css-unplugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"watch": "tsup --watch --tsconfig tsconfig.build.json",
2626
"copy-license": "node ../../scripts/pigment-license.mjs",
2727
"build": "tsup --tsconfig tsconfig.build.json",
28-
"typecheck": "tsc --noEmit -p .",
28+
"typescript": "tsc --noEmit -p .",
2929
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/pigment-css-unplugin/**/*.test.{js,ts,tsx}'",
3030
"test:ci": "cd ../../ && cross-env NODE_ENV=test BABEL_ENV=coverage nyc --reporter=lcov --report-dir=./coverage/pigment-css-unplugin mocha 'packages/pigment-css-unplugin/**/*.test.{js,ts,tsx}'"
3131
},
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"composite": false,
5-
"paths": {
6-
"@babel/core": ["./node_modules/@babel/core"]
7-
}
4+
"composite": false
85
}
96
}

packages/pigment-css-unplugin/tsconfig.json

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
{
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
4-
"resolveJsonModule": true,
5-
"target": "ES2022",
6-
"lib": ["ES2021", "DOM"],
7-
"paths": {
8-
"@babel/core": ["./node_modules/@babel/core"],
9-
"@pigment-css/react": ["./packages/pigment-css-react/src"],
10-
"@pigment-css/react/*": ["./packages/pigment-css-react/src/*"]
11-
},
12-
"types": ["node", "mocha", "chai"]
4+
"skipLibCheck": true
135
},
146
"include": ["src/**/*.ts"],
157
"exclude": ["./tsup.config.ts"]
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"name": "@pigment-css/utils",
3+
"version": "0.0.28",
4+
"main": "build/index.js",
5+
"module": "build/index.mjs",
6+
"types": "build/index.d.ts",
7+
"author": "MUI Team",
8+
"description": "Utilities to be used internally across different Pigment CSS packages.",
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/mui/pigment-css.git",
12+
"directory": "packages/pigment-css-utils"
13+
},
14+
"license": "MIT",
15+
"bugs": {
16+
"url": "https://github.com/mui/pigment-css/issues"
17+
},
18+
"homepage": "https://github.com/mui/pigment-css/tree/master/README.md",
19+
"funding": {
20+
"type": "opencollective",
21+
"url": "https://opencollective.com/mui-org"
22+
},
23+
"scripts": {
24+
"clean": "rimraf build types processors utils",
25+
"watch": "tsup --watch --clean false",
26+
"copy-license": "node ../../scripts/pigment-license.mjs",
27+
"build": "tsup",
28+
"test": "cd ../../ && cross-env NODE_ENV=test BABEL_ENV=coverage nyc --reporter=text mocha 'packages/pigment-css-utils/**/*.test.{js,ts,tsx}'",
29+
"test:update": "cd ../../ && cross-env NODE_ENV=test UPDATE_FIXTURES=true mocha 'packages/pigment-css-utils/**/*.test.{js,ts,tsx}'",
30+
"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}'",
31+
"typescript": "tsc --noEmit -p ."
32+
},
33+
"dependencies": {
34+
"@babel/types": "^7.26.5",
35+
"@babel/parser": "^7.26.5",
36+
"@emotion/unitless": "0.10.0",
37+
"@emotion/serialize": "^1.3.3",
38+
"@pigment-css/theme": "workspace:*",
39+
"@wyw-in-js/processor-utils": "^0.5.5",
40+
"@wyw-in-js/shared": "^0.5.5",
41+
"@wyw-in-js/transform": "^0.5.5",
42+
"cssesc": "^3.0.0",
43+
"lodash": "4.17.21"
44+
},
45+
"devDependencies": {
46+
"@types/cssesc": "3.0.2",
47+
"chai": "^4.4.1"
48+
},
49+
"sideEffects": false,
50+
"publishConfig": {
51+
"access": "public"
52+
},
53+
"files": [
54+
"src",
55+
"build",
56+
"package.json",
57+
"LICENSE"
58+
],
59+
"exports": {
60+
".": {
61+
"types": "./build/index.d.ts",
62+
"import": {
63+
"types": "./build/index.d.mts",
64+
"default": "./build/index.mjs"
65+
},
66+
"require": "./build/index.js",
67+
"default": "./build/index.js"
68+
},
69+
"./package.json": "./package.json"
70+
},
71+
"nx": {
72+
"targets": {
73+
"test": {
74+
"cache": false,
75+
"dependsOn": [
76+
"build"
77+
]
78+
},
79+
"test:update": {
80+
"cache": false,
81+
"dependsOn": [
82+
"build"
83+
]
84+
},
85+
"test:ci": {
86+
"cache": false,
87+
"dependsOn": [
88+
"build"
89+
]
90+
},
91+
"build": {
92+
"outputs": [
93+
"{projectRoot}/build"
94+
],
95+
"dependsOn": [
96+
"^build"
97+
]
98+
}
99+
}
100+
}
101+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { BaseProcessor as WywBaseProcessor } from '@wyw-in-js/processor-utils';
2+
3+
/**
4+
* This is going to be expanded when the react package comes into picture.
5+
* Right now, it only has the bare mimimum.
6+
*/
7+
export default abstract class BaseProcessor extends WywBaseProcessor {}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { Theme } from '@pigment-css/theme';
2+
import { PluginOptions } from '@wyw-in-js/transform';
3+
4+
export type GenerateClassData<M, E> = {
5+
/**
6+
* Original classname that would be used if not customized.
7+
*/
8+
slug: string;
9+
/**
10+
* The metadata argument that you passed to the function call, usually as the 2nd argument.
11+
*
12+
* ```js
13+
* const className = css(cssObject, metadata);
14+
*/
15+
metadata: M;
16+
/**
17+
* The variable name that the function call's return value is assigned to.
18+
*/
19+
displayName: string;
20+
/**
21+
* The name of the function that is being called, ie, `css`, `styled`, etc
22+
*/
23+
functionName: string;
24+
/**
25+
* All the extra data specific to the above `functionName` call.
26+
*/
27+
extraData: E;
28+
};
29+
30+
/**
31+
* Feature flags that user can choose to enable/disable to control the output
32+
*/
33+
type PigmentFeatures = {
34+
useLayer?: boolean;
35+
};
36+
37+
/**
38+
* This is the base Pigment Config that'll be used by bundler package with some extra bundler specific options.
39+
*/
40+
export type PigmentConfig = Omit<Partial<PluginOptions>, 'features'> & {
41+
wywFeatures?: PluginOptions['features'];
42+
features?: PigmentFeatures;
43+
themeArgs?: {
44+
theme: Theme;
45+
};
46+
/**
47+
*
48+
* @param {string} tag The function that was imported
49+
* @param {string} source The path that the function was imported from
50+
*
51+
* If returning a string, this will be used as the import path in the final transform.
52+
*
53+
* @example
54+
*
55+
* __Input Code__
56+
*
57+
* ```js
58+
* import { css } from '@pigment-css/core';
59+
*
60+
* const cls1 = css({});
61+
* ```
62+
* __config__
63+
* ```js
64+
* {
65+
* runtimeReplacementPath(tag, source) {
66+
* if (tag === 'css') {
67+
* return `@my-lib/runtime/css`;
68+
* }
69+
* return null;
70+
* }
71+
* }
72+
*```
73+
* __Output__
74+
*
75+
* ```js
76+
* import { css } from '@my-lib/runtime/css';
77+
*
78+
* const cls1 = css({});
79+
* ```
80+
*/
81+
runtimeReplacementPath?: (tag: string, source: string) => string | null;
82+
};
83+
84+
/**
85+
* @internal
86+
*/
87+
export type TransformedInternalConfig = Omit<PigmentConfig, 'wywFeatures' | 'features'> & {
88+
feautres?: PluginOptions['features'];
89+
pigmentFeatures?: PigmentFeatures;
90+
};
91+
92+
/**
93+
* Internal utility to convert Pigment CSS configuration object to be usable by WyW
94+
*/
95+
export function transformPigmentConfig(config?: PigmentConfig): TransformedInternalConfig {
96+
const { features, wywFeatures, ...rest } = config ?? {};
97+
return {
98+
...rest,
99+
pigmentFeatures: features,
100+
feautres: wywFeatures,
101+
};
102+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default as BaseProcessor } from './base-processor';
2+
export * from './config';
3+
export * from './utils';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Slightly unsafe but way faster way to evaluate JS code than using babel transforms.
3+
*
4+
* @param expressionString The JS code expression to evaluate
5+
*/
6+
export function evaluateClassNameArg<T>(expressionString: string): T {
7+
// Create sandbox context
8+
const context = Object.create(null);
9+
const safeEval = new Function('context', `with(context) { return ${expressionString}; }`);
10+
return safeEval(context);
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './processStyle';
2+
export * from './valueToLiteral';
3+
export * from './parseExpressions';
4+
export * from './evaluateExpresions';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { parseExpression, ParserOptions } from '@babel/parser';
2+
import { ArrayExpression } from '@babel/types';
3+
4+
/**
5+
* Parses array expression string to AST for locating sourcemap points.
6+
*/
7+
export function parseArray(
8+
expressionStr: string,
9+
opts: Pick<ParserOptions, 'startColumn' | 'startIndex' | 'startLine'>,
10+
): ArrayExpression | null {
11+
const expr = parseExpression(expressionStr, opts);
12+
if (expr.type === 'ArrayExpression') {
13+
return expr;
14+
}
15+
return null;
16+
}

0 commit comments

Comments
 (0)