Skip to content

Commit af283ef

Browse files
committed
Merge branch 'pr/36'
2 parents 9ab0ae9 + d07aea1 commit af283ef

File tree

5 files changed

+61
-9566
lines changed

5 files changed

+61
-9566
lines changed

generator/helpers.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ const fs = require('fs')
22

33
module.exports = function (api) {
44
return {
5-
getMain() {
6-
const tsPath = api.resolve('src/main.ts')
7-
8-
return fs.existsSync(tsPath) ? 'src/main.ts' : 'src/main.js'
9-
},
10-
115
updateBabelConfig (callback) {
126
let config, configPath
137

@@ -40,31 +34,14 @@ module.exports = function (api) {
4034
}
4135
},
4236

43-
updateMain (callback) {
44-
const tsPath = api.resolve('./src/main.ts')
45-
const jsPath = api.resolve('./src/main.js')
46-
47-
const mainPath = fs.existsSync(tsPath) ? tsPath : jsPath
48-
let content = fs.readFileSync(mainPath, { encoding: 'utf8' })
49-
50-
let lines = content.split(/\r?\n/g)
37+
updateFile(filepath, callback){
38+
let content = fs.readFileSync(filepath, 'utf-8')
5139

52-
lines = callback(lines)
40+
const lines = content.split(/\r?\n/)
41+
callback(lines)
5342

5443
content = lines.join('\n')
55-
fs.writeFileSync(mainPath, content, { encoding: 'utf8' })
44+
fs.writeFileSync(filepath, content, { encoding: 'utf-8' })
5645
},
57-
58-
//TODO: refactor since is equal to updateMain
59-
updateApp(callback){
60-
const appPath = api.resolve('./src/App.vue')
61-
62-
let content = fs.readFileSync(appPath, { encoding: 'utf8' })
63-
let lines = content.split(/\r?\n/g)
64-
lines = callback(lines)
65-
66-
content = lines.join('\n')
67-
fs.writeFileSync(appPath, content, { encoding: 'utf8' })
68-
}
6946
}
7047
}

generator/index.js

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require('fs')
2+
13
module.exports = (api, opts, rootOpts) => {
24
const helpers = require('./helpers')(api)
35

@@ -31,41 +33,64 @@ module.exports = (api, opts, rootOpts) => {
3133
// adapted from https://github.com/Akryum/vue-cli-plugin-apollo/blob/master/generator/index.js#L68-L91
3234
api.onCreateComplete(() => {
3335
// 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\'')
4039
})
4140

4241
if(opts.useScss){
43-
44-
4542
//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/))
4845

4946
if(styleBlockIndex === -1){ //no style block found
5047
//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>`)
5350

54-
styleBlockIndex = src.length - 2
51+
styleBlockIndex = srcLines.length - 2
5552
}
5653
else{
5754
//check if has the attr lang="scss"
58-
if(!src[styleBlockIndex].includes('lang="scss')){
55+
if(!srcLines[styleBlockIndex].includes('lang="scss')){
5956
//if not, replace line with lang="scss"
60-
src[styleBlockIndex] = '<style lang="scss">'
57+
srcLines[styleBlockIndex] = '<style lang="scss">'
6158
}
6259
}
6360

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)
6863
})
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+
}
6994
}
7095

7196

@@ -93,14 +118,13 @@ module.exports = (api, opts, rootOpts) => {
93118
return cfg
94119
})
95120

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\'')
99124
}
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\'')
102127
}
103-
return src
104128
})
105129
}
106130
})

index.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,4 @@ module.exports = (api, opts) => {
3232
})
3333
})
3434

35-
//Add bootstrap's variables/functions/mixins globally
36-
if(opts.useScss && opts.injectAbstracts){
37-
merge(opts.css, {
38-
loaderOptions:{
39-
sass: {
40-
additionalData: bootstrapCssAbstractsImports.join('\n')
41-
},
42-
scss: {
43-
additionalData: [...bootstrapCssAbstractsImports, ''].join(';\n')
44-
}
45-
}
46-
})
47-
}
4835
}

0 commit comments

Comments
 (0)