commit 2d7a5738115a1eb959545ec7e614e0c1028b6d5d Author: Sven Slootweg Date: Wed Feb 17 14:46:17 2021 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..c2455a9 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# postcss-icss-find-imports + +A PostCSS plugin for finding import statements in CSS that follows the ICSS specification, __but not removing them from the AST__, eg. for implementing dependency discovery. + +This code was adapted from [css-loader](https://github.com/webpack-contrib/css-loader) and modified to work as a stand-alone plugin. + +This documentation is rudimentary and will probably be improved at some point. Feel free to open an issue if something is unclear! + +## License + +``` +This module is derived from: css-loader (https://github.com/webpack-contrib/css-loader) + (c) JS Foundation and other contributors + +Modifications by: + (c) 2019, Sven Slootweg + +Licensed under: + MIT (https://opensource.org/licenses/MIT) +``` + +## API + +Exports a plugin that is compatible with PostCSS 8. + +Takes no options upon initialization. + +When applied, it will find import statements in the code, exposing them as the following types of `messages` (from plugin `postcss-icss-parser`): + +- `import`: an import statement. + - `url`: The path/URL being imported from. + +The import statements __are not removed__ from the AST. diff --git a/index.js b/index.js new file mode 100644 index 0000000..85cb67f --- /dev/null +++ b/index.js @@ -0,0 +1,42 @@ +"use strict"; + +/* +This module is derived from: css-loader (https://github.com/webpack-contrib/css-loader) + (c) JS Foundation and other contributors + +Modifications by: + (c) 2019, Sven Slootweg + +Licensed under: + MIT (https://opensource.org/licenses/MIT) +*/ + +const icssUtils = require("icss-utils"); +const loaderUtils = require("loader-utils"); + +const pluginName = 'postcss-icss-find-imports'; + +module.exports = function (_options = {}) { + return { + postcssPlugin: pluginName, + Once: (root, { result }) => { + let discoveredImports = new Set(); + + let { icssImports } = icssUtils.extractICSS(root, false); + + for (let importUrl of Object.keys(icssImports)) { + discoveredImports.add(loaderUtils.parseString(importUrl)); + } + + for (let url of discoveredImports) { + result.messages.push({ + pluginName: pluginName, + type: "import", + url: url + }); + } + } + }; +}; + +module.exports.postcss = true; diff --git a/package.json b/package.json new file mode 100644 index 0000000..a911c7d --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "postcss-icss-find-imports", + "version": "1.0.0", + "main": "index.js", + "repository": "https://git.cryto.net/joepie91/postcss-icss-find-imports.git", + "author": "Sven Slootweg ", + "license": "MIT", + "dependencies": { + "icss-utils": "^5.1.0", + "loader-utils": "^2.0.0" + }, + "peerDependencies": { + "postcss": "^8.2.6" + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..43d876e --- /dev/null +++ b/yarn.lock @@ -0,0 +1,39 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +big.js@^5.2.2: + version "5.2.2" + resolved "https://registry.yarnpkg.com/big.js/-/big.js-5.2.2.tgz#65f0af382f578bcdc742bd9c281e9cb2d7768328" + integrity sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ== + +emojis-list@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-3.0.0.tgz#5570662046ad29e2e916e71aae260abdff4f6a78" + integrity sha512-/kyM18EfinwXZbno9FyUGeFh87KC8HRQBQGildHZbEuRyWFOmv1U10o9BBp8XVZDVNNuQKyIGIu5ZYAAXJ0V2Q== + +icss-utils@^5.1.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/icss-utils/-/icss-utils-5.1.0.tgz#c6be6858abd013d768e98366ae47e25d5887b1ae" + integrity sha512-soFhflCVWLfRNOPU3iv5Z9VUdT44xFRbzjLsEzSr5AQmgqPMTHdU3PMT1Cf1ssx8fLNJDA1juftYl+PUcv3MqA== + +json5@^2.1.2: + version "2.2.0" + resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.0.tgz#2dfefe720c6ba525d9ebd909950f0515316c89a3" + integrity sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA== + dependencies: + minimist "^1.2.5" + +loader-utils@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-2.0.0.tgz#e4cace5b816d425a166b5f097e10cd12b36064b0" + integrity sha512-rP4F0h2RaWSvPEkD7BLDFQnvSf+nK+wr3ESUjNTyAGobqrijmW92zc+SO6d4p4B1wh7+B/Jg1mkQe5NYUEHtHQ== + dependencies: + big.js "^5.2.2" + emojis-list "^3.0.0" + json5 "^2.1.2" + +minimist@^1.2.5: + version "1.2.5" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602" + integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==