css-node-extract
Advanced tools
Comparing version 2.1.3 to 3.0.0
@@ -1,26 +0,207 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const postcss = require("postcss"); | ||
const postcssNodeExtract = require("./lib/postcss-node-extract"); | ||
const const_1 = require("./const"); | ||
/** | ||
* Synchronously extract nodes from a string. | ||
*/ | ||
exports.processSync = ({ css, filters, customFilters, postcssSyntax, preserveLines, }) => postcss(postcssNodeExtract(filters, customFilters, preserveLines)) | ||
.process(css, { syntax: postcssSyntax }).css | ||
.replace(new RegExp(`\\/\\* ${const_1.PRESERVE_LINES_START}|${const_1.PRESERVE_LINES_END} \\*\\/`, `g`), ``); | ||
/** | ||
* Asynchronously extract nodes from a string. | ||
*/ | ||
exports.process = (options) => new Promise((resolve) => { | ||
const result = exports.processSync(options); | ||
resolve(result); | ||
}); | ||
/** | ||
* cssNodeExtract | ||
*/ | ||
exports.default = { | ||
process: exports.process, | ||
processSync: exports.processSync, | ||
}; | ||
//# sourceMappingURL=index.js.map | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('change-case'), require('postcss')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'change-case', 'postcss'], factory) : | ||
(factory((global['css-node-extract'] = {}),global.changeCase,global.postcss)); | ||
}(this, (function (exports,changeCase,postcss) { 'use strict'; | ||
postcss = postcss && postcss.hasOwnProperty('default') ? postcss['default'] : postcss; | ||
/** | ||
* Check if a node matches the given filter. | ||
*/ | ||
function nodeMatchesFilter(node, filter) { | ||
if (!node[filter.property]) { | ||
return false; | ||
} else if (node[filter.property] === filter.value) { | ||
return true; | ||
} else if (filter.value instanceof RegExp && filter.value.test(node[filter.property])) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Whiteliste a node if it (or one of the nodes parents) matches the given filter. | ||
*/ | ||
function extractNodeRecursively(node, filterGroups) { | ||
if (node.parent && node.parent.type !== "root") { | ||
return extractNodeRecursively(node.parent, filterGroups); | ||
} | ||
var extractNode = false; | ||
filterGroups.some(function (filterGroup) { | ||
extractNode = filterGroup.filter(function (filter) { | ||
return !nodeMatchesFilter(node, filter); | ||
}).length === 0; | ||
return extractNode; | ||
}); | ||
return extractNode; | ||
} | ||
/** | ||
* Filter definitions. | ||
*/ | ||
var filterDefinitions = { | ||
atRules: [[{ | ||
property: "type", | ||
value: "atrule" | ||
}]], | ||
declarations: [[{ | ||
property: "type", | ||
value: "decl" | ||
}]], | ||
functions: [[{ | ||
property: "type", | ||
value: "atrule" | ||
}, { | ||
property: "name", | ||
value: "function" | ||
}]], | ||
mixins: [[{ | ||
property: "type", | ||
value: "atrule" | ||
}, { | ||
property: "name", | ||
value: "mixin" | ||
}], [{ | ||
property: "type", | ||
value: "rule" | ||
}, { | ||
property: "selector", | ||
value: /\(.*\)/ | ||
}]], | ||
rules: [[{ | ||
property: "type", | ||
value: "rule" | ||
}]], | ||
silent: [[{ | ||
property: "type", | ||
value: "atrule" | ||
}, { | ||
property: "name", | ||
value: "debug" | ||
}], [{ | ||
property: "type", | ||
value: "atrule" | ||
}, { | ||
property: "name", | ||
value: "error" | ||
}], [{ | ||
property: "type", | ||
value: "atrule" | ||
}, { | ||
property: "name", | ||
value: "function" | ||
}], [{ | ||
property: "type", | ||
value: "atrule" | ||
}, { | ||
property: "name", | ||
value: "mixin" | ||
}], [{ | ||
property: "type", | ||
value: "atrule" | ||
}, { | ||
property: "name", | ||
value: "warn" | ||
}], [{ | ||
property: "type", | ||
value: "decl" | ||
}, { | ||
property: "prop", | ||
value: /^[$|@]/ | ||
}], [{ | ||
property: "type", | ||
value: "rule" | ||
}, { | ||
property: "selector", | ||
value: /%/ | ||
}], [{ | ||
property: "type", | ||
value: "rule" | ||
}, { | ||
property: "selector", | ||
value: /\(.*\)/ | ||
}]], | ||
variables: [[{ | ||
property: "type", | ||
value: "decl" | ||
}, { | ||
property: "prop", | ||
value: /^[$|@]/ | ||
}]] | ||
}; | ||
var PRESERVE_LINES_START = "START preserve lines"; | ||
var PRESERVE_LINES_END = "preserve lines END"; | ||
/** | ||
* A PostCSS plugin for extracting nodes from CSS code. | ||
*/ | ||
function postcssNodeExtract(filterNames, customFilters, preserveLines) { | ||
var filterNamesArray = Array.isArray(filterNames) ? filterNames : [filterNames]; | ||
Object.assign(filterDefinitions, customFilters); | ||
return postcss.plugin("postcss-node-extract", function () { | ||
return function (nodes) { | ||
nodes.walk(function (rule) { | ||
var filterRule = false; | ||
filterNamesArray.some(function (filterName) { | ||
var filterNameCamelCase = changeCase.camelCase(filterName); | ||
filterRule = extractNodeRecursively(rule, filterDefinitions[filterNameCamelCase]); | ||
return filterRule; | ||
}); | ||
if (!filterRule) { | ||
if (preserveLines) { | ||
var ruleLines = rule.toString().split(/\r\n|\r|\n/).length; | ||
rule.cloneBefore({ | ||
type: "comment", | ||
text: "".concat(PRESERVE_LINES_START).concat("\n".repeat(ruleLines - 1)).concat(PRESERVE_LINES_END), | ||
raws: Object.assign(rule.raws, { | ||
left: " ", | ||
right: " " | ||
}) | ||
}); | ||
} | ||
rule.remove(); | ||
} | ||
}); | ||
}; | ||
}); | ||
} | ||
/** | ||
* Synchronously extract nodes from a string. | ||
*/ | ||
var processSync = function processSync(_ref) { | ||
var css = _ref.css, | ||
filters = _ref.filters, | ||
customFilters = _ref.customFilters, | ||
postcssSyntax = _ref.postcssSyntax, | ||
preserveLines = _ref.preserveLines; | ||
return postcss(postcssNodeExtract(filters, customFilters, preserveLines)).process(css, { | ||
syntax: postcssSyntax | ||
}).css.replace(new RegExp("\\/\\* ".concat(PRESERVE_LINES_START, "|").concat(PRESERVE_LINES_END, " \\*\\/"), "g"), ""); | ||
}; | ||
/** | ||
* Asynchronously extract nodes from a string. | ||
*/ | ||
var process = function process(options) { | ||
return new Promise(function (resolve) { | ||
var result = processSync(options); | ||
resolve(result); | ||
}); | ||
}; | ||
exports.processSync = processSync; | ||
exports.process = process; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
@@ -7,3 +7,3 @@ { | ||
], | ||
"version": "2.1.3", | ||
"version": "3.0.0", | ||
"author": "Markus Oberlehner", | ||
@@ -13,11 +13,38 @@ "homepage": "https://github.com/maoberlehner/css-node-extract", | ||
"scripts": { | ||
"build": "npm run clean && tsc", | ||
"watch": "npm run clean && tsc -w", | ||
"prescripts": "npm run clean", | ||
"scripts:umd": "rollup --config --output.format umd --name css-node-extract --output.file dist/index.js src/index.js", | ||
"scripts:es": "rollup --config --output.format es --name css-node-extract --output.file dist/index.esm.js src/index.js", | ||
"scripts:minify": "uglifyjs --compress --mangle --comments --output dist/index.min.js dist/index.js && uglifyjs --compress --mangle --comments --output dist/index.esm.min.js dist/index.esm.js", | ||
"scripts": "npm run scripts:umd && npm run scripts:es && npm run scripts:minify", | ||
"clean": "rimraf dist", | ||
"lint": "tslint src/**/*.ts", | ||
"lint:scripts": "eslint --ignore-path .gitignore .", | ||
"lint": "npm run lint:scripts", | ||
"coverage": "jest --coverage --maxWorkers=4", | ||
"coveralls": "npm run coverage && cat coverage/lcov.info | coveralls", | ||
"test": "jest", | ||
"prepublishOnly": "npm run build" | ||
"prepublishOnly": "npm run scripts" | ||
}, | ||
"dependencies": { | ||
"change-case": "^3.0.2", | ||
"postcss": "^6.0.21" | ||
}, | ||
"devDependencies": { | ||
"@avalanche/eslint-config": "^2.0.0", | ||
"@babel/core": "^7.0.0-beta.44", | ||
"@babel/preset-env": "^7.0.0-beta.44", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-jest": "^22.4.3", | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.19.1", | ||
"eslint-plugin-import": "^2.11.0", | ||
"jest": "^22.4.3", | ||
"postcss-less": "^1.1.5", | ||
"postcss-scss": "^1.0.5", | ||
"rimraf": "^2.6.2", | ||
"rollup": "^0.58.0", | ||
"rollup-plugin-babel": "^4.0.0-beta.4", | ||
"uglify-es": "^3.3.9" | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.esm.js", | ||
"repository": { | ||
@@ -30,30 +57,5 @@ "type": "git", | ||
}, | ||
"dependencies": { | ||
"change-case": "^3.0.1", | ||
"postcss": "^6.0.14" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^21.1.8", | ||
"coveralls": "^3.0.0", | ||
"jest": "^21.2.1", | ||
"postcss-less": "^1.1.3", | ||
"postcss-scss": "^1.0.2", | ||
"rimraf": "^2.6.2", | ||
"ts-jest": "^21.2.4", | ||
"tslint": "^5.8.0", | ||
"typescript": "^2.6.2" | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"jest": { | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"js" | ||
], | ||
"transform": { | ||
"^.+\\.ts$": "<rootDir>/node_modules/ts-jest/preprocessor.js" | ||
}, | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|ts)$", | ||
"mapCoverage": true | ||
} | ||
"browserslist": [ | ||
"last 2 versions" | ||
] | ||
} |
# css-node-extract | ||
[![Patreon](https://img.shields.io/badge/patreon-donate-blue.svg)](https://www.patreon.com/maoberlehner) | ||
[![Donate](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://paypal.me/maoberlehner) | ||
[![Build Status](https://travis-ci.org/maoberlehner/css-node-extract.svg?branch=master)](https://travis-ci.org/maoberlehner/css-node-extract) | ||
@@ -9,2 +12,3 @@ [![Coverage Status](https://coveralls.io/repos/github/maoberlehner/css-node-extract/badge.svg?branch=master)](https://coveralls.io/github/maoberlehner/css-node-extract?branch=master) | ||
## Filters | ||
- **at-rules**: `@media`, `@supports`, `@mixin`,... | ||
@@ -20,2 +24,3 @@ - **declarations**: `$variable`, `@variable`,... | ||
## Demos | ||
```js | ||
@@ -45,2 +50,3 @@ const cssNodeExtract = require('css-node-extract'); | ||
### Custom filter | ||
```js | ||
@@ -69,2 +75,3 @@ const cssNodeExtract = require('css-node-extract'); | ||
### Preserve lines | ||
Usually `css-node-extract` removes all nodes which do not match the given filters. However under some circumstances it might be useful to preserve the original line numbers (e.g. to keep source map references intact). | ||
@@ -90,2 +97,3 @@ | ||
### ES2015 named exports | ||
```js | ||
@@ -115,5 +123,7 @@ import { process, processSync } from 'css-node-extract'; | ||
## Upgrade from 1.x.x to 2.x.x | ||
With version 2.0.0 the handling of custom filters was changed. The `customFilter` option was renamed to `customFilters` and this option now takes an object instead of an array. Instead of defining one custom filter named `custom`, you can now define unlimited custom filters with custom names. | ||
## Upgrade from 0.x.x to 1.x.x | ||
With version 1.0.0 the `filterNames` option was renamed to `filters`. | ||
@@ -138,5 +148,7 @@ | ||
## Development | ||
See [CONTRIBUTING.md](https://github.com/maoberlehner/css-node-extract/blob/master/CONTRIBUTING.md) | ||
### Testing | ||
```bash | ||
@@ -147,9 +159,13 @@ npm test | ||
## About | ||
### Author | ||
Markus Oberlehner | ||
Website: https://markus.oberlehner.net | ||
Twitter: https://twitter.com/MaOberlehner | ||
PayPal.me: https://paypal.me/maoberlehner | ||
PayPal.me: https://paypal.me/maoberlehner | ||
Patreon: https://www.patreon.com/maoberlehner | ||
### License | ||
MIT |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
164
37040
15
20
765
1
1
1
Updatedchange-case@^3.0.2
Updatedpostcss@^6.0.21