Skip to content

Commit 2717c5b

Browse files
committed
feat: 🎸 add globalStyle.sourceMap and globalRule.sourceMap opts
1 parent 12c3af3 commit 2717c5b

File tree

8 files changed

+118
-36
lines changed

8 files changed

+118
-36
lines changed

‎README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,15 @@ const options = {
557557
[/@endawait$/gim, '{/await}'],
558558
[/@debug\s*\((.*?)\)$/gim, '{@debug $1}'],
559559
[/@html\s*\((.*?)\)$/gim, '{@html $1}'],
560-
];
560+
],
561+
562+
/** Configure globalStyle and globalRule source map options */
563+
globalStyle: {
564+
sourceMap: true,
565+
},
566+
globalRule: {
567+
sourceMap: true,
568+
},
561569
};
562570
563571
svelte.preprocess(input, sveltePreprocess(options));

‎src/autoProcess.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ interface Transformers {
2424
postcss?: TransformerOptions<Options.Postcss>;
2525
coffeescript?: TransformerOptions<Options.Coffeescript>;
2626
pug?: TransformerOptions<Options.Pug>;
27-
globalStyle?: TransformerOptions;
28-
globalRule?: TransformerOptions;
27+
globalStyle?: Options.GlobalStyle;
28+
globalRule?: Options.GlobalRule;
2929
replace?: Options.Replace;
3030
[languageName: string]: TransformerOptions;
3131
}
@@ -53,8 +53,8 @@ type AutoPreprocessOptions = {
5353
babel?: TransformerOptions<Options.Babel>;
5454
coffeescript?: TransformerOptions<Options.Coffeescript>;
5555
pug?: TransformerOptions<Options.Pug>;
56-
globalStyle?: TransformerOptions<Options.Typescript>;
57-
globalRule?: TransformerOptions<Options.Typescript>;
56+
globalStyle?: Options.GlobalStyle;
57+
globalRule?: Options.GlobalRule;
5858
// workaround while we don't have this
5959
// https://github.com/microsoft/TypeScript/issues/17867
6060
[languageName: string]:
@@ -263,21 +263,29 @@ export function autoPreprocess(
263263

264264
if (await hasPostCssInstalled()) {
265265
if (attributes.global) {
266-
const transformed = await runTransformer('globalStyle', null, {
267-
content: code,
268-
map,
269-
filename,
270-
});
266+
const transformed = await runTransformer(
267+
'globalStyle',
268+
transformers?.globalStyle,
269+
{
270+
content: code,
271+
map,
272+
filename,
273+
},
274+
);
271275

272276
code = transformed.code;
273277
map = transformed.map;
274278
}
275279

276-
const transformed = await runTransformer('globalRule', null, {
277-
content: code,
278-
map,
279-
filename,
280-
});
280+
const transformed = await runTransformer(
281+
'globalRule',
282+
transformers?.globalRule,
283+
{
284+
content: code,
285+
map,
286+
filename,
287+
},
288+
);
281289

282290
code = transformed.code;
283291
map = transformed.map;

‎src/transformers/globalRule.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
import postcss from 'postcss';
22

3-
import { Transformer } from '../types';
3+
import { Transformer, Options } from '../types';
44
import { globalifySelector } from '../modules/globalifySelector';
55

66
const selectorPattern = /:global(?!\()/;
77

8-
const globalifyRulePlugin = (root: any) => {
9-
root.walkRules(selectorPattern, (rule: any) => {
10-
const [beginning, ...rest] = rule.selector.split(selectorPattern);
8+
const globalifyRulePlugin: postcss.Transformer = (root) => {
9+
root.walkRules(selectorPattern, (rule) => {
10+
const modifiedSelectors = rule.selectors.map((selector) => {
11+
const [beginning, ...rest] = selector.split(selectorPattern);
1112

12-
rule.selector = [beginning, ...rest.map(globalifySelector)]
13-
.map((str) => str.trim())
14-
.join(' ')
15-
.trim();
13+
if (rest.length === 0) return;
14+
15+
return [beginning, ...rest.map(globalifySelector)]
16+
.map((str) => str.trim())
17+
.join(' ')
18+
.trim();
19+
});
20+
21+
rule.replaceWith(
22+
rule.clone({
23+
selectors: modifiedSelectors,
24+
}),
25+
);
1626
});
1727
};
1828

19-
const transformer: Transformer<never> = async ({ content, filename }) => {
29+
// todo - this can be merged with the globalStyle
30+
const transformer: Transformer<Options.GlobalRule> = async ({
31+
content,
32+
filename,
33+
options,
34+
}) => {
2035
const { css, map: newMap } = await postcss()
2136
.use(globalifyRulePlugin)
22-
.process(content, { from: filename, map: false });
37+
.process(content, {
38+
from: filename,
39+
map: options?.sourceMap ?? false,
40+
});
2341

2442
return { code: css, map: newMap };
2543
};

‎src/transformers/globalStyle.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
1-
import postcss from 'postcss';
1+
import postcss, { AtRule } from 'postcss';
22

3-
import { Transformer } from '../types';
3+
import { Transformer, Options } from '../types';
44
import { globalifySelector } from '../modules/globalifySelector';
55

6-
const globalifyPlugin = (root: any) => {
7-
root.walkAtRules(/keyframes$/, (atrule: any) => {
6+
const globalifyPlugin = (root: postcss.Root) => {
7+
root.walkAtRules(/keyframes$/, (atrule) => {
88
if (!atrule.params.startsWith('-global-')) {
9-
atrule.params = `-global-${atrule.params}`;
9+
atrule.replaceWith(
10+
atrule.clone({
11+
params: `-global-${atrule.params}`,
12+
}),
13+
);
1014
}
1115
});
1216

13-
root.walkRules((rule: any) => {
14-
if (rule.parent && rule.parent.name === 'keyframes') {
17+
root.walkRules((rule) => {
18+
if ((rule?.parent as AtRule)?.name === 'keyframes') {
1519
return;
1620
}
1721

18-
rule.selectors = rule.selectors.map(globalifySelector);
22+
rule.replaceWith(
23+
rule.clone({
24+
selectors: rule.selectors.map(globalifySelector),
25+
}),
26+
);
1927
});
2028
};
2129

22-
const transformer: Transformer<never> = async ({ content, filename }) => {
30+
const transformer: Transformer<Options.GlobalStyle> = async ({
31+
content,
32+
filename,
33+
options,
34+
}) => {
2335
const { css, map: newMap } = await postcss()
2436
.use(globalifyPlugin)
25-
.process(content, { from: filename, map: false });
37+
.process(content, {
38+
from: filename,
39+
map: options?.sourceMap ?? false,
40+
});
2641

2742
return { code: css, map: newMap };
2843
};

‎src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ export type Transformer<T> = (
3535

3636
export type TransformerOptions<T = any> =
3737
| boolean
38-
| Record<string, any>
38+
| Record<keyof T | string, T[keyof T] | any>
3939
| Transformer<T>;

‎src/types/options.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,11 @@ export interface Typescript {
6767
transpileOnly?: boolean;
6868
reportDiagnostics?: boolean;
6969
}
70+
71+
export interface GlobalRule {
72+
sourceMap: boolean;
73+
}
74+
75+
export interface GlobalStyle {
76+
sourceMap: boolean;
77+
}

‎test/transformers/globalRule.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ describe('transformer - globalRule', () => {
99
expect(() => preprocess(template, opts)).not.toThrow();
1010
});
1111

12+
it('adds sourceMap with { sourceMap: true }', async () => {
13+
const template = `<style>:global div{color:red}:global .test{}</style>`;
14+
const opts = autoProcess({
15+
globalRule: {
16+
sourceMap: true,
17+
},
18+
});
19+
const preprocessed = await preprocess(template, opts);
20+
21+
expect(preprocessed.toString()).toContain(`sourceMappingURL`);
22+
});
23+
1224
it('wraps selector in :global(...) modifier', async () => {
1325
const template = `<style>:global div{color:red}:global .test{}</style>`;
1426
const opts = autoProcess();

‎test/transformers/globalStyle.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import autoProcess from '../../src';
22
import { preprocess } from '../utils';
33

44
describe('transformer - globalStyle', () => {
5+
// todo: why it isn't generating a sourcemap?
6+
it.skip('adds sourceMap with { sourceMap: true }', async () => {
7+
const template = `<style global>div,span{color:red}.test{}</style>`;
8+
const opts = autoProcess({
9+
globalStyle: {
10+
sourceMap: true,
11+
},
12+
});
13+
const preprocessed = await preprocess(template, opts);
14+
15+
expect(preprocessed.toString()).toContain(`sourceMappingURL`);
16+
});
17+
518
it('wraps selector in :global(...) modifier', async () => {
619
const template = `<style global>div{color:red}.test{}</style>`;
720
const opts = autoProcess();

0 commit comments

Comments
 (0)