From 3fefcb7cf6904cd229da6671292f1974bc1a4123 Mon Sep 17 00:00:00 2001 From: f0x Date: Sun, 20 Dec 2020 21:13:17 +0100 Subject: [PATCH 1/2] Update validatem to @validatem/core validateOptions now requires an arguments object. --- index.js | 9 ++++++++- package.json | 11 +++++++++-- src/postcss/postcss-icss-parser.js | 9 ++++++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index bcaca91..b35a59f 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,13 @@ "use strict"; -const { validateArguments, required, oneOf, arrayOf, isString, isBoolean, allowExtraProperties, ValidationError } = require("validatem"); +const { validateArguments } = require("@validatem/core"); +const ValidationError = require("@validatem/error"); +const isBoolean = require("@validatem/is-boolean"); +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"); function isPostcssPlugin(value) { if (value.postcssPlugin == null || value.postcssVersion == null || typeof value !== "function") { diff --git a/package.json b/package.json index 9d8dcc1..1cdbe96 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,14 @@ "url": "https://git.cryto.net/joepie91/icssify/issues" }, "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-string": "^1.0.0", + "@validatem/one-of": "^0.1.1", + "@validatem/required": "^0.1.1", "assure-array": "^1.0.0", "bl": "^4.0.0", "bluebird": "^3.7.1", @@ -36,8 +44,7 @@ "postcss-modules-local-by-default": "^3.0.2", "postcss-modules-scope": "^2.1.0", "postcss-modules-values": "^3.0.0", - "through2": "^2.0.3", - "validatem": "^0.2.0" + "through2": "^2.0.3" }, "devDependencies": { "@joepie91/eslint-config": "^1.1.0", diff --git a/src/postcss/postcss-icss-parser.js b/src/postcss/postcss-icss-parser.js index 5aad5ec..3f33394 100644 --- a/src/postcss/postcss-icss-parser.js +++ b/src/postcss/postcss-icss-parser.js @@ -14,12 +14,15 @@ Licensed under: const postcss = require("postcss"); const icssUtils = require("icss-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'; -module.exports = postcss.plugin(pluginName, (options = {}) => { - validateOptions([options], { +module.exports = postcss.plugin(pluginName, function(options = {}) { + validateOptions(arguments, { keyReplacer: [ required, isFunction ], autoExportImports: [ isBoolean ] }); -- 2.40.1 From 83cad5307d256b9beb001e10f5ace2081ee285e1 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Thu, 3 Feb 2022 00:33:08 +0100 Subject: [PATCH 2/2] Update to PostCSS 8 --- index.js | 7 +- package.json | 16 +++-- src/postcss/postcss-icss-find-imports.js | 42 ++++++------ src/postcss/postcss-icss-parser.js | 86 +++++++++++++----------- 4 files changed, 79 insertions(+), 72 deletions(-) diff --git a/index.js b/index.js index b35a59f..6f94cf3 100644 --- a/index.js +++ b/index.js @@ -3,18 +3,13 @@ const { validateArguments } = require("@validatem/core"); const ValidationError = require("@validatem/error"); const isBoolean = require("@validatem/is-boolean"); +const isPostcssPlugin = require("@validatem/is-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"); -function isPostcssPlugin(value) { - if (value.postcssPlugin == null || value.postcssVersion == null || typeof value !== "function") { - throw new ValidationError(`Must be a PostCSS plugin`); - } -} - // 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 module.exports = function (browserify, options) { diff --git a/package.json b/package.json index 1cdbe96..5c9d8ca 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@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", @@ -39,15 +40,18 @@ "insert-css": "^2.0.0", "map-obj": "^4.1.0", "object.fromentries": "^2.0.1", - "postcss": "^6.0.9", - "postcss-modules-extract-imports": "^2.0.0", - "postcss-modules-local-by-default": "^3.0.2", - "postcss-modules-scope": "^2.1.0", - "postcss-modules-values": "^3.0.0", + "postcss-modules-extract-imports": "^3.0.0", + "postcss-modules-local-by-default": "^4.0.0", + "postcss-modules-scope": "^3.0.0", + "postcss-modules-values": "^4.0.0", "through2": "^2.0.3" }, "devDependencies": { "@joepie91/eslint-config": "^1.1.0", - "eslint": "^6.8.0" + "eslint": "^6.8.0", + "postcss": "^8.0.0" + }, + "peeerDependencies": { + "postcss": "^8.0.0" } } diff --git a/src/postcss/postcss-icss-find-imports.js b/src/postcss/postcss-icss-find-imports.js index 0cb85c2..6e9ce95 100644 --- a/src/postcss/postcss-icss-find-imports.js +++ b/src/postcss/postcss-icss-find-imports.js @@ -11,28 +11,32 @@ Licensed under: MIT (https://opensource.org/licenses/MIT) */ -const postcss = require("postcss"); const icssUtils = require("icss-utils"); const loaderUtils = require("loader-utils"); const pluginName = 'postcss-icss-find-imports'; -module.exports = postcss.plugin(pluginName, (_options = {}) => { - return function process(css, result) { - let discoveredImports = new Set(); - - let { icssImports } = icssUtils.extractICSS(css, false); - - for (let importUrl of Object.keys(icssImports)) { - discoveredImports.add(loaderUtils.parseString(importUrl)); - } - - for (let url of discoveredImports) { - result.messages.push({ - pluginName: pluginName, - type: "import", - url: url - }); - } +module.exports = (_options = {}) => { + return { + postcssPlugin: pluginName, + Once(css, { result }) { + let discoveredImports = new Set(); + + let { icssImports } = icssUtils.extractICSS(css, false); + + for (let importUrl of Object.keys(icssImports)) { + discoveredImports.add(loaderUtils.parseString(importUrl)); + } + + for (let url of discoveredImports) { + result.messages.push({ + pluginName: pluginName, + type: "import", + url: url + }); + } + }, }; -}); +}; + +module.exports.postcss = true; diff --git a/src/postcss/postcss-icss-parser.js b/src/postcss/postcss-icss-parser.js index 3f33394..6edf25b 100644 --- a/src/postcss/postcss-icss-parser.js +++ b/src/postcss/postcss-icss-parser.js @@ -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, function(options = {}) { +module.exports = function(options = {}) { validateOptions(arguments, { 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; -- 2.40.1