Skip to content

Commit 49b236f

Browse files
author
cand
committed
feat: 使用css2react-native
1 parent ef58bfa commit 49b236f

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

dist/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
function l(e){return e}const s={name:"My Plugin",code:{css:{title:"Stylus",lang:"stylus",transform({style:e}){return Object.entries(e).map(([n,t])=>`${n} ${t}`).join(`
2-
`)}},js:!1}};export{s as default};
1+
import r from"css-to-react-native";function u(t){return t}const s={name:"React Native Plugin",code:{css:{title:"Stylus",lang:"stylus",transform({style:t}){return Object.entries(t).map(([e,n])=>`${e} ${n}`).join(`
2+
`)}},js:{title:"JavaScript",lang:"js",transform({style:t}){try{const e=Object.entries(t),n=r(e);return`${JSON.stringify(n,null,2)}`}catch(e){return`// \u8F6C\u6362\u5931\u8D25: ${e.message}`}}}}};export{s as default};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@tempad-dev/plugins": "catalog:",
4545
"@types/node": "catalog:types",
4646
"bumpp": "catalog:cli",
47+
"css-to-react-native": "catalog:cli",
4748
"eslint": "catalog:cli",
4849
"lint-staged": "catalog:cli",
4950
"simple-git-hooks": "catalog:cli",

pnpm-lock.yaml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ catalogs:
1616
'@antfu/eslint-config': ^5.4.1
1717
'@antfu/ni': ^26.0.1
1818
bumpp: ^10.2.3
19+
css-to-react-native: ^3.2.0
1920
eslint: ^9.36.0
2021
lint-staged: ^16.2.0
2122
simple-git-hooks: ^2.13.1

src/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { definePlugin } from '@tempad-dev/plugins'
2+
import cssToReactNative from 'css-to-react-native'
23

34
export default definePlugin({
4-
name: 'My Plugin',
5+
name: 'React Native Plugin',
56
code: {
67
css: {
78
title: 'Stylus', // Custom code block title
@@ -12,6 +13,24 @@ export default definePlugin({
1213
.join('\n')
1314
},
1415
},
15-
js: false, // Hides the built-in JavaScript code block
16+
js: {
17+
title: 'JavaScript',
18+
lang: 'js',
19+
// 直接引用 css-to-react-native 库进行转换并返回
20+
transform({ style }) {
21+
// 动态引入,防止包未安装时报错
22+
try {
23+
const styleArr = Object.entries(style)
24+
const rnStyle = cssToReactNative(styleArr)
25+
return (
26+
`${
27+
JSON.stringify(rnStyle, null, 2)}`
28+
)
29+
}
30+
catch (e) {
31+
return `// 转换失败: ${(e as Error).message}`
32+
}
33+
},
34+
},
1635
},
1736
})

0 commit comments

Comments
 (0)