Skip to content

chore(release): 4.0.0 #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
coverage
lib
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NEED RETURN before release (require for fast migration and testing)

34 changes: 30 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
sudo: false

git:
depth: 10

branches:
only:
- master

language: node_js
node_js:
- "4"
- "6"
- "node"

# cache node modules
cache:
directories:
- $HOME/.npm
- node_modules

matrix:
fast_finish: true
include:
- node_js: '10'
script: npm run test -- --no-coverage
- node_js: '8'
script: npm run test -- --no-coverage
- node_js: '6'
script: npm run test -- --no-coverage

before_install:
- npm install -g npm@latest
- node --version
- npm --version
45 changes: 27 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Build Status](https://travis-ci.org/css-modules/icss-utils.svg)](https://travis-ci.org/css-modules/icss-utils)

# ICSS Utils
# ICSS Utils

## replaceSymbols

Expand All @@ -10,9 +10,10 @@ This is broken into its own module in case the behaviour needs to be replicated
(i.e. [CSS Modules Values](https://github.com/css-modules/postcss-modules-values))

```js
import { replaceSymbols, replaceValueSymbols } from "icss-utils"
replaceSymbols(css, replacements)
replaceValueSymbols(string, replacements)
import { replaceSymbols, replaceValueSymbols } from "icss-utils";

replaceSymbols(css, replacements);
replaceValueSymbols(string, replacements);
```

Where:
Expand All @@ -30,24 +31,28 @@ A symbol is a string of alphanumeric, `-` or `_` characters. A replacement can b
Extracts and remove (if removeRules is equal true) from PostCSS tree `:import` and `:export` statements.

```js
import postcss from 'postcss';
import { extractICSS } from 'icss-utils'
import postcss from "postcss";
import { extractICSS } from "icss-utils";

const css = postcss.parse(`
:import(colors) {
:import("colors.css" screen and (orientation:landscape)) {
a: b;
}
:export {
c: d;
}
`)
`);

extractICSS(css)
extractICSS(css);
/*
{
icssImports: {
colors: {
a: 'b'
'"colors.css" screen and (orientation:landscape)': {
path: "colors.css",
extra: "screen and (orientation:landscape)",
tokens: {
a: 'b'
}
}
},
icssExports: {
Expand All @@ -62,18 +67,22 @@ extractICSS(css)
Converts icss imports and exports definitions to postcss ast

```js
createICSSRules({
colors: {
a: 'b'
createICSSRules(
{
'"colors.css"': {
a: "b"
}
},
{
c: "d"
}
}, {
c: 'd'
})
);
```

## License

ISC

---
Glen Maddern and Bogdan Chadkin, 2015.

Glen Maddern, Bogdan Chadkin and Evilebottnawi 2015-present.
65 changes: 65 additions & 0 deletions lib/createICSSRules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _postcss = _interopRequireDefault(require("postcss"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const createImports = imports => {
return Object.keys(imports).map(token => {
const aliases = imports[token];
const declarations = Object.keys(aliases).map(key => _postcss.default.decl({
prop: key,
value: aliases[key],
raws: {
before: "\n "
}
}));
const hasDeclarations = declarations.length > 0;

const rule = _postcss.default.rule({
selector: `:import(${token})`,
raws: {
after: hasDeclarations ? "\n" : ""
}
});

if (hasDeclarations) {
rule.append(declarations);
}

return rule;
});
};

const createExports = exports => {
const declarations = Object.keys(exports).map(key => _postcss.default.decl({
prop: key,
value: exports[key],
raws: {
before: "\n "
}
}));

if (declarations.length === 0) {
return [];
}

const rule = _postcss.default.rule({
selector: `:export`,
raws: {
after: "\n"
}
}).append(declarations);

return [rule];
};

const createICSSRules = (imports, exports) => [...createImports(imports), ...createExports(exports)];

var _default = createICSSRules;
exports.default = _default;
64 changes: 64 additions & 0 deletions lib/extractICSS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const importPattern = /^:import\(("[^"]*"|'[^']*')(?:\s(.+))?\)$/;

const getDeclsObject = rule => {
const object = {};
rule.walkDecls(decl => {
object[decl.raws.before.trim() + decl.prop] = decl.value;
});
return object;
};

const extractICSS = (css, removeRules = true) => {
const icssImports = {};
const icssExports = {};
css.each(node => {
if (node.type === "rule") {
if (node.selector.slice(0, 7) === ":import") {
const matches = importPattern.exec(node.selector);

if (matches) {
const path = matches[1].replace(/'|"/g, "");
const extra = matches[2] ? matches[2] : "";
const dep = `"${path}"${extra ? ` ${extra}` : ""}`;
icssImports[dep] = Object.assign(icssImports[dep] || {}, _objectSpread({
path
}, extra ? {
extra
} : {}, {
tokens: Object.assign(icssImports[dep] ? icssImports[dep].tokens : {}, getDeclsObject(node))
}));

if (removeRules) {
node.remove();
}
}
}

if (node.selector === ":export") {
Object.assign(icssExports, getDeclsObject(node));

if (removeRules) {
node.remove();
}
}
}
});
return {
icssImports,
icssExports
};
};

var _default = extractICSS;
exports.default = _default;
39 changes: 39 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "replaceValueSymbols", {
enumerable: true,
get: function get() {
return _replaceValueSymbols.default;
}
});
Object.defineProperty(exports, "replaceSymbols", {
enumerable: true,
get: function get() {
return _replaceSymbols.default;
}
});
Object.defineProperty(exports, "extractICSS", {
enumerable: true,
get: function get() {
return _extractICSS.default;
}
});
Object.defineProperty(exports, "createICSSRules", {
enumerable: true,
get: function get() {
return _createICSSRules.default;
}
});

var _replaceValueSymbols = _interopRequireDefault(require("./replaceValueSymbols.js"));

var _replaceSymbols = _interopRequireDefault(require("./replaceSymbols.js"));

var _extractICSS = _interopRequireDefault(require("./extractICSS.js"));

var _createICSSRules = _interopRequireDefault(require("./createICSSRules.js"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25 changes: 25 additions & 0 deletions lib/replaceSymbols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _replaceValueSymbols = _interopRequireDefault(require("./replaceValueSymbols.js"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const replaceSymbols = (css, replacements) => {
css.walk(node => {
if (node.type === "decl") {
node.value = (0, _replaceValueSymbols.default)(node.value, replacements);
} else if (node.type === "rule") {
node.selector = (0, _replaceValueSymbols.default)(node.selector, replacements);
} else if (node.type === "atrule" && ["media", "supports"].includes(node.name.toLowerCase())) {
node.params = (0, _replaceValueSymbols.default)(node.params, replacements);
}
});
};

var _default = replaceSymbols;
exports.default = _default;
25 changes: 25 additions & 0 deletions lib/replaceValueSymbols.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
const matchValueName = /[$]?[\w-]+/g;

const replaceValueSymbols = (value, replacements) => {
let matches;

while (matches = matchValueName.exec(value)) {
const replacement = replacements[matches[0]];

if (replacement) {
value = value.slice(0, matches.index) + replacement + value.slice(matchValueName.lastIndex);
matchValueName.lastIndex -= matches[0].length - replacement.length;
}
}

return value;
};

var _default = replaceValueSymbols;
exports.default = _default;
Loading