Skip to content

Commit

Permalink
make PostCSS dependency messages more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke committed Dec 28, 2023
1 parent 3dbff7f commit fbb0cf3
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes to PostCSS Sass

### 5.1.1 (December 28, 2023)

- Make PostCSS dependency messages more consistent.

### 5.1.0 (December 28, 2023)

- Updated: `dart-sass` to 1.69.5 (minor)
Expand Down
19 changes: 11 additions & 8 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,13 @@ const plugin = (opts = {}) => {
const doneWrap = importerResult => {
const file = importerResult && importerResult.file;
if (file) {
const parent = path.resolve(parentId);
const parent = path.resolve(parentId.replace(/#sass$/, ''));

// push the dependency to watch tasks
result.messages.push({
type: 'dependency',
file,
plugin: 'postcss-sass',
file: file,
parent
});
}
Expand Down Expand Up @@ -219,12 +220,14 @@ const plugin = (opts = {}) => {
}

// push the dependency to watch tasks
result.messages.push({
type: 'dependency',
plugin: 'postcss-sass',
file,
parent
});
if (file) {
result.messages.push({
type: 'dependency',
plugin: 'postcss-sass',
file: file,
parent: parent
});
}
}
return mergeSourceMaps(postMap.toJSON(), JSON.parse(sassMap)).then(prev => {
// update root to post-node-sass ast
Expand Down
19 changes: 11 additions & 8 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,13 @@ const plugin = (opts = {}) => {
const doneWrap = importerResult => {
const file = importerResult && importerResult.file;
if (file) {
const parent = resolve(parentId);
const parent = resolve(parentId.replace(/#sass$/, ''));

// push the dependency to watch tasks
result.messages.push({
type: 'dependency',
file,
plugin: 'postcss-sass',
file: file,
parent
});
}
Expand Down Expand Up @@ -200,12 +201,14 @@ const plugin = (opts = {}) => {
}

// push the dependency to watch tasks
result.messages.push({
type: 'dependency',
plugin: 'postcss-sass',
file,
parent
});
if (file) {
result.messages.push({
type: 'dependency',
plugin: 'postcss-sass',
file: file,
parent: parent
});
}
}
return mergeSourceMaps(postMap.toJSON(), JSON.parse(sassMap)).then(prev => {
// update root to post-node-sass ast
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@csstools/postcss-sass",
"version": "5.1.0",
"version": "5.1.1",
"description": "Use Sass as a PostCSS plugin",
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
"license": "CC0-1.0",
Expand Down
19 changes: 11 additions & 8 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ const plugin = (opts = {}) => {
const file = importerResult && importerResult.file;

if (file) {
const parent = pathResolve(parentId);
const parent = pathResolve(parentId.replace(/#sass$/, ''));

// push the dependency to watch tasks
result.messages.push({
type: 'dependency',
file,
plugin: 'postcss-sass',
file: file,
parent,
});
}
Expand Down Expand Up @@ -129,12 +130,14 @@ const plugin = (opts = {}) => {
}

// push the dependency to watch tasks
result.messages.push({
type: 'dependency',
plugin: 'postcss-sass',
file,
parent,
});
if (file) {
result.messages.push({
type: 'dependency',
plugin: 'postcss-sass',
file: file,
parent: parent,
});
}
}

return mergeSourceMaps(postMap.toJSON(), JSON.parse(sassMap)).then(
Expand Down
83 changes: 83 additions & 0 deletions test/_messages.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import plugin from '@csstools/postcss-sass';
import postcss from 'postcss';
import test from 'node:test';
import assert from 'node:assert/strict';

test('emits dependency messages', async () => {
const source = '@import "components/colors";';

const result = await postcss([plugin]).process(source, {
from: 'test/basic.scss',
});

assert.deepStrictEqual(result.warnings(), []);
assert.deepStrictEqual(
result.messages.map((x) => {
x.file = x.file.split('test')[1];
x.parent = x.parent.split('test')[1];

return x;
}),
[
{
type: 'dependency',
plugin: 'postcss-sass',
file: '/components/colors.scss',
parent: '/basic.scss',
},
],
);
});

test('emits dependency messages with custom importer', async () => {
const source = `
@import "custom:colors";
@import "components/fonts";
@import "components/page";
`;

const result = await postcss([plugin({
importer: (id, parentId, done) => {
if (id === 'custom:colors') {
done({ file: 'test/components/colors.scss', contents: '$primary-color: #333;' });
}
},
})]).process(source, {
from: 'test/basic.scss',
});

assert.deepStrictEqual(result.warnings(), []);
assert.deepStrictEqual(
result.messages.map((x) => {
x.file = x.file.split('test')[1];
x.parent = x.parent.split('test')[1];

return x;
}),
[
{
type: 'dependency',
plugin: 'postcss-sass',
file: '/components/colors.scss',
parent: '/basic.scss',
},
{
type: 'dependency',
plugin: 'postcss-sass',
file: '/components/colors.scss',
parent: '/basic.scss',
},
{
type: 'dependency',
plugin: 'postcss-sass',
file: '/components/fonts.scss',
parent: '/basic.scss',
},
{
type: 'dependency',
plugin: 'postcss-sass',
file: '/components/page.scss',
parent: '/basic.scss',
}],
);
});

0 comments on commit fbb0cf3

Please sign in to comment.