Skip to content
This repository was archived by the owner on Dec 26, 2018. It is now read-only.

Commit f880235

Browse files
committed
expose options for built-in languages
1 parent 1ab2f5b commit f880235

File tree

10 files changed

+75
-33
lines changed

10 files changed

+75
-33
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,19 @@ The default options used for Babel is:
103103
104104
These options result in faster and smaller built code. This also means when using es6, **you need install both `babel` and `babel-runtime`**.
105105
106-
## Registering Custom Pre-Processors
106+
## Pre-Processor Configuration
107107
108108
Create a `vue.config.js` file at where your build command is run (usually y the root level of your project):
109109
110110
``` js
111111
module.exports = function (compiler) {
112-
113-
// register a compile function for <script lang="es">
112+
113+
// configure the options for a built-in language
114+
compiler.option('sass', {
115+
includePaths: [...]
116+
})
117+
118+
// register a custom compile function for <script lang="es">
114119
compiler.register({
115120
lang: 'es',
116121
type: 'script',
@@ -154,4 +159,6 @@ If you use Webpack, there's also [vue-loader](https://github.com/vuejs/vue-loade
154159
155160
- `es6` transforms now uses loose mode and optional runtime by default. This means in addition to installing `babel`, you should also install `babel-runtime`.
156161
162+
- Options for built-in pre-processors can now be configured in `vue.config.js`.
163+
157164
- `vue-component-compiler` has been merged into `vueify`. It is now exposed as `require('vueify').compiler`.

lib/compilers/coffee.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
var options = require('./options')
2+
13
module.exports = function (raw, cb) {
24
try {
35
var coffee = require('coffee-script')
46
} catch (err) {
57
return cb(err)
68
}
79
try {
8-
var js = coffee.compile(raw, {
9-
bare: true
10-
})
10+
var js = coffee.compile(raw, options.coffee || {})
1111
} catch (err) {
1212
return cb(err)
1313
}
1414
cb(null, js)
15-
}
15+
}

lib/compilers/es6.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
var options = require('./options')
2+
13
module.exports = function (raw, cb) {
24
try {
35
var babel = require('babel')
46
} catch (err) {
57
return cb(err)
68
}
79
try {
8-
var res = babel.transform(raw, {
9-
loose: 'all',
10-
optional: ['runtime']
11-
})
10+
var res = babel.transform(raw, options.es6 || {})
1211
} catch (err) {
1312
return cb(err)
1413
}

lib/compilers/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var options = require('./options')
2+
13
// built-in compilers
24
var compilers = module.exports = {
35
script: {
@@ -50,7 +52,10 @@ compilers.loadConfig = function () {
5052
var configPath = path.resolve(process.cwd(), 'vue.config.js')
5153
if (fs.existsSync(configPath)) {
5254
require(configPath)({
53-
register: compilers.register
55+
register: compilers.register,
56+
option: function (name, opts) {
57+
options[name] = opts
58+
}
5459
})
5560
}
5661
}
@@ -60,4 +65,4 @@ function warn (msg) {
6065
'[vue-component-compiler] Error attempting to' +
6166
'register custom compiler: ' + msg
6267
)
63-
}
68+
}

lib/compilers/jade.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
var options = require('./options')
2+
13
module.exports = function (raw, cb) {
24
try {
35
var jade = require('jade')
46
} catch (err) {
57
return cb(err)
68
}
79
try {
8-
var html = jade.compile(raw)({})
10+
var html = jade.compile(raw)(options.jade || {})
911
} catch (err) {
1012
return cb(err)
1113
}
1214
cb(null, html)
13-
}
15+
}

lib/compilers/less.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
var options = require('./options')
2+
13
module.exports = function (raw, cb) {
24
try {
35
var less = require('less')
46
} catch (err) {
57
return cb(err)
68
}
7-
less.render(raw, function (err, res) {
9+
less.render(raw, options.less || {}, function (err, res) {
810
// Less 2.0 returns an object instead rendered string
911
if (typeof res === 'object') {
1012
res = res.css
1113
}
1214
cb(err, res)
1315
})
14-
}
16+
}

lib/compilers/myth.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
var options = require('./options')
2+
13
module.exports = function (raw, cb) {
24
try {
35
var myth = require('myth')
46
} catch (err) {
57
return cb(err)
68
}
79
try {
8-
var css = myth(raw)
10+
var css = myth(raw, options || {})
911
} catch (err) {
1012
return cb(err)
1113
}

lib/compilers/options.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
es6: {
3+
loose: 'all',
4+
optional: ['runtime']
5+
},
6+
coffee: {
7+
bare: true
8+
},
9+
sass: {
10+
sourceComments: true
11+
}
12+
}

lib/compilers/sass.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
var options = require('./options')
2+
13
module.exports = function (raw, cb) {
24
try {
35
var sass = require('node-sass')
46
} catch (err) {
57
return cb(err)
68
}
7-
sass.render({
9+
10+
var sassOptions = extend({
811
data: raw,
9-
options: {
10-
sourceComments: 'normal'
11-
},
1212
success: function (res) {
1313
if (typeof res === 'object') {
1414
cb(null, res.css)
@@ -19,13 +19,24 @@ module.exports = function (raw, cb) {
1919
error: function (err) {
2020
cb(err)
2121
}
22-
},
23-
// callback for node-sass > 3.0.0
24-
function (err, res) {
25-
if (err) {
26-
cb(err)
27-
} else {
28-
cb(null, res.css.toString())
22+
}, options.sass || {})
23+
24+
sass.render(
25+
sassOptions,
26+
// callback for node-sass > 3.0.0
27+
function (err, res) {
28+
if (err) {
29+
cb(err)
30+
} else {
31+
cb(null, res.css.toString())
32+
}
2933
}
30-
})
31-
}
34+
)
35+
}
36+
37+
function extend (a, b) {
38+
for (var key in b) {
39+
a[key] = b[key]
40+
}
41+
return a
42+
}

lib/compilers/stylus.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
var options = require('./options')
2+
13
module.exports = function (raw, cb) {
24
try {
35
var stylus = require('stylus')
46
} catch (err) {
57
return cb(err)
68
}
7-
stylus.render(raw, {}, cb)
8-
}
9+
stylus.render(raw, options.stylus || {}, cb)
10+
}

0 commit comments

Comments
 (0)