Skip to content

Commit

Permalink
Feature: Lang aliasing
Browse files Browse the repository at this point in the history
= Problem

- Vue automatically handles postcss by default, there is many issues
around using custom postcss lang option.
- Using postcss without a `lang` attribute causes SFC to loose syntax
highlghting

= Solution

- Enable option to alias langs to loader settings.
  - specify `postcss` in lang and maintain default loader configurations
  • Loading branch information
blake-newman committed Nov 12, 2017
1 parent 21fb818 commit 8f0544a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
24 changes: 24 additions & 0 deletions docs/en/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ module.exports = {
Note this is automatically set to `false` if the `devtool` option is not present in the main Webpack config.
### langAlias
- type: `Object`
- default: `{}`
Alias lang attributes. This can be useful for syntax highlighting support (such as postcss) to alias to alternative loaders or default loaders.
``` js
// Webpack 2.x config
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
langAlias: {
postcss: 'css'
}
}
}
]
}
```
### esModule
- type: `boolean`
Expand Down
6 changes: 4 additions & 2 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ module.exports = function (content) {
esModule: true
}, this.options.vue, this.vue, query)

var langAlias = options.langAlias || {}

// disable esModule in inject mode
// because import/export must be top-level
if (query.inject) {
Expand Down Expand Up @@ -480,14 +482,14 @@ module.exports = function (content) {

function getLangString (type, part) {
if (type === 'script' || type === 'template' || type === 'styles') {
return part.lang || defaultLang[type]
return langAlias[part.lang] || part.lang || defaultLang[type]
} else {
return type
}
}

function getRawLoaderString (type, part, index, scoped) {
var lang = part.lang || defaultLang[type]
var lang = langAlias[part.lang] || part.lang || defaultLang[type]

var styleCompiler = ''
if (type === 'styles') {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/lang-alias.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<style lang="postcss">
h1
color: red
font-size: 14px
</style>
23 changes: 22 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function mockRender (options, data) {
}
function e (text = '') {
return {
text: text,
text: text,
isComment: true
}
}
Expand Down Expand Up @@ -496,6 +496,27 @@ describe('vue-loader', function () {
})
})

it('lang alias', done => {
test({
entry: './test/fixtures/postcss.vue',
vue: {
postcss: {
options: {
parser: require('sugarss')
}
},
langAlias: {
postcss: 'css'
}
}
}, (window) => {
var style = window.document.querySelector('style').textContent
style = normalizeNewline(style)
expect(style).to.contain('h1 {\n color: red;\n font-size: 14px\n}')
done()
})
})

it('transpile ES2015 features in template', done => {
test({
entry: './test/fixtures/es2015.vue'
Expand Down

0 comments on commit 8f0544a

Please sign in to comment.