|
| 1 | +// Imports |
| 2 | +const fs = require('fs') |
| 3 | + |
| 4 | +// Check for existence of file and add import |
| 5 | +function addImport (api, file, data, folder, end) { |
| 6 | + // sass & scss file types |
| 7 | + for (const ext of ['sass', 'scss']) { |
| 8 | + const path = `${folder}/${file}.${ext}` |
| 9 | + |
| 10 | + // If file doesn't exist in user |
| 11 | + // project, continue to next |
| 12 | + if (!fileExists(api, `src/${path}`)) continue |
| 13 | + |
| 14 | + // If file exists, push it into |
| 15 | + // the import statement |
| 16 | + data.push(`@import '@/${path}${end}`) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +// Go through each folder and add available imports |
| 21 | +function addImports (api, file, data, end) { |
| 22 | + // supported folders that can contain |
| 23 | + // a variables or lists file |
| 24 | + for (const folder of ['sass', 'scss', 'styles']) { |
| 25 | + addImport(api, file, data, folder, end) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// Check if file exists in user project |
| 30 | +function fileExists (api, file) { |
| 31 | + return fs.existsSync(api.resolve(file)) |
| 32 | +} |
| 33 | + |
| 34 | +// Create an import statement |
| 35 | +// to bootstrap a users variables |
| 36 | +function mergeRules (api, opt, ext) { |
| 37 | + const data = [] |
| 38 | + const end = ext === 'sass' ? "'" : "';" |
| 39 | + |
| 40 | + addImports(api, 'variables', data, end) |
| 41 | + |
| 42 | + // Vuetify styles must go between variables and lists |
| 43 | + // since variables must be defined at the top level |
| 44 | + // and lists require existing variables to merge |
| 45 | + data.push(`@import '~vuetify/src/styles/styles.sass${end}`) |
| 46 | + |
| 47 | + addImports(api, 'lists', data, end) |
| 48 | + |
| 49 | + opt.data = data.join('\n') |
| 50 | + |
| 51 | + return opt |
| 52 | +} |
| 53 | + |
| 54 | +module.exports = { |
| 55 | + addImport, |
| 56 | + addImports, |
| 57 | + fileExists, |
| 58 | + mergeRules, |
| 59 | +} |
0 commit comments