Compare commits

..

2 Commits

@ -19,6 +19,7 @@ This plugin changes quite a few things in the Browserify pipeline to make CSS wo
In particular, the following things should be kept in mind:
- This plugin changes Browserify's sorting algorithm, so that files are always processed in 'dependency order'. While this *shouldn't* be an issue because the new algorithm is deterministic just like the old one, it's possible for a different plugin to break this one, if it changes the sorting algorithm. To prevent this, always load `icssify` __last__ (but still before `watchify` and `css-extract`, if you're using those).
- The sorting algorithm cannot currently deal with circular dependencies. __Trying to use this plugin will break, when you have circular dependencies.__ You really shouldn't have those, but this is still worth keeping in mind. PRs to fix this are welcome.
- CSS files are not JS files. This plugin sneaks the CSS past Browserify's syntax checker, but it will still send CSS files through the pipeline. Plugins that operate on JS *must* ignore non-JS files, otherwise they will break.
This plugin will always bundle all CSS __into a single file__. For complexity reasons, there is currently no support for splitting up CSS into multiple bundles. PRs that add support for this (without breaking ICSS or `css-extract` support!) are welcome.
@ -71,20 +72,11 @@ Plugin options (all optional):
* __extensions:__ An array of extensions (without the leading dot!) that should be considered "CSS files"; this is useful when you eg. name your files `.postcss` to indicate that you are using non-standard syntax. This list of extensions will *replace* the default list of extensions, so you will need to explicitly specify `"css"` in the list, if you want to keep parsing `.css` files. Defaults to `[ "css" ]`.
* __mode:__ Whether to assume that untagged class names in your CSS (ie. those without a `:local` or `:global` tag) are local or global. Defaults to `"local"`, but you can set this to `"global"` if you want to make the class name mangling *opt-in*. You'll generally want to leave this at the default setting.
* __autoExportImports:__ Whether to automatically re-export all imports in a CSS file. When disabled, only *explicitly-defined* class names are exported from a CSS file. Defaults to `true`, ie. all imports are automatically re-exported under their local name.
* __before:__ PostCSS transforms to run *before* the ICSS transforms, ie. before imports/exports are analyzed. This is usually where you want custom PostCSS plugins to go.
* __after:__ PostCSS transforms to run *after* the ICSS transforms, ie. after mangling the class names, but before bundling it all together into a single file. You'll rarely need to use this.
* __ignoreCycles:__ When enabling this, `icssify` will handle cyclical dependencies by randomly (but deterministically) ignoring a dependency relation during sorting. This *should* be safe to do for JS files, but there's no guarantee - and if things break in strange ways when you enable this, that is probably why.
__Cyclical dependencies in CSS files are *still* not allowed__, even when enabling this setting! It's just intended to deal with cyclical JS dependencies during sorting, which are a bad practice but technically valid to have. It's opt-in to ensure that you understand the risks of enabling it.
## Changelog
### v2.0.0 (April 25, 2022)
- __Breaking:__ The CSS transform is now global by default, to better handle cases where CSS in a third-party module needs to be included in the bundle. This *should* not cause any issues, but it's possible that this might break some existing bundling setups unexpectedly, so it's technically a breaking change. Please file an issue if this change causes problems for you!
- __Feature:__ Added more sensible handling of cyclical dependencies. It will now throw a clear error instead of silently dropping modules on the floor, and gives you the `ignoreCycles` option to continue bundling anyway.
- __Feature:__ Automatically re-export imported classes. This makes it possible to transparently move a certain class definition to another file, without breaking existing imports. This can be disabled by changing the `autoExportImports` option.
- __Feature:__ icssify now uses PostCSS 8+, and therefore supports plugins in the new format.
## Changelog
### v1.2.1 (March 6, 2020)

@ -1,39 +1,34 @@
"use strict";
const { validateArguments } = require("@validatem/core");
const ValidationError = require("@validatem/error");
const isBoolean = require("@validatem/is-boolean");
const isPostcssPlugin = require("@validatem/is-postcss-plugin");
const {validateArguments} = require("@validatem/core");
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");
const isPostcssPlugin = require("@validatem/is-postcss-plugin");
const createKahnSortingStream = require("./src/phase-streams/sort-kahn");
// 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) {
validateArguments(arguments, [
[ "browserify", required ],
[ "browserify" ],
[ "options", allowExtraProperties({
mode: oneOf([ "local", "global" ]),
before: arrayOf([ required, isPostcssPlugin ]),
after: arrayOf([ required, isPostcssPlugin ]),
extensions: arrayOf([ required, isString ]),
ignoreCycles: isBoolean
extensions: arrayOf([ required, isString ])
})]
]);
let state = {
extensions: options.extensions,
ignoreCycles: options.ignoreCycles
};
let state = { extensions: options.extensions };
const createTransform = require("./src/transform")(state);
const createCssDepsStream = require("./src/phase-streams/deps-css")(state);
const createSyntaxHideStream = require("./src/phase-streams/syntax-hide")(state);
const createSyntaxUnhideStream = require("./src/phase-streams/syntax-unhide")(state);
const createKahnSortingStream = require("./src/phase-streams/sort-kahn")(state);
const createDedupeBundleCssStream = require("./src/phase-streams/dedupe-bundle-css")(state);
function setupPipeline() {
@ -61,9 +56,7 @@ module.exports = function (browserify, options) {
dedupePhase.push(createDedupeBundleCssStream(options));
}
// NOTE: This is global because otherwise the transform will not run when processing node_modules. If this causes problems for you, please file an issue!
// FIXME: Figure out if there's a better solution for this
browserify.transform(createTransform, { ... options, global: true });
browserify.transform(createTransform, options);
browserify.on("reset", () => {
setupPipeline();

@ -1,3 +0,0 @@
# FIXME
- Check that `autoExportImports` works together correctly with `mode`

@ -1,6 +1,6 @@
{
"name": "icssify",
"version": "2.0.0",
"version": "1.2.1",
"description": "A Browserify plugin for handling CSS (through PostCSS), with **full and correct support** for ICSS and CSS modules.",
"main": "index.js",
"repository": {
@ -25,9 +25,7 @@
"@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.1.1",
"@validatem/is-postcss-plugin": "^0.1.0",
"@validatem/is-string": "^1.0.0",
"@validatem/one-of": "^0.1.1",
"@validatem/required": "^0.1.1",
@ -35,16 +33,14 @@
"bl": "^4.0.0",
"bluebird": "^3.7.1",
"default-value": "^1.0.0",
"find-cycle": "^1.0.0",
"generic-names": "^2.0.1",
"insert-css": "^2.0.0",
"map-obj": "^4.1.0",
"object.fromentries": "^2.0.1",
"postcss": "^8.4.12",
"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",
"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",
"through2": "^2.0.3"
},
"devDependencies": {

@ -1,5 +0,0 @@
"use strict";
const createError = require("create-error");
module.exports = createError("CycleError");

@ -1,40 +0,0 @@
"use strict";
const findCycle = require("find-cycle/directed");
module.exports = function findCycleSets(nodes) {
let queue = nodes.slice();
let cycleSets = [];
let innocentNodes = [];
let lastQueueLength = queue.length;
while (queue.length > 0) {
let queueSet = new Set(queue);
let cycle = findCycle(queue, (node) => {
return node.children.filter((child) => {
// We want to prevent findCycle from discovering and traversing any nodes that are not in our "conflict set"; otherwise a second pass will fail, because it will rediscover already-handled nodes (that have been removed from the queue) indirectly, and endlessly loop on discovering that cycle.
return queueSet.has(child);
});
});
if (cycle != null) {
cycleSets.push(cycle);
queue = queue.filter((node) => !cycle.includes(node));
if (queue.length === lastQueueLength) {
throw new Error(`Failed to reduce queue length; this is a bug, please report it!`);
} else {
lastQueueLength = queue.length;
}
} else {
innocentNodes = queue;
break;
}
}
return {
cycleSets: cycleSets,
innocentNodes: innocentNodes
};
};

@ -1,20 +0,0 @@
"use strict";
/*
parents == dependencies == before
children == dependents == after
*/
const sortDependencies = require("../sort-dependencies");
let items = [
{ id: 0, deps: { 1:1 } },
{ id: 1, deps: { 2:2, 3:3, 4:4 } },
// { id: 2, deps: {} },
{ id: 2, deps: { 0:0 } },
{ id: 3, deps: {} },
{ id: 4, deps: {} },
];
console.log(sortDependencies(items, {}, { ignoreCycles: true }));

@ -2,8 +2,6 @@
const util = require("util");
const CycleError = require("./cycle-error");
// TODO: Publish this as a stand-alone module
module.exports = function kahn(nodes) {
@ -38,14 +36,5 @@ module.exports = function kahn(nodes) {
}
}
if (list.length !== nodes.length) {
let processedNodes = new Set(list);
let affectedNodes = nodes.filter((node) => !processedNodes.has(node));
throw new CycleError(`One or more cycles were detected, involving ${affectedNodes.length} nodes`, {
affectedNodes: affectedNodes
});
}
return list;
};

@ -2,7 +2,6 @@
const Promise = require("bluebird");
const path = require("path");
const mapObj = require("map-obj");
const stream = require("../stream");
const createFilePostprocessor = require("../postcss/postprocess");
@ -19,7 +18,7 @@ module.exports = function (state) {
let allCss = "";
let loaderItem;
let entryPoint = (options._flags != null && options._flags.entries != null)
let entryPoint = (options._flags.entries != null)
// Get the (absolute!) path of the folder containing the initial entry file, as a reference point for relative paths in the output
? path.dirname(path.resolve(options._flags.entries[0]))
// ... or, if no entry file is specified, go off the current working directory
@ -28,22 +27,17 @@ module.exports = function (state) {
return stream((item) => {
// And the same for the loader shim path. All this relative-path stuff is to prevent absolute filesystem URLs from leaking into the output, as those might contain sensitive information.
let relativeLoaderPath = path.relative(path.dirname(item.file), loaderShimPath);
if (isCss(item)) {
return Promise.try(() => {
return processFile(item);
}).then(({ result, icssExports }) => {
// NOTE: This is a workaround, until we find a more robust solution to this. While composed classes should be delimited by dots in CSS, they should be delimited by *spaces* in HTML. In "double compose" cases, ie. when one class composes another class which was already composed with a third class, we can get a class string like "foo bar.baz" which is wrong; this replaces all the erroneous dots with spaces.
let icssExportsHTML = mapObj(icssExports, (key, value) => {
return [ key, value.replace(/\./g, " ") ];
});
allCss += `/* from ${path.relative(entryPoint, item.file)} */\n\n${result.css}\n\n`;
if (!item.__icssify__discardable) {
return {
... item,
source: `require(${JSON.stringify(relativeLoaderPath)}); module.exports = ${JSON.stringify(icssExportsHTML)};`
source: `require(${JSON.stringify(relativeLoaderPath)}); module.exports = ${JSON.stringify(icssExports)};`
};
}
});
@ -62,7 +56,6 @@ module.exports = function (state) {
source: loaderItem.source.replace('"## CONTENT MARKER ##"', JSON.stringify(allCss))
};
} else {
// FIXME: Can occur if some dependency also gets processed and require()s a CSS file, but does not specify icssify as a transform (but the using project does) -- like with ui-lib + site-builder
throw new Error("Processed CSS, but global loader was not encountered. This should never happen, please report it as a bug!");
}
}

@ -3,21 +3,19 @@
const sortDependencies = require("../sort-dependencies");
const stream = require("../stream");
module.exports = function({ ignoreCycles }) {
return function createKahnSortingStream() {
let sortables = [];
module.exports = function createKahnSortingStream() {
let sortables = [];
function handleItem(item) {
sortables.push(item);
}
function handleItem(item) {
sortables.push(item);
}
function flush() {
let sorted = sortDependencies(sortables, {}, { ignoreCycles });
function flush() {
let sorted = sortDependencies(sortables);
// TODO: Verify that the 'null' push here is necessary
return sorted.concat([ null ]);
}
// TODO: Verify that the 'null' push here is necessary
return sorted.concat([ null ]);
}
return stream(handleItem, flush);
};
return stream(handleItem, flush);
};

@ -11,32 +11,28 @@ 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 = function (_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.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.postcss = true;
});

@ -11,74 +11,57 @@ 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 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 ]
module.exports = postcss.plugin(pluginName, (options = {}) => {
validateOptions([options], {
keyReplacer: [ required, isFunction ]
});
return {
postcssPlugin: pluginName,
Once(css, { result }) {
const importReplacements = Object.create(null);
const { icssImports, icssExports } = icssUtils.extractICSS(css);
return function process(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 [ importUrl, imports ] of Object.entries(icssImports)) {
const url = loaderUtils.parseString(importUrl);
for (const [ localKey, remoteKey ] of Object.entries(imports)) {
index += 1;
for (const [ localKey, remoteKey ] of Object.entries(imports)) {
index += 1;
let newKey = options.keyReplacer({ localKey, remoteKey, index, url });
importReplacements[localKey] = newKey;
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
}
});
}
}
result.messages.push({
pluginName,
type: 'icss-import',
item: { url, localKey, remoteKey, newKey, index },
});
}
}
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;
});

@ -15,7 +15,6 @@ module.exports = function createFilePostprocessor(options) {
// TODO: Reuse instance, figure out how to pass the file metadata to the callback for an individual `process` call
let postcssInstance = postcss([
icssParser({
autoExportImports: options.autoExportImports,
keyReplacer: ({ url, remoteKey }) => {
let resolvedSourcePath = item.deps[url];

@ -1,87 +1,8 @@
"use strict";
const kahn = require("./kahn");
const CycleError = require("./cycle-error");
const findCycleSets = require("./find-cycle-sets");
function printNodes(nodes) {
return nodes
.map((node) => node.id)
.map((id) => ` - ${id}`)
.join("\n");
}
function mergeRemovedEdges(a, b) {
let newObject = {};
function addEdges(key, values) {
if (newObject[key] == null) {
newObject[key] = values;
} else {
newObject[key] = newObject[key].concat(values);
}
}
for (let [ key, value ] of Object.entries(a)) {
addEdges(key, value);
}
for (let [ key, value ] of Object.entries(b)) {
addEdges(key, value);
}
return newObject;
}
function findEdgesToRemove(affectedNodes) {
let { cycleSets } = findCycleSets(affectedNodes);
let edgesToRemove = {};
function addEdge(key, value) {
if (edgesToRemove[key] == null) {
edgesToRemove[key] = [];
}
edgesToRemove[key].push(value);
}
for (let cycle of cycleSets) {
let firstModule = cycle[0];
let firstConflictingModule = cycle.find((module_) => firstModule.parents.includes(module_));
addEdge(firstModule.id, firstConflictingModule.id);
}
return edgesToRemove;
}
function createDependencyCycleError(affectedNodes) {
let { cycleSets, innocentNodes } = findCycleSets(affectedNodes);
let cycleSetStrings = cycleSets.map((set) => printNodes(set));
let innocentNodeString = (innocentNodes.length === 0)
? " (none)"
: printNodes(innocentNodes);
let message = [
`At least ${cycleSets.length} circular dependency chain(s) were detected, involving the following modules:`,
"",
cycleSetStrings.join("\n\n"),
"",
"Additionally, the following modules could not be processed as a result:",
"",
innocentNodeString,
"",
"Please read the icssify documentation for the 'ignoreCycles' option to understand how to deal with this."
].join("\n");
return new CycleError(message);
}
module.exports = function sortDependencies(items, removedEdges, options = {}) {
let { ignoreCycles } = options;
module.exports = function sortDependencies(items) {
let dependencyMap = new Map();
let nodeMap = new Map();
@ -93,45 +14,20 @@ module.exports = function sortDependencies(items, removedEdges, options = {}) {
});
items.forEach((item) => {
let removedEdgesForItem = new Set(removedEdges[item.id]);
Object.values(item.deps)
.filter((dep => dep !== item.id))
.forEach((dep) => {
let parent = nodeMap.get(dep);
if (removedEdgesForItem == null || !removedEdgesForItem.has(parent.id)) {
parent.children.push(nodeMap.get(item.id));
}
nodeMap.get(dep).children.push(nodeMap.get(item.id));
});
nodeMap.get(item.id).parents = Object.values(item.deps)
.filter((id => id !== item.id))
.map((id) => nodeMap.get(id))
.filter((node) => !removedEdgesForItem.has(node.id));
.map((id) => nodeMap.get(id));
});
try {
let sortedNodes = kahn(Array.from(nodeMap.values()));
return sortedNodes.map((node) => {
return dependencyMap.get(node.id);
});
} catch (error) {
if (error instanceof CycleError) {
if (ignoreCycles) {
// TODO: Disallow cycles for extensions processed by icssify, *even* when this option is active, because cycles just aren't (sensibly) possible in ICSS
let newRemovedEdges = findEdgesToRemove(error.affectedNodes);
// TODO: Debug-log removed edges here, and the full cycle, to debug potential future infinite recursion bugs
// We will keep throwing out more (deterministically) random edges until we're left with no cycles. If there are enough cycles to cause an exceeded stack size here, you probably have bigger issues to worry about...
return sortDependencies(items, mergeRemovedEdges(removedEdges, newRemovedEdges), options);
} else {
throw createDependencyCycleError(error.affectedNodes);
}
} else {
throw error;
}
}
let sortedNodes = kahn(Array.from(nodeMap.values()));
return sortedNodes.map((node) => {
return dependencyMap.get(node.id);
});
};

@ -14,7 +14,7 @@ module.exports = function (state) {
return function createTransform(file, options) {
// TODO: Reuse instance?
let preprocessFile = createFilePreprocessor(options);
if (isCss({ file: file })) {
let buffer = new bl.BufferList();

Loading…
Cancel
Save