Skip to content

Commit 630575d

Browse files
hewIngrameddyerburgh
authored andcommitted
feat: handle external css (#158)
1 parent da9589e commit 630575d

File tree

6 files changed

+51
-22
lines changed

6 files changed

+51
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div :class="$style.testClass">
3+
<div class="a" />
4+
</div>
5+
</template>
6+
7+
<style module src="./styles/external.css" />
8+
9+
<style module="css">
10+
.a {
11+
background: color(red a(90%));
12+
}
13+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.testClass {
2+
background-color: red;
3+
}

e2e/__projects__/style/test.js

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Scss from './components/Scss.vue'
44
import Sass from './components/Sass.vue'
55
import Less from './components/Less.vue'
66
import PostCss from './components/PostCss.vue'
7+
import External from './components/External.vue'
78

89
test('processes Less', () => {
910
const wrapper = mount(Less)
@@ -39,3 +40,10 @@ test('process Stylus', () => {
3940
expect(wrapper.vm.css.a).toEqual('a')
4041
expect(wrapper.vm.$style.b).toEqual('b')
4142
})
43+
44+
test('process External', () => {
45+
const wrapper = mount(External)
46+
expect(wrapper.vm).toBeTruthy()
47+
expect(wrapper.vm.$style.testClass).toEqual('testClass')
48+
expect(wrapper.vm.css.a).toEqual('a')
49+
})

lib/process-style.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const compileStyle = require('@vue/component-compiler-utils').compileStyle
66
const applyModuleNameMapper = require('./module-name-mapper-helper')
77
const getCustomTransformer = require('./utils').getCustomTransformer
88
const logResultErrors = require('./utils').logResultErrors
9+
const loadSrc = require('./utils').loadSrc
910

1011
function getGlobalResources(resources, lang) {
1112
let globalResources = ''
@@ -48,9 +49,13 @@ function getPreprocessOptions(lang, filePath, jestConfig) {
4849
}
4950
}
5051

51-
module.exports = function processStyle(stylePart, filename, config = {}) {
52+
module.exports = function processStyle(stylePart, filePath, config = {}) {
5253
const vueJestConfig = getVueJestConfig(config)
5354

55+
if (stylePart.src && !stylePart.content) {
56+
stylePart.content = loadSrc(stylePart.src, filePath)
57+
}
58+
5459
if (vueJestConfig.experimentalCSSCompile === false || !stylePart.content) {
5560
return '{}'
5661
}
@@ -64,21 +69,21 @@ module.exports = function processStyle(stylePart, filename, config = {}) {
6469

6570
// pre process
6671
if (transformer.preprocess) {
67-
content = transformer.preprocess(content, filename, config, stylePart.attrs)
72+
content = transformer.preprocess(content, filePath, config, stylePart.attrs)
6873
}
6974

7075
// transform
7176
if (transformer.process) {
72-
content = transformer.process(content, filename, config, stylePart.attrs)
77+
content = transformer.process(content, filePath, config, stylePart.attrs)
7378
} else {
7479
const preprocessOptions = getPreprocessOptions(
7580
stylePart.lang,
76-
filename,
81+
filePath,
7782
config
7883
)
7984
const result = compileStyle({
8085
source: content,
81-
filename,
86+
filePath,
8287
preprocessLang: stylePart.lang,
8388
preprocessOptions,
8489
scoped: false
@@ -89,7 +94,7 @@ module.exports = function processStyle(stylePart, filename, config = {}) {
8994

9095
// post process
9196
if (transformer.postprocess) {
92-
return transformer.postprocess(content, filename, config, stylePart.attrs)
97+
return transformer.postprocess(content, filePath, config, stylePart.attrs)
9398
}
9499

95100
return JSON.stringify(extractClassMap(content))

lib/process.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ const generateSourceMap = require('./generate-source-map')
55
const typescriptTransformer = require('./typescript-transformer')
66
const coffeescriptTransformer = require('./coffee-transformer')
77
const _processStyle = require('./process-style')
8-
const fs = require('fs')
9-
const path = require('path')
108
const getVueJestConfig = require('./utils').getVueJestConfig
119
const logResultErrors = require('./utils').logResultErrors
1210
const stripInlineSourceMap = require('./utils').stripInlineSourceMap
1311
const getCustomTransformer = require('./utils').getCustomTransformer
14-
const throwError = require('./utils').throwError
12+
const loadSrc = require('./utils').loadSrc
1513
const babelTransformer = require('babel-jest')
1614
const compilerUtils = require('@vue/component-compiler-utils')
1715
const convertSourceMap = require('convert-source-map')
@@ -28,18 +26,6 @@ function resolveTransformer(lang = 'js', vueJestConfig) {
2826
}
2927
}
3028

31-
function loadSrc(src, filePath) {
32-
var dir = path.dirname(filePath)
33-
var srcPath = path.resolve(dir, src)
34-
try {
35-
return fs.readFileSync(srcPath, 'utf-8')
36-
} catch (e) {
37-
throwError(
38-
'Failed to load src: "' + src + '" from file: "' + filePath + '"'
39-
)
40-
}
41-
}
42-
4329
function processScript(scriptPart, filePath, config) {
4430
if (!scriptPart) {
4531
return null

lib/utils.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const loadPartialConfig = require('@babel/core').loadPartialConfig
22
const createTransformer = require('ts-jest').createTransformer
33
const chalk = require('chalk')
44
const path = require('path')
5+
const fs = require('fs')
56

67
const fetchTransformer = function fetchTransformer(key, obj) {
78
for (const exp in obj) {
@@ -114,6 +115,18 @@ const logResultErrors = result => {
114115
}
115116
}
116117

118+
const loadSrc = (src, filePath) => {
119+
var dir = path.dirname(filePath)
120+
var srcPath = path.resolve(dir, src)
121+
try {
122+
return fs.readFileSync(srcPath, 'utf-8')
123+
} catch (e) {
124+
throwError(
125+
'Failed to load src: "' + src + '" from file: "' + filePath + '"'
126+
)
127+
}
128+
}
129+
117130
module.exports = {
118131
stripInlineSourceMap,
119132
throwError,
@@ -126,5 +139,6 @@ module.exports = {
126139
info,
127140
warn,
128141
resolvePath,
129-
fetchTransformer
142+
fetchTransformer,
143+
loadSrc
130144
}

0 commit comments

Comments
 (0)