|
|
|
"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 icssUtils = require("icss-utils");
|
|
|
|
const loaderUtils = require("loader-utils");
|
|
|
|
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';
|
|
|
|
|
|
|
|
module.exports = function(options = {}) {
|
|
|
|
validateOptions(arguments, {
|
|
|
|
keyReplacer: [ required, isFunction ],
|
|
|
|
autoExportImports: [ isBoolean ]
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
postcssPlugin: pluginName,
|
|
|
|
Once(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 },
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.autoExportImports !== false) {
|
|
|
|
result.messages.push({
|
|
|
|
pluginName: pluginName,
|
|
|
|
type: "icss-export",
|
|
|
|
item: {
|
|
|
|
name: localKey,
|
|
|
|
value: newKey
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.postcss = true;
|