@@ -82,32 +82,36 @@ module.exports = (api, options, rootOptions) => {
82
82
}
83
83
84
84
// Router
85
- {
85
+ try {
86
86
const file = getFile ( api , './src/router.js' )
87
87
if ( file ) {
88
88
let contents = fs . readFileSync ( file , { encoding : 'utf8' } )
89
- contents = contents . replace ( / e x p o r t d e f a u l t n e w R o u t e r \( { ( ( .| \s ) + ) } \) / , `export function createRouter () {
90
- return new Router({
91
- ${ contents . includes ( 'mode:' ) ? '' : 'mode: \'history\',' } $1})
92
- }` )
89
+ const { wrapRouterToExportedFunction } = require ( './codemod/router' )
90
+ contents = wrapRouterToExportedFunction ( contents )
93
91
contents = contents . replace ( / m o d e : \s * ( " | ' ) ( h a s h | a b s t r a c t ) ( " | ' ) , / , '' )
94
92
fs . writeFileSync ( file , contents , { encoding : 'utf8' } )
95
93
}
94
+ } catch ( e ) {
95
+ console . error ( 'An error occured while transforming router code' , e . stack )
96
96
}
97
97
98
98
// Vuex
99
99
if ( api . hasPlugin ( 'vuex' ) ) {
100
- const file = getFile ( api , './src/store.js' )
101
- if ( file ) {
102
- let contents = fs . readFileSync ( file , { encoding : 'utf8' } )
103
- contents = contents . replace ( / e x p o r t d e f a u l t n e w V u e x \. S t o r e \( { ( ( .| \s ) + ) } \) / , `export function createStore () {
104
- return new Vuex.Store({$1})
105
- }` )
106
- contents = contents . replace ( / s t a t e : \s * { ( ( .| \s ) * ?) } , \s * ( g e t t e r s | m u t a t i o n s | a c t i o n s | m o d u l e s | n a m e s p a c e d ) : / , `state () {
107
- return {$1}
108
- },
109
- $3:` )
110
- fs . writeFileSync ( file , contents , { encoding : 'utf8' } )
100
+ try {
101
+ const file = getFile ( api , './src/store.js' )
102
+ if ( file ) {
103
+ let contents = fs . readFileSync ( file , { encoding : 'utf8' } )
104
+ contents = contents . replace ( / e x p o r t d e f a u l t n e w V u e x \. S t o r e \( { ( ( .| \s ) + ) } \) / , `export function createStore () {
105
+ return new Vuex.Store({$1})
106
+ }` )
107
+ contents = contents . replace ( / s t a t e : \s * { ( ( .| \s ) * ?) } , \s * ( g e t t e r s | m u t a t i o n s | a c t i o n s | m o d u l e s | n a m e s p a c e d ) : / , `state () {
108
+ return {$1}
109
+ },
110
+ $3:` )
111
+ fs . writeFileSync ( file , contents , { encoding : 'utf8' } )
112
+ }
113
+ } catch ( e ) {
114
+ console . error ( 'An error occured while transforming vuex code' , e . stack )
111
115
}
112
116
}
113
117
0 commit comments