【发布时间】:2022-01-25 03:38:55
【问题描述】:
我已将 styled-jsx 添加到我的 Preact(使用此 TS template 设置)项目中,并且可以通过 <style> 标记内联样式,没有任何问题,例如:
<div>
<h1>Hello{name && `, ${name}`}!</h1>
<form onSubmit={handleNameSubmit}>
<input type="text" value={input} onInput={handleInput} />
<button type="submit" className="test">
Update {input && `as ${input}`}
</button>
</form>
<style jsx>
{`
h1 {
color: red;
}
.test {
color: blue;
}
`}
</style>
</div>
但是,如果我在关注 their instructions 时将该样式提取到单独的 css 变量中(在同一文件中或外部),则会收到以下错误:
if you are getting this error it means that your `css` tagged template literals were not transpiled.
在 SO 和 Github 上有一些答案/解决方案,但它们都涉及更改 webpack 配置——而且我没有 webpack 配置。看起来没有一个开箱即用的preact-cli。我已按照建议将 babel 规则添加到我的babelrc:
{
"presets": ["preact-cli/babel"],
"plugins": [["styled-jsx/babel", { "optimizeForSpeed": true }]]
}
但是,我没有安装任何额外的 macros 或 babel 插件,因为那不在 styled-jsx 自述文件的原始说明中。
【问题讨论】:
标签: webpack babeljs preact css-in-js styled-jsx