@@ -8,40 +8,40 @@ const {
8
8
const getTemplate = require ( "./get-template" ) ;
9
9
const loadSyntax = require ( "postcss-syntax/load-syntax" ) ;
10
10
11
- const partSport = {
11
+ const supports = {
12
12
// https://github.com/Khan/aphrodite
13
- aphrodite : [
14
- "StyleSheet" ,
15
- "create" ,
16
- ] ,
17
-
18
13
// https://github.com/necolas/react-native-web
19
- "react-native" : [
20
- "StyleSheet" ,
21
- "create" ,
22
- ] ,
23
- } ;
14
+ StyleSheet : true ,
15
+
16
+ // https://github.com/emotion-js/emotion
17
+ // import styled from '@emotion/styled'
18
+ // https://github.com/threepointone/glamor
19
+ // import { styled } from 'glamor/styled'
20
+ // https://github.com/rtsao/styletron
21
+ // import { styled } from "styletron-react";
22
+ styled : true ,
23
+
24
+ // https://github.com/typestyle/typestyle
25
+ // import { style } from "typestyle";
26
+ style : true ,
24
27
25
- const supports = {
26
28
// https://github.com/4Catalyzer/astroturf
27
- astroturf : true ,
29
+ // import { css } from 'astroturf';
30
+ // https://github.com/bashmish/lit-css
31
+ // import { css } from 'lit-css';
32
+ // https://github.com/threepointone/glamor
33
+ // import { css } from 'glamor'
34
+ css : true ,
28
35
29
36
// https://github.com/emotion-js/emotion
37
+ // import styled from "react-emotion";
30
38
emotion : true ,
31
- "@emotion/core" : true ,
32
- "@emotion/styled" : true ,
33
39
"react-emotion" : true ,
34
40
"preact-emotion" : true ,
35
41
36
- // https://github.com/threepointone/glamor
37
- glamor : true ,
38
-
39
42
// https://github.com/paypal/glamorous
40
43
glamorous : true ,
41
44
42
- // https://github.com/bashmish/lit-css
43
- "lit-css" : true ,
44
-
45
45
// https://github.com/js-next/react-style
46
46
"react-style" : true ,
47
47
@@ -52,14 +52,9 @@ const supports = {
52
52
"styled-components" : true ,
53
53
54
54
// https://github.com/rtsao/styletron
55
+ // import {styled} from "styletron-react";
56
+ // import {withStyle} from "styletron-react";
55
57
"styletron-react" : true ,
56
-
57
- // https://github.com/typestyle/typestyle
58
- typestyle : true ,
59
- } ;
60
-
61
- const cssPropImpliesStyle = {
62
- "@emotion/core" : true
63
58
} ;
64
59
65
60
const plugins = [
@@ -119,7 +114,6 @@ function literalParser (source, opts, styles) {
119
114
let objLiteral = new Set ( ) ;
120
115
let tplLiteral = new Set ( ) ;
121
116
const jobs = [ ] ;
122
- let cssPropIsStyle = false ;
123
117
124
118
function addObjectJob ( path ) {
125
119
jobs . push ( ( ) => {
@@ -155,6 +149,11 @@ function literalParser (source, opts, styles) {
155
149
}
156
150
157
151
function setSpecifier ( id , nameSpace ) {
152
+ nameSpace . unshift . apply (
153
+ nameSpace ,
154
+ nameSpace . shift ( ) . replace ( / ^ \W + / , "" ) . split ( / [ / \\ ] + / g)
155
+ ) ;
156
+
158
157
if ( types . isIdentifier ( id ) ) {
159
158
specifiers . set ( id . name , nameSpace ) ;
160
159
specifiers . set ( id , nameSpace ) ;
@@ -187,36 +186,21 @@ function literalParser (source, opts, styles) {
187
186
nameSpace . unshift ( node . name ) ;
188
187
}
189
188
} else {
190
- if ( node . name ) {
191
- getNameSpace ( path . get ( "name" ) , nameSpace ) ;
192
- } else if ( node . property ) {
193
- getNameSpace ( path . get ( "property" ) , nameSpace ) ;
194
- }
195
- if ( node . object ) {
196
- getNameSpace ( path . get ( "object" ) , nameSpace ) ;
197
- } else if ( node . callee ) {
198
- getNameSpace ( path . get ( "callee" ) , nameSpace ) ;
199
- }
189
+ [
190
+ "name" ,
191
+ "property" ,
192
+ "object" ,
193
+ "callee" ,
194
+ ] . forEach ( prop => {
195
+ node [ prop ] && getNameSpace ( path . get ( prop ) , nameSpace ) ;
196
+ } ) ;
200
197
}
201
198
202
199
return nameSpace ;
203
200
}
204
201
205
202
function isStylePath ( path ) {
206
- const nameSpace = getNameSpace ( path , [ ] ) . filter ( Boolean ) ;
207
- if ( nameSpace . length ) {
208
- if ( / ^ (?: c s s | S t y l e d ? ) (?: S h e e t s ? ) ? $ / i. test ( nameSpace [ 0 ] ) || supports [ nameSpace [ 0 ] ] ) {
209
- return nameSpace ;
210
- }
211
-
212
- const prefix = partSport [ nameSpace . shift ( ) ] ;
213
-
214
- if ( prefix && nameSpace . length >= prefix . length && prefix . every ( ( name , i ) => name === nameSpace [ i ] ) ) {
215
- return nameSpace ;
216
- }
217
- }
218
-
219
- return false ;
203
+ return getNameSpace ( path , [ ] ) . some ( name => name && supports [ name ] ) ;
220
204
}
221
205
222
206
const visitor = {
@@ -228,23 +212,12 @@ function literalParser (source, opts, styles) {
228
212
nameSpace . push ( specifier . imported . name ) ;
229
213
}
230
214
setSpecifier ( specifier . local , nameSpace ) ;
231
- if ( cssPropImpliesStyle [ moduleId ] ) {
232
- cssPropIsStyle = true ;
233
- }
234
215
} ) ;
235
216
} ,
236
217
JSXAttribute : ( path ) => {
237
- const attrName = path . node . name . name ;
238
- if ( attrName === "css" ) {
239
- const elePath = path . findParent ( p => p . isJSXOpeningElement ( ) ) ;
240
- if ( ! cssPropIsStyle && ! isStylePath ( elePath ) ) {
241
- return ;
242
- }
243
- } else if ( attrName !== "style" ) {
244
- return ;
218
+ if ( supports [ path . node . name . name ] ) {
219
+ addObjectJob ( path . get ( "value.expression" ) ) ;
245
220
}
246
-
247
- addObjectJob ( path . get ( "value.expression" ) ) ;
248
221
} ,
249
222
VariableDeclarator : ( path ) => {
250
223
variableDeclarator . set ( path . node . id , path . node . init ? [ path . get ( "init" ) ] : [ ] ) ;
@@ -331,25 +304,26 @@ function literalParser (source, opts, styles) {
331
304
node . start > style . endIndex || node . end < style . startIndex
332
305
) )
333
306
) ) . map ( node => {
334
- const quasis = node . quasis ;
335
- const value = getTemplate ( node , source ) ;
336
-
307
+ const quasis = node . quasis . map ( node => ( {
308
+ start : node . start ,
309
+ end : node . end ,
310
+ } ) ) ;
337
311
const style = {
338
312
startIndex : quasis [ 0 ] . start ,
339
313
endIndex : quasis [ quasis . length - 1 ] . end ,
340
- content : value ,
314
+ content : getTemplate ( node , source ) ,
341
315
} ;
342
316
if ( node . expressions . length ) {
343
317
style . syntax = loadSyntax ( opts , __dirname ) ;
344
318
style . lang = "template-literal" ;
345
319
style . opts = {
346
- node : node ,
320
+ quasis : quasis ,
347
321
} ;
348
322
} else {
349
323
style . lang = "css" ;
350
324
}
351
325
return style ;
352
- } ) . filter ( Boolean ) ;
326
+ } ) ;
353
327
354
328
return ( styles || [ ] ) . concat ( objLiteral ) . concat ( tplLiteral ) ;
355
329
} ;
0 commit comments