Skip to content

Commit 683715d

Browse files
committed
feat: 🎸 add support for lang=sugarss & type=text/sugarss
✅ Closes: #250
1 parent ee3c6f2 commit 683715d

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

Diff for: ‎docs/preprocessing.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ The CoffeeScript processor accepts no extra options and only transpiles CoffeeSc
269269

270270
You can check the [Less API reference](http://lesscss.org/usage/#less-options) for Less specific options.
271271

272-
### PostCSS
272+
### PostCSS / SugarSS
273273

274274
The PostCSS preprocessor accepts three options:
275275

@@ -341,7 +341,7 @@ You can check the [Sass API reference](https://sass-lang.com/documentation/js-ap
341341

342342
### Stylus
343343

344-
You can check the [Stylus API reference](https://stylus-lang.com/docs/js.html) for specific Stylus options. The `filename` property is overriden.
344+
You can check the [Stylus API reference](https://stylus-lang.com/docs/js.html) for specific Stylus options. The `filename` property is overridden.
345345

346346
### TypeScript
347347

@@ -361,7 +361,7 @@ As we're only transpiling, it's not possible to import types or interfaces into
361361

362362
### `globalStyle`
363363

364-
The `globalStyle` preprocessor extends the functionalities of Svelte's `:global` pseudo selector.
364+
The `globalStyle` preprocessor extends the functionalities of Svelte's `:global` pseudo-selector.
365365

366366
**`global` attribute:**
367367

Diff for: ‎src/autoProcess.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
addLanguageAlias,
1414
getLanguageFromAlias,
1515
SOURCE_MAP_PROP_MAP,
16-
LANG_SPECIFIC_OPTIONS,
16+
getLanguage,
17+
getLanguageDefaults,
1718
} from './modules/language';
1819
import { prepareContent } from './modules/prepareContent';
1920
import { transformMarkup } from './modules/markup';
@@ -119,7 +120,7 @@ export function sveltePreprocess(
119120
Object.assign(opts, nameOpts);
120121
}
121122

122-
Object.assign(opts, LANG_SPECIFIC_OPTIONS[alias]);
123+
Object.assign(opts, getLanguageDefaults(name), getLanguageDefaults(alias));
123124

124125
if (name !== alias && typeof aliasOpts === 'object') {
125126
Object.assign(opts, aliasOpts);
@@ -250,9 +251,11 @@ export function sveltePreprocess(
250251
// istanbul ignore else
251252
if (await hasDepInstalled('postcss')) {
252253
if (transformers.postcss) {
254+
const { alias } = getLanguage(attributes);
255+
253256
const transformed = await transform(
254257
'postcss',
255-
getTransformerOptions('postcss'),
258+
getTransformerOptions('postcss', alias),
256259
{ content: code, map, filename, attributes },
257260
);
258261

Diff for: ‎src/modules/language.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { basename } from 'path';
33
import { PreprocessorArgs } from '../types';
44
import { isValidLocalPath } from './utils';
55

6-
export const LANG_SPECIFIC_OPTIONS: Record<string, any> = {
6+
const LANG_SPECIFIC_OPTIONS: Record<string, any> = {
77
sass: {
88
indentedSyntax: true,
99
stripIndent: true,
@@ -17,8 +17,25 @@ export const LANG_SPECIFIC_OPTIONS: Record<string, any> = {
1717
stylus: {
1818
stripIndent: true,
1919
},
20+
// We need to defer this require to make sugarss an optional dependency.
21+
sugarss: () => ({
22+
stripIndent: true,
23+
// eslint-disable-next-line @typescript-eslint/no-require-imports, global-require
24+
parser: require('sugarss'),
25+
}),
2026
};
2127

28+
export function getLanguageDefaults(lang: string): null | Record<string, any> {
29+
const defaults = LANG_SPECIFIC_OPTIONS[lang];
30+
31+
if (!defaults) return null;
32+
if (typeof defaults === 'function') {
33+
return defaults();
34+
}
35+
36+
return defaults;
37+
}
38+
2239
export const SOURCE_MAP_PROP_MAP: Record<string, [string, any]> = {
2340
babel: ['sourceMaps', true],
2441
typescript: ['sourceMap', true],
@@ -33,6 +50,7 @@ export const SOURCE_MAP_PROP_MAP: Record<string, [string, any]> = {
3350
export const ALIAS_MAP = new Map([
3451
['pcss', 'css'],
3552
['postcss', 'css'],
53+
['sugarss', 'css'],
3654
['sass', 'scss'],
3755
['styl', 'stylus'],
3856
['js', 'javascript'],

Diff for: ‎test/transformers/postcss.test.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { resolve } from 'path';
66
import sveltePreprocess from '../../src';
77
import { preprocess, spyConsole } from '../utils';
88

9-
spyConsole();
9+
spyConsole({ silent: true });
1010

1111
describe('transformer - postcss', () => {
1212
it('should not transform plain css with postcss if { postcss: falsy }', async () => {
@@ -106,8 +106,33 @@ div
106106

107107
const preprocessed = await preprocess(template, opts);
108108

109-
expect(preprocessed.toString()).toContain(`div {
110-
color: red
111-
}`);
109+
expect(preprocessed.toString()).toMatchInlineSnapshot(`
110+
"<style>
111+
div {
112+
color: red
113+
}</style>"
114+
`);
115+
});
116+
117+
it('should support lang=sugarss without automatic indentation removal', async () => {
118+
const template = `<style lang="sugarss">
119+
div
120+
color: red
121+
</style>`;
122+
123+
const opts = sveltePreprocess({
124+
postcss: {
125+
plugins: [],
126+
},
127+
});
128+
129+
const preprocessed = await preprocess(template, opts);
130+
131+
expect(preprocessed.toString()).toMatchInlineSnapshot(`
132+
"<style lang=\\"sugarss\\">
133+
div {
134+
color: red
135+
}</style>"
136+
`);
112137
});
113138
});

0 commit comments

Comments
 (0)