From 6c13a34aa05a105786240a6f93ce8f574a30cf9a Mon Sep 17 00:00:00 2001 From: JingkaiZhao Date: Sun, 6 Jan 2019 18:29:20 +0800 Subject: [PATCH 1/3] test: add failing test --- test/advanced.spec.js | 32 ++++++++++++++++++++++++++++ test/fixtures/empty-style.vue | 6 ++++++ test/fixtures/extract-css-chunks.vue | 21 ++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 test/fixtures/empty-style.vue create mode 100644 test/fixtures/extract-css-chunks.vue 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 @@ + + + + + From c2e3e1986fffdc29704e658d4b43a05aa55dd6e1 Mon Sep 17 00:00:00 2001 From: JingkaiZhao Date: Sun, 6 Jan 2019 18:34:48 +0800 Subject: [PATCH 2/3] fix: filter out empty styles before generating code --- lib/codegen/styleInjection.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/codegen/styleInjection.js b/lib/codegen/styleInjection.js index e8c676463..b8fc27473 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 allWhitespaceRE = /^\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 || !allWhitespaceRE.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__ From 823ebe2b92c1bb8e25dfa2f7ac91392273040d59 Mon Sep 17 00:00:00 2001 From: JingkaiZhao Date: Sun, 6 Jan 2019 19:29:25 +0800 Subject: [PATCH 3/3] feat: improve regex --- lib/codegen/styleInjection.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/codegen/styleInjection.js b/lib/codegen/styleInjection.js index b8fc27473..5d039f428 100644 --- a/lib/codegen/styleInjection.js +++ b/lib/codegen/styleInjection.js @@ -1,6 +1,6 @@ const { attrsToQuery } = require('./utils') const hotReloadAPIPath = JSON.stringify(require.resolve('vue-hot-reload-api')) -const allWhitespaceRE = /^\s+$/ +const nonWhitespaceRE = /\S+/ module.exports = function genStyleInjectionCode ( loaderContext, @@ -71,7 +71,7 @@ module.exports = function genStyleInjectionCode ( } // filter out empty styles (with no `src` specified or only contains whitespaces) - styles = styles.filter(style => style.src || !allWhitespaceRE.test(style.content)) + 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__