Skip to content

Commit

Permalink
[PostCSS] Support glob property in dir-dependency messages (#3404)
Browse files Browse the repository at this point in the history
* Support `glob` property in `dir-dependency` messages

* Add missing semi-colon

* Use `||` instead of `??` for better compatibility
  • Loading branch information
bradlc committed Jun 2, 2021
1 parent 0df0215 commit bc56243
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
2 changes: 2 additions & 0 deletions plugins/plugin-postcss/package.json
Expand Up @@ -13,6 +13,8 @@
"access": "public"
},
"dependencies": {
"minimatch": "^3.0.4",
"normalize-path": "^3.0.0",
"postcss-load-config": "^3.0.1",
"workerpool": "^6.1.2"
},
Expand Down
29 changes: 10 additions & 19 deletions plugins/plugin-postcss/plugin.js
Expand Up @@ -2,6 +2,8 @@

const path = require('path');
const workerpool = require('workerpool');
const minimatch = require('minimatch');
const normalizePath = require('normalize-path');

module.exports = function postcssPlugin(snowpackConfig, options) {
// options validation
Expand Down Expand Up @@ -48,16 +50,15 @@ module.exports = function postcssPlugin(snowpackConfig, options) {
});
const {css, map, messages} = JSON.parse(encodedResult);

const files = new Set();
const dirs = new Set();
const patterns = new Set();
for (const message of messages) {
if (message.type === 'dependency') {
files.add(message.file);
patterns.add(normalizePath(message.file));
} else if (message.type === 'dir-dependency') {
dirs.add(message.dir);
patterns.add(normalizePath(`${message.dir}/${message.glob || '**/*'}`));
}
}
dependencies.set(id, {files, dirs});
dependencies.set(id, patterns);

return {
code: css, // old API (keep)
Expand All @@ -66,20 +67,10 @@ module.exports = function postcssPlugin(snowpackConfig, options) {
};
},
onChange({filePath}) {
eachId: for (const [id, {files, dirs}] of dependencies) {
for (const file of files) {
if (file === filePath) {
this.markChanged(id);
continue eachId;
}
}
for (const dir of dirs) {
// https://stackoverflow.com/a/45242825
const relativePath = path.relative(dir, filePath);
const dirContainsFilePath =
relativePath && !relativePath.startsWith('..') && !path.isAbsolute(relativePath);

if (dirContainsFilePath) {
const normalizedFilePath = normalizePath(filePath);
eachId: for (const [id, patterns] of dependencies) {
for (const pattern of patterns) {
if (minimatch(normalizedFilePath, pattern)) {
this.markChanged(id);
continue eachId;
}
Expand Down

1 comment on commit bc56243

@vercel
Copy link

@vercel vercel bot commented on bc56243 Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.