Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css): export preprocessCSS API #10429

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { createServer } from './server'
export { preview } from './preview'
export { build } from './build'
export { optimizeDeps } from './optimizer'
export { formatPostcssSourceMap } from './plugins/css'
export { formatPostcssSourceMap, preprocessCSS } from './plugins/css'
export { transformWithEsbuild } from './plugins/esbuild'
export { resolvePackageEntry } from './plugins/resolve'
export { resolvePackageData } from './packages'
Expand Down Expand Up @@ -67,7 +67,11 @@ export type {
IndexHtmlTransformResult,
HtmlTagDescriptor
} from './plugins/html'
export type { CSSOptions, CSSModulesOptions } from './plugins/css'
export type {
CSSOptions,
CSSModulesOptions,
PreprocessCSSResult
} from './plugins/css'
export type { ChunkMetadata } from './plugins/metadata'
export type { JsonOptions } from './plugins/json'
export type { TransformOptions as EsbuildTransformOptions } from 'esbuild'
Expand Down
58 changes: 40 additions & 18 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
tryIndex: false,
extensions: []
})
const atImportResolvers = createCSSResolvers(config)

return {
name: 'vite:css',
Expand Down Expand Up @@ -225,14 +224,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
modules,
deps,
map
} = await compileCSS(
id,
raw,
config,
urlReplacer,
atImportResolvers,
server
)
} = await compileCSS(id, raw, config, urlReplacer)
if (modules) {
moduleCache.set(id, modules)
}
Expand Down Expand Up @@ -734,13 +726,16 @@ function getCssResolversKeys(
return Object.keys(resolvers) as unknown as Array<keyof CSSAtImportResolvers>
}

const configToAtImportResolvers = new WeakMap<
ResolvedConfig,
CSSAtImportResolvers
>()

async function compileCSS(
id: string,
code: string,
config: ResolvedConfig,
urlReplacer: CssUrlReplacer,
atImportResolvers: CSSAtImportResolvers,
server?: ViteDevServer
urlReplacer?: CssUrlReplacer
): Promise<{
code: string
map?: SourceMapInput
Expand Down Expand Up @@ -776,6 +771,12 @@ async function compileCSS(
let modules: Record<string, string> | undefined
const deps = new Set<string>()

let atImportResolvers = configToAtImportResolvers.get(config)!
if (!atImportResolvers) {
atImportResolvers = createCSSResolvers(config)
configToAtImportResolvers.set(config, atImportResolvers)
}

// 2. pre-processors: sass etc.
if (isPreProcessor(lang)) {
const preProcessor = preProcessors[lang]
Expand Down Expand Up @@ -870,12 +871,15 @@ async function compileCSS(
})
)
}
postcssPlugins.push(
UrlRewritePostcssPlugin({
replacer: urlReplacer,
logger: config.logger
})
)

if (urlReplacer) {
postcssPlugins.push(
UrlRewritePostcssPlugin({
replacer: urlReplacer,
logger: config.logger
})
)
}

if (isModule) {
postcssPlugins.unshift(
Expand Down Expand Up @@ -1002,6 +1006,24 @@ async function compileCSS(
}
}

export interface PreprocessCSSResult {
code: string
map?: SourceMapInput
modules?: Record<string, string>
deps?: Set<string>
}

/**
* @experimental
*/
export async function preprocessCSS(
code: string,
filename: string,
config: ResolvedConfig
): Promise<PreprocessCSSResult> {
return await compileCSS(filename, code, config)
}

export async function formatPostcssSourceMap(
rawMap: ExistingRawSourceMap,
file: string
Expand Down