Skip to content

Commit

Permalink
fix: tailwind css sourcemap warning (#7480)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Mar 29, 2022
1 parent 2b7dad1 commit 90df0bb
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 9 deletions.
13 changes: 13 additions & 0 deletions packages/playground/tailwind-sourcemap/__tests__/build.spec.ts
@@ -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)
})
}
13 changes: 13 additions & 0 deletions packages/playground/tailwind-sourcemap/__tests__/serve.spec.ts
@@ -0,0 +1,13 @@
import { isBuild } from 'testUtils'

if (!isBuild) {
test('should not output missing source file warning', () => {
serverLogs.forEach((log) => {
expect(log).not.toMatch(/Sourcemap for .+ points to missing source files/)
})
})
} else {
test('this file only includes test for serve', () => {
expect(true).toBe(true)
})
}
9 changes: 9 additions & 0 deletions packages/playground/tailwind-sourcemap/index.html
@@ -0,0 +1,9 @@
<div class="wrapper">
<h1>Tailwind Sourcemap</h1>

<p class="text-red-400">foo</p>
</div>

<script type="module">
import './tailwind.css'
</script>
14 changes: 14 additions & 0 deletions packages/playground/tailwind-sourcemap/package.json
@@ -0,0 +1,14 @@
{
"name": "test-tailwind-sourcemap",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"tailwindcss": "^3.0.23"
}
}
5 changes: 5 additions & 0 deletions packages/playground/tailwind-sourcemap/postcss.config.js
@@ -0,0 +1,5 @@
module.exports = {
plugins: {
tailwindcss: { config: __dirname + '/tailwind.config.js' }
}
}
7 changes: 7 additions & 0 deletions packages/playground/tailwind-sourcemap/tailwind.config.js
@@ -0,0 +1,7 @@
module.exports = {
content: ['./index.html'],
theme: {
extend: {}
},
plugins: []
}
3 changes: 3 additions & 0 deletions packages/playground/tailwind-sourcemap/tailwind.css
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
11 changes: 11 additions & 0 deletions packages/playground/tailwind-sourcemap/vite.config.js
@@ -0,0 +1,11 @@
/**
* @type {import('vite').UserConfig}
*/
module.exports = {
css: {
devSourcemap: true
},
build: {
sourcemap: true
}
}
30 changes: 23 additions & 7 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -783,7 +783,9 @@ async function compileCSS(
map: {
inline: false,
annotation: false,
sourcesContent: false
// postcss may return virtual files
// we cannot obtain content of them, so this needs to be enabled
sourcesContent: true
// when "prev: preprocessorMap", the result map may include duplicate filename in `postcssResult.map.sources`
// prev: preprocessorMap,
}
Expand Down Expand Up @@ -862,19 +864,33 @@ export function formatPostcssSourceMap(
file: string
): ExistingRawSourceMap {
const inputFileDir = path.dirname(file)
const sources = rawMap.sources

const sources: string[] = []
const sourcesContent: string[] = []
for (const [i, source] of rawMap.sources.entries()) {
// remove <no source> from sources, to prevent source map to be combined incorrectly
.filter((source) => source !== '<no source>')
.map((source) => {
const cleanSource = cleanUrl(decodeURIComponent(source))
return normalizePath(path.resolve(inputFileDir, cleanSource))
})
if (source === '<no source>') continue

const cleanSource = cleanUrl(decodeURIComponent(source))

// postcss returns virtual files
if (/^<.+>$/.test(cleanSource)) {
sources.push(`\0${cleanSource}`)
} else {
sources.push(normalizePath(path.resolve(inputFileDir, cleanSource)))
}

if (rawMap.sourcesContent) {
sourcesContent.push(rawMap.sourcesContent[i])
}
}

return {
file,
mappings: rawMap.mappings,
names: rawMap.names,
sources,
sourcesContent,
version: rawMap.version
}
}
Expand Down
68 changes: 66 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 90df0bb

Please sign in to comment.