You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB
JavaScript

"use strict";
/*
This module is derived from: css-loader (https://github.com/webpack-contrib/css-loader)
(c) JS Foundation and other contributors
Modifications by:
(c) 2019, Sven Slootweg <admin@cryto.net>
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");
const required = require("@validatem/required");
const isFunction = require("@validatem/is-function");
const pluginName = 'postcss-icss-parser';
module.exports = postcss.plugin(pluginName, (options = {}) => {
validateOptions([options], {
keyReplacer: [ required, isFunction ]
});
return function process(css, result) {
const importReplacements = Object.create(null);
const { icssImports, icssExports } = icssUtils.extractICSS(css);
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;
let newKey = options.keyReplacer({ localKey, remoteKey, index, url });
importReplacements[localKey] = newKey;
result.messages.push({
pluginName,
type: 'icss-import',
item: { url, localKey, remoteKey, newKey, index },
});
}
}
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
);
result.messages.push({
pluginName: pluginName,
type: "icss-export",
item: { name, value: parsedValue }
});
}
};
});