Skip to content

Commit 233f0a5

Browse files
committed
Set defaultOptions properly
1 parent 8dbb49a commit 233f0a5

File tree

3 files changed

+47
-46
lines changed

3 files changed

+47
-46
lines changed

src/index.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ import type { PluginOptions, PreprocessorOptions, PreprocessorResult } from './t
44
import { nativeProcessor, mixedProcessor, scopedProcessor } from './processors';
55
import { getLocalIdent, isFileIncluded, hasModuleImports, hasModuleAttribute } from './lib';
66

7-
let pluginOptions: PluginOptions = {
8-
mode: 'native',
9-
getLocalIdent,
10-
hashSeeder: ['style', 'filepath', 'classname'],
11-
includePaths: [],
12-
includeAttributes: [],
13-
localIdentName: '[local]-[hash:base64:6]',
14-
parseStyleTag: true,
15-
parseExternalStylesheet: false,
16-
useAsDefaultScoping: false,
7+
const defaultOptions = (): PluginOptions => {
8+
return {
9+
getLocalIdent,
10+
hashSeeder: ['style', 'filepath', 'classname'],
11+
includeAttributes: [],
12+
includePaths: [],
13+
localIdentName: '[local]-[hash:base64:6]',
14+
mode: 'native',
15+
parseExternalStylesheet: false,
16+
parseStyleTag: true,
17+
useAsDefaultScoping: false,
18+
};
1719
};
1820

21+
let pluginOptions: PluginOptions;
22+
1923
const markup = async ({ content, filename }: PreprocessorOptions): Promise<PreprocessorResult> => {
2024
const isIncluded = await isFileIncluded(pluginOptions.includePaths, filename);
2125

@@ -75,10 +79,9 @@ const markup = async ({ content, filename }: PreprocessorOptions): Promise<Prepr
7579
};
7680
};
7781

78-
// eslint-disable-next-line no-multi-assign
7982
export default module.exports = (options: Partial<PluginOptions>) => {
8083
pluginOptions = {
81-
...pluginOptions,
84+
...defaultOptions(),
8285
...options,
8386
};
8487
return {

src/types/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { GetLocalIdent } from '../lib';
22

33
export type PluginOptions = {
4-
mode: 'native' | 'mixed' | 'scoped';
4+
getLocalIdent: GetLocalIdent;
5+
hashSeeder: Array<'style' | 'filepath' | 'classname'>;
56
includeAttributes: string[];
67
includePaths: string[];
78
localIdentName: string;
8-
getLocalIdent: GetLocalIdent;
9-
hashSeeder: Array<'style' | 'filepath' | 'classname'>;
10-
parseStyleTag: boolean;
9+
mode: 'native' | 'mixed' | 'scoped';
1110
parseExternalStylesheet: boolean;
11+
parseStyleTag: boolean;
1212
useAsDefaultScoping: boolean;
1313
};
1414

test/globalFixtures/options.test.js

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

53+
test('Use the filepath only as hash seeder', async () => {
54+
const output = await compiler({
55+
source: '<style module>.red { color: red; } .bold { color: bold; }</style><span class="red bold">Red</span>',
56+
}, {
57+
mode: 'native',
58+
localIdentName: '[local]-[hash:6]',
59+
hashSeeder: ['filepath'],
60+
});
61+
62+
expect(output).toBe(
63+
'<style module>:global(.red-027d15) { color: red; } :global(.bold-027d15) { color: bold; }</style><span class="red-027d15 bold-027d15">Red</span>'
64+
);
65+
});
66+
67+
describe('When the hashSeeder has a wrong key', () => {
68+
const source = '<style module>.red { color: red; }</style>';
69+
70+
it('throws an exception', async () => {
71+
await expect(compiler({
72+
source
73+
}, {
74+
hashSeeder: ['filepath', 'content'],
75+
})).rejects.toThrow(
76+
`The hash seeder only accepts the keys 'style', 'filepath' and 'classname': 'content' was passed.`
77+
);
78+
});
79+
});
80+
5381
describe('When the preprocessor is set as default scoping', () => {
5482
it('parses the style tag with no module attributes', async () => {
5583
const source = '<style>.red { color: red; }</style><p class="red">red</p>';
@@ -94,40 +122,10 @@ describe('When the preprocessor is set as default scoping', () => {
94122
const output = await compiler({
95123
source
96124
}, {
97-
parseStyleTag: true,
98125
useAsDefaultScoping: true,
99126
});
100127

101128
expect(output).toBe('<p class="red">red</p>')
102129
});
103130
});
104131

105-
test('Use the filepath only as hash seeder', async () => {
106-
const output = await compiler({
107-
source: '<style module>.red { color: red; } .bold { color: bold; }</style><span class="red bold">Red</span>',
108-
}, {
109-
mode: 'native',
110-
localIdentName: '[local]-[hash:6]',
111-
hashSeeder: ['filepath'],
112-
});
113-
114-
expect(output).toBe(
115-
'<style module>:global(.red-027d15) { color: red; } :global(.bold-027d15) { color: bold; }</style><span class="red-027d15 bold-027d15">Red</span>'
116-
);
117-
});
118-
119-
describe('When the hashSeeder has a wrong key', () => {
120-
const source = '<style module>.red { color: red; }</style>';
121-
122-
it('throws an exception', async () => {
123-
await expect(compiler({
124-
source
125-
}, {
126-
hashSeeder: ['filepath', 'content'],
127-
})).rejects.toThrow(
128-
`The hash seeder only accepts the keys 'style', 'filepath' and 'classname': 'content' was passed.`
129-
);
130-
});
131-
});
132-
133-

0 commit comments

Comments
 (0)