-
-
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][vite] Create a package for vite plugin
This internally wraps linaria's vite plugin with ability to generate theme CSS file as well.
- Loading branch information
1 parent
34c6464
commit 243469d
Showing
12 changed files
with
432 additions
and
13 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
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 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,5 @@ | ||
{ | ||
"rules": { | ||
"@typescript-eslint/consistent-type-imports": "error" | ||
} | ||
} |
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-vite-plugin | ||
|
||
Vite plugin to support MUI's `styled` processor. |
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,60 @@ | ||
{ | ||
"name": "@mui/zero-vite-plugin", | ||
"version": "0.0.1-alpha.0", | ||
"private": true, | ||
"author": "MUI Team", | ||
"description": "Vite plugin for MUI zero styled implementation.", | ||
"main": "./src/index.ts", | ||
"keywords": [ | ||
"zero runtime", | ||
"css-in-js", | ||
"mui" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mui/material-ui.git", | ||
"directory": "packages/zero-vite-plugin" | ||
}, | ||
"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:types && 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", | ||
"build:types": "node ../../scripts/buildTypes.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}'", | ||
"typescript": "tslint -p tsconfig.json \"{src,test}/**/*.{spec,d}.{ts,tsx}\" && tsc -p tsconfig.json", | ||
"typescript:module-augmentation": "node scripts/testModuleAugmentation.js" | ||
}, | ||
"dependencies": { | ||
"@babel/core": "^7.22.10", | ||
"@linaria/vite": "^4.5.4", | ||
"@mui/zero-tag-processor": "0.0.1-alpha.0" | ||
}, | ||
"devDependencies": { | ||
"@types/babel__core": "^7.20.1", | ||
"vite": "^4.4.9" | ||
}, | ||
"peerDependencies": { | ||
"vite": "^4.0.0" | ||
}, | ||
"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,89 @@ | ||
import type { PluginOption } from 'vite'; | ||
import { generateCss } from '@mui/zero-tag-processor/generateCss'; | ||
import { transformAsync } from '@babel/core'; | ||
import linaria from '@linaria/vite'; | ||
|
||
type LinariaOptions = Exclude<Parameters<typeof linaria>[0], undefined>; | ||
|
||
export interface ZeroVitePluginOptions extends LinariaOptions { | ||
/** | ||
* An object of the themes that you want passed in as an argument in the callback argument of `styled`. | ||
*/ | ||
themeArgs?: Record<string, unknown>; | ||
/** | ||
* Prefix string to use in the generated css variables. | ||
*/ | ||
cssVariablesPrefix?: string; | ||
/** | ||
* Whether the css variables for the default theme should target the :root selector or not. | ||
* @default true | ||
*/ | ||
injectDefaultThemeInRoot?: boolean; | ||
} | ||
|
||
export function zeroVitePlugin(options?: ZeroVitePluginOptions): PluginOption { | ||
const { | ||
cssVariablesPrefix = 'mui', | ||
themeArgs = {}, | ||
injectDefaultThemeInRoot = true, | ||
} = options ?? {}; | ||
|
||
function injectMUITokensPlugin(): PluginOption { | ||
return { | ||
name: 'vite-mui-theme-injection-plugin', | ||
load(id) { | ||
if (id.endsWith('@mui/zero-runtime/styles.css')) { | ||
return { | ||
code: generateCss( | ||
{ | ||
cssVariablesPrefix, | ||
themeArgs, | ||
}, | ||
{ | ||
defaultThemeKey: 'theme', | ||
injectInRoot: injectDefaultThemeInRoot, | ||
}, | ||
), | ||
map: null, | ||
}; | ||
} | ||
return null; | ||
}, | ||
}; | ||
} | ||
const extensions = ['.ts', '.tsx', '.js', '.jsx', '.mts', '.mjs', '.cts', '.cjs', '.mtsx']; | ||
|
||
function intermediateBabelPlugin(): PluginOption { | ||
return { | ||
name: 'vite-intermediate-plugin', | ||
async transform(code, id) { | ||
const [filename] = id.split('?'); | ||
if (!extensions.some((ext) => filename.endsWith(ext))) { | ||
return null; | ||
} | ||
const result = await transformAsync(code, { | ||
filename, | ||
babelrc: false, | ||
configFile: false, | ||
plugins: [['@mui/zero-tag-processor/pre-linaria-plugin']], | ||
}); | ||
return { | ||
code: result?.code ?? code, | ||
map: result?.map, | ||
}; | ||
}, | ||
}; | ||
} | ||
|
||
const linariaPlugin = linaria({ | ||
cssVariablesPrefix, | ||
themeArgs, | ||
...options, | ||
babelOptions: { | ||
...options?.babelOptions, | ||
plugins: ['@babel/plugin-syntax-jsx'], | ||
}, | ||
}); | ||
|
||
return [injectMUITokensPlugin(), intermediateBabelPlugin(), linariaPlugin]; | ||
} |
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,21 @@ | ||
{ | ||
// This config is for emitting declarations (.d.ts) only | ||
// Actual .ts source files are transpiled via babel | ||
"extends": "./tsconfig", | ||
"compilerOptions": { | ||
"target": "ES2015", | ||
"composite": true, | ||
"declaration": true, | ||
"noEmit": false, | ||
"emitDeclarationOnly": true, | ||
"outDir": "build", | ||
"rootDir": "./src" | ||
}, | ||
"include": ["./src/**/*.ts*"], | ||
"exclude": ["src/**/*.spec.ts*", "src/**/*.test.ts*"], | ||
"references": [ | ||
{ "path": "../mui-system/tsconfig.build.json" }, | ||
{ "path": "../mui-material/tsconfig.build.json" }, | ||
{ "path": "../zero-tag-processor/tsconfig.build.json" } | ||
] | ||
} |
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig", | ||
"include": ["src/**/*", "test/**/*"], | ||
"compilerOptions": { | ||
"lib": ["ES2022", "DOM"], | ||
"target": "ES2015", | ||
"types": ["mocha", "node", "chai"] | ||
} | ||
} |
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
Oops, something went wrong.