1
1
"use strict" ;
2
- const traverse = require ( "@babel/traverse" ) . default ;
3
- const t = require ( "@babel/types" ) ;
4
- const parse = require ( "@babel/parser" ) . parse ;
2
+ const {
3
+ parse,
4
+ types,
5
+ traverse,
6
+ loadOptions,
7
+ } = require ( "@babel/core" ) ;
5
8
const getTemplate = require ( "./get-template" ) ;
6
9
const loadSyntax = require ( "postcss-syntax/load-syntax" ) ;
7
10
@@ -67,7 +70,7 @@ function getSourceType (filename) {
67
70
return "module" ;
68
71
}
69
72
try {
70
- return require ( "@babel/core" ) . loadOptions ( {
73
+ return loadOptions ( {
71
74
filename,
72
75
} ) . sourceType ;
73
76
} catch ( ex ) {
@@ -82,13 +85,19 @@ function getOptions (opts) {
82
85
sourceFilename : filename ,
83
86
sourceType : getSourceType ( filename ) || "unambiguous" ,
84
87
plugins,
88
+ allowImportExportEverywhere : true ,
89
+ allowAwaitOutsideFunction : true ,
90
+ allowReturnOutsideFunction : true ,
91
+ allowSuperOutsideMethod : true ,
85
92
} ;
86
93
}
87
94
88
95
function literalParser ( source , opts , styles ) {
89
96
let ast ;
90
97
try {
91
- ast = parse ( source , getOptions ( opts ) ) ;
98
+ ast = parse ( source , {
99
+ parserOpts : getOptions ( opts ) ,
100
+ } ) ;
92
101
} catch ( ex ) {
93
102
// console.error(ex);
94
103
return styles || [ ] ;
@@ -134,12 +143,12 @@ function literalParser (source, opts, styles) {
134
143
}
135
144
136
145
function setSpecifier ( id , nameSpace ) {
137
- if ( t . isIdentifier ( id ) ) {
146
+ if ( types . isIdentifier ( id ) ) {
138
147
specifiers . set ( id . name , nameSpace ) ;
139
148
specifiers . set ( id , nameSpace ) ;
140
- } else if ( t . isObjectPattern ( id ) ) {
149
+ } else if ( types . isObjectPattern ( id ) ) {
141
150
id . properties . forEach ( property => {
142
- if ( t . isObjectProperty ( property ) ) {
151
+ if ( types . isObjectProperty ( property ) ) {
143
152
const key = property . key ;
144
153
nameSpace = nameSpace . concat ( key . name || key . value ) ;
145
154
id = property . value ;
@@ -148,7 +157,7 @@ function literalParser (source, opts, styles) {
148
157
}
149
158
setSpecifier ( id , nameSpace ) ;
150
159
} ) ;
151
- } else if ( t . isArrayPattern ( id ) ) {
160
+ } else if ( types . isArrayPattern ( id ) ) {
152
161
id . elements . forEach ( ( element , i ) => {
153
162
setSpecifier ( element , nameSpace . concat ( String ( i ) ) ) ;
154
163
} ) ;
@@ -226,7 +235,7 @@ function literalParser (source, opts, styles) {
226
235
variableDeclarator . set ( path . node . id , path . node . init ? [ path . get ( "init" ) ] : [ ] ) ;
227
236
} ,
228
237
AssignmentExpression : ( path ) => {
229
- if ( t . isIdentifier ( path . node . left ) && t . isObjectExpression ( path . node . right ) ) {
238
+ if ( types . isIdentifier ( path . node . left ) && types . isObjectExpression ( path . node . right ) ) {
230
239
const identifier = path . scope . getBindingIdentifier ( path . node . left . name ) ;
231
240
const variable = variableDeclarator . get ( identifier ) ;
232
241
const valuePath = path . get ( "right" ) ;
@@ -239,8 +248,8 @@ function literalParser (source, opts, styles) {
239
248
} ,
240
249
CallExpression : ( path ) => {
241
250
const callee = path . node . callee ;
242
- if ( t . isIdentifier ( callee , { name : "require" } ) && ! path . scope . getBindingIdentifier ( callee . name ) ) {
243
- path . node . arguments . filter ( t . isStringLiteral ) . forEach ( arg => {
251
+ if ( types . isIdentifier ( callee , { name : "require" } ) && ! path . scope . getBindingIdentifier ( callee . name ) ) {
252
+ path . node . arguments . filter ( types . isStringLiteral ) . forEach ( arg => {
244
253
const moduleId = arg . value ;
245
254
const nameSpace = [ moduleId ] ;
246
255
let currPath = path ;
@@ -251,7 +260,7 @@ function literalParser (source, opts, styles) {
251
260
if ( id ) {
252
261
id = path . scope . getBindingIdentifier ( id . name ) || id ;
253
262
} else {
254
- if ( t . isIdentifier ( currPath . parent . property ) ) {
263
+ if ( types . isIdentifier ( currPath . parent . property ) ) {
255
264
nameSpace . push ( currPath . parent . property . name ) ;
256
265
}
257
266
currPath = currPath . parentPath ;
0 commit comments