Skip to content

Commit 3c4361c

Browse files
committed
fix: split imports to variables and lists
different variable types require different overrides. single variables above vuetify styles and merges below
1 parent 033e8bf commit 3c4361c

File tree

7 files changed

+1662
-54
lines changed

7 files changed

+1662
-54
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/node_modules/
22
/dev/
3+
/coverage/
34

45
*.swp
56

index.js

+5-24
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
4-
function resolve (dir) {
5-
return path.join(__dirname, '..', dir)
6-
}
7-
8-
function mergeRules (opt, sass = true, type) {
9-
const end = sass ? "'" : "';"
10-
11-
opt.data = `@import '~vuetify/src/styles/styles.sass${end}`
12-
opt.data += `\n@import '@/sass/variables.${type}${end}`
13-
14-
return opt
15-
}
1+
// Imports
2+
const { mergeRules } = require('./util/helpers')
163

174
module.exports = (api) => {
185
const dependencies = api.service.pkg.dependencies || {}
@@ -69,23 +56,17 @@ module.exports = (api) => {
6956
}
7057

7158
// Bootstrap SASS Variables
72-
const hasSassVariables = fs.existsSync(api.resolve('src/sass/variables.sass'))
73-
const hasScssVariables = fs.existsSync(api.resolve('src/sass/variables.scss'))
74-
75-
if (!hasSassVariables && !hasScssVariables) return
76-
77-
const type = hasSassVariables ? 'sass' : 'scss'
78-
7959
api.chainWebpack(config => {
8060
['vue-modules', 'vue', 'normal-modules', 'normal'].forEach(match => {
8161
for (let i = 0; i < 2; i++) {
8262
const boolean = Boolean(i)
63+
const rule = boolean ? 'sass' : 'scss'
8364

8465
config.module
85-
.rule(boolean ? 'sass' : 'scss')
66+
.rule(rule)
8667
.oneOf(match)
8768
.use('sass-loader')
88-
.tap(opt => mergeRules(opt, boolean, type))
69+
.tap(opt => mergeRules(api, opt, rule))
8970
}
9071
})
9172
})

package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Vuetify Framework Plugin for Vue CLI 3",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "jest"
88
},
99
"repository": {
1010
"type": "git",
@@ -29,10 +29,17 @@
2929
"devDependencies": {
3030
"@release-it/conventional-changelog": "^1.1.0",
3131
"@vue/cli-service": "^3.11.0",
32+
"husky": "^3.0.8",
33+
"jest": "^24.9.0",
3234
"vuetify": "^2.1.0",
3335
"vuetify-loader": "^1.2.2"
3436
},
3537
"dependencies": {
3638
"shelljs": "^0.8.3"
39+
},
40+
"husky": {
41+
"hooks": {
42+
"pre-commit": "yarn test"
43+
}
3744
}
3845
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`helpers.js should merge sass variable rules 1`] = `
4+
Object {
5+
"data": "@import '@/sass/variables.sass'
6+
@import '@/sass/variables.scss'
7+
@import '@/scss/variables.sass'
8+
@import '@/scss/variables.scss'
9+
@import '@/styles/variables.sass'
10+
@import '@/styles/variables.scss'
11+
@import '~vuetify/src/styles/styles.sass'
12+
@import '@/sass/lists.sass'
13+
@import '@/sass/lists.scss'
14+
@import '@/scss/lists.sass'
15+
@import '@/scss/lists.scss'
16+
@import '@/styles/lists.sass'
17+
@import '@/styles/lists.scss'",
18+
}
19+
`;
20+
21+
exports[`helpers.js should merge sass variable rules 2`] = `
22+
Object {
23+
"data": "@import '@/sass/variables.sass';
24+
@import '@/sass/variables.scss';
25+
@import '@/scss/variables.sass';
26+
@import '@/scss/variables.scss';
27+
@import '@/styles/variables.sass';
28+
@import '@/styles/variables.scss';
29+
@import '~vuetify/src/styles/styles.sass';
30+
@import '@/sass/lists.sass';
31+
@import '@/sass/lists.scss';
32+
@import '@/scss/lists.sass';
33+
@import '@/scss/lists.scss';
34+
@import '@/styles/lists.sass';
35+
@import '@/styles/lists.scss';",
36+
}
37+
`;

util/__tests__/helpers.spec.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const helpers = require('../helpers')
2+
const fs = require('fs')
3+
4+
jest.mock('fs')
5+
6+
describe('helpers.js', () => {
7+
const api = {
8+
resolve: val => val
9+
}
10+
11+
afterEach(() => {
12+
fs.existsSync.mockReturnValue(false)
13+
})
14+
15+
it('should check if file exists', () => {
16+
fs.existsSync.mockReturnValue(true)
17+
18+
expect(helpers.fileExists(api, 'foobar')).toBe(true)
19+
20+
fs.existsSync.mockReturnValue(false)
21+
22+
expect(helpers.fileExists(api, 'foobar')).toBe(false)
23+
})
24+
25+
it('should add an import to the data array', () => {
26+
fs.existsSync.mockReturnValue(true)
27+
28+
const data = []
29+
30+
helpers.addImport(api, 'foobar', data, 'variables', "'")
31+
helpers.addImport(api, 'fizzbuzz', data, 'lists', "'")
32+
33+
expect(data.length).toBe(4)
34+
35+
fs.existsSync.mockReturnValue(false)
36+
37+
helpers.addImport(api, 'foobar1', data, 'variables', "'")
38+
helpers.addImport(api, 'fizzbuzz1', data, 'lists', "'")
39+
40+
expect(data.length).toBe(4)
41+
})
42+
43+
it('should add imports to the data array', () => {
44+
fs.existsSync.mockReturnValue(true)
45+
46+
const data = []
47+
48+
helpers.addImports(api, 'foobar', data, "';")
49+
50+
expect(data.length).toBe(6)
51+
})
52+
53+
it('should merge sass variable rules', () => {
54+
fs.existsSync.mockReturnValue(true)
55+
56+
const opt = {}
57+
58+
helpers.mergeRules(api, opt, 'sass')
59+
60+
expect(opt).toMatchSnapshot()
61+
62+
const opt2 = {}
63+
64+
helpers.mergeRules(api, opt2, 'scss')
65+
66+
expect(opt2).toMatchSnapshot()
67+
})
68+
})

util/helpers.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)