Skip to content
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

chore(release): v1.0.0 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 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
103 changes: 103 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"use strict";

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

var _postcssValueParser = _interopRequireDefault(
require("postcss-value-parser")
);

var _icssUtils = require("icss-utils");

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

const plugin = "postcss-plugin-import";

const getArg = nodes =>
nodes.length !== 0 && nodes[0].type === "string"
? nodes[0].value
: _postcssValueParser.default.stringify(nodes);

const getUrl = node => {
if (node.type === "function" && node.value.toLowerCase() === "url") {
return getArg(node.nodes);
}

if (node.type === "string") {
return node.value;
}

return "";
};

const parseImport = params => {
const _valueParser = (0, _postcssValueParser.default)(params),
nodes = _valueParser.nodes;

if (nodes.length === 0) {
return null;
}

const url = getUrl(nodes[0]);

if (url.trim().length === 0) {
return null;
}

return {
url,
media: _postcssValueParser.default.stringify(nodes.slice(1)).trim()
};
};

const defaultFilter = url => !/^\w+:\/\//.test(url) && !url.startsWith("//");

module.exports = _postcss.default.plugin(
plugin,
(options = {}) => (css, result) => {
const imports = {};
const filter = options.filter || defaultFilter;
css.walkAtRules(/^import$/i, atrule => {
// Convert only top-level @import
if (atrule.parent.type !== "root") {
return;
}

if (atrule.nodes) {
return result.warn(
"It looks like you didn't end your @import statement correctly. " +
"Child nodes are attached to it.",
{
node: atrule
}
);
}

const parsed = parseImport(atrule.params);

if (parsed === null) {
return result.warn(`Unable to find uri in '${atrule.toString()}'`, {
node: atrule
});
}

if (filter && !filter(parsed.url)) {
return;
}

atrule.remove();
imports[
`"${parsed.url}"${
parsed.media.length > 0 ? ` ${parsed.media.toLowerCase()}` : ""
}`
] = {};
});

if (Object.keys(imports).length === 0) {
return;
}

css.prepend((0, _icssUtils.createICSSRules)(imports, {}));
}
);
41 changes: 27 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-icss-import",
"version": "0.1.0",
"version": "1.0.0",
"description": "PostCSS plugin for css-modules to convert @import statements to ICSS",
"main": "lib/index.js",
"files": [
@@ -20,41 +20,54 @@
]
},
"eslintConfig": {
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"env": {
"es6": true,
"node": true,
"jest": true
},
"extends": "eslint:recommended"
},
"babel": {
"presets": [
[
"env",
"@babel/preset-env",
{
"targets": {
"node": 4
"node": 6
}
}
]
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"repository": "css-modules/postcss-icss-import",
"author": "Bogdan Chadkin <trysound@yandex.ru>",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-jest": "^20.0.3",
"babel-preset-env": "^1.5.1",
"eslint": "^3.19.0",
"husky": "^0.13.4",
"jest": "^20.0.4",
"lint-staged": "^3.5.1",
"prettier": "^1.3.1",
"@babel/cli": "^7.1.0",
"@babel/core": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^10.0.0",
"babel-jest": "^23.6.0",
"eslint": "^5.6.0",
"husky": "^1.0.0",
"jest": "^23.6.0",
"lint-staged": "^7.3.0",
"prettier": "^1.4.4",
"strip-indent": "^2.0.0"
},
"dependencies": {
"icss-utils": "^2.1.0",
"postcss": "^6.0.1",
"icss-utils": "css-modules/icss-utils#chore-release-4",
"postcss": "^7.0.2",
"postcss-value-parser": "^3.3.0"
}
}
41 changes: 22 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-env node */
import postcss from "postcss";
import valueParser from "postcss-value-parser";
import { createICSSRules } from "icss-utils";
@@ -11,7 +10,7 @@ const getArg = nodes =>
: valueParser.stringify(nodes);

const getUrl = node => {
if (node.type === "function" && node.value === "url") {
if (node.type === "function" && node.value.toLowerCase() === "url") {
return getArg(node.nodes);
}
if (node.type === "string") {
@@ -35,19 +34,16 @@ const parseImport = params => {
};
};

const isExternalUrl = url => /^\w+:\/\//.test(url) || url.startsWith("//");
const defaultFilter = url => !/^\w+:\/\//.test(url) && !url.startsWith("//");

const walkImports = (css, callback) => {
css.each(node => {
if (node.type === "atrule" && node.name.toLowerCase() === "import") {
callback(node);
}
});
};

module.exports = postcss.plugin(plugin, () => (css, result) => {
module.exports = postcss.plugin(plugin, (options = {}) => (css, result) => {
const imports = {};
walkImports(css, atrule => {
const filter = options.filter || defaultFilter;
css.walkAtRules(/^import$/i, atrule => {
// Convert only top-level @import
if (atrule.parent.type !== "root") {
return;
}
if (atrule.nodes) {
return result.warn(
"It looks like you didn't end your @import statement correctly. " +
@@ -61,13 +57,20 @@ module.exports = postcss.plugin(plugin, () => (css, result) => {
node: atrule
});
}
if (!isExternalUrl(parsed.url)) {
atrule.remove();
imports[`'${parsed.url}'`] = {
import: "default" +
(parsed.media.length === 0 ? "" : ` ${parsed.media}`)
};
if (filter && !filter(parsed.url)) {
return;
}
atrule.remove();
imports[
`"${parsed.url}"${
parsed.media.length > 0 ? ` ${parsed.media.toLowerCase()}` : ""
}`
] = {};
});

if (Object.keys(imports).length === 0) {
return;
}

css.prepend(createICSSRules(imports, {}));
});
Loading