Commit c444ab61 authored by JK's avatar JK Committed by Haoqun Jiang

fix: avoid to generate empty css chunk files (#1464)

* test: add failing test

* fix: filter out empty styles before generating code

* feat: improve regex
parent 93376556
const { attrsToQuery } = require('./utils')
const hotReloadAPIPath = JSON.stringify(require.resolve('vue-hot-reload-api'))
const nonWhitespaceRE = /\S+/
module.exports = function genStyleInjectionCode (
loaderContext,
......@@ -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__
......
......@@ -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({
......
<template>
<h1>empty style</h1>
</template>
<style>
</style>
<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>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment