@@ -190,20 +190,31 @@ function normalizeSourceMap(map) {
190190 return newMap ;
191191}
192192
193- function getApiCode ( loaderContext , sourceMap ) {
194- const url = stringifyRequest ( loaderContext , require . resolve ( './runtime/api' ) ) ;
195-
196- return `exports = module.exports = require( ${ url } )( ${ sourceMap } );\n` ;
197- }
198-
199- function getImportCode ( loaderContext , imports , options ) {
193+ function getImportCode (
194+ loaderContext ,
195+ imports ,
196+ exportType ,
197+ sourceMap ,
198+ importLoaders
199+ ) {
200200 const importItems = [ ] ;
201201 const codeItems = [ ] ;
202202 const urlImportNames = new Map ( ) ;
203203
204204 let hasUrlHelperCode = false ;
205205 let importPrefix ;
206206
207+ if ( exportType === 'full' ) {
208+ const url = stringifyRequest (
209+ loaderContext ,
210+ require . resolve ( './runtime/api' )
211+ ) ;
212+ importItems . push ( `var ___CSS_LOADER_API_IMPORT___ = require(${ url } );` ) ;
213+ codeItems . push (
214+ `exports = module.exports = ___CSS_LOADER_API_IMPORT___(${ sourceMap } );`
215+ ) ;
216+ }
217+
207218 imports . forEach ( ( item ) => {
208219 if ( item . type === '@import' || item . type === 'icss-import' ) {
209220 const media = item . media ? `, ${ JSON . stringify ( item . media ) } ` : '' ;
@@ -216,7 +227,7 @@ function getImportCode(loaderContext, imports, options) {
216227 }
217228
218229 if ( ! importPrefix ) {
219- importPrefix = getImportPrefix ( loaderContext , options . importLoaders ) ;
230+ importPrefix = getImportPrefix ( loaderContext , importLoaders ) ;
220231 }
221232
222233 const url = stringifyRequest (
@@ -226,7 +237,7 @@ function getImportCode(loaderContext, imports, options) {
226237
227238 importItems . push ( `var ${ item . name } = require(${ url } );` ) ;
228239
229- if ( options . exportType === 'full' ) {
240+ if ( exportType === 'full' ) {
230241 codeItems . push ( `exports.i(${ item . name } ${ media } );` ) ;
231242 }
232243 }
@@ -267,12 +278,25 @@ function getImportCode(loaderContext, imports, options) {
267278 }
268279 } ) ;
269280
270- return `// Imports\n${ importItems . join ( '\n' ) } \n${ codeItems . join ( '\n' ) } \n` ;
281+ const items = importItems . concat ( codeItems ) ;
282+
283+ return items . length > 0 ? `// Imports\n${ items . join ( '\n' ) } \n` : '' ;
271284}
272285
273- function getModuleCode ( loaderContext , result , replacers , sourceMap ) {
286+ function getModuleCode (
287+ loaderContext ,
288+ result ,
289+ exportType ,
290+ sourceMap ,
291+ replacers
292+ ) {
293+ if ( exportType !== 'full' ) {
294+ return '' ;
295+ }
296+
274297 const { css, map } = result ;
275298 const sourceMapValue = sourceMap && map ? `,${ map } ` : '' ;
299+
276300 let cssCode = JSON . stringify ( css ) ;
277301
278302 replacers . forEach ( ( replacer ) => {
@@ -301,7 +325,17 @@ function dashesCamelCase(str) {
301325 ) ;
302326}
303327
304- function getExportCode ( loaderContext , exports , replacers , options ) {
328+ function getExportCode (
329+ loaderContext ,
330+ exports ,
331+ exportType ,
332+ replacers ,
333+ localsConvention
334+ ) {
335+ if ( exports . length === 0 ) {
336+ return '' ;
337+ }
338+
305339 const items = [ ] ;
306340
307341 function addExportedItem ( name , value ) {
@@ -311,7 +345,7 @@ function getExportCode(loaderContext, exports, replacers, options) {
311345 exports . forEach ( ( item ) => {
312346 const { name, value } = item ;
313347
314- switch ( options . localsConvention ) {
348+ switch ( localsConvention ) {
315349 case 'camelCase' : {
316350 addExportedItem ( name , value ) ;
317351
@@ -348,7 +382,7 @@ function getExportCode(loaderContext, exports, replacers, options) {
348382 } ) ;
349383
350384 let exportCode = `// Exports\n${
351- options . exportType === 'locals' ? 'module.exports' : 'exports.locals'
385+ exportType === 'locals' ? 'module.exports' : 'exports.locals'
352386 } = {\n${ items . join ( ',\n' ) } \n};`;
353387
354388 replacers . forEach ( ( replacer ) => {
@@ -357,7 +391,7 @@ function getExportCode(loaderContext, exports, replacers, options) {
357391 const localName = JSON . stringify ( replacer . localName ) ;
358392
359393 exportCode = exportCode . replace ( new RegExp ( name , 'g' ) , ( ) =>
360- options . exportType === 'locals'
394+ exportType === 'locals'
361395 ? `" + ${ importName } [${ localName } ] + "`
362396 : `" + ${ importName } .locals[${ localName } ] + "`
363397 ) ;
@@ -371,7 +405,6 @@ export {
371405 getFilter ,
372406 getModulesPlugins ,
373407 normalizeSourceMap ,
374- getApiCode ,
375408 getImportCode ,
376409 getModuleCode ,
377410 getExportCode ,
0 commit comments