11"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" ) ;
58const getTemplate = require ( "./get-template" ) ;
69const loadSyntax = require ( "postcss-syntax/load-syntax" ) ;
710
@@ -67,7 +70,7 @@ function getSourceType (filename) {
6770 return "module" ;
6871 }
6972 try {
70- return require ( "@babel/core" ) . loadOptions ( {
73+ return loadOptions ( {
7174 filename,
7275 } ) . sourceType ;
7376 } catch ( ex ) {
@@ -82,13 +85,19 @@ function getOptions (opts) {
8285 sourceFilename : filename ,
8386 sourceType : getSourceType ( filename ) || "unambiguous" ,
8487 plugins,
88+ allowImportExportEverywhere : true ,
89+ allowAwaitOutsideFunction : true ,
90+ allowReturnOutsideFunction : true ,
91+ allowSuperOutsideMethod : true ,
8592 } ;
8693}
8794
8895function literalParser ( source , opts , styles ) {
8996 let ast ;
9097 try {
91- ast = parse ( source , getOptions ( opts ) ) ;
98+ ast = parse ( source , {
99+ parserOpts : getOptions ( opts ) ,
100+ } ) ;
92101 } catch ( ex ) {
93102 // console.error(ex);
94103 return styles || [ ] ;
@@ -134,12 +143,12 @@ function literalParser (source, opts, styles) {
134143 }
135144
136145 function setSpecifier ( id , nameSpace ) {
137- if ( t . isIdentifier ( id ) ) {
146+ if ( types . isIdentifier ( id ) ) {
138147 specifiers . set ( id . name , nameSpace ) ;
139148 specifiers . set ( id , nameSpace ) ;
140- } else if ( t . isObjectPattern ( id ) ) {
149+ } else if ( types . isObjectPattern ( id ) ) {
141150 id . properties . forEach ( property => {
142- if ( t . isObjectProperty ( property ) ) {
151+ if ( types . isObjectProperty ( property ) ) {
143152 const key = property . key ;
144153 nameSpace = nameSpace . concat ( key . name || key . value ) ;
145154 id = property . value ;
@@ -148,7 +157,7 @@ function literalParser (source, opts, styles) {
148157 }
149158 setSpecifier ( id , nameSpace ) ;
150159 } ) ;
151- } else if ( t . isArrayPattern ( id ) ) {
160+ } else if ( types . isArrayPattern ( id ) ) {
152161 id . elements . forEach ( ( element , i ) => {
153162 setSpecifier ( element , nameSpace . concat ( String ( i ) ) ) ;
154163 } ) ;
@@ -226,7 +235,7 @@ function literalParser (source, opts, styles) {
226235 variableDeclarator . set ( path . node . id , path . node . init ? [ path . get ( "init" ) ] : [ ] ) ;
227236 } ,
228237 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 ) ) {
230239 const identifier = path . scope . getBindingIdentifier ( path . node . left . name ) ;
231240 const variable = variableDeclarator . get ( identifier ) ;
232241 const valuePath = path . get ( "right" ) ;
@@ -239,8 +248,8 @@ function literalParser (source, opts, styles) {
239248 } ,
240249 CallExpression : ( path ) => {
241250 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 => {
244253 const moduleId = arg . value ;
245254 const nameSpace = [ moduleId ] ;
246255 let currPath = path ;
@@ -251,7 +260,7 @@ function literalParser (source, opts, styles) {
251260 if ( id ) {
252261 id = path . scope . getBindingIdentifier ( id . name ) || id ;
253262 } else {
254- if ( t . isIdentifier ( currPath . parent . property ) ) {
263+ if ( types . isIdentifier ( currPath . parent . property ) ) {
255264 nameSpace . push ( currPath . parent . property . name ) ;
256265 }
257266 currPath = currPath . parentPath ;
0 commit comments