Skip to content

Commit 7c2f55b

Browse files
committed
Initial commit of working plugin
ToDo: Add App.vue & HelloWorld.vue templates as an optional install via prompts.js
1 parent 3e6db27 commit 7c2f55b

File tree

9 files changed

+8862
-1
lines changed

9 files changed

+8862
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
11
# vue-cli-plugin-bootstrap-vue
2-
vue-cli 3 plugin to add bootstrap-vue
2+
3+
Bootstrap-Vue Plugin for [vue-cli@3.0](https://github.com/vuejs/vue-cli)
4+
5+
## Install
6+
7+
If you haven't yet installed vue-cli 3, first follow the install instructions here: https://github.com/vuejs/vue-cli
8+
9+
Generate a project using vue-cli 3.0
10+
```
11+
vue create my-app
12+
```
13+
14+
Before installing the bootstrap-vue plugin, make sure to commit or stash your changes in case you need to revert
15+
16+
To install the bootstrap-vue plugin...
17+
```
18+
cd my-app
19+
vue add bootstrap-vue
20+
```
21+

generator/helpers.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const fs = require('fs')
2+
3+
module.exports = function (api) {
4+
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+
11+
updateBabelConfig (callback) {
12+
let config, configPath
13+
14+
const rcPath = api.resolve('./babel.config.js')
15+
const pkgPath = api.resolve('./package.json')
16+
if (fs.existsSync(rcPath)) {
17+
configPath = rcPath
18+
config = callback(require(rcPath))
19+
} else if (fs.existsSync(pkgPath)) {
20+
configPath = pkgPath
21+
config = JSON.parse(fs.readFileSync(pkgPath, { encoding: 'utf8' }))
22+
23+
if (config.babel) {
24+
config.babel = callback(config.babel)
25+
} else {
26+
// TODO: error handling here?
27+
}
28+
}
29+
30+
if (configPath) {
31+
const moduleExports = configPath !== pkgPath ? 'module.exports = ' : ''
32+
33+
fs.writeFileSync(
34+
configPath,
35+
`${moduleExports}${JSON.stringify(config, null, 2)}`,
36+
{ encoding: 'utf8' }
37+
)
38+
} else {
39+
// TODO: handle if babel config doesn't exist
40+
}
41+
},
42+
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)
51+
52+
lines = callback(lines)
53+
54+
content = lines.join('\n')
55+
fs.writeFileSync(mainPath, content, { encoding: 'utf8' })
56+
}
57+
}
58+
}

generator/index.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module.exports = (api, opts, rootOpts) => {
2+
const helpers = require('./helpers')(api)
3+
4+
api.extendPackage({
5+
dependencies: {
6+
'bootstrap-vue': '^2.0.0-rc.11'
7+
}
8+
})
9+
10+
if (opts.usePolyfill) {
11+
api.extendPackage({
12+
devDependencies: {
13+
'@babel/polyfill': '^7.0.0-beta.53',
14+
}
15+
})
16+
}
17+
18+
// Render bootstrap-vue plugin file
19+
api.render({
20+
'./src/plugins/bootstrap-vue.js': './templates/default/src/plugins/bootstrap-vue.js'
21+
}, opts)
22+
23+
// adapted from https://github.com/Akryum/vue-cli-plugin-apollo/blob/master/generator/index.js#L68-L91
24+
api.onCreateComplete(() => {
25+
// Modify main.js
26+
helpers.updateMain(src => {
27+
const vueImportIndex = src.findIndex(line => line.match(/^import Vue/))
28+
29+
src.splice(vueImportIndex + 1, 0, 'import \'./plugins/bootstrap-vue\'')
30+
31+
return src
32+
})
33+
34+
// Add polyfill
35+
if (opts.usePolyfill) {
36+
helpers.updateBabelConfig(cfg => {
37+
if (!cfg.presets) return cfg
38+
39+
const vuePresetIndex = cfg.presets.findIndex(p => Array.isArray(p) ? p[0] === '@vue/app' : p === '@vue/app')
40+
const isArray = Array.isArray(cfg.presets[vuePresetIndex])
41+
42+
if (vuePresetIndex < 0) return cfg
43+
44+
if (isArray) {
45+
cfg.presets[vuePresetIndex][1]['useBuiltIns'] = 'entry'
46+
} else {
47+
cfg.presets[vuePresetIndex] = [
48+
'@vue/app',
49+
{
50+
useBuiltIns: 'entry'
51+
}
52+
]
53+
}
54+
55+
return cfg
56+
})
57+
58+
helpers.updateMain(src => {
59+
if (!src.find(l => l.match(/^(import|require).*@babel\/polyfill.*$/))) {
60+
src.unshift('import \'@babel/polyfill\'')
61+
}
62+
63+
return src
64+
})
65+
}
66+
})
67+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Vue from 'vue'
2+
3+
import BootstrapVue from 'bootstrap-vue'
4+
import 'bootstrap/dist/css/bootstrap.min.css'
5+
import 'bootstrap-vue/dist/bootstrap-vue.css'
6+
7+
Vue.use(BootstrapVue)

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = (api, opts) => {}

logo.png

1.99 KB
Loading

0 commit comments

Comments
 (0)