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

refactor(compiler-sfc): support sass modern api #11992

Merged
merged 9 commits into from
Oct 11, 2024
48 changes: 34 additions & 14 deletions packages/compiler-sfc/src/style/preprocessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,48 @@ export interface StylePreprocessorResults {

// .scss/.sass processor
const scss: StylePreprocessor = (source, map, options, load = require) => {
const nodeSass = load('sass')
const finalOptions = {
...options,
data: getSource(source, options.filename, options.additionalData),
file: options.filename,
outFile: options.filename,
sourceMap: !!map,
}
const nodeSass: typeof import('sass') = load('sass')
const { compileString, renderSync } = nodeSass

const data = getSource(source, options.filename, options.additionalData)
let css: string
let dependencies: string[]
let sourceMap: any

try {
const result = nodeSass.renderSync(finalOptions)
const dependencies = result.stats.includedFiles
if (compileString) {
const { pathToFileURL, fileURLToPath }: typeof import('url') = load('url')

const result = compileString(data, {
...options,
url: pathToFileURL(options.filename),
sourceMap: !!map,
})
css = result.css
dependencies = result.loadedUrls.map(url => fileURLToPath(url))
sourceMap = map ? result.sourceMap! : undefined
} else {
const result = renderSync({
...options,
data,
file: options.filename,
outFile: options.filename,
sourceMap: !!map,
})
css = result.css.toString()
dependencies = result.stats.includedFiles
sourceMap = map ? JSON.parse(result.map!.toString()) : undefined
}

if (map) {
return {
code: result.css.toString(),
map: merge(map, JSON.parse(result.map.toString())),
code: css,
errors: [],
dependencies,
map: merge(map, sourceMap!),
}
}

return { code: result.css.toString(), errors: [], dependencies }
return { code: css, errors: [], dependencies }
} catch (e: any) {
return { code: '', errors: [e], dependencies: [] }
}
Expand Down