Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9985f1f

Browse files
committed
autoprefixer + new configuration API
1 parent 9bc4901 commit 9985f1f

File tree

6 files changed

+58
-16
lines changed

6 files changed

+58
-16
lines changed

‎lib/loader.js

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var loaderUtils = require('loader-utils')
2+
var assign = require('object-assign')
23
var selectorPath = require.resolve('./selector')
34
var parserPath = require.resolve('./parser')
45

@@ -15,23 +16,21 @@ var defaultLoaders = {
1516
}
1617

1718
var rewriters = {
18-
template: require.resolve('./template-rewriter') + '!',
19-
style: require.resolve('./style-rewriter') + '!'
19+
template: require.resolve('./template-rewriter'),
20+
style: require.resolve('./style-rewriter')
2021
}
2122

2223
module.exports = function (content) {
2324
var self = this
2425
this.cacheable()
2526
var cb = this.async()
2627
var output = ''
28+
var options = this.options.vue || {}
2729
var vueUrl = loaderUtils.getRemainingRequest(this)
2830

2931
// check if there are custom loaders specified with
3032
// vueLoader.withLoaders(), otherwise use defaults
31-
var loaders = loaderUtils.parseQuery(this.query)
32-
loaders.html = loaders.html || defaultLoaders.html
33-
loaders.css = loaders.css || defaultLoaders.css
34-
loaders.js = loaders.js || defaultLoaders.js
33+
var loaders = assign({}, defaultLoaders, options.loaders)
3534

3635
function getRequire (type, part, index, scoped) {
3736
return 'require(' +
@@ -64,8 +63,8 @@ module.exports = function (content) {
6463

6564
function getLoaderString (type, part, scoped) {
6665
var lang = part.lang || defaultLang[type]
67-
var rewriter = scoped ? rewriters[type] || '' : ''
6866
var loader = loaders[lang]
67+
var rewriter = getRewriter(type, scoped)
6968
if (loader !== undefined) {
7069
// lang with default or pre-configured loader
7170
if (loader) loader += '!'
@@ -83,6 +82,17 @@ module.exports = function (content) {
8382
}
8483
}
8584

85+
function getRewriter (type, scoped) {
86+
switch (type) {
87+
case 'template':
88+
return scoped ? (rewriters.template + '!') : ''
89+
case 'style':
90+
return rewriters.style + (scoped ? '?scoped=true!' : '!')
91+
default:
92+
return ''
93+
}
94+
}
95+
8696
function getSelectorString (type, index) {
8797
return selectorPath +
8898
'?type=' + type +
@@ -164,10 +174,9 @@ module.exports = function (content) {
164174
})
165175
}
166176

167-
/**
168-
* Expose a way to specify custom loaders to be used at the
169-
* end for the extracted parts of a component.
170-
*/
171-
module.exports.withLoaders = function (opts) {
172-
return 'vue?' + JSON.stringify(opts).replace(/!/g, '\\u0021')
177+
module.exports.withLoaders = function () {
178+
throw new Error(
179+
'vue.withLoaders has been deprecated in vue-loader 6.0. ' +
180+
'Add a "vue" section to the webpack config instead.'
181+
)
173182
}

‎lib/style-rewriter.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var postcss = require('postcss')
22
var selectorParser = require('postcss-selector-parser')
33
var hash = require('hash-sum')
4+
var loaderUtils = require('loader-utils')
45

56
var currentId
67
var addId = postcss.plugin('add-id', function () {
@@ -20,8 +21,23 @@ var addId = postcss.plugin('add-id', function () {
2021
module.exports = function (css) {
2122
this.cacheable()
2223
var cb = this.async()
24+
25+
var query = loaderUtils.parseQuery(this.query)
26+
var options = this.options.vue || {}
27+
var autoprefixOptions = options.autoprefix
28+
29+
var processors = []
30+
if (query.scoped) {
31+
processors.push(addId)
32+
}
33+
if (autoprefixOptions !== false) {
34+
autoprefixOptions = autoprefixOptions || { remove: false }
35+
var autoprefixer = require('autoprefixer')(autoprefixOptions)
36+
processors.push(autoprefixer)
37+
}
38+
2339
currentId = '_v-' + hash(this.resourcePath)
24-
postcss([addId])
40+
postcss(processors)
2541
.process(css)
2642
.then(function (result) {
2743
cb(null, result)

‎package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
"test": "eslint lib && mocha test/test.js --slow 5000"
2424
},
2525
"dependencies": {
26+
"autoprefixer": "^6.0.3",
2627
"hash-sum": "^1.0.2",
2728
"loader-utils": "^0.2.10",
29+
"object-assign": "^3.0.0",
2830
"parse5": "^1.5.0",
29-
"postcss": "^4.1.16",
31+
"postcss": "^5.0.10",
3032
"postcss-selector-parser": "^1.1.2"
3133
},
3234
"peerDependencies": {
@@ -49,7 +51,6 @@
4951
"mkdirp": "^0.5.1",
5052
"mocha": "^2.2.5",
5153
"node-libs-browser": "^0.5.3",
52-
"object-assign": "^3.0.0",
5354
"rimraf": "^2.4.0",
5455
"source-map": "^0.5.1",
5556
"style-loader": "^0.13.0",

‎test/fixtures/autoprefix.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
window.testModule = require('./autoprefix.vue')

‎test/fixtures/autoprefix.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<style>
2+
body {
3+
transform: scale(1);
4+
}
5+
</style>

‎test/test.js

+10
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,14 @@ describe('vue-loader', function () {
148148
})
149149
})
150150

151+
it('autoprefix', function (done) {
152+
test({
153+
entry: './test/fixtures/autoprefix.js'
154+
}, function (window) {
155+
var style = window.document.querySelector('style').textContent
156+
expect(style).to.contain('body {\n -webkit-transform: scale(1);\n -ms-transform: scale(1);\n transform: scale(1);\n}')
157+
done()
158+
})
159+
})
160+
151161
})

0 commit comments

Comments
 (0)