@@ -5,56 +5,59 @@ const loaderUtils = require('loader-utils');
55
66/** @type {import('webpack').loader.Loader } */
77module . exports = function ( content ) {
8- this . cacheable ( ) ;
8+ this . cacheable && this . cacheable ( ) ;
9+
10+ const options = loaderUtils . getOptions ( this ) || { } ;
911 const callback = this . async ( ) ;
10- const options = loaderUtils . getOptions ( this ) ;
11- genTypings ( this . resourcePath , content , options , callback ) ;
12- } ;
1312
14- /**
15- * @param {string } [path]
16- * @param {string | Buffer } [content]
17- * @param {import('loader-utils').OptionObject } [options]
18- * @param {import('webpack').loader.loaderCallback } [callback]
19- */
20- function genTypings ( path , content , options , callback ) {
21- if ( Buffer . isBuffer ( content ) ) {
22- content = content . toString ( 'utf8' ) ;
23- }
13+ let typings = '' ;
2414
25- /** @type {string[] } */
26- let classes = [ ] ;
27- {
28- /** @type {RegExpExecArray } */
29- let match ;
30- while ( match = classesRegex . exec ( content ) ) {
31- if ( classes . indexOf ( match [ 1 ] ) === - 1 ) {
32- classes . push ( match [ 1 ] ) ;
33- }
34- }
15+ if ( options . banner ) {
16+ typings = `${ options . banner } \n` ;
3517 }
3618
37- let typings = '' ;
3819 {
39- if ( options . banner ) {
40- typings = `${ options . banner } \n` ;
41- }
20+ let classes = getClasses ( content ) ;
21+
4222 if ( options . namedExport ) {
4323 for ( let c of classes ) {
4424 typings += `export const ${ c } : string;\n` ;
4525 }
4626 } else {
47- const name = getInterfaceName ( path ) ;
48- typings += `export interface ${ name } {\n` ;
27+ const i = getInterfaceName ( this . resourcePath ) ;
28+ typings += `export interface ${ i } {\n` ;
4929 for ( let c of classes ) {
5030 typings += ` '${ c } ': string\n` ;
5131 }
52- typings += `}\ndeclare const styles: ${ name } ;\nexport default styles;\n` ;
32+ typings += `}\ndeclare const styles: ${ i } ;\nexport default styles;\n` ;
5333 }
5434 }
55- writeFile ( getDtsPath ( path ) , typings ) ;
35+
36+ fs . writeFileSync ( getDtsPath ( this . resourcePath ) , typings ) ;
5637
5738 callback ( null , content ) ;
39+ } ;
40+
41+ /**
42+ * @param {string | Buffer } [content]
43+ */
44+ function getClasses ( content ) {
45+ if ( Buffer . isBuffer ( content ) ) {
46+ content = content . toString ( 'utf8' ) ;
47+ }
48+
49+ /** @type {string[] } */
50+ let classes = [ ] ;
51+
52+ /** @type {RegExpExecArray } */
53+ let match ;
54+ while ( match = classesRegex . exec ( content ) ) {
55+ if ( classes . indexOf ( match [ 1 ] ) === - 1 ) {
56+ classes . push ( match [ 1 ] ) ;
57+ }
58+ }
59+
60+ return classes ;
5861}
5962
6063const classesRegex = / " ( [ ^ " \\ ] + ) " : / g;
@@ -74,13 +77,3 @@ function getInterfaceName(path) {
7477 . replace ( / ^ ( \w ) / , ( _ , c ) => 'I' + c . toUpperCase ( ) )
7578 . replace ( / \W + ( \w ) / g, ( _ , c ) => c . toUpperCase ( ) ) ;
7679}
77-
78- /**
79- * @param {string } [path]
80- * @param {string } [content]
81- */
82- function writeFile ( path , content ) {
83- if ( ! fs . existsSync ( path ) || fs . readFileSync ( path ) . toString ( 'utf8' ) !== content ) {
84- fs . writeFileSync ( path , content ) ;
85- }
86- }
0 commit comments