Skip to content

Commit 364bad1

Browse files
author
X
authored
Merge pull request #41 from shadowtime2000/master
SASS Support for SASS Plugin
2 parents 88c8929 + b645f94 commit 364bad1

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

plugins/sass.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { renderSync } from 'https://esm.sh/sass@1.29.0'
1+
import { Options, renderSync } from 'https://esm.sh/sass@1.29.0'
22

3-
export default {
3+
const defaultPlugin = {
44
name: 'sass-loader',
55
test: /.(sass|scss)$/,
66
acceptHMR: true,
77
transform(content: Uint8Array, path: string) {
88
const ret = renderSync({
99
file: path,
1010
data: (new TextDecoder).decode(content),
11-
sourceMap: true
11+
sourceMap: true,
12+
indentedSyntax: path.endsWith('.sass')
1213
})
1314
return {
1415
code: (new TextDecoder).decode(ret.css),
@@ -17,3 +18,28 @@ export default {
1718
}
1819
}
1920
}
21+
22+
const pluginFactory = (opts: Options) => ({
23+
...defaultPlugin,
24+
transform(content: Uint8Array, path: string) {
25+
const ret = renderSync({
26+
indentedSyntax: path.endsWith('.sass'),
27+
...opts,
28+
file: path,
29+
data: (new TextDecoder).decode(content),
30+
sourceMap: true
31+
})
32+
return {
33+
code: (new TextDecoder).decode(ret.css),
34+
map: ret.map ? (new TextDecoder).decode(ret.map) : undefined,
35+
loader: 'css'
36+
}
37+
}
38+
})
39+
40+
pluginFactory.displayName = defaultPlugin.name
41+
pluginFactory.test = defaultPlugin.test
42+
pluginFactory.acceptHMR = defaultPlugin.acceptHMR
43+
pluginFactory.transform = defaultPlugin.transform
44+
45+
export default pluginFactory

plugins/sass_test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts';
22
import plugin from './sass.ts';
33

4-
Deno.test('project sass loader plugin', () => {
4+
Deno.test('project scss loader plugin', () => {
55
Object.assign(window, {
66
location: {
77
href: 'https://localhost/'
88
}
99
})
1010
const { code, loader } = plugin.transform(
1111
(new TextEncoder).encode('$someVar: 123px; .some-selector { width: $someVar; }'),
12-
'test.sass'
12+
'test.scss'
1313
)
1414
assertEquals(plugin.test.test('test.sass'), true)
1515
assertEquals(plugin.test.test('test.scss'), true)
1616
assertEquals(plugin.acceptHMR, true)
1717
assertEquals(code, '.some-selector {\n width: 123px;\n}')
1818
assertEquals(loader, 'css')
1919
})
20+
21+
Deno.test('project sass loader plugin', () => {
22+
Object.assign(window, {
23+
location: {
24+
href: 'https://localhost/'
25+
}
26+
})
27+
let ret = plugin.transform(
28+
(new TextEncoder).encode('$someVar: 123px\n.some-selector\n width: 123px'),
29+
'test.sass'
30+
)
31+
assertEquals(ret.code, '.some-selector {\n width: 123px;\n}')
32+
ret = plugin({ indentType: 'tab', indentWidth: 2 }).transform(
33+
(new TextEncoder).encode('$someVar: 123px\n.some-selector\n width: 123px'),
34+
'test.sass'
35+
)
36+
assertEquals(ret.code, '.some-selector {\n\t\twidth: 123px;\n}')
37+
})

0 commit comments

Comments
 (0)