Skip to content

Commit

Permalink
upgrade linaria to latest version (#16747)
Browse files Browse the repository at this point in the history
* upgrade linaria to latest version

* use className instead style

* format

* restore

* use space instead of tab

* tweak for submit fail

* t

---------

Co-authored-by: Administrator <Administrator@tech8>
  • Loading branch information
2 people authored and zhiqingchen committed Nov 6, 2024
1 parent aa05db4 commit 7519a93
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
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 = {

Check warning on line 189 in packages/taro-rn-transformer/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-rn-transformer/src/utils.ts#L189

Added line #L189 was not covered by tests
babelOptions: {
babelrc: false
}
}

const services = {

Check warning on line 195 in packages/taro-rn-transformer/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-rn-transformer/src/utils.ts#L195

Added line #L195 was not covered by tests
options: { root, filename, pluginOptions },
}

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

Check warning on line 199 in packages/taro-rn-transformer/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-rn-transformer/src/utils.ts#L199

Added line #L199 was not covered by tests

// 生成样式文件
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)))

Check warning on line 291 in packages/taro-rn-transformer/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-rn-transformer/src/utils.ts#L291

Added line #L291 was not covered by tests
}
}
})
Expand Down

0 comments on commit 7519a93

Please sign in to comment.