|
1 | 1 | const { resolve } = require('path')
|
2 | 2 |
|
3 |
| -function pickFirst(...args) { |
4 |
| - for (const arg of args) { |
5 |
| - if (arg !== undefined) { |
6 |
| - return arg |
| 3 | +module.exports = function nuxtBootstrapVue(moduleOptions = {}) { |
| 4 | + this.nuxt.hook('build:before', () => { |
| 5 | + function pickFirst(...args) { |
| 6 | + for (const arg of args) { |
| 7 | + if (arg !== undefined) { |
| 8 | + return arg |
| 9 | + } |
| 10 | + } |
7 | 11 | }
|
8 |
| - } |
9 |
| -} |
10 | 12 |
|
11 |
| -module.exports = function nuxtBootstrapVue(moduleOptions = {}) { |
12 |
| - // Merge moduleOptions with default |
13 |
| - const options = { |
14 |
| - ...this.options.bootstrapVue, |
15 |
| - ...moduleOptions |
16 |
| - } |
17 |
| - |
18 |
| - const bootstrapVueCSS = pickFirst( |
19 |
| - options.bootstrapVueCSS, |
20 |
| - options.bootstrapVueCss, |
21 |
| - options.bvCSS, |
22 |
| - // Defaults to `true` if no other options provided |
23 |
| - true |
24 |
| - ) |
25 |
| - if (bootstrapVueCSS) { |
26 |
| - // Add BootstrapVue CSS |
27 |
| - this.options.css.unshift('bootstrap-vue/dist/bootstrap-vue.css') |
28 |
| - } |
29 |
| - |
30 |
| - const bootstrapCSS = pickFirst( |
31 |
| - options.bootstrapCSS, |
32 |
| - options.bootstrapCss, |
33 |
| - options.css, |
34 |
| - // Defaults to `true` if no other options provided |
35 |
| - true |
36 |
| - ) |
37 |
| - if (bootstrapCSS) { |
38 |
| - // Add Bootstrap CSS before BootstrapVue CSS |
39 |
| - this.options.css.unshift('bootstrap/dist/css/bootstrap.css') |
40 |
| - } |
41 |
| - |
42 |
| - // Transpile src |
43 |
| - this.options.build.transpile.push('bootstrap-vue/src') |
44 |
| - |
45 |
| - // Register plugin, pasing options to plugin template |
46 |
| - this.addPlugin({ |
47 |
| - src: resolve(__dirname, 'plugin.template.js'), |
48 |
| - fileName: 'bootstrap-vue.js' |
| 13 | + const kebabCase = str => |
| 14 | + str.replace( |
| 15 | + /([_\s]+([a-zA-Z])|([A-Z]))/g, |
| 16 | + (m, $1, $2, $3, o) => (o ? '-' : '') + ($2 || $3 || '').toLowerCase() |
| 17 | + ) |
| 18 | + const pascalCase = str => str.replace(/(^|[-_\s]+)(.)/g, (m, $1, $2) => $2.toUpperCase()) |
| 19 | + |
| 20 | + // Merge moduleOptions with default |
| 21 | + const options = { |
| 22 | + ...this.options.bootstrapVue, |
| 23 | + ...moduleOptions |
| 24 | + } |
| 25 | + |
| 26 | + const bootstrapVueCSS = pickFirst( |
| 27 | + options.bootstrapVueCSS, |
| 28 | + options.bootstrapVueCss, |
| 29 | + options.bvCSS, |
| 30 | + // Defaults to `true` if no other options provided |
| 31 | + true |
| 32 | + ) |
| 33 | + if (bootstrapVueCSS) { |
| 34 | + // Add BootstrapVue CSS |
| 35 | + this.options.css.unshift('bootstrap-vue/dist/bootstrap-vue.css') |
| 36 | + } |
| 37 | + |
| 38 | + const bootstrapCSS = pickFirst( |
| 39 | + options.bootstrapCSS, |
| 40 | + options.bootstrapCss, |
| 41 | + options.css, |
| 42 | + // Defaults to `true` if no other options provided |
| 43 | + true |
| 44 | + ) |
| 45 | + if (bootstrapCSS) { |
| 46 | + // Add Bootstrap CSS |
| 47 | + this.options.css.unshift('bootstrap/dist/css/bootstrap.css') |
| 48 | + } |
| 49 | + |
| 50 | + // Transpile src |
| 51 | + this.options.build.transpile.push('bootstrap-vue/src') |
| 52 | + |
| 53 | + const templateOptions = {} |
| 54 | + |
| 55 | + // TODO: also add support for individual components & directives |
| 56 | + for (const type of ['componentPlugins', 'directivePlugins']) { |
| 57 | + const bvPlugins = Array.isArray(options[type]) ? options[type] : [] |
| 58 | + |
| 59 | + templateOptions[type] = bvPlugins |
| 60 | + // convert everything to kebab |
| 61 | + .map(p => kebabCase(p)) |
| 62 | + // remove duplicate items |
| 63 | + .filter((p, i, arr) => arr.indexOf(p) === i) |
| 64 | + .map(pluginDir => { |
| 65 | + const moduleName = (type === 'directivePlugins' ? 'v' : '') + pascalCase(pluginDir) |
| 66 | + return [moduleName, pluginDir] |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + // Register plugin, pasing options to plugin template |
| 71 | + this.addPlugin({ |
| 72 | + src: resolve(__dirname, 'plugin.template.js'), |
| 73 | + fileName: 'bootstrap-vue.js', |
| 74 | + options: templateOptions |
| 75 | + }) |
49 | 76 | })
|
50 | 77 | }
|
51 | 78 |
|
|
0 commit comments