Skip to content

Commit 3a64402

Browse files
authored
fix: When using extract CSS add scopeId to component for scoped CSS (vuejs#188)
Fixes vuejs#186
1 parent 4110dbb commit 3a64402

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"rollup": "^0.58.2",
5656
"rollup-plugin-babel": "^4.0.0-beta.4",
5757
"rollup-plugin-commonjs": "^9.1.0",
58+
"rollup-plugin-css-only": "^0.4.0",
5859
"rollup-plugin-image": "^1.0.2",
5960
"rollup-plugin-md": "^0.0.7",
6061
"rollup-plugin-node-resolve": "^3.3.0",

src/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import * as path from 'path'
99
import { parse } from '@vue/component-compiler-utils'
1010
import { createDefaultCompiler, assemble } from '@vue/component-compiler'
1111
import hash from 'hash-sum'
12-
import { relative } from 'path'
1312

1413
export default function vue(opts = {}) {
1514
const isVue = createVueFilter(opts.include, opts.exclude)
@@ -91,7 +90,7 @@ export default function vue(opts = {}) {
9190
if (input.template.errors && input.template.errors.length) {
9291
console.error(
9392
'> Errors: ' +
94-
relative(process.cwd(), filename) +
93+
path.relative(process.cwd(), filename) +
9594
'\n' +
9695
input.template.errors.map(it => ' - ' + it).join('\n')
9796
)
@@ -100,7 +99,7 @@ export default function vue(opts = {}) {
10099
if (input.template.tips && input.template.tips.length) {
101100
console.log(
102101
'> Tips: ' +
103-
relative(process.cwd(), filename) +
102+
path.relative(process.cwd(), filename) +
104103
'\n' +
105104
input.template.tips.map(it => ' - ' + it).join('\n')
106105
)
@@ -139,7 +138,7 @@ export default function vue(opts = {}) {
139138
index
140139
)}'`
141140

142-
if (style.module) {
141+
if (style.module || descriptor.styles[index].scoped) {
143142
return { ...style, code: '' }
144143
}
145144
})

test/baseline.spec.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ beforeAll(async () => {
1616
})
1717
afterAll(async () => browser && (await browser.close()))
1818

19-
const testRunner = async fixture => {
19+
const testRunner = async (fixture, extractCss) => {
2020
const filename = join(__dirname, 'fixtures', fixture + '.vue')
21-
const code = await build(filename)
22-
const page = await open(fixture, browser, code)
21+
const code = await build(filename, extractCss)
22+
const page = await open(
23+
fixture + (extractCss ? '-extract' : ''),
24+
browser,
25+
code
26+
)
2327
expect(await page.$('#test')).toBeTruthy()
2428
expect(
2529
await page.evaluate(() => document.getElementById('test').textContent)
@@ -35,4 +39,5 @@ const testRunner = async fixture => {
3539
}
3640
fixtures.forEach(fixture => {
3741
test(fixture, () => testRunner(fixture, false))
42+
test(fixture + ' (extract css)', () => testRunner(fixture, true))
3843
})

test/setup/index.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { rollup } = require('rollup')
22
const babel = require('rollup-plugin-babel')
3-
// const commonjs = require('rollup-plugin-commonjs')
3+
const css = require('rollup-plugin-css-only')
44
const nodeResolve = require('rollup-plugin-node-resolve')
55
const image = require('rollup-plugin-image')
66
const { readFileSync } = require('fs')
@@ -30,16 +30,20 @@ const babelIt = babel({
3030

3131
const cache = {}
3232

33-
async function build(filename) {
34-
const cacheKey = filename
33+
async function build(filename, extractCss = false) {
34+
const cacheKey = JSON.stringify({filename, extractCss})
3535
if (cacheKey in cache) return cache[cacheKey]
3636
const input = filename + '__app.js'
3737

38-
const options = { defaultLang: { markdown: 'md' } }
38+
const options = { defaultLang: { markdown: 'md' }, css: extractCss }
39+
let style
3940
let bundle = await rollup({
4041
input,
4142
plugins: [
4243
md(),
44+
css({ include: '**/*.css?*', output(s) {
45+
style = s
46+
} }),
4347
vue(options),
4448
image(),
4549
nodeResolve(),
@@ -66,7 +70,12 @@ async function build(filename) {
6670
cache[cacheKey] = (await bundle.generate({
6771
format: 'iife',
6872
name: 'App'
69-
})).code
73+
})).code + (style ? `;(function() {
74+
var s = document.createElement('style');
75+
s.type = 'text/css';
76+
document.head.appendChild(s);
77+
s.appendChild(document.createTextNode(${JSON.stringify(style)}))
78+
})()` : '')
7079

7180
return cache[cacheKey]
7281
}

yarn.lock

+10-3
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@
486486
invariant "^2.2.2"
487487
semver "^5.3.0"
488488

489-
"@babel/runtime@^7.0.0-beta.39":
489+
"@babel/runtime@^7.0.0-beta.46":
490490
version "7.0.0-beta.46"
491491
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.0.0-beta.46.tgz#466a9c0498f6d12d054a185981eef742d59d4871"
492492
dependencies:
@@ -3889,7 +3889,7 @@ preserve@^0.2.0:
38893889
version "0.2.0"
38903890
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
38913891

3892-
prettier@^1.11.1:
3892+
prettier@^1.11.1, prettier@^1.12.1:
38933893
version "1.12.1"
38943894
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.12.1.tgz#c1ad20e803e7749faf905a409d2367e06bbe7325"
38953895

@@ -4361,6 +4361,13 @@ rollup-plugin-commonjs@^9.1.0:
43614361
resolve "^1.5.0"
43624362
rollup-pluginutils "^2.0.1"
43634363

4364+
rollup-plugin-css-only@^0.4.0:
4365+
version "0.4.0"
4366+
resolved "https://registry.yarnpkg.com/rollup-plugin-css-only/-/rollup-plugin-css-only-0.4.0.tgz#eaf10c79b17c88dc95712fe91518e3afedfb657a"
4367+
dependencies:
4368+
mkdirp "^0.5.1"
4369+
rollup-pluginutils "^1.5.2"
4370+
43644371
rollup-plugin-image@^1.0.2:
43654372
version "1.0.2"
43664373
resolved "https://registry.yarnpkg.com/rollup-plugin-image/-/rollup-plugin-image-1.0.2.tgz#77c6782daedebee6e0a858c4017905846919da2a"
@@ -4384,7 +4391,7 @@ rollup-plugin-node-resolve@^3.3.0:
43844391
is-module "^1.0.0"
43854392
resolve "^1.1.6"
43864393

4387-
rollup-pluginutils@^1.3.1, rollup-pluginutils@^1.5.0, rollup-pluginutils@^1.5.1:
4394+
rollup-pluginutils@^1.3.1, rollup-pluginutils@^1.5.0, rollup-pluginutils@^1.5.1, rollup-pluginutils@^1.5.2:
43884395
version "1.5.2"
43894396
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-1.5.2.tgz#1e156e778f94b7255bfa1b3d0178be8f5c552408"
43904397
dependencies:

0 commit comments

Comments
 (0)