Skip to content

Commit c7a8e4c

Browse files
committed
support for custom transformers
1 parent 4c42e1b commit c7a8e4c

14 files changed

+343
-153
lines changed

e2e/__projects__/babel-config/__snapshots__/test.js.snap

+3-1
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ render._withStripped = true
138138
__options__.render = render
139139
__options__.staticRenderFns = staticRenderFns
140140
141-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkJhc2ljU3JjLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUhBO0FBS0E7QUFQQTtBQVNBO0FBQ0E7QUFDQTtBQUNBO0FBRkE7QUFJQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBSEE7QUFqQkEiLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCB7XG4gIG5hbWU6ICdiYXNpYycsXG4gIGNvbXB1dGVkOiB7XG4gICAgaGVhZGluZ0NsYXNzZXM6IGZ1bmN0aW9uIGhlYWRpbmdDbGFzc2VzKCkge1xuICAgICAgcmV0dXJuIHtcbiAgICAgICAgcmVkOiB0aGlzLmlzQ3JhenksXG4gICAgICAgIGJsdWU6ICF0aGlzLmlzQ3JhenksXG4gICAgICAgIHNoYWRvdzogdGhpcy5pc0NyYXp5XG4gICAgICB9XG4gICAgfVxuICB9LFxuICBkYXRhOiBmdW5jdGlvbiBkYXRhKCkge1xuICAgIHJldHVybiB7XG4gICAgICBtc2c6ICdXZWxjb21lIHRvIFlvdXIgVnVlLmpzIEFwcCcsXG4gICAgICBpc0NyYXp5OiBmYWxzZVxuICAgIH1cbiAgfSxcbiAgbWV0aG9kczoge1xuICAgIHRvZ2dsZUNsYXNzOiBmdW5jdGlvbiB0b2dnbGVDbGFzcygpIHtcbiAgICAgIHRoaXMuaXNDcmF6eSA9ICF0aGlzLmlzQ3JhenlcbiAgICB9XG4gIH1cbn1cbiJdfQ=="
141+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkJhc2ljU3JjLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUhBO0FBS0E7QUFQQTtBQVNBO0FBQ0E7QUFDQTtBQUNBO0FBRkE7QUFJQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBSEE7QUFqQkEiLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCB7XG4gIG5hbWU6ICdiYXNpYycsXG4gIGNvbXB1dGVkOiB7XG4gICAgaGVhZGluZ0NsYXNzZXM6IGZ1bmN0aW9uIGhlYWRpbmdDbGFzc2VzKCkge1xuICAgICAgcmV0dXJuIHtcbiAgICAgICAgcmVkOiB0aGlzLmlzQ3JhenksXG4gICAgICAgIGJsdWU6ICF0aGlzLmlzQ3JhenksXG4gICAgICAgIHNoYWRvdzogdGhpcy5pc0NyYXp5XG4gICAgICB9XG4gICAgfVxuICB9LFxuICBkYXRhOiBmdW5jdGlvbiBkYXRhKCkge1xuICAgIHJldHVybiB7XG4gICAgICBtc2c6ICdXZWxjb21lIHRvIFlvdXIgVnVlLmpzIEFwcCcsXG4gICAgICBpc0NyYXp5OiBmYWxzZVxuICAgIH1cbiAgfSxcbiAgbWV0aG9kczoge1xuICAgIHRvZ2dsZUNsYXNzOiBmdW5jdGlvbiB0b2dnbGVDbGFzcygpIHtcbiAgICAgIHRoaXMuaXNDcmF6eSA9ICF0aGlzLmlzQ3JhenlcbiAgICB9XG4gIH1cbn0iXX0="
142142
`;
143143

144+
exports[`processes PostCSS 1`] = `"<section><div></div> <div class=\\"red\\"></div></section>"`;
145+
144146
exports[`processes SCSS using user specified post transforms 1`] = `"<div class=\\"testA\\"><span class=\\"g\\"></span> <span class=\\"f\\"></span></div>"`;
145147
146148
exports[`processes SCSS using user specified pre transforms 1`] = `"<div class=\\"testA\\"><span class=\\"g\\"></span> <span class=\\"f\\"></span></div>"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { createTransformer } = require('babel-jest')
2+
module.exports = createTransformer({
3+
presets: ['@babel/preset-env'],
4+
plugins: ['transform-vue-jsx']
5+
})
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
<template>
2-
<div :class="$style.testPcss"></div>
2+
<section>
3+
<div :class="$style.testPcss"></div>
4+
<div :class="$style.red"></div>
5+
</section>
36
</template>
47

58
<style lang="postcss">
69
.testPcss {
7-
background-color: purple;
10+
background: color(purple a(90%));
811
}
912
</style>
1013

1114
<style module lang="pcss">
12-
/* This syntax is for postcss-custom-properties */
13-
--primary-color: green;
14-
15-
/* This syntax is for postcss-nesting, but invalid as Pure CSS */
16-
body {
17-
@media screen {
18-
background-color: grey;
19-
}
15+
.red {
16+
background: color(red a(90%));
2017
}
2118
</style>

e2e/__projects__/babel-config/cssPostTransform.js

-17
This file was deleted.

e2e/__projects__/babel-config/cssPreTransform.js

-4
This file was deleted.

e2e/__projects__/babel-config/package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"babel-plugin-transform-vue-jsx": "^3.7.0",
2222
"jest": "^23.6.0",
2323
"node-sass": "^4.11.0",
24+
"postcss": "^7.0.13",
25+
"postcss-color-function": "^4.0.1",
2426
"vue-jest": "file:../../../"
2527
},
2628
"jest": {
@@ -41,11 +43,10 @@
4143
"pug": {
4244
"basedir": "./"
4345
},
44-
"cssTransform": {
45-
"scss": {
46-
"post": "./cssPostTransform.js",
47-
"pre": "./cssPreTransform.js"
48-
}
46+
"transform": {
47+
"^scss$": "./scssTransform.js",
48+
"^pcss|postcss$": "./pcssTransform.js",
49+
"^js$": "./babel-transformer.js"
4950
}
5051
}
5152
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const postcss = require('postcss')
2+
var colorFunction = require('postcss-color-function')
3+
module.exports = {
4+
process: function(content, filepath, config, attrs) {
5+
return postcss([colorFunction()]).process(content).css
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const cssExtract = require('extract-from-css')
2+
module.exports = {
3+
postProcess: function postProcess(src, filepath, config, attrs) {
4+
const cssNames = cssExtract.extractClasses(src)
5+
const obj = {}
6+
for (let i = 0, l = cssNames.length; i < l; i++) {
7+
obj[cssNames[i]] = cssNames[i]
8+
}
9+
10+
if (attrs.themed) {
11+
return {
12+
light: obj,
13+
dark: obj
14+
}
15+
}
16+
return obj
17+
},
18+
preProcess: function postProcess(src, filepath, config, attrs) {
19+
return `${src}\n .g{width: 10px}`
20+
}
21+
}

e2e/__projects__/babel-config/test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Constructor from './components/Constructor.vue'
2525

2626
test('processes .vue files', () => {
2727
const wrapper = mount(Basic)
28+
expect(wrapper.vm.msg).toEqual('Welcome to Your Vue.js App')
2829
wrapper.vm.toggleClass()
2930
})
3031

@@ -119,7 +120,9 @@ it('processes Less', () => {
119120

120121
it('processes PostCSS', () => {
121122
const wrapper = mount(PostCss)
122-
expect(wrapper.is('div')).toBeTruthy()
123+
expect(wrapper.is('section')).toBeTruthy()
124+
expect(wrapper.vm.$style.red).toEqual('red')
125+
expect(wrapper.html()).toMatchSnapshot()
123126
})
124127

125128
test('processes pug templates', () => {

e2e/test-runner.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ function runTest(dir) {
3333

3434
log('Running tests')
3535

36-
log('Removing node_modules')
37-
fs.removeSync(`${resolvedPath}/node_modules`)
36+
// log('Removing node_modules')
37+
// fs.removeSync(`${resolvedPath}/node_modules`)
3838

39-
log('Removing package-lock.json')
40-
fs.removeSync(`${resolvedPath}/package-lock.json`)
39+
// log('Removing package-lock.json')
40+
// fs.removeSync(`${resolvedPath}/package-lock.json`)
4141

42-
log('Installing node_modules')
43-
run('npm install --silent')
42+
// log('Installing node_modules')
43+
// run('npm install --silent')
4444

45-
log('Running tests')
46-
run('npm run test')
45+
// log('Running tests')
46+
run('npm run test')
4747

4848
success(`(${dir}) Complete`)
4949
}

lib/compilers/typescript-compiler.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ const babelJest = require('babel-jest')
33
const getBabelOptions = require('../utils').getBabelOptions
44
const getTsJestConfig = require('../utils').getTsJestConfig
55
const stripInlineSourceMap = require('../utils').stripInlineSourceMap
6+
const getCustomTransformer = require('../utils').getCustomTransformer
7+
const getVueJestConfig = require('../utils').getVueJestConfig
68

79
module.exports = function compileTypescript(scriptContent, filePath, config) {
810
ensureRequire('typescript', ['typescript'])
911
const typescript = require('typescript')
10-
11-
const { tsconfig } = getTsJestConfig(config)
12+
const vueJestConfig = getVueJestConfig(config)
13+
const {
14+
tsconfig
15+
} = getTsJestConfig(config)
1216
const babelOptions = getBabelOptions(filePath)
1317

1418
const res = typescript.transpileModule(scriptContent, tsconfig)
@@ -26,10 +30,12 @@ module.exports = function compileTypescript(scriptContent, filePath, config) {
2630
plugins: [require('@babel/plugin-transform-modules-commonjs')]
2731
}
2832
}
29-
30-
const transformer = babelJest.createTransformer(
31-
Object.assign(inlineBabelOptions, { inputSourceMap })
33+
const customTransformer = getCustomTransformer(vueJestConfig['transform'], 'js');
34+
const transformer = customTransformer.process ? customTransformer : babelJest.createTransformer(
35+
Object.assign(inlineBabelOptions, {
36+
inputSourceMap
37+
})
3238
)
3339

3440
return transformer.process(res.outputText, filePath, config)
35-
}
41+
}

0 commit comments

Comments
 (0)