Merge pull request 'Update to PostCSS 8' (#2) from jtojnar/icssify:postcss-8 into master

Reviewed-on: #2
master
Sven Slootweg 2 years ago
commit 49766a0ab6

@ -1,12 +1,14 @@
"use strict"; "use strict";
const { validateArguments, required, oneOf, arrayOf, isString, isBoolean, allowExtraProperties, ValidationError } = require("validatem"); const { validateArguments } = require("@validatem/core");
const ValidationError = require("@validatem/error");
function isPostcssPlugin(value) { const isBoolean = require("@validatem/is-boolean");
if (value.postcssPlugin == null || value.postcssVersion == null || typeof value !== "function") { const isPostcssPlugin = require("@validatem/is-postcss-plugin");
throw new ValidationError(`Must be a PostCSS plugin`); const isString = require("@validatem/is-string");
} const required = require("@validatem/required");
} const arrayOf = require("@validatem/array-of");
const oneOf = require("@validatem/one-of");
const allowExtraProperties = require("@validatem/allow-extra-properties");
// FIXME: When there's >1 icssify instance set up on the Browserify instance (can this happen when a module specifies its own dependency on icssify?), either throw an error (if incompatible?) or merge the two somehow, so that there's only one resulting .css file // FIXME: When there's >1 icssify instance set up on the Browserify instance (can this happen when a module specifies its own dependency on icssify?), either throw an error (if incompatible?) or merge the two somehow, so that there's only one resulting .css file

@ -22,6 +22,15 @@
"url": "https://git.cryto.net/joepie91/icssify/issues" "url": "https://git.cryto.net/joepie91/icssify/issues"
}, },
"dependencies": { "dependencies": {
"@validatem/allow-extra-properties": "^0.1.0",
"@validatem/array-of": "^0.1.2",
"@validatem/core": "^0.3.15",
"@validatem/error": "^1.1.0",
"@validatem/is-boolean": "^0.1.1",
"@validatem/is-postcss-plugin": "^0.2.0",
"@validatem/is-string": "^1.0.0",
"@validatem/one-of": "^0.1.1",
"@validatem/required": "^0.1.1",
"assure-array": "^1.0.0", "assure-array": "^1.0.0",
"bl": "^4.0.0", "bl": "^4.0.0",
"bluebird": "^3.7.1", "bluebird": "^3.7.1",
@ -31,16 +40,18 @@
"insert-css": "^2.0.0", "insert-css": "^2.0.0",
"map-obj": "^4.1.0", "map-obj": "^4.1.0",
"object.fromentries": "^2.0.1", "object.fromentries": "^2.0.1",
"postcss": "^6.0.9", "postcss-modules-extract-imports": "^3.0.0",
"postcss-modules-extract-imports": "^2.0.0", "postcss-modules-local-by-default": "^4.0.0",
"postcss-modules-local-by-default": "^3.0.2", "postcss-modules-scope": "^3.0.0",
"postcss-modules-scope": "^2.1.0", "postcss-modules-values": "^4.0.0",
"postcss-modules-values": "^3.0.0", "through2": "^2.0.3"
"through2": "^2.0.3",
"validatem": "^0.2.0"
}, },
"devDependencies": { "devDependencies": {
"@joepie91/eslint-config": "^1.1.0", "@joepie91/eslint-config": "^1.1.0",
"eslint": "^6.8.0" "eslint": "^6.8.0",
"postcss": "^8.0.0"
},
"peeerDependencies": {
"postcss": "^8.0.0"
} }
} }

@ -11,28 +11,32 @@ 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 pluginName = 'postcss-icss-find-imports'; const pluginName = 'postcss-icss-find-imports';
module.exports = postcss.plugin(pluginName, (_options = {}) => { module.exports = (_options = {}) => {
return function process(css, result) { return {
let discoveredImports = new Set(); postcssPlugin: pluginName,
Once(css, { result }) {
let { icssImports } = icssUtils.extractICSS(css, false); let discoveredImports = new Set();
for (let importUrl of Object.keys(icssImports)) { let { icssImports } = icssUtils.extractICSS(css, false);
discoveredImports.add(loaderUtils.parseString(importUrl));
} for (let importUrl of Object.keys(icssImports)) {
discoveredImports.add(loaderUtils.parseString(importUrl));
for (let url of discoveredImports) { }
result.messages.push({
pluginName: pluginName, for (let url of discoveredImports) {
type: "import", result.messages.push({
url: url pluginName: pluginName,
}); type: "import",
} url: url
});
}
},
}; };
}); };
module.exports.postcss = true;

@ -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)) {
index += 1;
let newKey = options.keyReplacer({ localKey, remoteKey, index, url }); for (const [ localKey, remoteKey ] of Object.entries(imports)) {
importReplacements[localKey] = newKey; 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({ 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;

Loading…
Cancel
Save