Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: css-modules/icss-utils
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3442cc3ba047d67035ab7129e0da01a6247718f0
Choose a base ref
...
head repository: css-modules/icss-utils
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2bfed2e97ee5409a6c0c0a36b298a078b11988e8
Choose a head ref
  • 16 commits
  • 13 files changed
  • 6 contributors

Commits on Jun 3, 2017

  1. Verified

    This commit was signed with the committer’s verified signature.
    hyoban Stephen Zhou
    Copy the full SHA
    f3b7f93 View commit details
  2. Trim quote from import path

    TrySound committed Jun 3, 2017
    Copy the full SHA
    1100b7e View commit details

Commits on Jun 14, 2017

  1. Upgrade dependencies

    TrySound committed Jun 14, 2017

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    2411980 View commit details
  2. Copy the full SHA
    4fd013f View commit details
  3. Copy the full SHA
    19ffee9 View commit details
  4. 3.0.0

    TrySound committed Jun 14, 2017
    Copy the full SHA
    f40da13 View commit details
  5. Copy the full SHA
    c4da800 View commit details
  6. 3.0.1

    TrySound committed Jun 14, 2017
    Copy the full SHA
    911bfc6 View commit details

Commits on May 20, 2018

  1. Copy the full SHA
    8dc1425 View commit details

Commits on Sep 25, 2018

  1. Merge pull request #56 from mportuga/patch-1

    chore(LICENSE.md): Add missing license file.
    evilebottnawi authored Sep 25, 2018
    Copy the full SHA
    55414c5 View commit details

Commits on Oct 23, 2018

  1. update postcss to version 7

    EECOLOR committed Oct 23, 2018
    Copy the full SHA
    54d8bc5 View commit details

Commits on Oct 30, 2018

  1. Merge pull request #58 from EECOLOR/master

    update postcss to version 7
    mightyaleksey authored Oct 30, 2018
    Copy the full SHA
    228271a View commit details
  2. drop node@4 support

    mightyaleksey committed Oct 30, 2018
    Copy the full SHA
    21495be View commit details
  3. Revert "drop node@4 support"

    This reverts commit 21495be.
    mightyaleksey committed Oct 30, 2018
    Copy the full SHA
    726f513 View commit details
  4. drop node@4

    mightyaleksey committed Oct 30, 2018
    Copy the full SHA
    946910e View commit details
  5. Merge pull request #59 from css-modules/remove-node4-support

    drop node@4 support
    mightyaleksey authored Oct 30, 2018
    Copy the full SHA
    2bfed2e View commit details
Showing with 1,005 additions and 481 deletions.
  1. +0 −1 .travis.yml
  2. +6 −0 LICENSE.md
  3. +16 −11 package.json
  4. +18 −18 src/createICSSRules.js
  5. +2 −2 src/extractICSS.js
  6. +4 −4 src/index.js
  7. +12 −10 src/replaceSymbols.js
  8. +8 −8 src/replaceValueSymbols.js
  9. +20 −25 test/createICSSRules.test.js
  10. +6 −6 test/extractICSS.test.js
  11. +47 −110 test/replaceSymbols.test.js
  12. +70 −0 test/replaceValueSymbols.test.js
  13. +796 −286 yarn.lock
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: node_js
node_js:
- "4"
- "6"
- "node"
6 changes: 6 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ISC License (ISC)
Copyright 2018 Glen Maddern

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 changes: 16 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "icss-utils",
"version": "2.1.0",
"version": "4.0.0",
"description": "ICSS utils for postcss ast",
"main": "lib/index.js",
"engines": {
"node": ">= 6"
},
"files": [
"lib"
],
@@ -14,16 +17,18 @@
},
"lint-staged": {
"*.js": [
"eslint",
"prettier --write",
"eslint",
"git add"
]
},
"eslintConfig": {
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"env": {
"es6": true
},
"extends": "eslint:recommended"
},
"babel": {
@@ -32,7 +37,7 @@
"env",
{
"targets": {
"node": 4
"node": 6
}
}
]
@@ -55,16 +60,16 @@
},
"homepage": "https://github.com/css-modules/icss-utils#readme",
"dependencies": {
"postcss": "^6.0.1"
"postcss": "^7.0.5"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-jest": "^20.0.3",
"babel-preset-env": "^1.5.1",
"eslint": "^3.19.0",
"husky": "^0.13.3",
"jest": "^20.0.3",
"lint-staged": "^3.4.2",
"prettier": "^1.3.1"
"babel-preset-env": "^1.5.2",
"eslint": "^4.0.0",
"husky": "^0.13.4",
"jest": "^20.0.4",
"lint-staged": "^3.6.1",
"prettier": "^1.4.4"
}
}
36 changes: 18 additions & 18 deletions src/createICSSRules.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import postcss from 'postcss'
import postcss from "postcss";

const createImports = imports => {
return Object.keys(imports).map(path => {
const aliases = imports[path]
const aliases = imports[path];
const declarations = Object.keys(aliases).map(key =>
postcss.decl({
prop: key,
value: aliases[key],
raws: { before: '\n ' }
raws: { before: "\n " }
})
)
);
return postcss
.rule({
selector: `:import(${path})`,
raws: { after: '\n' }
selector: `:import('${path}')`,
raws: { after: "\n" }
})
.append(declarations)
})
}
.append(declarations);
});
};

const createExports = exports => {
const declarations = Object.keys(exports).map(key =>
postcss.decl({
prop: key,
value: exports[key],
raws: { before: '\n ' }
raws: { before: "\n " }
})
)
);
if (declarations.length === 0) {
return []
return [];
}
const rule = postcss
.rule({
selector: `:export`,
raws: { after: '\n' }
raws: { after: "\n" }
})
.append(declarations)
return [rule]
}
.append(declarations);
return [rule];
};

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

export default createICSSRules
export default createICSSRules;
4 changes: 2 additions & 2 deletions src/extractICSS.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const importPattern = /^:import\(("[^"]*"|'[^']*'|[\w-\.]+)\)$/;
const importPattern = /^:import\(("[^"]*"|'[^']*'|[^"']+)\)$/;

const getDeclsObject = rule => {
const object = {};
@@ -16,7 +16,7 @@ const extractICSS = (css, removeRules = true) => {
if (node.selector.slice(0, 7) === ":import") {
const matches = importPattern.exec(node.selector);
if (matches) {
const path = matches[1];
const path = matches[1].replace(/'|"/g, "");
const aliases = Object.assign(
icssImports[path] || {},
getDeclsObject(node)
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as replaceValueSymbols } from './replaceValueSymbols.js'
export { default as replaceSymbols } from './replaceSymbols.js'
export { default as extractICSS } from './extractICSS.js'
export { default as createICSSRules } from './createICSSRules.js'
export { default as replaceValueSymbols } from "./replaceValueSymbols.js";
export { default as replaceSymbols } from "./replaceSymbols.js";
export { default as extractICSS } from "./extractICSS.js";
export { default as createICSSRules } from "./createICSSRules.js";
22 changes: 12 additions & 10 deletions src/replaceSymbols.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import replaceValueSymbols from './replaceValueSymbols.js'
import replaceValueSymbols from "./replaceValueSymbols.js";

const replaceSymbols = (css, replacements) => {
css.walkDecls(
decl => (decl.value = replaceValueSymbols(decl.value, replacements))
)
css.walkAtRules(
'media',
atRule => (atRule.params = replaceValueSymbols(atRule.params, replacements))
)
}
css.walk(node => {
if (node.type === "decl") {
node.value = replaceValueSymbols(node.value, replacements);
} else if (node.type === "rule") {
node.selector = replaceValueSymbols(node.selector, replacements);
} else if (node.type === "atrule" && node.name === "media") {
node.params = replaceValueSymbols(node.params, replacements);
}
});
};

export default replaceSymbols
export default replaceSymbols;
16 changes: 8 additions & 8 deletions src/replaceValueSymbols.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const matchValueName = /[$#]?[\w-\.]+/g
const matchValueName = /[$]?[\w-]+/g;

const replaceValueSymbols = (value, replacements) => {
let matches
let matches;
while ((matches = matchValueName.exec(value))) {
const replacement = replacements[matches[0]]
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
value.slice(matchValueName.lastIndex);
matchValueName.lastIndex -= matches[0].length - replacement.length;
}
}
return value
}
return value;
};

export default replaceValueSymbols
export default replaceValueSymbols;
45 changes: 20 additions & 25 deletions test/createICSSRules.test.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,47 @@
import postcss from 'postcss'
import { createICSSRules } from '../src'
/* eslint-env jest */
import postcss from "postcss";
import { createICSSRules } from "../src";

const run = (imports, exports) => {
return postcss.root().append(createICSSRules(imports, exports)).toString()
}
return postcss.root().append(createICSSRules(imports, exports)).toString();
};

test('create :import statement', () => {
test("create :import statement", () => {
expect(
run(
{
colors: {
a: 'b',
c: 'd'
},
'"path/file"': {
e: 'f'
"path/file": {
e: "f"
}
},
{}
)
).toEqual(
':import(colors) {\n a: b;\n c: d\n}\n:import("path/file") {\n e: f\n}'
)
})
).toEqual(":import('path/file') {\n e: f\n}");
});

test('create :export statement', () => {
test("create :export statement", () => {
expect(
run(
{},
{
a: 'b',
c: 'd'
a: "b",
c: "d"
}
)
).toEqual(':export {\n a: b;\n c: d\n}')
})
).toEqual(":export {\n a: b;\n c: d\n}");
});

test('create :import and :export', () => {
test("create :import and :export", () => {
expect(
run(
{
colors: {
a: 'b'
a: "b"
}
},
{
c: 'd'
c: "d"
}
)
).toEqual(':import(colors) {\n a: b\n}\n:export {\n c: d\n}')
})
).toEqual(":import('colors') {\n a: b\n}\n:export {\n c: d\n}");
});
12 changes: 6 additions & 6 deletions test/extractICSS.test.js
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ test("extract :import statements with identifier", () => {
test("extract :import statements with single quoted path", () => {
expect(runExtract(`:import('./colors.css') {}`)).toEqual({
icssImports: {
"'./colors.css'": {}
"./colors.css": {}
},
icssExports: {}
});
@@ -30,18 +30,18 @@ test("extract :import statements with single quoted path", () => {
test("extract :import statements with double quoted path", () => {
expect(runExtract(':import("./colors.css") {}')).toEqual({
icssImports: {
'"./colors.css"': {}
"./colors.css": {}
},
icssExports: {}
});
});

test("extract :import with values", () => {
test("not extract :import with values", () => {
expect(
runExtract(":import(colors) { i__blue: blue; i__red: red; }")
runExtract(":import(./colors.css) { i__blue: blue; i__red: red; }")
).toEqual({
icssImports: {
colors: {
"./colors.css": {
i__blue: "blue",
i__red: "red"
}
@@ -51,7 +51,7 @@ test("extract :import with values", () => {
});

test("not extract invalid :import", () => {
expect(runExtract(":import(./color.css) {}")).toEqual({
expect(runExtract(":import(\\'./colors.css) {}")).toEqual({
icssImports: {},
icssExports: {}
});
Loading