Skip to content

Commit

Permalink
feat: add inectGlobalStyle (#7)
Browse files Browse the repository at this point in the history
* 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
nonzzz authored Apr 17, 2024
1 parent 6754854 commit 1ef9f09
Show file tree
Hide file tree
Showing 57 changed files with 5,063 additions and 5,505 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install berry
- name: Install pnpm
run: corepack enable
- uses: actions/setup-node@v3
with:
node-version: 20.5.1
- name: Install Dependices
run: yarn install
run: pnpm install

- name: Run Test
run: yarn test
run: pnpm test
Binary file removed .yarn/install-state.gz
Binary file not shown.
15 changes: 0 additions & 15 deletions .yarnrc.yml

This file was deleted.

2 changes: 1 addition & 1 deletion eslint.config.js
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']
})
1 change: 1 addition & 0 deletions examples/vite-vue-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"preview": "vite preview"
},
"devDependencies": {
"@stylex-extend/vite-plugin": "workspace:*",
"@stylex-extend/vue": "workspace:*",
"@vitejs/plugin-vue": "^5.0.4",
"@vitejs/plugin-vue-jsx": "^3.1.0",
Expand Down
5 changes: 5 additions & 0 deletions examples/vite-vue-demo/src/colors.stylex.ts
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'
})
14 changes: 13 additions & 1 deletion examples/vite-vue-demo/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createApp, defineComponent } from 'vue'
import { keyframes } from '@stylexjs/stylex'
import { injectGlobalStyle } from '@stylex-extend/core'
import { colors } from './colors.stylex'

const pulse = keyframes({
'0%': { transform: 'scale(1)' },
'50%': { transform: 'scale(1.1)' },
'50%': { transform: 'scale(0.5)' },
'100%': { transform: 'scale(1)' }
})

Expand All @@ -13,13 +15,23 @@ const A = defineComponent(() => {

const color = 'purple'

export { colors }

injectGlobalStyle({
p: {
color: colors.purple
}
})

const App = defineComponent({

setup() {
return () => (
<>
<div stylex={{ color: 'red' }}>
456
</div>
<p>text</p>
<A stylex={{
color,
animationName: pulse,
Expand Down
5 changes: 2 additions & 3 deletions examples/vite-vue-demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import jsx from '@vitejs/plugin-vue-jsx'
import { stylexPlugin } from 'vite-plugin-stylex-dev'
import stylexBabelPlugin from '@stylex-extend/babel-plugin'
import inspect from 'vite-plugin-inspect'
import { stylexExtendPlugin } from '@stylex-extend/vite-plugin'

export default defineConfig({
plugins: [vue(), jsx({ babelPlugins: [stylexBabelPlugin.default.withOptions({ stylex: { helper: 'attrs' } })] }), stylexPlugin(), inspect({ dev: true })]
plugins: [vue(), stylexExtendPlugin({ stylex: { helper: 'attrs' } }), jsx(), stylexPlugin()]
})
17 changes: 17 additions & 0 deletions internal/vite-plugin/package.json
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"
}
}
81 changes: 81 additions & 0 deletions internal/vite-plugin/src/index.ts
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)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*":["src/*"]
}
}
}
11 changes: 11 additions & 0 deletions internal/vite-plugin/tsup.config.ts
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']
})
13 changes: 2 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 38 additions & 0 deletions packages/babel-plugin/PRINCIPLE.md
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.

```
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
}
})
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'
})
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 = "";
4 changes: 2 additions & 2 deletions packages/babel-plugin/__tests__/stylex-macro.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import plugin from '../src'

pluginTester({
plugin,
pluginName: 'stylex-macro',
fixtures: path.join(__dirname, 'fixtures', 'stylex-macro'),
pluginName: 'stylex-extend',
fixtures: path.join(__dirname, 'fixtures'),
babelOptions: {
parserOpts: {
plugins: ['jsx']
Expand Down
21 changes: 16 additions & 5 deletions packages/babel-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"test": "yarn run -T vitest",
"dev": "yarn run -T tsup --watch",
"build": "yarn run -T tsup"
"test": "vitest",
"dev": "tsup --watch",
"build": "tsup"
},
"license": "MIT",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./package.json": "./package.json",
"./*": "./*"
},
"devDependencies": {
"@stylex-extend/react": "0.1.3",
"@stylex-extend/react": "workspace:*",
"@types/babel__core": "^7.20.5",
"@types/stylis": "^4.2.5",
"babel-plugin-tester": "^11.0.4"
},
"dependencies": {
"@babel/core": "^7.23.9"
"@babel/core": "^7.23.9",
"@stylexjs/shared": "^0.5.1",
"stylis": "^4.3.1"
},
"files": [
"dist",
Expand Down
Loading

0 comments on commit 1ef9f09

Please sign in to comment.