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): support sass compiler api #2

Closed
wants to merge 2 commits into from
Closed
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
80 changes: 77 additions & 3 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2275,6 +2275,78 @@ const makeModernScssWorker = (
return worker
}

const makeModernCompilerScssWorker = (
resolvers: CSSAtImportResolvers,
alias: Alias[],
_maxWorkers: number | undefined,
) => {
let compiler: Sass.AsyncCompiler | undefined

const worker: Awaited<ReturnType<typeof makeModernScssWorker>> = {
async run(sassPath, data, options) {
const { default: sass }: { default: typeof Sass } = await import(sassPath)
const path = await import('node:path')
const { fileURLToPath, pathToFileURL } = await import('node:url')
compiler ??= await sass.initAsyncCompiler()

const sassOptions = { ...options } as Sass.StringOptions<'async'>
sassOptions.url = pathToFileURL(options.filename)
sassOptions.sourceMap = options.enableSourcemap

const internalImporter: Sass.Importer<'async'> = {
async canonicalize(url, context) {
const importer = context.containingUrl
? fileURLToPath(context.containingUrl)
: options.filename
const resolved = await resolvers.sass(url, cleanScssBugUrl(importer))
return resolved ? pathToFileURL(resolved) : null
},
async load(canonicalUrl) {
const ext = path.extname(canonicalUrl.pathname)
let syntax: Sass.Syntax = 'scss'
if (ext && ext.toLowerCase() === '.sass') {
syntax = 'indented'
} else if (ext && ext.toLowerCase() === '.css') {
syntax = 'css'
}
const result = await rebaseUrls(
fileURLToPath(canonicalUrl),
options.filename,
alias,
'$',
resolvers.sass,
)
const contents =
result.contents ??
(await fs.promises.readFile(result.file, 'utf-8'))
return { contents, syntax }
},
}
sassOptions.importers = [
...(sassOptions.importers ?? []),
internalImporter,
]

const result = await compiler.compileStringAsync(data, sassOptions)
return {
css: result.css,
map: result.sourceMap ? JSON.stringify(result.sourceMap) : undefined,
stats: {
includedFiles: result.loadedUrls
.filter((url) => url.protocol === 'file:')
.map((url) => fileURLToPath(url)),
},
} satisfies ScssWorkerResult
},
async stop() {
compiler?.dispose()
compiler = undefined
},
}

return worker
}

type ScssWorkerResult = {
css: string
map?: string | undefined
Expand All @@ -2298,9 +2370,11 @@ const scssProcessor = (
if (!workerMap.has(options.alias)) {
workerMap.set(
options.alias,
options.api === 'modern'
? makeModernScssWorker(resolvers, options.alias, maxWorkers)
: makeScssWorker(resolvers, options.alias, maxWorkers),
options.api === 'modern-compiler'
? makeModernCompilerScssWorker(resolvers, options.alias, maxWorkers)
: options.api === 'modern'
? makeModernScssWorker(resolvers, options.alias, maxWorkers)
: makeScssWorker(resolvers, options.alias, maxWorkers),
)
}
const worker = workerMap.get(options.alias)!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../css.spec'
31 changes: 31 additions & 0 deletions playground/css/vite.config-sass-modern-compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { defineConfig } from 'vite'
import baseConfig from './vite.config.js'

export default defineConfig({
...baseConfig,
css: {
...baseConfig.css,
preprocessorOptions: {
...baseConfig.css.preprocessorOptions,
scss: {
api: 'modern-compiler',
additionalData: `$injectedColor: orange;`,
importers: [
{
canonicalize(url) {
return url === 'virtual-dep'
? new URL('custom-importer:virtual-dep')
: null
},
load() {
return {
contents: ``,
syntax: 'scss',
}
},
},
],
},
},
},
})
2 changes: 1 addition & 1 deletion playground/css/vite.config-sass-modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default defineConfig({
preprocessorOptions: {
...baseConfig.css.preprocessorOptions,
scss: {
api: 'modern',
api: 'modern-compiler',
additionalData: `$injectedColor: orange;`,
importers: [
{
Expand Down
4 changes: 4 additions & 0 deletions playground/vitestGlobalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export async function setup({ provide }: GlobalSetupContext): Promise<void> {
path.resolve(tempDir, 'css'),
path.resolve(tempDir, 'css__sass-modern'),
)
await fs.copy(
path.resolve(tempDir, 'css'),
path.resolve(tempDir, 'css__sass-modern-compiler'),
)
}

export async function teardown(): Promise<void> {
Expand Down
Loading