Skip to content

Commit a61ada6

Browse files
committed
fix: merge globalStyle and globalRule transformers
1 parent a67b487 commit a61ada6

File tree

10 files changed

+233
-284
lines changed

10 files changed

+233
-284
lines changed

README.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,14 @@ import { mdsvex } from 'mdsvex'
394394
In case you want to manually configure your preprocessing step, `svelte-preprocess` exports these named processors:
395395
396396
- `pug`
397-
- `coffeescript` or `coffee`
397+
- `coffeescript`
398398
- `typescript`
399399
- `less`
400400
- `scss` or `sass`
401401
- `stylus`
402402
- `postcss`
403403
- `babel`
404-
- `globalStyle` - transform `<style global>` into global styles.
404+
- `globalStyle` - transform `<style global>` into global styles and supports `:global` nested selectors.
405405
- `replace` - replace string patterns in your markup.
406406
407407
```js
@@ -524,14 +524,14 @@ const options = {
524524
},
525525
526526
/** Use a custom preprocess method by passing a function. */
527-
pug({ content, filename }) {
527+
pug({ content, filename, attributes }) {
528528
const code = pug.render(content);
529529
530530
return { code, map: null };
531531
},
532532
533533
/** Add a custom language preprocessor */
534-
customLanguage({ content, filename }) {
534+
customLanguage({ content, filename, attributes }) {
535535
const { code, map } = require('custom-language-compiler')(content);
536536
537537
return { code, map };
@@ -559,13 +559,10 @@ const options = {
559559
[/@html\s*\((.*?)\)$/gim, '{@html $1}'],
560560
],
561561
562-
/** Configure globalStyle and globalRule source map options */
562+
/** Configure globalStyle source map options */
563563
globalStyle: {
564564
sourceMap: true,
565565
},
566-
globalRule: {
567-
sourceMap: true,
568-
},
569566
};
570567
571568
svelte.preprocess(input, sveltePreprocess(options));
@@ -660,4 +657,4 @@ import preprocess from 'svelte-preprocess'
660657
...
661658
```
662659
663-
Warning: If you do this, you can't import types or interfaces into your svelte component without using the new TS 3.8 `type` import modifier: `import type { SomeInterface } from './MyModule.ts'` otherwise Rollup (and possibly others) will complain that the name is not exported by `MyModule`)
660+
Warning: If you do this, you can't import types or interfaces into your svelte component without using the new TS 3.8 `type` import modifier: `import type { SomeInterface } from './MyModule.ts'` otherwise Rollup (and possibly others) will complain that the name is not exported by `MyModule`)

src/autoProcess.ts

+14-25
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ interface Transformers {
2525
coffeescript?: TransformerOptions<Options.Coffeescript>;
2626
pug?: TransformerOptions<Options.Pug>;
2727
globalStyle?: Options.GlobalStyle;
28-
globalRule?: Options.GlobalRule;
2928
replace?: Options.Replace;
3029
[languageName: string]: TransformerOptions;
3130
}
@@ -54,7 +53,6 @@ type AutoPreprocessOptions = {
5453
coffeescript?: TransformerOptions<Options.Coffeescript>;
5554
pug?: TransformerOptions<Options.Pug>;
5655
globalStyle?: Options.GlobalStyle;
57-
globalRule?: Options.GlobalRule;
5856
// workaround while we don't have this
5957
// https://github.com/microsoft/TypeScript/issues/17867
6058
[languageName: string]:
@@ -121,10 +119,14 @@ export function autoPreprocess(
121119
const getTransformerTo = (targetLanguage: string): Preprocessor => async (
122120
svelteFile,
123121
) => {
124-
const { content, filename, lang, alias, dependencies } = await parseFile(
125-
svelteFile,
126-
targetLanguage,
127-
);
122+
const {
123+
content,
124+
filename,
125+
lang,
126+
alias,
127+
dependencies,
128+
attributes,
129+
} = await parseFile(svelteFile, targetLanguage);
128130

129131
if (preserve.includes(lang) || preserve.includes(alias)) {
130132
return;
@@ -141,7 +143,7 @@ export function autoPreprocess(
141143
const transformed = await runTransformer(
142144
lang,
143145
getTransformerOptions(lang, alias),
144-
{ content: stripIndent(content), filename },
146+
{ content: stripIndent(content), filename, attributes },
145147
);
146148

147149
return {
@@ -228,6 +230,7 @@ export function autoPreprocess(
228230
content: code,
229231
map,
230232
filename,
233+
attributes,
231234
});
232235

233236
code = transformed.code;
@@ -253,7 +256,7 @@ export function autoPreprocess(
253256
const transformed = await runTransformer(
254257
'postcss',
255258
transformers.postcss,
256-
{ content: code, map, filename },
259+
{ content: code, map, filename, attributes },
257260
);
258261

259262
code = transformed.code;
@@ -262,28 +265,14 @@ export function autoPreprocess(
262265
}
263266

264267
if (await hasPostCssInstalled()) {
265-
if (attributes.global) {
266-
const transformed = await runTransformer(
267-
'globalStyle',
268-
transformers?.globalStyle,
269-
{
270-
content: code,
271-
map,
272-
filename,
273-
},
274-
);
275-
276-
code = transformed.code;
277-
map = transformed.map;
278-
}
279-
280268
const transformed = await runTransformer(
281-
'globalRule',
282-
transformers?.globalRule,
269+
'globalStyle',
270+
transformers?.globalStyle,
283271
{
284272
content: code,
285273
map,
286274
filename,
275+
attributes,
287276
},
288277
);
289278

src/modules/transformers.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ const TRANSFORMERS = {} as {
1010
export const runTransformer = async (
1111
name: string,
1212
options: TransformerOptions,
13-
{ content, map, filename }: TransformerArgs<any>,
13+
{ content, map, filename, attributes }: TransformerArgs<any>,
1414
): Promise<ReturnType<Transformer<unknown>>> => {
1515
// remove any unnecessary indentation (useful for coffee, pug and sugarss)
1616
content = stripIndent(content);
1717

1818
if (typeof options === 'function') {
19-
return options({ content, map, filename });
19+
return options({ content, map, filename, attributes });
2020
}
2121

2222
try {
@@ -31,6 +31,7 @@ export const runTransformer = async (
3131
content,
3232
filename,
3333
map,
34+
attributes,
3435
options: typeof options === 'boolean' ? null : options,
3536
});
3637
} catch (e) {

src/processors/globalRule.ts

-13
This file was deleted.

src/transformers/globalRule.ts

-53
This file was deleted.

src/transformers/globalStyle.ts

+43-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,38 @@ import postcss, { AtRule } from 'postcss';
33
import { Transformer, Options } from '../types';
44
import { globalifySelector } from '../modules/globalifySelector';
55

6-
const globalifyPlugin = (root: postcss.Root) => {
6+
const selectorPattern = /:global(?!\()/;
7+
8+
const globalifyRulePlugin: postcss.Transformer = (root) => {
9+
root.walkRules(selectorPattern, (rule) => {
10+
const modifiedSelectors = rule.selectors
11+
.filter((selector) => selector !== ':global')
12+
.map((selector) => {
13+
const [beginning, ...rest] = selector.split(selectorPattern);
14+
15+
if (rest.length === 0) return beginning;
16+
17+
return [beginning, ...rest.map(globalifySelector)]
18+
.map((str) => str.trim())
19+
.join(' ')
20+
.trim();
21+
});
22+
23+
if (modifiedSelectors.length === 0) {
24+
rule.remove();
25+
26+
return;
27+
}
28+
29+
rule.replaceWith(
30+
rule.clone({
31+
selectors: modifiedSelectors,
32+
}),
33+
);
34+
});
35+
};
36+
37+
const globalAttrPlugin = (root: postcss.Root) => {
738
root.walkAtRules(/keyframes$/, (atrule) => {
839
if (!atrule.params.startsWith('-global-')) {
940
atrule.replaceWith(
@@ -31,13 +62,18 @@ const transformer: Transformer<Options.GlobalStyle> = async ({
3162
content,
3263
filename,
3364
options,
65+
map,
66+
attributes,
3467
}) => {
35-
const { css, map: newMap } = await postcss()
36-
.use(globalifyPlugin)
37-
.process(content, {
38-
from: filename,
39-
map: options?.sourceMap ?? false,
40-
});
68+
const plugins = [
69+
globalifyRulePlugin,
70+
attributes?.global && globalAttrPlugin,
71+
].filter(Boolean);
72+
73+
const { css, map: newMap } = await postcss(plugins).process(content, {
74+
from: filename,
75+
map: options?.sourceMap ? { prev: map } : false,
76+
});
4177

4278
return { code: css, map: newMap };
4379
};

src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type PreprocessorArgs = Preprocessor extends (options: infer T) => any
2020
export interface TransformerArgs<T> {
2121
content: string;
2222
filename: string;
23+
attributes?: Record<string, any>;
2324
map?: string | object;
2425
dianostics?: unknown[];
2526
options?: T;

src/types/options.ts

-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ export interface Typescript {
6868
reportDiagnostics?: boolean;
6969
}
7070

71-
export interface GlobalRule {
72-
sourceMap: boolean;
73-
}
74-
7571
export interface GlobalStyle {
7672
sourceMap: boolean;
7773
}

0 commit comments

Comments
 (0)