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

Commit c75b7bf

Browse files
committed
Inject CSS dynamically
Fixes #69
1 parent 43a6f48 commit c75b7bf

File tree

5 files changed

+81
-31
lines changed

5 files changed

+81
-31
lines changed

src/style/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const compilers = {
1010
less: compileLESS
1111
}
1212

13-
export async function compile(style, options) {
13+
export async function compile (style, options) {
1414
let output
1515

1616
if (style.lang === 'css') {
@@ -22,7 +22,7 @@ export async function compile(style, options) {
2222
return output
2323
}
2424

25-
function ensureDirectory(directory) {
25+
function ensureDirectory (directory) {
2626
if (!exists(directory)) {
2727
ensureDirectory(dirname(directory))
2828

@@ -31,7 +31,7 @@ function ensureDirectory(directory) {
3131
}
3232

3333
export default function (files, options) {
34-
if (options.css === false) {
34+
if (typeof (options.css) === 'boolean') {
3535
return
3636
}
3737

@@ -66,7 +66,6 @@ export default function (files, options) {
6666
}
6767

6868
dest = isAbsolute(dest) ? dest : resolvePath(process.cwd(), dest)
69-
console.log('OUTPUT:', dest)
7069
// Emit styles to file
7170
ensureDirectory(dirname(dest))
7271
writeFile(dest, css, (err) => {

src/vueTransform.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,26 @@ export default async function vueTransform (code, id, options) {
190190
const style = css.map((s, i) => 'import ' + JSON.stringify(`${id}.${i}.vue.component.${s.lang}`) + ';').join(' ')
191191

192192
return { css, code: style + js.code, map: js.map }
193+
} else if (options.css === true) {
194+
const style = css.map(s => '$compiled' in s ? s.$compiled.code : s.code).join('\n').replace(/(\r?\n|[\s])+/g, ' ')
195+
const styleCode = `
196+
(function(){
197+
if(document){
198+
var head=document.head||document.getElementsByTagName('head')[0],
199+
style=document.create('style'),
200+
css=${JSON.stringify(style)};
201+
style.type='text/css';
202+
if (style.styleSheet){
203+
style.styleSheet.cssText = css;
204+
} else {
205+
style.appendChild(document.createTextNode(css));
206+
}
207+
head.appendChild(style);
208+
}
209+
})();
210+
`.replace(/(\r?\n|[\s])+/g, ' ').trim()
211+
212+
return { css, code: styleCode + js.code, map: js.map }
193213
}
194214

195215
return { css, code: js.code, map: js.map }

test/expects/no-css-extract.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(function(){ if(document){ var head=document.head||document.getElementsByTagName('head')[0], style=document.create('style'), css=" .bar { color: blue } .foo { color: red; } "; style.type='text/css'; if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } head.appendChild(style); } })();
2+
3+
4+
5+
6+
var noCssExtract = { template: "<div class=\"foo bar\">test</div>",};
7+
8+
export default noCssExtract;

test/fixtures/no-css-extract.vue

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template lang="html">
2+
<div class="foo bar">test</div>
3+
</template>
4+
5+
<script lang="babel">
6+
export default {}
7+
</script>
8+
9+
10+
<style lang="scss">
11+
$red: red;
12+
.foo {
13+
color: $red
14+
}
15+
</style>
16+
17+
<style lang="css">
18+
.bar {
19+
color: blue
20+
}
21+
</style>

test/test.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,23 @@ function test(name) {
2929
return rollup.rollup({
3030
entry: entry,
3131
plugins: [vuePlugin({
32-
css: cssHandler,
32+
css: ['no-css-extract'].includes(name) ? true : cssHandler,
3333
modules: {
3434
generateScopedName: '[name]__[local]'
3535
},
3636
compileTemplate: ['compileTemplate', 'slot', 'table', 'table-n-slot'].indexOf(name) > -1
3737
})]
3838
}).then(function (bundle) {
39-
var result = bundle.generate({format: 'es'})
39+
var result = bundle.generate({ format: 'es' })
4040
var code = result.code
4141
assert.equal(code.trim(), expected.trim(), 'should compile code correctly')
4242

4343
// Check css output
44-
if (['style', 'css-modules', 'css-modules-static', 'scss', 'pug', 'less'].indexOf(name) > -1) {
44+
if (['style', 'css-modules', 'css-modules-static', 'scss', 'pug', 'less'].includes(name)) {
4545
var css = read('expects/' + name + '.css')
4646
assert.equal(css.trim(), actualCss.trim(), 'should output style tag content')
47+
} else if (['no-css-extract'].includes(name)) {
48+
assert.equal(undefined, actualCss, 'should ignore css()')
4749
} else {
4850
assert.equal('', actualCss.trim(), 'should always call css()')
4951
}
@@ -61,30 +63,30 @@ describe('rollup-plugin-vue', function () {
6163
})
6264

6365
describe('styleToImports', function () {
64-
it('should convert style to import', function () {
65-
var entry = './fixtures/style.vue'
66-
var expectedCss = read('expects/style.css')
67-
var actualCss
66+
it('should convert style to import', function () {
67+
var entry = './fixtures/style.vue'
68+
var expectedCss = read('expects/style.css')
69+
var actualCss
6870

69-
return rollup.rollup({
70-
format: 'cjs',
71-
entry: entry,
72-
plugins: [
73-
vuePlugin({
74-
styleToImports: true,
75-
}),
76-
cssPlugin({
77-
output: function (css) {
78-
actualCss = css
79-
},
80-
}),
81-
],
82-
}).then(function (bundle) {
83-
bundle.generate({ format: 'es' })
71+
return rollup.rollup({
72+
format: 'cjs',
73+
entry: entry,
74+
plugins: [
75+
vuePlugin({
76+
styleToImports: true,
77+
}),
78+
cssPlugin({
79+
output: function (css) {
80+
actualCss = css
81+
},
82+
}),
83+
],
84+
}).then(function (bundle) {
85+
bundle.generate({ format: 'es' })
8486

85-
assert.equal(expectedCss.trim(), actualCss.trim(), 'should import style')
86-
}).catch(function (error) {
87-
throw error
88-
})
89-
})
87+
assert.equal(expectedCss.trim(), actualCss.trim(), 'should import style')
88+
}).catch(function (error) {
89+
throw error
90+
})
91+
})
9092
})

0 commit comments

Comments
 (0)