Skip to content

Commit 583ec9c

Browse files
committed
load vue-jest config only once for all files instead of for each file
1 parent f17f970 commit 583ec9c

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
lines changed

lib/load-config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const path = require('path')
2+
3+
module.exports = function loadConfig () {
4+
5+
let initConfig = {
6+
resources: {}
7+
}
8+
9+
const rootPJSONPath = path.resolve(process.cwd(), 'package.json')
10+
if (!fs.existsSync(rootPJSONPath)) return initConfig
11+
12+
const vueJestConfig = require(rootPJSONPath).vueJest
13+
14+
return Object.assign(initConfig, vueJestConfig)
15+
}

lib/process-style/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const path = require('path')
22
const cssExtract = require('extract-from-css')
33

4-
module.exports = function processStyle (stylePart, filePath) {
4+
module.exports = function processStyle (stylePart, filePath, config) {
55
if (!stylePart) return {}
66

77
const dir = path.dirname(filePath)
8-
const cwd = process.cwd()
9-
const processStyleByLang = lang => require('./' + lang)(stylePart.content, dir, cwd)
8+
const processStyleByLang = lang => require('./' + lang)(stylePart.content, dir, config)
109

1110
let cssCode = ''
1211
switch (stylePart.lang) {

lib/process-style/scss.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
const path = require('path')
22
const fs = require('fs')
33
const sass = require('node-sass')
4+
const cwd = process.cwd()
45

5-
module.exports = (content, dir, cwd) => {
6+
module.exports = (content, dir, config) => {
67

78
const getRelativeImportPath = (oldImportPath, absoluteImportPath) => (/^\~/.test(oldImportPath))
89
? oldImportPath
910
: path.relative(cwd, absoluteImportPath);
1011

11-
const scssResources = require(cwd + '/package.json').vueJest.resources.scss
12-
.map(scssResource => path.resolve(cwd, scssResource))
13-
.filter(scssResourcePath => fs.existsSync(scssResourcePath))
14-
.map(scssResourcePath => fs.readFileSync(scssResourcePath).toString()
15-
.replace(/@import\s+(?:'([^']+)'|"([^"]+)"|([^\s;]+))/g, (entire, single, double, unquoted) => {
16-
var oldImportPath = single || double || unquoted;
17-
var absoluteImportPath = path.join(path.dirname(scssResourcePath), oldImportPath);
18-
var relImportPath = getRelativeImportPath(oldImportPath, absoluteImportPath);
19-
var newImportPath = relImportPath.split(path.sep).join('/');
20-
var lastCharacter = entire[entire.length - 1];
21-
var quote = lastCharacter === "'" || lastCharacter === '"' ? lastCharacter : '';
22-
return '@import ' + quote + newImportPath + quote;
23-
})
24-
)
25-
.join('\n')
12+
const scssResources = (!config.resources || !config.resources.scss)
13+
? ''
14+
: config.resources.scss
15+
.map(scssResource => path.resolve(cwd, scssResource))
16+
.filter(scssResourcePath => fs.existsSync(scssResourcePath))
17+
.map(scssResourcePath => fs.readFileSync(scssResourcePath).toString()
18+
.replace(/@import\s+(?:'([^']+)'|"([^"]+)"|([^\s;]+))/g, (entire, single, double, unquoted) => {
19+
var oldImportPath = single || double || unquoted;
20+
var absoluteImportPath = path.join(path.dirname(scssResourcePath), oldImportPath);
21+
var relImportPath = getRelativeImportPath(oldImportPath, absoluteImportPath);
22+
var newImportPath = relImportPath.split(path.sep).join('/');
23+
var lastCharacter = entire[entire.length - 1];
24+
var quote = lastCharacter === "'" || lastCharacter === '"' ? lastCharacter : '';
25+
return '@import ' + quote + newImportPath + quote;
26+
})
27+
)
28+
.join('\n')
2629

2730
return sass.renderSync({
2831
data: scssResources + content,
2932
outputStyle: 'compressed'
3033
}).css.toString()
31-
34+
3235
}

lib/process-style/stylus.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
const stylus = require('stylus')
2-
module.exports = (content, dir, cwd) => stylus.render(content, { paths: [dir, cwd] })
2+
module.exports = (content, dir, config) => stylus.render(content, { paths: [dir, process.cwd()] })

lib/process.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const join = path.join
1313
const cssExtract = require('extract-from-css')
1414
const logger = require('./logger')
1515
const splitRE = /\r?\n/g
16+
const config = require('./load-config')()
1617

1718
function processScript (scriptPart) {
1819
if (!scriptPart) {
@@ -90,7 +91,8 @@ module.exports = function (src, filePath) {
9091
if (!module) return
9192
const styleObj = (/^sass|less|pcss|postcss/.test(ast.lang))
9293
? {}
93-
: processStyle(ast, filePath)
94+
: processStyle(ast, filePath, config)
95+
9496
const moduleName = ast.module === true ? '$style' : ast.module
9597

9698
return '\n this[\'' + moduleName + '\'] = ' + JSON.stringify(styleObj)

0 commit comments

Comments
 (0)