diff --git a/plugins/sass.ts b/plugins/sass.ts index 5d689e438..632afde61 100644 --- a/plugins/sass.ts +++ b/plugins/sass.ts @@ -1,6 +1,6 @@ -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, @@ -8,7 +8,8 @@ export default { 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), @@ -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 diff --git a/plugins/sass_test.ts b/plugins/sass_test.ts index 6fdeb2056..e6c2e3847 100644 --- a/plugins/sass_test.ts +++ b/plugins/sass_test.ts @@ -1,7 +1,7 @@ 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/' @@ -9,7 +9,7 @@ Deno.test('project sass loader plugin', () => { }) 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) @@ -17,3 +17,21 @@ Deno.test('project sass loader plugin', () => { 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}') +})