@ -11,67 +11,74 @@ Licensed under:
MIT ( https : //opensource.org/licenses/MIT)
MIT ( https : //opensource.org/licenses/MIT)
* /
* /
const postcss = require ( "postcss" ) ;
const icssUtils = require ( "icss-utils" ) ;
const icssUtils = require ( "icss-utils" ) ;
const loaderUtils = require ( "loader-utils" ) ;
const loaderUtils = require ( "loader-utils" ) ;
const { validateOptions , required , isFunction , isBoolean } = require ( "validatem" ) ;
const { validateOptions } = require ( "@validatem/core" ) ;
const required = require ( "@validatem/required" ) ;
const isBoolean = require ( "@validatem/is-boolean" ) ;
const isFunction = require ( "@validatem/is-function" ) ;
const pluginName = 'postcss-icss-parser' ;
const pluginName = 'postcss-icss-parser' ;
module . exports = postcss . plugin ( pluginName , ( options = { } ) => {
module . exports = function ( options = { } ) {
validateOptions ( [ options ] , {
validateOptions ( arguments , {
keyReplacer : [ required , isFunction ] ,
keyReplacer : [ required , isFunction ] ,
autoExportImports : [ isBoolean ]
autoExportImports : [ isBoolean ]
} ) ;
} ) ;
return function process ( css , result ) {
return {
const importReplacements = Object . create ( null ) ;
postcssPlugin : pluginName ,
const { icssImports , icssExports } = icssUtils . extractICSS ( css ) ;
Once ( css , { result } ) {
const importReplacements = Object . create ( null ) ;
const { icssImports , icssExports } = icssUtils . extractICSS ( css ) ;
let index = 0 ;
let index = 0 ;
for ( const [ importUrl , imports ] of Object . entries ( icssImports ) ) {
for ( const [ importUrl , imports ] of Object . entries ( icssImports ) ) {
const url = loaderUtils . parseString ( importUrl ) ;
const url = loaderUtils . parseString ( importUrl ) ;
for ( const [ localKey , remoteKey ] of Object . entries ( imports ) ) {
for ( const [ localKey , remoteKey ] of Object . entries ( imports ) ) {
index += 1 ;
index += 1 ;
let newKey = options . keyReplacer ( { localKey , remoteKey , index , url } ) ;
let newKey = options . keyReplacer ( { localKey , remoteKey , index , url } ) ;
importReplacements [ localKey ] = newKey ;
importReplacements [ localKey ] = newKey ;
result . messages . push ( {
pluginName ,
type : 'icss-import' ,
item : { url , localKey , remoteKey , newKey , index } ,
} ) ;
if ( options . autoExportImports !== false ) {
result . messages . push ( {
result . messages . push ( {
pluginName : pluginName ,
pluginName ,
type : "icss-export" ,
type : 'icss-import' ,
item : {
item : { url , localKey , remoteKey , newKey , index } ,
name : localKey ,
value : newKey
}
} ) ;
} ) ;
if ( options . autoExportImports !== false ) {
result . messages . push ( {
pluginName : pluginName ,
type : "icss-export" ,
item : {
name : localKey ,
value : newKey
}
} ) ;
}
}
}
}
}
}
icssUtils . replaceSymbols ( css , importReplacements ) ;
icssUtils . replaceSymbols ( css , importReplacements ) ;
for ( const [ name , value ] of Object . entries ( icssExports ) ) {
for ( const [ name , value ] of Object . entries ( icssExports ) ) {
/* This is to handle re-exports of imported items */
/* This is to handle re-exports of imported items */
const parsedValue = icssUtils . replaceValueSymbols (
const parsedValue = icssUtils . replaceValueSymbols (
value ,
value ,
importReplacements
importReplacements
) ;
) ;
result . messages . push ( {
result . messages . push ( {
pluginName : pluginName ,
pluginName : pluginName ,
type : "icss-export" ,
type : "icss-export" ,
item : { name , value : parsedValue }
item : { name , value : parsedValue }
} ) ;
} ) ;
}
}
} ,
} ;
} ;
} ) ;
} ;
module . exports . postcss = true ;