-
Notifications
You must be signed in to change notification settings - Fork 170
/
sass.ts
32 lines (30 loc) · 956 Bytes
/
sass.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Options, Result } from 'https://esm.sh/sass@1.32.8'
import type { LoaderPlugin } from '../types.ts'
let renderSync: null | ((options: Options) => Result) = null
export default (opts?: Options): LoaderPlugin => ({
name: 'sass-loader',
type: 'loader',
test: /\.(sass|scss)$/i,
acceptHMR: true,
async transform({ content, url }) {
if (!('userAgent' in window.navigator)) {
Object.assign(window.navigator, { userAgent: `Deno/${Deno.version.deno}` })
}
if (renderSync === null) {
const sass = await import('https://esm.sh/sass@1.32.8')
renderSync = sass.renderSync
}
const { css, map } = renderSync({
indentedSyntax: url.endsWith('.sass'),
...opts,
file: url,
data: (new TextDecoder).decode(content),
sourceMap: true
})
return {
code: (new TextDecoder).decode(css),
type: 'css',
map: map ? (new TextDecoder).decode(map) : undefined,
}
}
})