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

Commit 13291e2

Browse files
committed
support CSS extraction
1 parent daf4294 commit 13291e2

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@ module.exports = function vueify (file, options) {
99

1010
var data = ''
1111
var stream = through(write, end)
12+
stream.vueify = true
1213

1314
function dependency(file) {
1415
stream.emit('file', file)
1516
}
1617

18+
function emitStyle (style) {
19+
stream.emit('vueify-style', style)
20+
}
21+
1722
function write(buf) {
1823
data += buf
1924
}
2025

2126
function end () {
2227
stream.emit('file', file)
2328
compiler.on('dependency', dependency)
29+
compiler.on('style', emitStyle)
2430

2531
compiler.compile(data, file, function(error, result) {
2632
compiler.removeListener('dependency', dependency)
33+
compiler.removeListener('style', emitStyle)
2734
if (error) {
2835
stream.emit('error', error)
2936
// browserify doesn't log the stack by default...

lib/compiler.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,16 @@ compiler.compile = function (content, filePath, cb) {
9191
// styles
9292
var style = resolvedParts.styles.join('\n')
9393
if (style) {
94-
style = JSON.stringify(style)
95-
output +=
96-
'var __vueify_style_dispose__ = require("' + insertCSSPath + '").insert(' + style + ')\n'
94+
// emit style
95+
compiler.emit('style', {
96+
file: filePath,
97+
style: style
98+
})
99+
if (!process.env.VUEIFY_EXTRACT_CSS) {
100+
style = JSON.stringify(style)
101+
output +=
102+
'var __vueify_style_dispose__ = require("' + insertCSSPath + '").insert(' + style + ')\n'
103+
}
97104
}
98105
// script
99106
var script = resolvedParts.script
@@ -127,7 +134,9 @@ compiler.compile = function (content, filePath, cb) {
127134
' if (!hotAPI.compatible) return\n' +
128135
' module.hot.accept()\n' +
129136
// remove style tag on dispose
130-
(style ? ' module.hot.dispose(__vueify_style_dispose__)\n' : '') +
137+
(style && !process.env.VUEIFY_EXTRACT_CSS
138+
? ' module.hot.dispose(__vueify_style_dispose__)\n'
139+
: '') +
131140
' if (!module.hot.data) {\n' +
132141
// initial insert
133142
' hotAPI.createRecord("' + id + '", __vue__options__)\n' +

plugins/extract-css.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
process.env.VUEIFY_EXTRACT_CSS = true
2+
3+
var fs = require('fs')
4+
5+
module.exports = function (b, opts) {
6+
var outPath = opts.out || opts.o || 'bundle.css'
7+
var styles = Object.create(null)
8+
9+
b.on('bundle', function (bs) {
10+
bs.on('end', function () {
11+
fs.writeFile(outPath, Object.keys(styles)
12+
.map(function (file) { return styles[file] })
13+
.join('\n'))
14+
})
15+
})
16+
17+
b.on('reset', listen)
18+
listen()
19+
20+
function listen () {
21+
b.on('transform', function (tr, file) {
22+
if (tr.vueify) {
23+
tr.on('vueify-style', function (e) {
24+
styles[e.file] = e.style
25+
})
26+
}
27+
})
28+
}
29+
}

0 commit comments

Comments
 (0)