@@ -15,47 +15,21 @@ class ImportParserPlugin {
1515
1616 apply ( parser ) {
1717 const options = this . options ;
18- let parsedComments , chunkNameAssignment ;
19-
20- parser . plugin ( "program" , ( ast , comments ) => {
21- parsedComments = comments ;
22- } ) ;
23-
24- // use /* webpackChunkName = "chunkName" */ to specify a chunkName
25- parser . plugin ( "evaluate AssignmentExpression" , ( assignment ) => {
26- if ( assignment . left . name === "webpackChunkName" ) {
27- chunkNameAssignment = assignment ;
28- }
29- } ) ;
3018
3119 parser . plugin ( [ "call System.import" , "import-call" ] , ( expr ) => {
3220 if ( expr . arguments . length !== 1 )
3321 throw new Error ( "Incorrect number of arguments provided to 'import(module: string) -> Promise'." ) ;
3422
3523 const param = parser . evaluateExpression ( expr . arguments [ 0 ] ) ;
36- const exprEndPos = expr . end ;
37- const paramEndPos = expr . arguments [ 0 ] . end ;
24+
3825 let chunkName = null ;
3926
40- //check chunkName from comments
41- if ( parsedComments . length && exprEndPos - paramEndPos > "/*webpackChunkName=*/" . length ) {
42- for ( let i = 0 ; i < parsedComments . length ; i ++ ) {
43- let comment = parsedComments [ i ] ;
44- // should match the location and length
45- if ( comment . type === "Block" && comment . start >= paramEndPos && comment . end < exprEndPos ) {
46- chunkNameAssignment = null ;
47- parser . evaluate ( comment . value ) ;
48- // expect an AssignmentExpression after evaluate
49- if ( chunkNameAssignment ) {
50- const chunkNameExpr = parser . evaluateExpression ( chunkNameAssignment . right ) ;
51- if ( chunkNameExpr . isString ( ) ) {
52- chunkName = chunkNameExpr . string ;
53- } else {
54- throw new Error ( `\`webpackChunkName\` expected a String, but received: ${ comment . value } .` ) ;
55- }
56- }
57- break ;
58- }
27+ const importOptions = parser . getCommentOptions ( expr . range ) ;
28+ if ( importOptions ) {
29+ if ( typeof importOptions . webpackChunkName !== "undefined" ) {
30+ if ( typeof importOptions . webpackChunkName !== "string" )
31+ throw new Error ( `\`webpackChunkName\` expected a string, but received: ${ importOptions . webpackChunkName } .` ) ;
32+ chunkName = importOptions . webpackChunkName ;
5933 }
6034 }
6135
0 commit comments