@@ -4,11 +4,11 @@ const {
4
4
filenameToTypingsFilename,
5
5
getCssModuleKeys,
6
6
generateGenericExportInterface,
7
- } = require ( "./utils" ) ;
8
- const persist = require ( "./persist" ) ;
9
- const verify = require ( "./verify" ) ;
10
- const { getOptions } = require ( "loader-utils" ) ;
11
- const validateOptions = require ( "schema-utils" ) ;
7
+ } = require ( "./utils" )
8
+ const persist = require ( "./persist" )
9
+ const verify = require ( "./verify" )
10
+ const { getOptions } = require ( "loader-utils" )
11
+ const validateOptions = require ( "schema-utils" )
12
12
13
13
const schema = {
14
14
type : "object" ,
@@ -40,70 +40,75 @@ const schema = {
40
40
description :
41
41
"Path to prettier config file" ,
42
42
type : "string" ,
43
+ } ,
44
+ lazy : {
45
+ description : "Emit 'use()' & 'unuse()' for *.lazy.css. Defaults to `true`" ,
46
+ type : "boolean" ,
43
47
}
44
48
} ,
45
49
additionalProperties : false ,
46
- } ;
50
+ }
47
51
48
52
/** @type {any } */
49
53
const configuration = {
50
54
name : "typings-for-css-modules-loader" ,
51
55
baseDataPath : "options" ,
52
- } ;
56
+ }
53
57
54
58
/** @type {((this: import('webpack').loader.LoaderContext, ...args: any[]) => void) & {pitch?: import('webpack').loader.Loader['pitch']} } */
55
59
module . exports = function ( content , ...args ) {
56
- const options = getOptions ( this ) || { } ;
60
+ const options = getOptions ( this ) || { }
61
+ if ( typeof options . lazy === 'undefined' ) options . lazy = true
57
62
58
- validateOptions ( schema , options , configuration ) ;
63
+ validateOptions ( schema , options , configuration )
59
64
60
65
if ( this . cacheable ) {
61
- this . cacheable ( ) ;
66
+ this . cacheable ( )
62
67
}
63
68
64
69
// let's only check `exports.locals` for keys to avoid getting keys from the sourcemap when it's enabled
65
70
// if we cannot find locals, then the module only contains global styles
66
- const indexOfLocals = content . indexOf ( ".locals" ) ;
71
+ const indexOfLocals = content . indexOf ( ".locals" )
67
72
const cssModuleKeys =
68
73
indexOfLocals === - 1
69
74
? [ ]
70
- : getCssModuleKeys ( content . substring ( indexOfLocals ) ) ;
75
+ : getCssModuleKeys ( content . substring ( indexOfLocals ) )
71
76
72
77
/** @type {any } */
73
- const callback = this . async ( ) ;
78
+ const callback = this . async ( )
74
79
75
80
const successfulCallback = ( ) => {
76
- callback ( null , content , ...args ) ;
77
- } ;
81
+ callback ( null , content , ...args )
82
+ }
78
83
79
84
if ( cssModuleKeys . length === 0 ) {
80
85
// no css module output found
81
- successfulCallback ( ) ;
82
- return ;
86
+ successfulCallback ( )
87
+ return
83
88
}
84
89
85
- const filename = this . resourcePath ;
90
+ const filename = this . resourcePath
86
91
87
- const cssModuleInterfaceFilename = filenameToTypingsFilename ( filename ) ;
92
+ const cssModuleInterfaceFilename = filenameToTypingsFilename ( filename )
88
93
const cssModuleDefinition = generateGenericExportInterface (
89
94
cssModuleKeys ,
90
95
filenameToPascalCase ( filename ) ,
91
96
options . disableLocalsExport
92
- ) ;
97
+ )
93
98
94
99
applyFormattingAndOptions ( cssModuleDefinition , options )
95
100
. then ( ( output ) => {
96
101
if ( options . verifyOnly === true ) {
97
- return verify ( cssModuleInterfaceFilename , output ) ;
102
+ return verify ( cssModuleInterfaceFilename , output )
98
103
} else {
99
- persist ( cssModuleInterfaceFilename , output ) ;
104
+ persist ( cssModuleInterfaceFilename , output )
100
105
}
101
106
} )
102
107
. catch ( ( err ) => {
103
- this . emitError ( err ) ;
108
+ this . emitError ( err )
104
109
} )
105
- . then ( successfulCallback ) ;
106
- } ;
110
+ . then ( successfulCallback )
111
+ }
107
112
108
113
/**
109
114
* @param {string } cssModuleDefinition
@@ -112,23 +117,23 @@ module.exports = function (content, ...args) {
112
117
async function applyFormattingAndOptions ( cssModuleDefinition , options ) {
113
118
if ( options . banner ) {
114
119
// Prefix banner to CSS module
115
- cssModuleDefinition = options . banner + "\n" + cssModuleDefinition ;
120
+ cssModuleDefinition = options . banner + "\n" + cssModuleDefinition
116
121
}
117
122
118
123
if (
119
124
options . formatter === "prettier" ||
120
125
( ! options . formatter && canUsePrettier ( ) )
121
126
) {
122
- cssModuleDefinition = await applyPrettier ( cssModuleDefinition , options ) ;
127
+ cssModuleDefinition = await applyPrettier ( cssModuleDefinition , options )
123
128
} else {
124
129
// at very least let's ensure we're using OS eol if it's not provided
125
130
cssModuleDefinition = cssModuleDefinition . replace (
126
131
/ \r ? \n / g,
127
132
options . eol || require ( "os" ) . EOL
128
- ) ;
133
+ )
129
134
}
130
135
131
- return cssModuleDefinition ;
136
+ return cssModuleDefinition
132
137
}
133
138
134
139
/**
@@ -137,32 +142,32 @@ async function applyFormattingAndOptions(cssModuleDefinition, options) {
137
142
* @returns {Promise<string> }
138
143
*/
139
144
async function applyPrettier ( input , options ) {
140
- const prettier = require ( "prettier" ) ;
145
+ const prettier = require ( "prettier" )
141
146
142
- const configPath = options . prettierConfigFile ? options . prettierConfigFile : "./" ;
143
- const config = await prettier . resolveConfig ( configPath , {
147
+ const configPath = options . prettierConfigFile ? options . prettierConfigFile : "./"
148
+ const config = await prettier . resolveConfig ( configPath , {
144
149
editorconfig : true ,
145
- } ) ;
150
+ } )
146
151
147
152
return prettier . format (
148
153
input ,
149
154
Object . assign ( { } , config , { parser : "typescript" } )
150
- ) ;
155
+ )
151
156
}
152
157
153
- let isPrettierInstalled ;
158
+ let isPrettierInstalled
154
159
/**
155
160
* @returns {boolean }
156
161
*/
157
162
function canUsePrettier ( ) {
158
163
if ( typeof isPrettierInstalled !== "boolean" ) {
159
164
try {
160
- require . resolve ( "prettier" ) ;
161
- isPrettierInstalled = true ;
165
+ require . resolve ( "prettier" )
166
+ isPrettierInstalled = true
162
167
} catch ( _ ) {
163
- isPrettierInstalled = false ;
168
+ isPrettierInstalled = false
164
169
}
165
170
}
166
171
167
- return isPrettierInstalled ;
172
+ return isPrettierInstalled
168
173
}
0 commit comments