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

Commit c4ce9a0

Browse files
thghznck
authored andcommitted
Update style behaviour
1 parent e260dcd commit c4ce9a0

File tree

7 files changed

+98
-69
lines changed

7 files changed

+98
-69
lines changed

dist/rollup-plugin-vue.common.js

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
99

10+
var humanSize = _interopDefault(require('human-size'));
1011
var rollupPluginutils = require('rollup-pluginutils');
12+
var fs = require('fs');
1113
var deIndent = _interopDefault(require('de-indent'));
1214
var htmlMinifier = _interopDefault(require('html-minifier'));
1315
var parse5 = _interopDefault(require('parse5'));
1416
var validateTemplate = _interopDefault(require('vue-template-validator'));
1517
var path = require('path');
16-
var fs = require('fs');
17-
var humanSize = _interopDefault(require('human-size'));
1818

1919
var options = {
2020
htmlMinifier: {
@@ -145,25 +145,6 @@ function vueTransform (code, filePath) {
145145
return output
146146
}
147147

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-
}
154-
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-
}
165-
}
166-
167148
function vue (options) {
168149
if ( options === void 0 ) options = {};
169150

@@ -174,10 +155,6 @@ function vue (options) {
174155

175156
return {
176157
name: 'vue',
177-
options: function options$1 (options) {
178-
// Get the bundle destination
179-
dest = options.dest
180-
},
181158
transform: function transform (source, id) {
182159
if (!filter(id) || !id.endsWith('.vue')) {
183160
return null
@@ -194,16 +171,30 @@ function vue (options) {
194171
// Component javascript with inlined html template
195172
return ref.js
196173
},
197-
banner: function banner () {
198-
// Abusing the banner method to write styles
199-
var count = 0
174+
ongenerate: function ongenerate (opts) {
175+
176+
// Combine all stylesheets
177+
var css = ''
200178
for (var key in cssContent) {
201-
count += cssContent[key].length
179+
css += cssContent[key]
202180
}
203-
if (count) {
204-
writeStyles(cssContent, cssLang, dest)
181+
182+
// Emit styles through callback or file
183+
if (typeof options.css === 'function') {
184+
return options.css(css)
185+
}
186+
187+
// Guess destination filename
188+
if (typeof options.css !== 'string') {
189+
dest = opts.dest || 'bundle.js'
190+
if (dest.endsWith('.js')) {
191+
dest = dest.slice(0, -3)
192+
}
193+
options.css = dest + '.css'
205194
}
206-
return ''
195+
196+
console.log('Writing', humanSize(css.length), 'to', options.css)
197+
fs.writeFileSync(options.css, css)
207198
}
208199
}
209200
}

src/index.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import humanSize from 'human-size'
12
import {createFilter} from 'rollup-pluginutils'
3+
import {writeFileSync} from 'fs'
4+
25
import vueTransform from './vueTransform'
3-
import writeStyles from './writeStyles'
46

57
export default function vue (options = {}) {
68
let filter = createFilter(options.include, options.exclude)
@@ -10,10 +12,6 @@ export default function vue (options = {}) {
1012

1113
return {
1214
name: 'vue',
13-
options (options) {
14-
// Get the bundle destination
15-
dest = options.dest
16-
},
1715
transform (source, id) {
1816
if (!filter(id) || !id.endsWith('.vue')) {
1917
return null
@@ -30,16 +28,30 @@ export default function vue (options = {}) {
3028
// Component javascript with inlined html template
3129
return ref.js
3230
},
33-
banner () {
34-
// Abusing the banner method to write styles
35-
var count = 0
31+
ongenerate (opts) {
32+
33+
// Combine all stylesheets
34+
var css = ''
3635
for (let key in cssContent) {
37-
count += cssContent[key].length
36+
css += cssContent[key]
3837
}
39-
if (count) {
40-
writeStyles(cssContent, cssLang, dest)
38+
39+
// Emit styles through callback or file
40+
if (typeof options.css === 'function') {
41+
return options.css(css)
4142
}
42-
return ''
43+
44+
// Guess destination filename
45+
if (typeof options.css !== 'string') {
46+
dest = opts.dest || 'bundle.js'
47+
if (dest.endsWith('.js')) {
48+
dest = dest.slice(0, -3)
49+
}
50+
options.css = dest + '.css'
51+
}
52+
53+
console.log('Writing', humanSize(css.length), 'to', options.css)
54+
writeFileSync(options.css, css)
4355
}
4456
}
4557
}

src/writeStyles.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/expects/imports.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var evens = [2,4,6,8]
2+
var odds = evens.map(v => v + 1)
3+
var Basic = { template: "<h1 :id=id @click=hi>hello</h1><input type=text>",
4+
data() {
5+
return odds
6+
}
7+
}
8+
9+
var imports = { template: "<main><basic></basic></main>",
10+
components: {
11+
Basic
12+
}
13+
}
14+
15+
export default imports;

test/expects/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
input[type=text] {
3+
color: red;
4+
}

test/fixtures/imports.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<main>
3+
<basic></basic>
4+
</main>
5+
</template>
6+
7+
<script>
8+
import Basic from './Basic.vue'
9+
10+
export default {
11+
components: {
12+
Basic
13+
}
14+
}
15+
</script>

test/test.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,36 @@ function test (name) {
1616

1717
var entry = './fixtures/' + name + '.vue'
1818
var expected = read('expects/' + name + '.js').replace(/\r/g, '')
19+
var actualCss
1920
return rollup.rollup({
2021
format: 'cjs',
2122
entry: entry,
22-
plugins: [vuePlugin()]
23+
plugins: [vuePlugin({
24+
css (css) {
25+
actualCss = css
26+
}
27+
})]
2328
}).then(function (bundle) {
2429
var result = bundle.generate()
2530
var code = result.code
26-
assert.equal(code, expected, 'should compile correctly')
31+
assert.equal(code, expected, 'should compile code correctly')
32+
33+
// Check css output
34+
if (name === 'style') {
35+
var css = read('expects/' + name + '.css').replace(/\r/g, '')
36+
assert.equal(actualCss, css, 'should output style tag content')
37+
} else {
38+
assert.equal(actualCss, '', 'should always call css()')
39+
}
2740
}).catch(function (error) {
2841
throw error
2942
})
3043
})
3144
}
3245

3346
describe('rollup-plugin-vue', function () {
34-
fs.readdirSync(path.resolve(__dirname, 'expects'))
47+
fs.readdirSync(path.resolve(__dirname, 'fixtures'))
3548
.forEach(function (file) {
36-
test(file.substr(0, file.length - 3))
49+
test(file.substr(0, file.length - 4))
3750
})
3851
})

0 commit comments

Comments
 (0)