|
| 1 | +const fs = require('fs') |
| 2 | + |
1 | 3 | module.exports = (api, opts, rootOpts) => { |
2 | 4 | const helpers = require('./helpers')(api) |
3 | 5 |
|
@@ -31,41 +33,64 @@ module.exports = (api, opts, rootOpts) => { |
31 | 33 | // adapted from https://github.com/Akryum/vue-cli-plugin-apollo/blob/master/generator/index.js#L68-L91 |
32 | 34 | api.onCreateComplete(() => { |
33 | 35 | // Modify main.js |
34 | | - helpers.updateMain(src => { |
35 | | - const vueImportIndex = src.findIndex(line => line.match(/^import Vue/)) |
36 | | - |
37 | | - src.splice(vueImportIndex + 1, 0, 'import \'./plugins/bootstrap-vue\'') |
38 | | - |
39 | | - return src |
| 36 | + helpers.updateFile(api.resolve(api.entryFile), srcLines => { |
| 37 | + const vueImportIndex = srcLines.findIndex(line => line.match(/^import Vue/)) |
| 38 | + srcLines.splice(vueImportIndex + 1, 0, 'import \'./plugins/bootstrap-vue\'') |
40 | 39 | }) |
41 | 40 |
|
42 | 41 | if(opts.useScss){ |
43 | | - |
44 | | - |
45 | 42 | //Modify App.vue (import bootstrap styles) |
46 | | - helpers.updateApp(src => { |
47 | | - let styleBlockIndex = src.findIndex(line => line.match(/^<style/)) |
| 43 | + helpers.updateFile(api.resolve('./src/App.vue'), srcLines => { |
| 44 | + let styleBlockIndex = srcLines.findIndex(line => line.match(/^<style/)) |
48 | 45 |
|
49 | 46 | if(styleBlockIndex === -1){ //no style block found |
50 | 47 | //create it with lang scss |
51 | | - src.push(`<style lang="scss">`) |
52 | | - src.push(`</style>`) |
| 48 | + srcLines.push(`<style lang="scss">`) |
| 49 | + srcLines.push(`</style>`) |
53 | 50 |
|
54 | | - styleBlockIndex = src.length - 2 |
| 51 | + styleBlockIndex = srcLines.length - 2 |
55 | 52 | } |
56 | 53 | else{ |
57 | 54 | //check if has the attr lang="scss" |
58 | | - if(!src[styleBlockIndex].includes('lang="scss')){ |
| 55 | + if(!srcLines[styleBlockIndex].includes('lang="scss')){ |
59 | 56 | //if not, replace line with lang="scss" |
60 | | - src[styleBlockIndex] = '<style lang="scss">' |
| 57 | + srcLines[styleBlockIndex] = '<style lang="scss">' |
61 | 58 | } |
62 | 59 | } |
63 | 60 |
|
64 | | - const bootstrapImportString = `@import "~@/assets/scss/vendors/bootstrap-vue/index";\n` |
65 | | - src.splice(styleBlockIndex + 1, 0, bootstrapImportString) |
66 | | - |
67 | | - return src |
| 61 | + const bootstrapImportString = `@import "~@/assets/scss/vendors/bootstrap-vue/index";` |
| 62 | + srcLines.splice(styleBlockIndex + 1, 0, bootstrapImportString) |
68 | 63 | }) |
| 64 | + |
| 65 | + if(opts.injectAbstracts){ |
| 66 | + //create/modify vue.config.js |
| 67 | + const vueConfigPath = api.resolve('./vue.config.js') |
| 68 | + if(!fs.existsSync(vueConfigPath)){ |
| 69 | + const content = `module.exports = {\n}` |
| 70 | + fs.writeFileSync(vueConfigPath, content, { encoding: 'utf-8' }) |
| 71 | + } |
| 72 | + |
| 73 | + helpers.updateFile(vueConfigPath, srcLines => { |
| 74 | + let index = 0 |
| 75 | + srcLines.splice(index, 0, `const bootstrapSassAbstractsImports = require('vue-cli-plugin-bootstrap-vue/sassAbstractsImports.js')`) |
| 76 | + |
| 77 | + const bootstrapAbstractsContentLines = [ |
| 78 | + "\tcss: {", |
| 79 | + "\t\tloaderOptions: {", |
| 80 | + "\t\t\tsass: {", |
| 81 | + "\t\t\t\tadditionalData: bootstrapSassAbstractsImports.join('\\n')", |
| 82 | + "\t\t\t},", |
| 83 | + "\t\t\tscss: {", |
| 84 | + "\t\t\t\tadditionalData: [...bootstrapSassAbstractsImports, ''].join(';\\n')", |
| 85 | + "\t\t\t}", |
| 86 | + "\t\t}", |
| 87 | + '\t}' |
| 88 | + ] |
| 89 | + index = srcLines.length - 1 |
| 90 | + srcLines.splice(index, 0, bootstrapAbstractsContentLines.join('\n')) |
| 91 | + |
| 92 | + }) |
| 93 | + } |
69 | 94 | } |
70 | 95 |
|
71 | 96 |
|
@@ -93,14 +118,13 @@ module.exports = (api, opts, rootOpts) => { |
93 | 118 | return cfg |
94 | 119 | }) |
95 | 120 |
|
96 | | - helpers.updateMain(src => { |
97 | | - if (!src.find(l => l.match(/^(import|require).+mutationobserver-shim.*$/))) { |
98 | | - src.unshift('import \'mutationobserver-shim\'') |
| 121 | + helpers.updateFile(api.resolve(api.entryFile), srcLines => { |
| 122 | + if (!srcLines.find(line => line.match(/^(import|require).+mutationobserver-shim.*$/))) { |
| 123 | + srcLines.unshift('import \'mutationobserver-shim\'') |
99 | 124 | } |
100 | | - if (!src.find(l => l.match(/^(import|require).+@babel\/polyfill.*$/))) { |
101 | | - src.unshift('import \'@babel/polyfill\'') |
| 125 | + if (!srcLines.find(line => line.match(/^(import|require).+@babel\/polyfill.*$/))) { |
| 126 | + srcLines.unshift('import \'@babel/polyfill\'') |
102 | 127 | } |
103 | | - return src |
104 | 128 | }) |
105 | 129 | } |
106 | 130 | }) |
|
0 commit comments