-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: css sourcemap support during dev (#7173)
- Loading branch information
1 parent
68d76c9
commit 38a655f
Showing
40 changed files
with
1,210 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { isBuild } from 'testUtils' | ||
|
||
if (isBuild) { | ||
test('should not output sourcemap warning (#4939)', () => { | ||
serverLogs.forEach((log) => { | ||
expect(log).not.toMatch('Sourcemap is likely to be incorrect') | ||
}) | ||
}) | ||
} else { | ||
test('this file only includes test for build', () => { | ||
expect(true).toBe(true) | ||
}) | ||
} |
214 changes: 214 additions & 0 deletions
214
packages/playground/css-sourcemap/__tests__/serve.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import { fromComment } from 'convert-source-map' | ||
import { URL } from 'url' | ||
import { normalizePath } from 'vite' | ||
import { isBuild, testDir } from 'testUtils' | ||
|
||
if (!isBuild) { | ||
const root = normalizePath(testDir) | ||
|
||
const getStyleTagContentIncluding = async (content: string) => { | ||
const styles = await page.$$('style') | ||
for (const style of styles) { | ||
const text = await style.textContent() | ||
if (text.includes(content)) { | ||
return text | ||
} | ||
} | ||
throw new Error('Not found') | ||
} | ||
|
||
const extractSourcemap = (content: string) => { | ||
const lines = content.trim().split('\n') | ||
return fromComment(lines[lines.length - 1]).toObject() | ||
} | ||
|
||
const formatSourcemapForSnapshot = (map: any) => { | ||
const m = { ...map } | ||
delete m.file | ||
delete m.names | ||
m.sources = m.sources.map((source) => source.replace(root, '/root')) | ||
return m | ||
} | ||
|
||
test('linked css', async () => { | ||
const res = await page.request.get( | ||
new URL('./linked.css', page.url()).href, | ||
{ | ||
headers: { | ||
accept: 'text/css' | ||
} | ||
} | ||
) | ||
const css = await res.text() | ||
const lines = css.split('\n') | ||
expect(lines[lines.length - 1].includes('/*')).toBe(false) // expect no sourcemap | ||
}) | ||
|
||
test('linked css with import', async () => { | ||
const res = await page.request.get( | ||
new URL('./linked-with-import.css', page.url()).href, | ||
{ | ||
headers: { | ||
accept: 'text/css' | ||
} | ||
} | ||
) | ||
const css = await res.text() | ||
const map = extractSourcemap(css) | ||
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` | ||
Object { | ||
"mappings": "AAAA;EACE,UAAU;AACZ;;ACAA;EACE,UAAU;AACZ", | ||
"sources": Array [ | ||
"/root/be-imported.css", | ||
"/root/linked-with-import.css", | ||
], | ||
"sourcesContent": Array [ | ||
".be-imported { | ||
color: red; | ||
} | ||
", | ||
"@import '@/be-imported.css'; | ||
.linked-with-import { | ||
color: red; | ||
} | ||
", | ||
], | ||
"version": 3, | ||
} | ||
`) | ||
}) | ||
|
||
test('imported css', async () => { | ||
const css = await getStyleTagContentIncluding('.imported ') | ||
const map = extractSourcemap(css) | ||
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` | ||
Object { | ||
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACX,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACb,CAAC;", | ||
"sources": Array [ | ||
"/root/imported.css", | ||
], | ||
"sourcesContent": Array [ | ||
".imported { | ||
color: red; | ||
} | ||
", | ||
], | ||
"version": 3, | ||
} | ||
`) | ||
}) | ||
|
||
test('imported css with import', async () => { | ||
const css = await getStyleTagContentIncluding('.imported-with-import ') | ||
const map = extractSourcemap(css) | ||
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` | ||
Object { | ||
"mappings": "AAAA;EACE,UAAU;AACZ;;ACAA;EACE,UAAU;AACZ", | ||
"sources": Array [ | ||
"/root/be-imported.css", | ||
"/root/imported-with-import.css", | ||
], | ||
"sourcesContent": Array [ | ||
".be-imported { | ||
color: red; | ||
} | ||
", | ||
"@import '@/be-imported.css'; | ||
.imported-with-import { | ||
color: red; | ||
} | ||
", | ||
], | ||
"version": 3, | ||
} | ||
`) | ||
}) | ||
|
||
test('imported sass', async () => { | ||
const css = await getStyleTagContentIncluding('.imported-sass ') | ||
const map = extractSourcemap(css) | ||
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` | ||
Object { | ||
"mappings": "AACE;EACE", | ||
"sources": Array [ | ||
"/root/imported.sass", | ||
], | ||
"sourcesContent": Array [ | ||
".imported | ||
&-sass | ||
color: red | ||
", | ||
], | ||
"version": 3, | ||
} | ||
`) | ||
}) | ||
|
||
test('imported sass module', async () => { | ||
const css = await getStyleTagContentIncluding('._imported-sass-module_') | ||
const map = extractSourcemap(css) | ||
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` | ||
Object { | ||
"mappings": "AACE;EACE", | ||
"sources": Array [ | ||
"/root/imported.module.sass", | ||
], | ||
"sourcesContent": Array [ | ||
".imported | ||
&-sass-module | ||
color: red | ||
", | ||
], | ||
"version": 3, | ||
} | ||
`) | ||
}) | ||
|
||
test('imported less', async () => { | ||
const css = await getStyleTagContentIncluding('.imported-less ') | ||
const map = extractSourcemap(css) | ||
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` | ||
Object { | ||
"mappings": "AACE;EACE", | ||
"sources": Array [ | ||
"/root/imported.less", | ||
], | ||
"sourcesContent": Array [ | ||
".imported { | ||
&-less { | ||
color: @color; | ||
} | ||
} | ||
", | ||
], | ||
"version": 3, | ||
} | ||
`) | ||
}) | ||
|
||
test('imported stylus', async () => { | ||
const css = await getStyleTagContentIncluding('.imported-stylus ') | ||
const map = extractSourcemap(css) | ||
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` | ||
Object { | ||
"mappings": "AACE;EACE,cAAM", | ||
"sources": Array [ | ||
"/root/imported.styl", | ||
], | ||
"sourcesContent": Array [ | ||
".imported | ||
&-stylus | ||
color blue-red-mixed | ||
", | ||
], | ||
"version": 3, | ||
} | ||
`) | ||
}) | ||
} else { | ||
test('this file only includes test for serve', () => { | ||
expect(true).toBe(true) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.be-imported { | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@import '@/be-imported.css'; | ||
|
||
.imported-with-import { | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.imported { | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.imported { | ||
&-less { | ||
color: @color; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.imported | ||
&-sass-module | ||
color: red |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.imported | ||
&-sass | ||
color: red |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.imported | ||
&-stylus | ||
color blue-red-mixed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<link rel="stylesheet" href="./linked.css" /> | ||
<link rel="stylesheet" href="./linked-with-import.css" /> | ||
|
||
<div class="wrapper"> | ||
<h1>CSS Sourcemap</h1> | ||
|
||
<p class="linked"><linked>: no import</p> | ||
<p class="linked-with-import"><linked>: with import</p> | ||
|
||
<p class="imported"><imported>: no import</p> | ||
<p class="imported-with-import"><imported>: with import</p> | ||
|
||
<p class="imported-sass"><imported sass></p> | ||
<p class="imported-sass-module"><imported sass> with module</p> | ||
|
||
<p class="imported-less"><imported less> with string additionalData</p> | ||
|
||
<p class="imported-stylus"><imported stylus></p> | ||
</div> | ||
|
||
<script type="module"> | ||
import './imported.css' | ||
import './imported-with-import.css' | ||
|
||
import './imported.sass' | ||
import sassModule from './imported.module.sass' | ||
|
||
document | ||
.querySelector('.imported-sass-module') | ||
.classList.add(sassModule['imported-sass-module']) | ||
|
||
import './imported.less' | ||
|
||
import './imported.styl' | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@import '@/be-imported.css'; | ||
|
||
.linked-with-import { | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.linked { | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "test-css-sourcemap", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"debug": "node --inspect-brk ../../vite/bin/vite", | ||
"preview": "vite preview" | ||
}, | ||
"devDependencies": { | ||
"convert-source-map": "^1.8.0", | ||
"less": "^4.1.2", | ||
"magic-string": "^0.25.7", | ||
"sass": "^1.43.4", | ||
"stylus": "^0.55.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const MagicString = require('magic-string') | ||
|
||
/** | ||
* @type {import('vite').UserConfig} | ||
*/ | ||
module.exports = { | ||
resolve: { | ||
alias: { | ||
'@': __dirname | ||
} | ||
}, | ||
css: { | ||
preprocessorOptions: { | ||
less: { | ||
additionalData: '@color: red;' | ||
}, | ||
styl: { | ||
additionalData: (content, filename) => { | ||
const ms = new MagicString(content, { filename }) | ||
|
||
const willBeReplaced = 'blue-red-mixed' | ||
const start = content.indexOf(willBeReplaced) | ||
ms.overwrite(start, start + willBeReplaced.length, 'purple') | ||
|
||
const map = ms.generateMap({ hires: true }) | ||
map.file = filename | ||
map.sources = [filename] | ||
|
||
return { | ||
content: ms.toString(), | ||
map | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
build: { | ||
sourcemap: true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<template> | ||
<p class="css"><css></p> | ||
<p :class="$style['css-module']"><css> module</p> | ||
<p class="css-scoped"><css> scoped</p> | ||
</template> | ||
|
||
<style> | ||
.css { | ||
color: red; | ||
} | ||
</style> | ||
|
||
<style module> | ||
.css-module { | ||
color: red; | ||
} | ||
</style> | ||
|
||
<style scoped> | ||
.css-scoped { | ||
color: red; | ||
} | ||
</style> |
Oops, something went wrong.