Skip to content

Commit 791a38f

Browse files
committed
Merge branch 'release/1.0.4'
2 parents 7077e08 + 78e7298 commit 791a38f

File tree

6 files changed

+64
-18
lines changed

6 files changed

+64
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-plugin-css-modules",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"main": "lib/index.js",
55
"author": "Brody McKee <[email protected]>",
66
"license": "MIT",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createMatchers } from '../createMatchers';
2+
3+
describe('utils / createMatchers', () => {
4+
it('should match `customMatcher` regexp', () => {
5+
const options: IOptions = { customMatcher: '\\.css$' };
6+
const { isCSS, isRelativeCSS } = createMatchers(options);
7+
8+
expect(isCSS('./myfile.css')).toBe(true);
9+
expect(isCSS('./myfile.m.css')).toBe(true);
10+
expect(isCSS('./myfile.scss')).toBe(false);
11+
expect(isRelativeCSS('../folder/myfile.css')).toBe(true);
12+
expect(isRelativeCSS('../folder/myfile.m.css')).toBe(true);
13+
expect(isRelativeCSS('../folders/myfile.scss')).toBe(false);
14+
});
15+
16+
it('should handle bad `customMatcher` regexp', () => {
17+
const options: IOptions = { customMatcher: '$([a' };
18+
const { isCSS, isRelativeCSS } = createMatchers(options);
19+
20+
expect(isCSS('./myfile.module.css')).toBe(true);
21+
expect(isRelativeCSS('../folders/myfile.module.scss')).toBe(true);
22+
});
23+
});

src/helpers/__tests__/cssExtensions.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { isCSS, isRelativeCSS } from '../cssExtensions';
1+
import { createIsCSS, createIsRelativeCSS } from '../cssExtensions';
22

33
describe('utils / cssExtensions', () => {
44
describe('isCSS', () => {
5+
const isCSS = createIsCSS();
6+
57
it('should match CSS module extensions', () => {
68
expect(isCSS('./myfile.module.scss')).toBe(true);
79
expect(isCSS('./myfile.module.sass')).toBe(true);
@@ -17,6 +19,9 @@ describe('utils / cssExtensions', () => {
1719
});
1820

1921
describe('isRelativeCSS', () => {
22+
const isCSS = createIsCSS();
23+
const isRelativeCSS = createIsRelativeCSS(isCSS);
24+
2025
it('should match relative CSS modules', () => {
2126
expect(isRelativeCSS('./myfile.module.css')).toBe(true);
2227
expect(isRelativeCSS('../folder/myfile.module.css')).toBe(true);

src/helpers/createMatchers.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createIsCSS, createIsRelativeCSS } from './cssExtensions';
2+
3+
export const createMatchers = (options: IOptions = {}) => {
4+
// Allow custom matchers to be used, and handle bad matcher patterns.
5+
let isCSS = createIsCSS();
6+
try {
7+
const { customMatcher } = options;
8+
if (customMatcher) {
9+
const customMatcherRegExp = new RegExp(customMatcher);
10+
isCSS = createIsCSS(customMatcherRegExp);
11+
}
12+
} catch (e) {
13+
// TODO: Provide error/warning to user.
14+
}
15+
16+
// Create the relative CSS checker.
17+
const isRelativeCSS = createIsRelativeCSS(isCSS);
18+
19+
return { isCSS, isRelativeCSS };
20+
};

src/helpers/cssExtensions.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
const isRelative = (fileName: string) => /^\.\.?($|[\\/])/.test(fileName);
1+
export type isCSSFn = (fileName: string) => boolean;
2+
const DEFAULT_REGEXP = /\.module\.(sa|sc|c)ss$/;
23

3-
export const isCSS = (fileName: string) =>
4-
/\.module\.(sa|sc|c)ss$/.test(fileName);
4+
const isRelative = (fileName: string) => /^\.\.?($|[\\/])/.test(fileName);
55

6-
export const isRelativeCSS = (fileName: string) =>
6+
export const createIsCSS = (
7+
customMatcher: RegExp = DEFAULT_REGEXP,
8+
): isCSSFn => (fileName: string) => customMatcher.test(fileName);
9+
export const createIsRelativeCSS = (isCSS: isCSSFn) => (fileName: string) =>
710
isCSS(fileName) && isRelative(fileName);

src/index.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
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 { createMatchers } from './helpers/createMatchers';
4+
import { isCSSFn } from './helpers/cssExtensions';
45
import { getDtsSnapshot } from './helpers/cssSnapshots';
56

67
function init({ typescript: ts }: { typescript: typeof ts_module }) {
7-
let isCSS = _isCSS;
8+
let _isCSS: isCSSFn;
89
function create(info: ts.server.PluginCreateInfo) {
910
// User options for plugin.
1011
const options: IOptions = info.config.options || {};
1112

12-
// Allow custom matchers to be used, handling bad matcher patterns;
13-
try {
14-
const { customMatcher } = options;
15-
if (customMatcher) {
16-
isCSS = (fileName) => new RegExp(customMatcher).test(fileName);
17-
}
18-
} catch (e) {
19-
// TODO: Provide error/warning to user.
20-
}
13+
// Create matchers using options object.
14+
const { isCSS, isRelativeCSS } = createMatchers(options);
15+
_isCSS = isCSS;
2116

2217
// Creates new virtual source files for the CSS modules.
2318
const _createLanguageServiceSourceFile = ts.createLanguageServiceSourceFile;
@@ -97,7 +92,7 @@ function init({ typescript: ts }: { typescript: typeof ts_module }) {
9792
}
9893

9994
function getExternalFiles(project: ts_module.server.ConfiguredProject) {
100-
return project.getFileNames().filter(isCSS);
95+
return project.getFileNames().filter(_isCSS);
10196
}
10297

10398
return { create, getExternalFiles };

0 commit comments

Comments
 (0)