Skip to content

Commit 1f49c1a

Browse files
committed
refactor: extract runPostCSS
1 parent 2c28fb5 commit 1f49c1a

File tree

1 file changed

+71
-109
lines changed
  • packages/vite/src/node/plugins

1 file changed

+71
-109
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 71 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export const cssConfigDefaults = Object.freeze({
205205
} satisfies CSSOptions)
206206

207207
export type ResolvedCSSOptions = Omit<CSSOptions, 'lightningcss'> &
208-
Required<Pick<CSSOptions, 'transformer'>> & {
208+
Required<Pick<CSSOptions, 'transformer' | 'devSourcemap'>> & {
209209
lightningcss?: LightningCSSOptions
210210
}
211211

@@ -1294,7 +1294,11 @@ async function compileCSSPreprocessors(
12941294
lang: PreprocessLang,
12951295
code: string,
12961296
workerController: PreprocessorWorkerController,
1297-
): Promise<{ code: string; map?: ExistingRawSourceMap; deps?: Set<string> }> {
1297+
): Promise<{
1298+
code: string
1299+
map?: ExistingRawSourceMap | { mappings: '' }
1300+
deps?: Set<string>
1301+
}> {
12981302
const { config } = environment
12991303
const { preprocessorOptions, devSourcemap } = config.css
13001304
const atImportResolvers = getAtImportResolvers(
@@ -1372,7 +1376,7 @@ async function compileCSS(
13721376
const deps = new Set<string>()
13731377

13741378
// pre-processors: sass etc.
1375-
let preprocessorMap: ExistingRawSourceMap | undefined
1379+
let preprocessorMap: ExistingRawSourceMap | { mappings: '' } | undefined
13761380
if (isPreProcessor(lang)) {
13771381
const preprocessorResult = await compileCSSPreprocessors(
13781382
environment,
@@ -1595,18 +1599,58 @@ async function compilePostCSS(
15951599
return
15961600
}
15971601

1602+
const result = await runPostCSS(
1603+
id,
1604+
code,
1605+
postcssPlugins,
1606+
{ ...postcssOptions, parser: postcssParser },
1607+
deps,
1608+
environment.logger,
1609+
devSourcemap,
1610+
)
1611+
return { ...result, modules }
1612+
}
1613+
1614+
async function transformSugarSS(
1615+
environment: PartialEnvironment,
1616+
id: string,
1617+
code: string,
1618+
) {
1619+
const { config } = environment
1620+
const { devSourcemap } = config.css
1621+
1622+
const result = await runPostCSS(
1623+
id,
1624+
code,
1625+
[],
1626+
{ parser: loadSss(config.root) },
1627+
undefined,
1628+
environment.logger,
1629+
devSourcemap,
1630+
)
1631+
return result
1632+
}
1633+
1634+
async function runPostCSS(
1635+
id: string,
1636+
code: string,
1637+
plugins: PostCSS.AcceptedPlugin[],
1638+
options: PostCSS.ProcessOptions,
1639+
deps: Set<string> | undefined,
1640+
logger: Logger,
1641+
enableSourcemap: boolean,
1642+
) {
15981643
let postcssResult: PostCSS.Result
15991644
try {
16001645
const source = removeDirectQuery(id)
16011646
const postcss = await importPostcss()
16021647

16031648
// postcss is an unbundled dep and should be lazy imported
1604-
postcssResult = await postcss.default(postcssPlugins).process(code, {
1605-
...postcssOptions,
1606-
parser: postcssParser,
1649+
postcssResult = await postcss.default(plugins).process(code, {
1650+
...options,
16071651
to: source,
16081652
from: source,
1609-
...(devSourcemap
1653+
...(enableSourcemap
16101654
? {
16111655
map: {
16121656
inline: false,
@@ -1624,7 +1668,7 @@ async function compilePostCSS(
16241668
// record CSS dependencies from @imports
16251669
for (const message of postcssResult.messages) {
16261670
if (message.type === 'dependency') {
1627-
deps.add(normalizePath(message.file as string))
1671+
deps?.add(normalizePath(message.file as string))
16281672
} else if (message.type === 'dir-dependency') {
16291673
// https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#3-dependencies
16301674
const { dir, glob: globPattern = '**' } = message
@@ -1635,7 +1679,7 @@ async function compilePostCSS(
16351679
ignore: ['**/node_modules/**'],
16361680
})
16371681
for (let i = 0; i < files.length; i++) {
1638-
deps.add(files[i])
1682+
deps?.add(files[i])
16391683
}
16401684
} else if (message.type === 'warning') {
16411685
const warning = message as PostCSS.Warning
@@ -1653,7 +1697,7 @@ async function compilePostCSS(
16531697
}
16541698
: undefined,
16551699
)}`
1656-
environment.logger.warn(colors.yellow(msg))
1700+
logger.warn(colors.yellow(msg))
16571701
}
16581702
}
16591703
} catch (e) {
@@ -1667,96 +1711,10 @@ async function compilePostCSS(
16671711
throw e
16681712
}
16691713

1670-
if (!devSourcemap) {
1671-
return {
1672-
code: postcssResult.css,
1673-
map: { mappings: '' },
1674-
modules,
1675-
}
1676-
}
1677-
1678-
const rawPostcssMap = postcssResult.map.toJSON()
1679-
const postcssMap = await formatPostcssSourceMap(
1680-
// version property of rawPostcssMap is declared as string
1681-
// but actually it is a number
1682-
rawPostcssMap as Omit<RawSourceMap, 'version'> as ExistingRawSourceMap,
1683-
cleanUrl(id),
1684-
)
1685-
1686-
return {
1687-
code: postcssResult.css,
1688-
map: postcssMap,
1689-
modules,
1690-
}
1691-
}
1692-
1693-
// TODO: dedupe
1694-
async function transformSugarSS(
1695-
environment: PartialEnvironment,
1696-
id: string,
1697-
code: string,
1698-
) {
1699-
const { config } = environment
1700-
const { devSourcemap } = config.css
1701-
1702-
let postcssResult: PostCSS.Result
1703-
try {
1704-
const source = removeDirectQuery(id)
1705-
const postcss = await importPostcss()
1706-
// postcss is an unbundled dep and should be lazy imported
1707-
postcssResult = await postcss.default().process(code, {
1708-
parser: loadSss(config.root),
1709-
to: source,
1710-
from: source,
1711-
...(devSourcemap
1712-
? {
1713-
map: {
1714-
inline: false,
1715-
annotation: false,
1716-
// postcss may return virtual files
1717-
// we cannot obtain content of them, so this needs to be enabled
1718-
sourcesContent: true,
1719-
// when "prev: preprocessorMap", the result map may include duplicate filename in `postcssResult.map.sources`
1720-
// prev: preprocessorMap,
1721-
},
1722-
}
1723-
: {}),
1724-
})
1725-
1726-
for (const message of postcssResult.messages) {
1727-
if (message.type === 'warning') {
1728-
const warning = message as PostCSS.Warning
1729-
let msg = `[vite:css] ${warning.text}`
1730-
msg += `\n${generateCodeFrame(
1731-
code,
1732-
{
1733-
line: warning.line,
1734-
column: warning.column - 1, // 1-based
1735-
},
1736-
warning.endLine !== undefined && warning.endColumn !== undefined
1737-
? {
1738-
line: warning.endLine,
1739-
column: warning.endColumn - 1, // 1-based
1740-
}
1741-
: undefined,
1742-
)}`
1743-
environment.logger.warn(colors.yellow(msg))
1744-
}
1745-
}
1746-
} catch (e) {
1747-
e.message = `[postcss] ${e.message}`
1748-
e.code = code
1749-
e.loc = {
1750-
file: e.file,
1751-
line: e.line,
1752-
column: e.column - 1, // 1-based
1753-
}
1754-
throw e
1755-
}
1756-
1757-
if (!devSourcemap) {
1714+
if (!enableSourcemap) {
17581715
return {
17591716
code: postcssResult.css,
1717+
map: { mappings: '' as const },
17601718
}
17611719
}
17621720

@@ -1865,17 +1823,21 @@ export async function formatPostcssSourceMap(
18651823

18661824
function combineSourcemapsIfExists(
18671825
filename: string,
1868-
map1: ExistingRawSourceMap | undefined,
1869-
map2: ExistingRawSourceMap | undefined,
1870-
): ExistingRawSourceMap | undefined {
1871-
return map1 && map2
1872-
? (combineSourcemaps(filename, [
1873-
// type of version property of ExistingRawSourceMap is number
1874-
// but it is always 3
1875-
map1 as RawSourceMap,
1876-
map2 as RawSourceMap,
1877-
]) as ExistingRawSourceMap)
1878-
: map1
1826+
map1: ExistingRawSourceMap | { mappings: '' } | undefined,
1827+
map2: ExistingRawSourceMap | { mappings: '' } | undefined,
1828+
): ExistingRawSourceMap | { mappings: '' } | undefined {
1829+
if (!map1 || !map2) {
1830+
return map1
1831+
}
1832+
if (map1.mappings === '' || map2.mappings === '') {
1833+
return { mappings: '' }
1834+
}
1835+
return combineSourcemaps(filename, [
1836+
// type of version property of ExistingRawSourceMap is number
1837+
// but it is always 3
1838+
map1 as RawSourceMap,
1839+
map2 as RawSourceMap,
1840+
]) as ExistingRawSourceMap
18791841
}
18801842

18811843
const viteHashUpdateMarker = '/*$vite$:1*/'

0 commit comments

Comments
 (0)