@@ -14,17 +14,30 @@ module.exports = function injectImportsAndOptions (source, imports, injections)
1414
1515 if ( hasImports ) {
1616 const toImport = i => recast . parse ( `${ i } \n` ) . program . body [ 0 ]
17+ const importDeclarations = [ ]
1718 let lastImportIndex = - 1
19+
1820 recast . types . visit ( ast , {
1921 visitImportDeclaration ( { node } ) {
2022 lastImportIndex = ast . program . body . findIndex ( n => n === node )
23+ importDeclarations . push ( node )
2124 return false
2225 }
2326 } )
2427 // avoid blank line after the previous import
2528 delete ast . program . body [ lastImportIndex ] . loc
2629
27- const newImports = imports . map ( toImport )
30+ const nonDuplicates = i => {
31+ return ! importDeclarations . some ( node => {
32+ const result = node . source . raw === i . source . raw && node . specifiers . length === i . specifiers . length
33+
34+ return result && node . specifiers . every ( ( item , index ) => {
35+ return i . specifiers [ index ] . local . name === item . local . name
36+ } )
37+ } )
38+ }
39+
40+ const newImports = imports . map ( toImport ) . filter ( nonDuplicates )
2841 ast . program . body . splice ( lastImportIndex + 1 , 0 , ...newImports )
2942 }
3043
@@ -37,12 +50,17 @@ module.exports = function injectImportsAndOptions (source, imports, injections)
3750 if ( node . callee . name === 'Vue' ) {
3851 const options = node . arguments [ 0 ]
3952 if ( options && options . type === 'ObjectExpression' ) {
40- const props = options . properties
53+ const nonDuplicates = i => {
54+ return ! options . properties . slice ( 0 , - 1 ) . some ( p => {
55+ return p . key . name === i [ 0 ] . key . name &&
56+ recast . print ( p . value ) . code === recast . print ( i [ 0 ] . value ) . code
57+ } )
58+ }
4159 // inject at index length - 1 as it's usually the render fn
4260 options . properties = [
43- ...props . slice ( 0 , props . length - 1 ) ,
44- ...( [ ] . concat ( ...injections . map ( toProperty ) ) ) ,
45- ...props . slice ( props . length - 1 )
61+ ...options . properties . slice ( 0 , - 1 ) ,
62+ ...( [ ] . concat ( ...injections . map ( toProperty ) . filter ( nonDuplicates ) ) ) ,
63+ ...options . properties . slice ( - 1 )
4664 ]
4765 }
4866 }
0 commit comments