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

Add support for PostCSS dependency messages #3309

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
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
39 changes: 37 additions & 2 deletions plugins/plugin-postcss/plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const {resolve} = require('path');
const {resolve, relative, isAbsolute} = require('path');
const workerpool = require('workerpool');

module.exports = function postcssPlugin(snowpackConfig, options) {
Expand All @@ -14,6 +14,8 @@ module.exports = function postcssPlugin(snowpackConfig, options) {

let worker, pool;

const dependencies = new Map();

return {
name: '@snowpack/postcss-transform',
async transform({id, fileExt, contents}) {
Expand Down Expand Up @@ -41,13 +43,46 @@ module.exports = function postcssPlugin(snowpackConfig, options) {
}
: false,
});
const {css, map} = JSON.parse(encodedResult);
const {css, map, messages} = JSON.parse(encodedResult);

const files = new Set();
const dirs = new Set();
for (const message of messages) {
if (message.type === 'dependency') {
files.add(message.file);
} else if (message.type === 'dir-dependency') {
dirs.add(message.dir);
}
}
dependencies.set(id, {files, dirs});

return {
code: css, // old API (keep)
contents: css, // new API
map,
};
},
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 = relative(dir, filePath);
const dirContainsFilePath =
relativePath && !relativePath.startsWith('..') && !isAbsolute(relativePath);

if (dirContainsFilePath) {
this.markChanged(id);
continue eachId;
}
}
}
},
cleanup() {
pool && pool.terminate();
},
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugin-postcss/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function transformAsync(css, {filepath, config, cwd, map}) {
}

const result = await process(css);
return JSON.stringify({css: result.css, map: result.map});
return JSON.stringify({css: result.css, map: result.map, messages: result.messages});
}

// create a worker and register public functions
Expand Down