Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade linaria to latest version #16747

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/taro-rn-transformer/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function componentLoader ({ sourceCode, filename, projectRoot, so
// linaria transform
let linaria
try {
linaria = require('linaria')
linaria = require('@wyw-in-js/transform')
} catch (e) {} // eslint-disable-line no-empty

if (linaria) {
Expand Down
45 changes: 12 additions & 33 deletions packages/taro-rn-transformer/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ export function parseBase64Image (iconPath: string, baseRoot: string) {
export function transformLinaria ({ sourcePath, sourceCode }: TransformLinariaOption) {
// TODO:配置 option, 小程序和 h5 可配置 webpack loader 更改配置,RN没有loader,所以默认不可配置,后续可考虑加配置
const cacheDirectory = '.linaria-cache'
const preprocessor = undefined
const extension = '.linaria.css'
const root = process.cwd()

Expand All @@ -187,12 +186,17 @@ export function transformLinaria ({ sourcePath, sourceCode }: TransformLinariaOp
const filename = nodePath.relative(process.cwd(), sourcePath)

// linaria代码转换
const result = require('linaria/lib/node').transform(sourceCode, {
filename,
// inputSourceMap: inputSourceMap ?? undefined,
outputFilename,
preprocessor
})
const pluginOptions = {
babelOptions: {
babelrc: false
}
}

const services = {
options: { root, filename, pluginOptions },
}

const result = require('@wyw-in-js/transform/lib/transform').transformSync(services, sourceCode)

// 生成样式文件
if (result.cssText) {
Expand Down Expand Up @@ -284,32 +288,7 @@ export function transformLinaria ({ sourcePath, sourceCode }: TransformLinariaOp
attribute = null as any
}

if (attribute) {
if (types.isJSXAttribute(attribute) && types.isJSXExpressionContainer(attribute.value)) {
const expression = attribute.value.expression
let elements
if (types.isArrayExpression(expression)) {
elements = expression.elements
} else {
elements = expression
}
// 合并 style 对象
// style = Object.assign({}, linariaStyle, { color: 'red' })
const mergeStyleExpression = types.callExpression(
types.identifier('Object.assign'),
// @ts-ignore
[types.objectExpression([])].concat(linariaExpression, elements)
)
attribute.value = types.jSXExpressionContainer(mergeStyleExpression)
}
} else {
attributes.push(
types.jsxAttribute(
types.jsxIdentifier('style'),
types.jsxExpressionContainer(linariaExpression)
)
)
}
attributes.push(types.jsxAttribute(types.jsxIdentifier('className'), types.jsxExpressionContainer(linariaExpression)))
}
}
})
Expand Down