-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(babel-plugin): esm pkg expose * chore(babel-plugin): directory * feat(babel-plugin): add new option * feat: implmeent inject style * feat: add internal test plugin * fix(babel-plugin): injectGlobalStyle * chore: switch to pnpm * chore: ci
- Loading branch information
Showing
57 changed files
with
5,063 additions
and
5,505 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
Binary file not shown.
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,3 +1,3 @@ | ||
module.exports = require('eslint-config-kagura').nonzzz({ ts: true, jsx: true }, { | ||
module.exports = require('eslint-config-kagura').nonzzz({ ts: true, jsx: true, unusedImports: false }, { | ||
ignores: ['packages/**/output.js'] | ||
}) |
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 @@ | ||
import { defineVars } from '@stylexjs/stylex' | ||
|
||
export const colors = defineVars({ | ||
purple: 'purple' | ||
}) |
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,17 @@ | ||
{ | ||
"name": "@stylex-extend/vite-plugin", | ||
"type": "module", | ||
"types": "dist/index.d.ts", | ||
"module": "dist/index.js", | ||
"scripts": { | ||
"build": "tsup", | ||
"dev": "tsup --watch" | ||
}, | ||
"dependencies": { | ||
"@babel/core": "^7.23.9", | ||
"@types/babel__core": "^7.20.5" | ||
}, | ||
"exports": { | ||
".": "./dist/index.js" | ||
} | ||
} |
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,81 @@ | ||
import { transformAsync } from '@babel/core' | ||
import { type Plugin, ViteDevServer, searchForWorkspaceRoot } from 'vite' | ||
import stylexExtendBabelPlugin from '@stylex-extend/babel-plugin' | ||
import type { StylexExtendBabelPluginOptions } from '@stylex-extend/babel-plugin' | ||
|
||
const DEFINE_CSS_EXTRA = '\0stylex-extend' | ||
|
||
const DEFINE_CSS_EXTRA_CSS = DEFINE_CSS_EXTRA + '.css' | ||
|
||
const VITE_INTERNAL_CSS_PLUGIN_NAMES = ['vite:css', 'vite:css-post'] | ||
|
||
export function stylexExtendPlugin(options: StylexExtendBabelPluginOptions = {}): Plugin { | ||
let globalStyle = '' | ||
const viteCSSPlugins: Plugin[] = [] | ||
let viteServer: ViteDevServer | ||
return { | ||
name: 'stylex-extend', | ||
enforce: 'pre', | ||
buildStart() { | ||
globalStyle = '' | ||
}, | ||
configureServer(server) { | ||
viteServer = server | ||
}, | ||
resolveId(id) { | ||
if (id === DEFINE_CSS_EXTRA_CSS) return DEFINE_CSS_EXTRA_CSS | ||
}, | ||
load(id) { | ||
if (id === DEFINE_CSS_EXTRA_CSS) { | ||
return globalStyle | ||
} | ||
}, | ||
configResolved(config) { | ||
if (!options.unstable_moduleResolution) { | ||
options.unstable_moduleResolution = { | ||
type: 'commonJS', | ||
rootDir: searchForWorkspaceRoot(config.root) | ||
} | ||
} | ||
viteCSSPlugins.push(...config.plugins.filter(p => VITE_INTERNAL_CSS_PLUGIN_NAMES.includes(p.name))) | ||
viteCSSPlugins.sort((a, b) => a.name === 'vite:css' && b.name === 'vite:css-post' ? -1 : 1) | ||
}, | ||
async transform(code, id) { | ||
if (!/\.(mjs|js|ts|vue|jsx|tsx)(\?.*|)$/.test(id) || !code.includes('@stylex-extend')) return | ||
const res = await transformAsync(code, { babelrc: false, | ||
plugins: [stylexExtendBabelPlugin.withOptions({ | ||
...options | ||
})], | ||
filename: id }) | ||
if (!res) return | ||
if ('globalStyle' in res.metadata!) { | ||
globalStyle += res.metadata!.globalStyle | ||
} | ||
if (viteServer) { | ||
const module = viteServer.moduleGraph.getModuleById(DEFINE_CSS_EXTRA_CSS) | ||
if (module) { | ||
viteServer.moduleGraph.invalidateModule(module, new Set()) | ||
module.lastHMRTimestamp = Date.now() | ||
} | ||
} | ||
return { | ||
code: `import ${JSON.stringify(DEFINE_CSS_EXTRA_CSS)};\n${res.code}`, | ||
map: res.map! | ||
} | ||
}, | ||
async renderChunk(_, chunk) { | ||
const [plugin_1, plugin_2] = viteCSSPlugins | ||
|
||
for (const moudleId of chunk.moduleIds) { | ||
if (moudleId.includes(DEFINE_CSS_EXTRA_CSS)) { | ||
if (typeof plugin_1.transform === 'function' && typeof plugin_2.transform === 'function') { | ||
// @ts-expect-error | ||
const { code: css } = await plugin_1.transform.call(this, globalStyle, DEFINE_CSS_EXTRA_CSS) | ||
// @ts-expect-error | ||
await plugin_2.transform.call(this, css, DEFINE_CSS_EXTRA_CSS) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,8 +2,5 @@ | |
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@/*":["src/*"] | ||
} | ||
} | ||
} |
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 @@ | ||
import { defineConfig } from 'tsup' | ||
|
||
export default defineConfig({ | ||
entry: ['src/index.ts'], | ||
splitting: false, | ||
shims: true, | ||
dts: true, | ||
format: ['esm'], | ||
clean: true, | ||
external: ['vite', '@stylex-extend/babel-plugin'] | ||
}) |
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 |
---|---|---|
|
@@ -2,23 +2,14 @@ | |
"name": "stylex-extend", | ||
"description": "An unofficial stylexjs extension", | ||
"scripts": { | ||
"preinstall": "node ./scripts/yarn.js", | ||
"test": "yarn workspaces foreach --all run test", | ||
"build": "yarn build:babel-plugin && yarn build:shared", | ||
"build:babel-plugin": "yarn workspace @stylex-extend/babel-plugin run build", | ||
"build:shared": "yarn workspace @stylex-extend/shared run build" | ||
"test": "pnpm -r run test" | ||
}, | ||
"workspaces": [ | ||
"packages/*", | ||
"examples/*" | ||
], | ||
"keywords": [], | ||
"author": "kanno", | ||
"license": "MIT", | ||
"packageManager": "[email protected]", | ||
"devDependencies": { | ||
"@stylex-extend/babel-plugin": "workspace:*", | ||
"@stylex-extend/css": "workspace:*", | ||
"@stylex-extend/core": "workspace:*", | ||
"@stylex-extend/shared": "workspace:*", | ||
"@stylexjs/babel-plugin": "^0.5.1", | ||
"@stylexjs/stylex": "^0.5.1", | ||
|
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,38 @@ | ||
# Principle | ||
|
||
Welcome to `@stylex-extend/babel-plugin`. This is an unofficial `@stylexjs` extension. We support using `JSXAttribute` syntax to define inline style and etc. | ||
|
||
All of features are based on the `@stylexjs` RFC or some interesting ideas. | ||
|
||
## Features | ||
|
||
> JSXAttribute | ||
```jsx | ||
|
||
// acceptable syntax | ||
|
||
const color = 'red' | ||
|
||
function font(unit) { | ||
return ... | ||
} | ||
|
||
function Component(props) { | ||
|
||
return <div stylex={{ | ||
color, | ||
font: font(1), | ||
height: props.full ? '100px' : '50px', | ||
|
||
padding: `${props.padding}`, | ||
...(props.inline && {display:'inline'}), | ||
margin: { | ||
default: '10px' | ||
} | ||
}}></div> | ||
} | ||
|
||
// We don't support overly complex syntax. don't write nested spreads syntax. | ||
|
||
``` |
8 changes: 8 additions & 0 deletions
8
packages/babel-plugin/__tests__/fixtures/inject-global-style/code.js
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,8 @@ | ||
import { injectGlobalStyle } from '@stylex-extend/core' | ||
import { expression } from './expression.stylex' | ||
|
||
export const styles = injectGlobalStyle({ | ||
html: { | ||
fontSize: expression.font | ||
} | ||
}) |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin/__tests__/fixtures/inject-global-style/expression.stylex.js
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 @@ | ||
import { defineVars } from '@stylexjs/stylex' | ||
|
||
export const expression = defineVars({ | ||
font: '13px' | ||
}) |
4 changes: 4 additions & 0 deletions
4
packages/babel-plugin/__tests__/fixtures/inject-global-style/output.js
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 @@ | ||
import { create as _create, props as _props } from "@stylexjs/stylex"; | ||
import { injectGlobalStyle } from "@stylex-extend/core"; | ||
import { expression } from "./expression.stylex"; | ||
export const styles = ""; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.