Blame view

天文台pc/tianwentai-ui/node_modules/@emotion/babel-plugin/src/styled-macro.js 3.42 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  import {
    transformExpressionWithStyles,
    getStyledOptions,
    addImport,
    createTransformerMacro
  } from './utils'
  
  const getReferencedSpecifier = (path, specifierName) => {
    const specifiers = path.get('specifiers')
    return specifierName === 'default'
      ? specifiers.find(p => p.isImportDefaultSpecifier())
      : specifiers.find(p => p.node.local.name === specifierName)
  }
  
  export let styledTransformer = (
    {
      state,
      babel,
      path,
      importSource,
      reference,
      importSpecifierName,
      options: { styledBaseImport, isWeb }
    } /*: {
    state: Object,
    babel: Object,
    path: any,
    importSource: string,
    importSpecifierName: string,
    reference: Object,
    options: { styledBaseImport?: [string, string], isWeb: boolean }
  } */
  ) => {
    let t = babel.types
  
    let getStyledIdentifier = () => {
      if (
        !styledBaseImport ||
        (styledBaseImport[0] === importSource &&
          styledBaseImport[1] === importSpecifierName)
      ) {
        return t.cloneNode(reference.node)
      }
  
      if (path.node) {
        const referencedSpecifier = getReferencedSpecifier(
          path,
          importSpecifierName
        )
  
        if (referencedSpecifier) {
          referencedSpecifier.remove()
        }
  
        if (!path.get('specifiers').length) {
          path.remove()
        }
      }
  
      const [baseImportSource, baseSpecifierName] = styledBaseImport
  
      return addImport(state, baseImportSource, baseSpecifierName, 'styled')
    }
    let createStyledComponentPath = null
    if (
      t.isMemberExpression(reference.parent) &&
      reference.parent.computed === false
    ) {
      if (
        // checks if the first character is lowercase
        // becasue we don't want to transform the member expression if
        // it's in primitives/native
        reference.parent.property.name.charCodeAt(0) > 96
      ) {
        reference.parentPath.replaceWith(
          t.callExpression(getStyledIdentifier(), [
            t.stringLiteral(reference.parent.property.name)
          ])
        )
      } else {
        reference.replaceWith(getStyledIdentifier())
      }
  
      createStyledComponentPath = reference.parentPath
    } else if (
      reference.parentPath &&
      t.isCallExpression(reference.parentPath) &&
      reference.parent.callee === reference.node
    ) {
      reference.replaceWith(getStyledIdentifier())
      createStyledComponentPath = reference.parentPath
    }
  
    if (!createStyledComponentPath) {
      return
    }
  
    const styledCallLikeWithStylesPath = createStyledComponentPath.parentPath
  
    let node = transformExpressionWithStyles({
      path: styledCallLikeWithStylesPath,
      state,
      babel,
      shouldLabel: false
    })
  
    if (node && isWeb) {
      // we know the argument length will be 1 since that's the only time we will have a node since it will be static
      styledCallLikeWithStylesPath.node.arguments[0] = node
    }
  
    styledCallLikeWithStylesPath.addComment('leading', '#__PURE__')
  
    if (isWeb) {
      createStyledComponentPath.node.arguments[1] = getStyledOptions(
        t,
        createStyledComponentPath,
        state
      )
    }
  }
  
  export let createStyledMacro = (
    {
      importSource,
      originalImportSource = importSource,
      baseImportName = 'default',
      isWeb
    } /*: {
    importSource: string,
    originalImportSource?: string,
    baseImportName?: string,
    isWeb: boolean
  } */
  ) =>
    createTransformerMacro(
      {
        default: [
          styledTransformer,
          { styledBaseImport: [importSource, baseImportName], isWeb }
        ]
      },
      { importSource: originalImportSource }
    )