Skip to content

Commit 46df563

Browse files
committed
Do not process external stylesheets automatically
1 parent 7cecaf5 commit 46df563

File tree

6 files changed

+59
-11
lines changed

6 files changed

+59
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"build": "npm run build:cjs && npm run build:esm",
2020
"build:cjs": "tsc --module commonjs --target es6 --outDir dist --declaration true",
2121
"build:esm": "tsc --module esnext --target esnext --outDir dist/esm && node tasks/parser.mjs && rm -rf dist/esm/",
22-
"dev": "npm run build -- -w",
22+
"dev": "npm run build:cjs -- -w",
2323
"lint": "eslint --ext .ts --fix ./src",
2424
"format": "prettier --write --loglevel warn ./{src,test}",
2525
"test": "jest",

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let pluginOptions: PluginOptions = {
1919
const markup = async ({ content, filename }: PreprocessorOptions): Promise<PreprocessorResult> => {
2020
const isIncluded = await isFileIncluded(pluginOptions.includePaths, filename);
2121

22-
if (!isIncluded) {
22+
if (!isIncluded || (!pluginOptions.parseStyleTag && !pluginOptions.parseExternalStylesheet)) {
2323
return { code: content };
2424
}
2525

@@ -32,7 +32,7 @@ const markup = async ({ content, filename }: PreprocessorOptions): Promise<Prepr
3232
// eslint-disable-next-line prefer-const
3333
let { mode, hashSeeder } = pluginOptions;
3434

35-
if (hasModuleAttribute(ast)) {
35+
if (pluginOptions.parseStyleTag && hasModuleAttribute(ast)) {
3636
const moduleAttribute = ast.css.attributes.find((item) => item.name === 'module');
3737
mode = moduleAttribute.value !== true ? moduleAttribute.value[0].data : mode;
3838
}
@@ -77,7 +77,6 @@ export default module.exports = (options: Partial<PluginOptions>) => {
7777
...pluginOptions,
7878
...options,
7979
};
80-
8180
return {
8281
markup,
8382
};

src/processors/processor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ export default class Processor {
9696
* @returns The CssModule updated component
9797
*/
9898
public parse = (): string => {
99-
if (hasModuleAttribute(this.ast)) {
99+
if (this.options.parseStyleTag && hasModuleAttribute(this.ast)) {
100100
this.isParsingImports = false;
101101
this.styleParser(this);
102102
}
103103

104-
if (hasModuleImports(this.rawContent)) {
104+
if (this.options.parseExternalStylesheet && hasModuleImports(this.rawContent)) {
105105
this.isParsingImports = true;
106106
parseImportDeclaration(this);
107107
}

test/nativeFixtures/stylesImports.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('Native Mode Imports', () => {
2929
},{
3030
mode: 'native',
3131
localIdentName: '[local]-123',
32+
parseExternalStylesheet: true,
3233
});
3334

3435
expect(output).toBe(expectedOutput);

test/scopedFixtures/stylesAttribute.test.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('Scoped Mode', () => {
5858
expect(output).toBe('<script>const red = true;</script><style module="scoped">.red-123 { color: red; } .blue-123 { color: blue; }</style><span class:red-123={red} class:blue-123={red}>Red</span>');
5959
});
6060

61-
test('Replace Multiple Clasnames on attribute', async () => {
61+
test('Replace multiple classnames on attribute', async () => {
6262
const output = await compiler({
6363
source: '<style module="scoped">.red { color: red; } .bold { font-weight: bold }</style><span class="red bold">Red</span>'
6464
}, {
@@ -67,7 +67,7 @@ describe('Scoped Mode', () => {
6767
expect(output).toBe('<style module="scoped">.red-123 { color: red; } .bold-123 { font-weight: bold }</style><span class="red-123 bold-123">Red</span>');
6868
});
6969

70-
test('Replace Clasnames on conditional expression', async () => {
70+
test('Replace classnames on conditional expression', async () => {
7171
const output = await compiler({
7272
source: `<style module="scoped">.red { color: red; } .bold { font-weight: bold }</style><span class="red {true ? 'bold' : 'red'} bold">Red</span>`
7373
}, {
@@ -76,7 +76,7 @@ describe('Scoped Mode', () => {
7676
expect(output).toBe(`<style module="scoped">.red-123 { color: red; } .bold-123 { font-weight: bold }</style><span class="red-123 {true ? 'bold-123' : 'red-123'} bold-123">Red</span>`);
7777
});
7878

79-
test('Replace Clasnames on component', async () => {
79+
test('Replace classname on component', async () => {
8080
const output = await compiler({
8181
source: `<script>import Button from './Button.svelte';</script><style module="scoped">.red { color: red; }</style><Button class="red" />`
8282
}, {
@@ -113,4 +113,23 @@ describe('Scoped Mode', () => {
113113
expect(output).toBe(`<style module="scoped">.red-123 { color: red; }</style><span class="red-123" data-color="red-123">Red</span>`);
114114
});
115115

116+
test('Do not replace the classname', async () => {
117+
const output = await compiler({
118+
source: `<style>.red { color: red; }</style><span class="red">Red</span>`
119+
}, {
120+
localIdentName: '[local]-123',
121+
});
122+
expect(output).toBe(`<style>.red { color: red; }</style><span class="red">Red</span>`);
123+
});
124+
125+
test('Do not replace the classname when `parseStyleTag` is off', async () => {
126+
const output = await compiler({
127+
source: `<style module="scoped">.red { color: red; }</style><span class="red">Red</span>`
128+
}, {
129+
localIdentName: '[local]-123',
130+
parseStyleTag: false,
131+
});
132+
expect(output).toBe(`<style module="scoped">.red { color: red; }</style><span class="red">Red</span>`);
133+
});
134+
116135
});

test/scopedFixtures/stylesImports.test.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
const compiler = require('../compiler.js');
22

33
describe('Scoped Mode Imports', () => {
4+
test('do no apply styling', async () => {
5+
const source =
6+
`<script>
7+
import style from './assets/class.module.css';
8+
</script>
9+
<div class:style.error={true}>Error</div>
10+
<div class:style.success={true}>Success</div>`;
11+
12+
const expectedOutput =
13+
`<script>
14+
import style from './assets/class.module.css';
15+
</script>
16+
<div class:style.error={true}>Error</div>
17+
<div class:style.success={true}>Success</div>`;
18+
19+
const output = await compiler({
20+
source,
21+
},{
22+
mode: 'scoped',
23+
localIdentName: '[local]-123',
24+
});
25+
26+
expect(output).toBe(expectedOutput);
27+
});
28+
429
test('Import all classes from stylesheet', async () => {
530
const source =
631
`<script>
@@ -24,6 +49,7 @@ describe('Scoped Mode Imports', () => {
2449
},{
2550
mode: 'scoped',
2651
localIdentName: '[local]-123',
52+
parseExternalStylesheet: true,
2753
});
2854

2955
expect(output).toBe(expectedOutput);
@@ -52,6 +78,7 @@ describe('Scoped Mode Imports', () => {
5278
},{
5379
mode: 'scoped',
5480
localIdentName: '[local]-123',
81+
parseExternalStylesheet: true,
5582
});
5683

5784
expect(output).toBe(expectedOutput);
@@ -80,7 +107,8 @@ describe('Scoped Mode Imports', () => {
80107
source
81108
},{
82109
mode: 'scoped',
83-
localIdentName: '[local]-123'
110+
localIdentName: '[local]-123',
111+
parseExternalStylesheet: true,
84112
});
85113
});
86114

@@ -107,6 +135,7 @@ describe('Scoped Mode Imports', () => {
107135
},{
108136
mode: 'scoped',
109137
localIdentName: '[local]-123',
138+
parseExternalStylesheet: true,
110139
});
111140

112141
expect(output).toBe(expectedOutput);
@@ -140,9 +169,9 @@ describe('Scoped Mode Imports', () => {
140169
},{
141170
mode: 'scoped',
142171
localIdentName: '[local]-123',
172+
parseExternalStylesheet: true,
143173
});
144174

145175
expect(output).toBe(expectedOutput);
146176
});
147-
148177
});

0 commit comments

Comments
 (0)