Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 0915832

Browse files
thghznck
authored andcommitted
Add <style> support
1 parent e68e918 commit 0915832

File tree

5 files changed

+125
-17
lines changed

5 files changed

+125
-17
lines changed

dist/rollup-plugin-vue.common.js

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ var htmlMinifier = _interopDefault(require('html-minifier'));
1313
var parse5 = _interopDefault(require('parse5'));
1414
var validateTemplate = _interopDefault(require('vue-template-validator'));
1515
var path = require('path');
16+
var fs = require('fs');
17+
var humanSize = _interopDefault(require('human-size'));
1618

1719
var options = {
1820
htmlMinifier: {
@@ -126,31 +128,82 @@ function vueTransform (code, filePath) {
126128
throw new Error('There must be at least one script tag or one template tag per *.vue file.')
127129
}
128130

129-
// 4. Process style
131+
// 4. Process template
132+
var template = processTemplate(nodes.template, filePath, code)
133+
134+
// 5. Process script
135+
var output = {
136+
js: processScript(nodes.script, filePath, code, template)
137+
}
138+
139+
// 6. Process style
130140
if (nodes.style) {
131-
console.warn('<style> is not yet supported')
141+
output.css = parse5.serialize(nodes.style)
142+
output.cssLang = checkLang(nodes.style)
132143
}
133144

134-
// 5. Process template
135-
var template = processTemplate(nodes.template, filePath, code)
145+
return output
146+
}
147+
148+
function writeStyles (content, lang, bundle) {
149+
// Merge content and lang
150+
var data = {}
151+
for (var key in content) {
152+
data[lang[key]] = (data[lang[key]] || '') + content[key]
153+
}
136154

137-
// 6. Process script
138-
return processScript(nodes.script, filePath, code, template)
155+
// Write files
156+
for (var key$1 in data) {
157+
var ext = '.' + key$1
158+
var dest = bundle.replace('.js', ext)
159+
if (dest.indexOf(ext) === -1) {
160+
dest += ext
161+
}
162+
console.log('Writing', humanSize(data[key$1].length), 'to', dest)
163+
fs.writeFileSync(dest, data[key$1])
164+
}
139165
}
140166

141167
function vue (options) {
142168
if ( options === void 0 ) options = {};
143169

144170
var filter = rollupPluginutils.createFilter(options.include, options.exclude)
171+
var cssMap = {}
172+
var cssLang = {}
173+
var dest = 'bundle.js'
145174

146175
return {
147176
name: 'vue',
148-
transform: function transform (code, id) {
177+
options: function options$1 (options) {
178+
// Get the bundle destination
179+
dest = options.dest
180+
},
181+
transform: function transform (source, id) {
149182
if (!filter(id) || !id.endsWith('.vue')) {
150183
return null
151184
}
152185

153-
return vueTransform(code, id)
186+
var ref = vueTransform(source, id)
187+
188+
// Map of every stylesheet
189+
cssMap[id] = ref.css || ''
190+
191+
// Last custom style language
192+
cssLang[id] = ref.cssLang || 'css'
193+
194+
// Script with inlined template
195+
return ref.js
196+
},
197+
banner: function banner () {
198+
// Abusing the banner method to write styles
199+
var count = 0
200+
for (var key in cssMap) {
201+
count += cssMap[key].length
202+
}
203+
if (count) {
204+
writeStyles(cssMap, cssLang, dest)
205+
}
206+
return ''
154207
}
155208
}
156209
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"dependencies": {
2727
"de-indent": "latest",
2828
"html-minifier": "latest",
29+
"human-size": "^1.1.0",
2930
"parse5": "latest",
3031
"rollup-pluginutils": "latest",
3132
"vue-template-validator": "latest"

src/index.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
11
import {createFilter} from 'rollup-pluginutils'
22
import vueTransform from './vueTransform'
3+
import writeStyles from './writeStyles'
34

45
export default function vue (options = {}) {
56
let filter = createFilter(options.include, options.exclude)
7+
let cssMap = {}
8+
let cssLang = {}
9+
let dest = 'bundle.js'
610

711
return {
812
name: 'vue',
9-
transform (code, id) {
13+
options (options) {
14+
// Get the bundle destination
15+
dest = options.dest
16+
},
17+
transform (source, id) {
1018
if (!filter(id) || !id.endsWith('.vue')) {
1119
return null
1220
}
1321

14-
return vueTransform(code, id)
22+
var ref = vueTransform(source, id)
23+
24+
// Map of every stylesheet
25+
cssMap[id] = ref.css || ''
26+
27+
// Last custom style language
28+
cssLang[id] = ref.cssLang || 'css'
29+
30+
// Script with inlined template
31+
return ref.js
32+
},
33+
banner () {
34+
// Abusing the banner method to write styles
35+
var count = 0
36+
for (let key in cssMap) {
37+
count += cssMap[key].length
38+
}
39+
if (count) {
40+
writeStyles(cssMap, cssLang, dest)
41+
}
42+
return ''
1543
}
1644
}
1745
}

src/vueTransform.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,19 @@ export default function vueTransform (code, filePath) {
102102
throw new Error('There must be at least one script tag or one template tag per *.vue file.')
103103
}
104104

105-
// 4. Process style
106-
if (nodes.style) {
107-
console.warn('<style> is not yet supported')
105+
// 4. Process template
106+
const template = processTemplate(nodes.template, filePath, code)
107+
108+
// 5. Process script
109+
const output = {
110+
js: processScript(nodes.script, filePath, code, template)
108111
}
109112

110-
// 5. Process template
111-
const template = processTemplate(nodes.template, filePath, code)
113+
// 6. Process style
114+
if (nodes.style) {
115+
output.css = parse5.serialize(nodes.style)
116+
output.cssLang = checkLang(nodes.style)
117+
}
112118

113-
// 6. Process script
114-
return processScript(nodes.script, filePath, code, template)
119+
return output
115120
}

src/writeStyles.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {writeFileSync} from 'fs'
2+
import humanSize from 'human-size'
3+
4+
export default function writeStyles (content, lang, bundle) {
5+
// Merge content and lang
6+
var data = {}
7+
for (let key in content) {
8+
data[lang[key]] = (data[lang[key]] || '') + content[key]
9+
}
10+
11+
// Write files
12+
for (let key in data) {
13+
let ext = '.' + key
14+
let dest = bundle.replace('.js', ext)
15+
if (dest.indexOf(ext) === -1) {
16+
dest += ext
17+
}
18+
console.log('Writing', humanSize(data[key].length), 'to', dest)
19+
writeFileSync(dest, data[key])
20+
}
21+
}

0 commit comments

Comments
 (0)