Skip to content

Commit

Permalink
fix: avoid to generate empty css chunk files (#1464)
Browse files Browse the repository at this point in the history
* test: add failing test

* fix: filter out empty styles before generating code

* feat: improve regex
  • Loading branch information
jkzing authored and sodatea committed Jan 8, 2019
1 parent 9337655 commit c444ab6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/codegen/styleInjection.js
@@ -1,5 +1,6 @@
const { attrsToQuery } = require('./utils')
const hotReloadAPIPath = JSON.stringify(require.resolve('vue-hot-reload-api'))
const nonWhitespaceRE = /\S+/

module.exports = function genStyleInjectionCode (
loaderContext,
Expand Down Expand Up @@ -69,6 +70,8 @@ module.exports = function genStyleInjectionCode (
}
}

// filter out empty styles (with no `src` specified or only contains whitespaces)
styles = styles.filter(style => style.src || nonWhitespaceRE.test(style.content))
// explicit injection is needed in SSR (for critical CSS collection)
// or in Shadow Mode (for injection into shadow root)
// In these modes, vue-style-loader exports objects with the __inject__
Expand Down
32 changes: 32 additions & 0 deletions test/advanced.spec.js
Expand Up @@ -137,6 +137,38 @@ test('extract CSS', done => {
})
})

test('extract CSS with code spliting', done => {
bundle({
entry: 'extract-css-chunks.vue',
modify: config => {
config.module.rules = [
{
test: /\.vue$/,
use: 'vue-loader'
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: 'test.output.css'
})
]
}, code => {
const css = normalizeNewline(mfs.readFileSync('/test.output.css').toString())
expect(css).toContain(`h1 {\n color: red;\n}`)
expect(mfs.existsSync('/empty.test.output.css')).toBe(false)
expect(mfs.existsSync('/basic.test.output.css')).toBe(true)
done()
})
})

test('support rules with oneOf', async () => {
const run = (entry, assert) => new Promise((resolve, reject) => {
mockBundleAndRun({
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/empty-style.vue
@@ -0,0 +1,6 @@
<template>
<h1>empty style</h1>
</template>

<style>
</style>
21 changes: 21 additions & 0 deletions test/fixtures/extract-css-chunks.vue
@@ -0,0 +1,21 @@
<template>
<div>
<basic></basic>
<empty-style></empty-style>
</div>
</template>

<script>
export default {
components: {
Basic: () => import(/* webpackChunkName: "basic" */ './basic.vue'),
EmptyStyle: () => import(/* webpackChunkName: "empty" */ './empty-style.vue')
}
}
</script>

<style>
h1 {
color: red;
}
</style>

0 comments on commit c444ab6

Please sign in to comment.