Skip to content

Commit 23c2a4e

Browse files
committed
Add useAs defaultScoping option
1 parent 46df563 commit 23c2a4e

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ const markup = async ({ content, filename }: PreprocessorOptions): Promise<Prepr
2525

2626
const ast: Ast = parse(content, { filename });
2727

28-
if (!hasModuleAttribute(ast) && !hasModuleImports(content)) {
28+
if (
29+
!pluginOptions.useAsDefaultScoping &&
30+
!hasModuleAttribute(ast) &&
31+
!hasModuleImports(content)
32+
) {
2933
return { code: content };
3034
}
3135

src/processors/processor.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ export default class Processor {
9696
* @returns The CssModule updated component
9797
*/
9898
public parse = (): string => {
99-
if (this.options.parseStyleTag && hasModuleAttribute(this.ast)) {
99+
if (
100+
this.options.parseStyleTag &&
101+
(hasModuleAttribute(this.ast) || (this.options.useAsDefaultScoping && this.ast.css))
102+
) {
100103
this.isParsingImports = false;
101104
this.styleParser(this);
102105
}

test/globalFixtures/options.test.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,44 @@ describe('When the module attribute has an invalid value', () => {
5050
});
5151
});
5252

53+
describe('When the preprocessor is set as default scoping', () => {
54+
it('parses the style tag with no module attributes', async () => {
55+
const source = '<style>.red { color: red; }</style><p class="red">red</p>';
56+
const output = await compiler({
57+
source
58+
}, {
59+
mode: 'native',
60+
localIdentName: '[local]-123',
61+
useAsDefaultScoping: true,
62+
});
63+
64+
expect(output).toBe('<style>:global(.red-123) { color: red; }</style><p class="red-123">red</p>')
65+
});
66+
67+
it('parses the style tag with module attributes', async () => {
68+
const source = '<style module="scoped">.red { color: red; }</style><p class="red">red</p>';
69+
const output = await compiler({
70+
source
71+
}, {
72+
localIdentName: '[local]-123',
73+
useAsDefaultScoping: true,
74+
});
75+
76+
expect(output).toBe('<style module="scoped">.red-123 { color: red; }</style><p class="red-123">red</p>')
77+
});
78+
79+
it('does not parse when the style tag does not exist', async () => {
80+
const source = '<p class="red">red</p>';
81+
const output = await compiler({
82+
source
83+
}, {
84+
useAsDefaultScoping: true,
85+
});
86+
87+
expect(output).toBe('<p class="red">red</p>')
88+
});
89+
});
90+
5391
test('Use the filepath only as hash seeder', async () => {
5492
const output = await compiler({
5593
source: '<style module>.red { color: red; } .bold { color: bold; }</style><span class="red bold">Red</span>',
@@ -79,4 +117,3 @@ describe('When the hashSeeder has a wrong key', () => {
79117
});
80118

81119

82-

0 commit comments

Comments
 (0)