From 017f911668ddf1487292867c5dd5ace3bcbf879c Mon Sep 17 00:00:00 2001 From: Valentin Silytuin Date: Thu, 30 Sep 2021 21:17:23 +0400 Subject: [PATCH] Initial commit --- .editorconfig | 8 ++ .gitignore | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 29 ++++++++ index.js | 33 +++++++++ package.json | 31 ++++++++ test/from.css | 50 +++++++++++++ test/index.js | 26 +++++++ test/to.css | 33 +++++++++ yarn.lock | 27 +++++++ 9 files changed, 438 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json create mode 100644 test/from.css create mode 100644 test/index.js create mode 100644 test/to.css create mode 100644 yarn.lock diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d1ddcc7 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] +indent_style = tab +indent_size = 4 +end_of_line = lf +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89aad19 --- /dev/null +++ b/.gitignore @@ -0,0 +1,201 @@ +# Настройки редакторов + +.idea +.vscode + +# https://github.com/github/gitignore/blob/master/Global/Linux.gitignore + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# https://github.com/github/gitignore/blob/master/Global/macOS.gitignore + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# https://github.com/github/gitignore/blob/master/Global/Windows.gitignore + +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# https://github.com/github/gitignore/blob/master/Global/Archives.gitignore + +# It's better to unpack these files and commit the raw source because +# git has its own built in compression methods. +*.7z +*.jar +*.rar +*.zip +*.gz +*.tgz +*.bzip +*.bz2 +*.xz +*.lzma +*.cab + +# Packing-only formats +*.iso +*.tar + +# Package management formats +*.dmg +*.xpi +*.gem +*.egg +*.deb +*.rpm +*.msi +*.msm +*.msp + +# https://github.com/github/gitignore/blob/master/Global/Backup.gitignore + +*.bak +*.gho +*.ori +*.orig +*.tmp + +# https://github.com/github/gitignore/blob/master/Node.gitignore + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# Composer +vendor diff --git a/README.md b/README.md new file mode 100644 index 0000000..a758414 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# postcss-remove-bem-blocks + +[PostCSS](https://github.com/postcss/postcss)-плагин для удаления БЭМ-блоков (включая их модификаторы и элементы) из конечного CSS. + +## Установка + +```bash +yarn add -D @advdominion/postcss-remove-bem-blocks +``` + +## Использование + +`postcss.config.js` + +```js +const plugins = []; + +if (process.env.NODE_ENV === 'production') { + plugins.push( + require('@advdominion/postcss-remove-bem-blocks')({ + blocks: ['.stylepage'], + }) + ); +} + +module.exports = { + plugins, +}; +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..2df0f6f --- /dev/null +++ b/index.js @@ -0,0 +1,33 @@ +module.exports = (options = {blocks: []}) => { + return { + postcssPlugin: 'postcss-remove-bem-blocks', + Rule(rule) { + options.blocks.forEach((block) => { + if (rule.selector.includes(block)) { + const regexp = new RegExp(`${block}(?![A-Za-z0-9-]+)`); + if (rule.selectors.length === 1) { + if (regexp.test(rule.selectors[0])) { + rule.remove(); + } + } else { + const selectors = []; + rule.selectors.forEach((selector) => { + if (!regexp.test(selector)) { + selectors.push(selector); + } + }); + if (selectors.length) { + const cloned = rule.clone(); + cloned.selectors = selectors; + rule.replaceWith(cloned); + } else { + rule.remove(); + } + } + } + }); + }, + }; +}; + +module.exports.postcss = true; diff --git a/package.json b/package.json new file mode 100644 index 0000000..813ee6f --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "@advdominion/postcss-remove-bem-blocks", + "version": "1.0.0", + "main": "index.js", + "repository": { + "type": "git", + "url": "https://bitbucket.org/advdominion/postcss-remove-bem-blocks" + }, + "license": "MIT", + "peerDependencies": { + "postcss": "^8.2.8" + }, + "devDependencies": { + "postcss": "^8.2.8" + }, + "prettier": { + "printWidth": 120, + "tabWidth": 4, + "useTabs": true, + "semi": true, + "singleQuote": true, + "quoteProps": "as-needed", + "trailingComma": "es5", + "bracketSpacing": false, + "arrowParens": "always", + "proseWrap": "preserve", + "htmlWhitespaceSensitivity": "css", + "vueIndentScriptAndStyle": false, + "endOfLine": "lf" + } +} diff --git a/test/from.css b/test/from.css new file mode 100644 index 0000000..a402691 --- /dev/null +++ b/test/from.css @@ -0,0 +1,50 @@ +/* Удалить */ +.block-2 { +} + +/* Удалить */ +.block-2__element { +} + +/* Оставить */ +.block-1 { +} + +/* Оставить */ +.block-1__element { +} + +/* Оставить 1 */ +.block-1, +.block-2 { +} + +/* Удалить */ +.block-2, +.block-2__element { +} + +/* Удалить */ +.block-1 .block-2, +.block-2 .block-1 { +} + +/* Удалить */ +.block-1.block-2 { +} + +/* Удалить */ +.block-1 > .block-2 { +} + +/* Удалить */ +.block-2 ~ .block-1 { +} + +/* Оставить */ +.block2 { +} + +/* Оставить */ +.block-22 { +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..72102b6 --- /dev/null +++ b/test/index.js @@ -0,0 +1,26 @@ +const fs = require('fs'); +const path = require('path'); +const util = require('util'); +const readFile = util.promisify(fs.readFile); +const writeFile = util.promisify(fs.writeFile); +const postcss = require('postcss'); + +const plugin = require('./../'); + +readFile(path.join(__dirname, 'from.css'), { + encoding: 'utf-8', +}) + .then((css) => { + return postcss([ + plugin({ + blocks: ['.block-2'], + }), + ]).process(css, { + from: path.join(__dirname, 'from.css'), + }); + }) + .then(({css}) => { + writeFile(path.join(__dirname, 'to.css'), css, { + encoding: 'utf-8', + }); + }); diff --git a/test/to.css b/test/to.css new file mode 100644 index 0000000..b892072 --- /dev/null +++ b/test/to.css @@ -0,0 +1,33 @@ +/* Удалить */ + +/* Удалить */ + +/* Оставить */ +.block-1 { +} + +/* Оставить */ +.block-1__element { +} + +/* Оставить 1 */ +.block-1 { +} + +/* Удалить */ + +/* Удалить */ + +/* Удалить */ + +/* Удалить */ + +/* Удалить */ + +/* Оставить */ +.block2 { +} + +/* Оставить */ +.block-22 { +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..580ca28 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,27 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +nanocolors@^0.2.2: + version "0.2.12" + resolved "https://registry.yarnpkg.com/nanocolors/-/nanocolors-0.2.12.tgz#4d05932e70116078673ea4cc6699a1c56cc77777" + integrity sha512-SFNdALvzW+rVlzqexid6epYdt8H9Zol7xDoQarioEFcFN0JHo4CYNztAxmtfgGTVRCmFlEOqqhBpoFGKqSAMug== + +nanoid@^3.1.25: + version "3.1.28" + resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.28.tgz#3c01bac14cb6c5680569014cc65a2f26424c6bd4" + integrity sha512-gSu9VZ2HtmoKYe/lmyPFES5nknFrHa+/DT9muUFWFMi6Jh9E1I7bkvlQ8xxf1Kos9pi9o8lBnIOkatMhKX/YUw== + +postcss@^8.2.8: + version "8.3.8" + resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.3.8.tgz#9ebe2a127396b4b4570ae9f7770e7fb83db2bac1" + integrity sha512-GT5bTjjZnwDifajzczOC+r3FI3Cu+PgPvrsjhQdRqa2kTJ4968/X9CUce9xttIB0xOs5c6xf0TCWZo/y9lF6bA== + dependencies: + nanocolors "^0.2.2" + nanoid "^3.1.25" + source-map-js "^0.6.2" + +source-map-js@^0.6.2: + version "0.6.2" + resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-0.6.2.tgz#0bb5de631b41cfbda6cfba8bd05a80efdfd2385e" + integrity sha512-/3GptzWzu0+0MBQFrDKzw/DvvMTUORvgY6k6jd/VS6iCR4RDTKWH6v6WPwQoUO8667uQEf9Oe38DxAYWY5F/Ug==