Skip to content

Commit

Permalink
refactor: compiler-sfc process dependencies relation
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Jun 1, 2020
1 parent f2b1416 commit fd275b1
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 15 deletions.
3 changes: 3 additions & 0 deletions playground/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<TestCssModules />
<TestCssAtImport/>
<TestPreprocessors />
<TestScssAtImport/>
<TestAssets />
<TestSrcImport />
<TestJsonImport />
Expand Down Expand Up @@ -44,6 +45,7 @@ import TestAlias from './TestAlias.vue'
import TestTransform from './TestTransform.vue'
import TestRewriteOptimized from "./rewrite-optimized/TestRewriteOptimized.vue";
import TestCssAtImport from './css-@import/TestCssAtImport.vue'
import TestScssAtImport from './css-@import/TestSCssAtImport.vue'
export default {
data: () => ({
Expand All @@ -59,6 +61,7 @@ export default {
TestCssModules,
TestPreprocessors,
TestCssAtImport,
TestScssAtImport,
TestSrcImport,
TestAssets,
TestJsonImport,
Expand Down
22 changes: 22 additions & 0 deletions playground/css-@import/TestScssAtImport.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<template>
<h2>CSS Preprocessor @import</h2>
<div class="sfc-style-scss-at-import">
&lt;style lang="scss" @import &gt; this should be red
</div>
<div class="script-scss-at-import">
&lt;script scss @import &gt; this should be green
</div>
</template>

<style lang="scss" scoped>
@import './testScssAtImportFromStyle.scss';
.sfc-style-scss-at-import {
color: $colorSfc;
}
</style>

<script>
import './testScssAtImportFromScript.scss'
export default {}
</script>
1 change: 1 addition & 0 deletions playground/css-@import/testCssAtImportFromScript.css
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
@import './imported.css'

1 change: 1 addition & 0 deletions playground/css-@import/testScss.imported.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$colorScript: green;
4 changes: 4 additions & 0 deletions playground/css-@import/testScssAtImportFromScript.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import './testScss.imported.scss';
.script-scss-at-import {
color: $colorScript;
}
2 changes: 2 additions & 0 deletions playground/css-@import/testScssAtImportFromStyle.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// single comment is not support by css
$colorSfc: red;
24 changes: 9 additions & 15 deletions src/node/utils/cssUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export async function compileCss(

const postcssOptions = postcssConfig && postcssConfig.options
const postcssPlugins = postcssConfig ? postcssConfig.plugins : []
postcssPlugins.push(require('postcss-import')())
// parser plain css @import
postcssPlugins.unshift(require('postcss-import')())

const res = await compileStyleAsync({
source,
Expand All @@ -91,20 +92,13 @@ export async function compileCss(
postcssPlugins
})

// record css import dependencies
if (res.rawResult) {
res.rawResult.messages.forEach((msg) => {
let { type, file, parent } = msg
if (type === 'dependency') {
if (cssImportMap.has(file)) {
cssImportMap.get(file)!.add(parent)
} else {
cssImportMap.set(file, new Set([parent]))
}
}
})
}

res.dependencies.forEach((dependency) => {
if (cssImportMap.has(dependency)) {
cssImportMap.get(dependency)!.add(filename)
} else {
cssImportMap.set(dependency, new Set([filename]))
}
})
return res
}

Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,29 @@ describe('vite', () => {
}
})

test('CSS preprocessor @import', async () => {
const el = await page.$('.script-scss-at-import')
expect(await getComputedColor(el)).toBe('rgb(0, 128, 0)')
if (!isBuild) {
await updateFile('css-@import/testScss.imported.scss', (content) =>
content.replace('green', 'rgb(0, 0, 0)')
)
await expectByPolling(() => getComputedColor(el), 'rgb(0, 0, 0)')
}
})

test('SFC <style lang="sass"> w/ @import', async () => {
const el = await page.$('.sfc-style-scss-at-import')
expect(await getComputedColor(el)).toBe('rgb(255, 0, 0)')
if (!isBuild) {
await updateFile(
'css-@import/testSCssAtImportFromStyle.scss',
(content) => content.replace('red', 'rgb(0, 0, 0)')
)
await expectByPolling(() => getComputedColor(el), 'rgb(0, 0, 0)')
}
})

test('import *.module.css', async () => {
const el = await page.$('.css-modules-import')
expect(await getComputedColor(el)).toBe('rgb(255, 140, 0)')
Expand Down

0 comments on commit fd275b1

Please sign in to comment.