-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[zero][system] Create jest transformer to test
zero-runtime styled components
- Loading branch information
1 parent
7d063c0
commit 92e90fc
Showing
4 changed files
with
417 additions
and
68 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @mui/zero-jest | ||
|
||
Jest transformer for zero-runtime styled testing support. |
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,56 @@ | ||
{ | ||
"name": "@mui/zero-jest", | ||
"version": "0.0.1-alpha.1", | ||
"private": true, | ||
"author": "MUI Team", | ||
"description": "Jest transformer for zero-runtime styled testing support.", | ||
"main": "./src/index.ts", | ||
"keywords": [ | ||
"zero runtime", | ||
"css-in-js", | ||
"mui", | ||
"jest" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mui/material-ui.git", | ||
"directory": "packages/zero-jest" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mui/material-ui/issues" | ||
}, | ||
"homepage": "@TODO", | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/mui" | ||
}, | ||
"scripts": { | ||
"build": "yarn build:legacy && yarn build:modern && yarn build:node && yarn build:stable && yarn build:copy-files", | ||
"build:legacy": "node ../../scripts/build.mjs legacy", | ||
"build:modern": "node ../../scripts/build.mjs modern", | ||
"build:node": "node ../../scripts/build.mjs node", | ||
"build:stable": "node ../../scripts/build.mjs stable", | ||
"build:copy-files": "node ../../scripts/copyFiles.mjs", | ||
"prebuild": "rimraf build tsconfig.build.tsbuildinfo", | ||
"release": "yarn build && npm publish build", | ||
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/zero-babel-plugin/**/*.test.{js,ts,tsx}'" | ||
}, | ||
"dependencies": { | ||
"@linaria/babel-preset": "^4.5.4", | ||
"@linaria/utils": "^4.5.3", | ||
"@mui/zero-tag-processor": "0.0.1-alpha.1", | ||
"babel-jest": "^29.7.0" | ||
}, | ||
"devDependencies": { | ||
"@jest/transform": "^29.7.0" | ||
}, | ||
"peerDependencies": {}, | ||
"sideEffects": false, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"engines": { | ||
"node": ">=12.0.0" | ||
} | ||
} |
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,83 @@ | ||
import { TransformCacheCollection, transform as linariaTransform } from '@linaria/babel-preset'; | ||
import { syncResolve, asyncResolveFallback } from '@linaria/utils'; | ||
import babelJest from 'babel-jest'; | ||
|
||
const transformCache = new TransformCacheCollection(); | ||
|
||
function getLinariaConfig(filename, options) { | ||
const { theme = {}, cssVariablesPrefix = 'mui' } = options ?? {}; | ||
return { | ||
filename, | ||
pluginOptions: { | ||
themeArgs: { | ||
theme, | ||
}, | ||
cssVariablesPrefix, | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* @typedef {Object} TransformerConfig | ||
* @property {unknown} theme | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {TransformerConfig} userOptions | ||
* @returns | ||
*/ | ||
function createZeroTransformer(userOptions) { | ||
const { theme, cssVariablesPrefix = 'mui', ...rest } = userOptions; | ||
const babelTransformer = babelJest.createTransformer({ | ||
...rest, | ||
plugins: (userOptions?.rest ?? []).concat('@mui/zero-tag-processor/pre-linaria-plugin'), | ||
}); | ||
// eslint-disable-next-line global-require | ||
const { transformSync: linariaTransformSync } = require('@linaria/babel-preset/lib/transform'); | ||
|
||
/** @type {import("@jest/transform").Transformer<TransformerConfig>} */ | ||
const zeroTransformer = { | ||
...babelTransformer, | ||
process(sourceText, sourcePath, options) { | ||
const { code } = babelTransformer.process(sourceText, sourcePath, options); | ||
const linariaResult = linariaTransformSync( | ||
code ?? sourceText, | ||
getLinariaConfig(sourcePath, { | ||
theme, | ||
cssVariablesPrefix, | ||
}), | ||
syncResolve, | ||
{}, | ||
transformCache, | ||
); | ||
return { | ||
code: linariaResult?.code ?? sourceText, | ||
map: linariaResult?.map, | ||
}; | ||
}, | ||
async processAsync(sourceText, sourcePath, options) { | ||
const { code } = babelTransformer.processAsync(sourceText, sourcePath, options); | ||
const linariaResult = await linariaTransform( | ||
code ?? sourceText, | ||
getLinariaConfig(sourcePath, { | ||
theme, | ||
cssVariablesPrefix, | ||
}), | ||
asyncResolveFallback, | ||
{}, | ||
transformCache, | ||
); | ||
return { | ||
code: linariaResult?.code ?? code ?? sourceText, | ||
map: linariaResult?.map, | ||
}; | ||
}, | ||
}; | ||
|
||
return zeroTransformer; | ||
} | ||
|
||
const transformerFactory = { createTransformer: createZeroTransformer }; | ||
|
||
export default transformerFactory; |
Oops, something went wrong.