-
Notifications
You must be signed in to change notification settings - Fork 170
/
sass.ts
37 lines (33 loc) · 1006 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
33
34
35
36
37
import { Options, Result } from 'https://esm.sh/sass@1.32.8'
import type { LoaderPlugin } from '../types.ts'
type Sass = { renderSync(options: Options): Result }
export default (opts?: Options): LoaderPlugin => {
const decoder = new TextDecoder()
let sass: Sass | null = null
return {
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 (sass === null) {
sass = await import('https://esm.sh/sass@1.32.8')
}
const { css, map } = sass.renderSync({
indentedSyntax: url.endsWith('.sass'),
...opts,
file: url,
data: decoder.decode(content),
sourceMap: true
})
return {
code: decoder.decode(css),
type: 'css',
map: map ? decoder.decode(map) : undefined,
}
}
}
}