From c444ab6121248043978837247567bf5296abcf0d Mon Sep 17 00:00:00 2001 From: JK Date: Tue, 8 Jan 2019 14:45:02 +0800 Subject: [PATCH] fix: avoid to generate empty css chunk files (#1464) * test: add failing test * fix: filter out empty styles before generating code * feat: improve regex --- lib/codegen/styleInjection.js | 3 +++ test/advanced.spec.js | 32 ++++++++++++++++++++++++++++ test/fixtures/empty-style.vue | 6 ++++++ test/fixtures/extract-css-chunks.vue | 21 ++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 test/fixtures/empty-style.vue create mode 100644 test/fixtures/extract-css-chunks.vue diff --git a/lib/codegen/styleInjection.js b/lib/codegen/styleInjection.js index e8c676463..5d039f428 100644 --- a/lib/codegen/styleInjection.js +++ b/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, @@ -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__ diff --git a/test/advanced.spec.js b/test/advanced.spec.js index 138a7b47d..9379ff84f 100644 --- a/test/advanced.spec.js +++ b/test/advanced.spec.js @@ -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({ diff --git a/test/fixtures/empty-style.vue b/test/fixtures/empty-style.vue new file mode 100644 index 000000000..12b50f3a7 --- /dev/null +++ b/test/fixtures/empty-style.vue @@ -0,0 +1,6 @@ + + + diff --git a/test/fixtures/extract-css-chunks.vue b/test/fixtures/extract-css-chunks.vue new file mode 100644 index 000000000..80c5e182f --- /dev/null +++ b/test/fixtures/extract-css-chunks.vue @@ -0,0 +1,21 @@ + + + + +