Skip to content

Commit 09dfccd

Browse files
authored
Merge pull request #8 from fmal/fix/customMatcher
Improve `customMatcher` support
2 parents b7fbbce + a739deb commit 09dfccd

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

src/helpers/__tests__/cssExtensions.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { isCSS, isRelativeCSS } from '../cssExtensions';
1+
import {
2+
isCSS,
3+
isRelativeCSS,
4+
setExtensionsPattern,
5+
DEFAULT_EXTENSIONS_PATTERN,
6+
} from '../cssExtensions';
27

38
describe('utils / cssExtensions', () => {
49
describe('isCSS', () => {
@@ -26,4 +31,18 @@ describe('utils / cssExtensions', () => {
2631
expect(isRelativeCSS('myfile.module.css')).toBe(false);
2732
});
2833
});
34+
35+
describe('setExtensionsPattern', () => {
36+
afterEach(() => setExtensionsPattern(DEFAULT_EXTENSIONS_PATTERN));
37+
38+
it('should overwrite default extensions pattern', () => {
39+
const pattern = new RegExp('\\.css$');
40+
setExtensionsPattern(pattern);
41+
42+
expect(isCSS('./myfile.css')).toBe(true);
43+
expect(isCSS('./myfile.scss')).toBe(false);
44+
expect(isRelativeCSS('../folder/myfile.css')).toBe(true);
45+
expect(isRelativeCSS('../folders/myfile.scss')).toBe(false);
46+
});
47+
});
2948
});

src/helpers/cssExtensions.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
const isRelative = (fileName: string) => /^\.\.?($|[\\/])/.test(fileName);
22

3-
export const isCSS = (fileName: string) =>
4-
/\.module\.(sa|sc|c)ss$/.test(fileName);
3+
export const DEFAULT_EXTENSIONS_PATTERN = /\.module\.(sa|sc|c)ss$/;
4+
5+
let extensionsPattern: RegExp = DEFAULT_EXTENSIONS_PATTERN;
6+
7+
export const isCSS = (fileName: string) => extensionsPattern.test(fileName);
58

69
export const isRelativeCSS = (fileName: string) =>
710
isCSS(fileName) && isRelative(fileName);
11+
12+
export const setExtensionsPattern = (newPattern?: RegExp) => {
13+
if (newPattern !== undefined) {
14+
extensionsPattern = newPattern;
15+
}
16+
};

src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
import * as path from 'path';
22
import * as ts_module from 'typescript/lib/tsserverlibrary';
3-
import { isCSS as _isCSS, isRelativeCSS } from './helpers/cssExtensions';
3+
import {
4+
isCSS,
5+
isRelativeCSS,
6+
setExtensionsPattern,
7+
} from './helpers/cssExtensions';
48
import { getDtsSnapshot } from './helpers/cssSnapshots';
59

610
function init({ typescript: ts }: { typescript: typeof ts_module }) {
7-
let isCSS = _isCSS;
811
function create(info: ts.server.PluginCreateInfo) {
912
// User options for plugin.
1013
const options: IOptions = info.config.options || {};
1114

1215
// Allow custom matchers to be used, handling bad matcher patterns;
16+
let extensionsPattern: RegExp | undefined;
1317
try {
1418
const { customMatcher } = options;
1519
if (customMatcher) {
16-
isCSS = (fileName) => new RegExp(customMatcher).test(fileName);
20+
extensionsPattern = new RegExp(customMatcher);
1721
}
1822
} catch (e) {
1923
// TODO: Provide error/warning to user.
2024
}
2125

26+
setExtensionsPattern(extensionsPattern);
27+
2228
// Creates new virtual source files for the CSS modules.
2329
const _createLanguageServiceSourceFile = ts.createLanguageServiceSourceFile;
2430
ts.createLanguageServiceSourceFile = (

0 commit comments

Comments
 (0)