File tree Expand file tree Collapse file tree 3 files changed +40
-6
lines changed Expand file tree Collapse file tree 3 files changed +40
-6
lines changed Original file line number Diff line number Diff line change 1
- import { isCSS , isRelativeCSS } from '../cssExtensions' ;
1
+ import {
2
+ isCSS ,
3
+ isRelativeCSS ,
4
+ setExtensionsPattern ,
5
+ DEFAULT_EXTENSIONS_PATTERN ,
6
+ } from '../cssExtensions' ;
2
7
3
8
describe ( 'utils / cssExtensions' , ( ) => {
4
9
describe ( 'isCSS' , ( ) => {
@@ -26,4 +31,18 @@ describe('utils / cssExtensions', () => {
26
31
expect ( isRelativeCSS ( 'myfile.module.css' ) ) . toBe ( false ) ;
27
32
} ) ;
28
33
} ) ;
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
+ } ) ;
29
48
} ) ;
Original file line number Diff line number Diff line change 1
1
const isRelative = ( fileName : string ) => / ^ \. \. ? ( $ | [ \\ / ] ) / . test ( fileName ) ;
2
2
3
- export const isCSS = ( fileName : string ) =>
4
- / \. m o d u l e \. ( s a | s c | c ) s s $ / . test ( fileName ) ;
3
+ export const DEFAULT_EXTENSIONS_PATTERN = / \. m o d u l e \. ( s a | s c | c ) s s $ / ;
4
+
5
+ let extensionsPattern : RegExp = DEFAULT_EXTENSIONS_PATTERN ;
6
+
7
+ export const isCSS = ( fileName : string ) => extensionsPattern . test ( fileName ) ;
5
8
6
9
export const isRelativeCSS = ( fileName : string ) =>
7
10
isCSS ( fileName ) && isRelative ( fileName ) ;
11
+
12
+ export const setExtensionsPattern = ( newPattern ?: RegExp ) => {
13
+ if ( newPattern !== undefined ) {
14
+ extensionsPattern = newPattern ;
15
+ }
16
+ } ;
Original file line number Diff line number Diff line change 1
1
import * as path from 'path' ;
2
2
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' ;
4
8
import { getDtsSnapshot } from './helpers/cssSnapshots' ;
5
9
6
10
function init ( { typescript : ts } : { typescript : typeof ts_module } ) {
7
- let isCSS = _isCSS ;
8
11
function create ( info : ts . server . PluginCreateInfo ) {
9
12
// User options for plugin.
10
13
const options : IOptions = info . config . options || { } ;
11
14
12
15
// Allow custom matchers to be used, handling bad matcher patterns;
16
+ let extensionsPattern : RegExp | undefined ;
13
17
try {
14
18
const { customMatcher } = options ;
15
19
if ( customMatcher ) {
16
- isCSS = ( fileName ) => new RegExp ( customMatcher ) . test ( fileName ) ;
20
+ extensionsPattern = new RegExp ( customMatcher ) ;
17
21
}
18
22
} catch ( e ) {
19
23
// TODO: Provide error/warning to user.
20
24
}
21
25
26
+ setExtensionsPattern ( extensionsPattern ) ;
27
+
22
28
// Creates new virtual source files for the CSS modules.
23
29
const _createLanguageServiceSourceFile = ts . createLanguageServiceSourceFile ;
24
30
ts . createLanguageServiceSourceFile = (
You can’t perform that action at this time.
0 commit comments