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