Skip to content

Commit

Permalink
fix(compiler-sfc): use sass modern api if available and avoid depreca…
Browse files Browse the repository at this point in the history
…tion warning (#11992)
  • Loading branch information
KazariEX authored Oct 11, 2024
1 parent 9da1ac1 commit 4474c11
Showing 1 changed file with 34 additions and 14 deletions.
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

0 comments on commit 4474c11

Please sign in to comment.