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

SASS Support for SASS Plugin #41

Merged
merged 6 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions plugins/sass.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { renderSync } from 'https://esm.sh/sass@1.29.0'
import { Options, renderSync } from 'https://esm.sh/sass@1.29.0'

export default {
const defaultPlugin = {
name: 'sass-loader',
test: /.(sass|scss)$/,
acceptHMR: true,
transform(content: Uint8Array, path: string) {
const ret = renderSync({
file: path,
data: (new TextDecoder).decode(content),
sourceMap: true
sourceMap: true,
indentedSyntax: path.endsWith('.sass')
})
return {
code: (new TextDecoder).decode(ret.css),
Expand All @@ -17,3 +18,28 @@ export default {
}
}
}

const pluginFactory = (opts: Options) => ({
...defaultPlugin,
transform(content: Uint8Array, path: string) {
const ret = renderSync({
indentedSyntax: path.endsWith('.sass'),
...opts,
file: path,
data: (new TextDecoder).decode(content),
sourceMap: true
})
return {
code: (new TextDecoder).decode(ret.css),
map: ret.map ? (new TextDecoder).decode(ret.map) : undefined,
loader: 'css'
}
}
})

pluginFactory.displayName = defaultPlugin.name
pluginFactory.test = defaultPlugin.test
pluginFactory.acceptHMR = defaultPlugin.acceptHMR
pluginFactory.transform = defaultPlugin.transform

export default pluginFactory
22 changes: 20 additions & 2 deletions plugins/sass_test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts';
import plugin from './sass.ts';

Deno.test('project sass loader plugin', () => {
Deno.test('project scss loader plugin', () => {
Object.assign(window, {
location: {
href: 'https://localhost/'
}
})
const { code, loader } = plugin.transform(
(new TextEncoder).encode('$someVar: 123px; .some-selector { width: $someVar; }'),
'test.sass'
'test.scss'
)
assertEquals(plugin.test.test('test.sass'), true)
assertEquals(plugin.test.test('test.scss'), true)
assertEquals(plugin.acceptHMR, true)
assertEquals(code, '.some-selector {\n width: 123px;\n}')
assertEquals(loader, 'css')
})

Deno.test('project sass loader plugin', () => {
Object.assign(window, {
location: {
href: 'https://localhost/'
}
})
let ret = plugin.transform(
(new TextEncoder).encode('$someVar: 123px\n.some-selector\n width: 123px'),
'test.sass'
)
assertEquals(ret.code, '.some-selector {\n width: 123px;\n}')
ret = plugin({ indentType: 'tab', indentWidth: 2 }).transform(
(new TextEncoder).encode('$someVar: 123px\n.some-selector\n width: 123px'),
'test.sass'
)
assertEquals(ret.code, '.some-selector {\n\t\twidth: 123px;\n}')
})