| 
 | 1 | +const postcss = require('postcss');  | 
 | 2 | +const valueParser = require('postcss-value-parser');  | 
 | 3 | +const icssUtils = require('icss-utils');  | 
 | 4 | +const Tokenizer = require('css-selector-tokenizer');  | 
 | 5 | +const loaderUtils = require('loader-utils');  | 
 | 6 | + | 
 | 7 | +module.exports = postcss.plugin(  | 
 | 8 | +  'postcss-parser',  | 
 | 9 | +  (options) =>  | 
 | 10 | +    function process(css) {  | 
 | 11 | +      const imports = {};  | 
 | 12 | +      let exports = {};  | 
 | 13 | +      const importItems = options.importItems || [];  | 
 | 14 | +      const urlItems = options.urlItems || [];  | 
 | 15 | + | 
 | 16 | +      function replaceImportsInString(str) {  | 
 | 17 | +        if (options.import) {  | 
 | 18 | +          const tokens = valueParser(str);  | 
 | 19 | + | 
 | 20 | +          tokens.walk((node) => {  | 
 | 21 | +            if (node.type !== 'word') {  | 
 | 22 | +              return;  | 
 | 23 | +            }  | 
 | 24 | + | 
 | 25 | +            const token = node.value;  | 
 | 26 | +            const importIndex = imports[`$${token}`];  | 
 | 27 | + | 
 | 28 | +            if (typeof importIndex === 'number') {  | 
 | 29 | +              // eslint-disable-next-line no-param-reassign  | 
 | 30 | +              node.value = `___CSS_LOADER_IMPORT___${importIndex}___`;  | 
 | 31 | +            }  | 
 | 32 | +          });  | 
 | 33 | + | 
 | 34 | +          return tokens.toString();  | 
 | 35 | +        }  | 
 | 36 | +        return str;  | 
 | 37 | +      }  | 
 | 38 | + | 
 | 39 | +      const icss = icssUtils.extractICSS(css);  | 
 | 40 | + | 
 | 41 | +      exports = icss.icssExports;  | 
 | 42 | + | 
 | 43 | +      Object.keys(icss.icssImports).forEach((key) => {  | 
 | 44 | +        const url = loaderUtils.parseString(key);  | 
 | 45 | + | 
 | 46 | +        Object.keys(icss.icssImports[key]).forEach((prop) => {  | 
 | 47 | +          imports[`$${prop}`] = importItems.length;  | 
 | 48 | +          importItems.push({  | 
 | 49 | +            url,  | 
 | 50 | +            export: icss.icssImports[key][prop],  | 
 | 51 | +          });  | 
 | 52 | +        });  | 
 | 53 | +      });  | 
 | 54 | + | 
 | 55 | +      Object.keys(exports).forEach((exportName) => {  | 
 | 56 | +        exports[exportName] = replaceImportsInString(exports[exportName]);  | 
 | 57 | +      });  | 
 | 58 | + | 
 | 59 | +      function processNode(item) {  | 
 | 60 | +        switch (item.type) {  | 
 | 61 | +          case 'value':  | 
 | 62 | +            item.nodes.forEach(processNode);  | 
 | 63 | +            break;  | 
 | 64 | +          case 'nested-item':  | 
 | 65 | +            item.nodes.forEach(processNode);  | 
 | 66 | +            break;  | 
 | 67 | +          case 'item': {  | 
 | 68 | +            const importIndex = imports[`$${item.name}`];  | 
 | 69 | +            if (typeof importIndex === 'number') {  | 
 | 70 | +              // eslint-disable-next-line no-param-reassign  | 
 | 71 | +              item.name = `___CSS_LOADER_IMPORT___${importIndex}___`;  | 
 | 72 | +            }  | 
 | 73 | +            break;  | 
 | 74 | +          }  | 
 | 75 | +          // no default  | 
 | 76 | +        }  | 
 | 77 | +      }  | 
 | 78 | + | 
 | 79 | +      css.walkDecls((decl) => {  | 
 | 80 | +        const values = Tokenizer.parseValues(decl.value);  | 
 | 81 | + | 
 | 82 | +        values.nodes.forEach((value) => {  | 
 | 83 | +          value.nodes.forEach(processNode);  | 
 | 84 | +        });  | 
 | 85 | + | 
 | 86 | +        // eslint-disable-next-line no-param-reassign  | 
 | 87 | +        decl.value = Tokenizer.stringifyValues(values);  | 
 | 88 | +      });  | 
 | 89 | + | 
 | 90 | +      css.walkAtRules((atrule) => {  | 
 | 91 | +        if (typeof atrule.params === 'string') {  | 
 | 92 | +          // eslint-disable-next-line no-param-reassign  | 
 | 93 | +          atrule.params = replaceImportsInString(atrule.params);  | 
 | 94 | +        }  | 
 | 95 | +      });  | 
 | 96 | + | 
 | 97 | +      /* eslint-disable no-param-reassign */  | 
 | 98 | +      options.importItems = importItems;  | 
 | 99 | +      options.urlItems = urlItems;  | 
 | 100 | +      options.exports = exports;  | 
 | 101 | +      /* eslint-enable no-param-reassign */  | 
 | 102 | +    }  | 
 | 103 | +);  | 
0 commit comments