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

Update plugin to PostCSS 8 and visitors API #95

Closed
wants to merge 6 commits into from
Closed
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
52 changes: 34 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,39 @@ module.exports = function postcssPrefixSelector(options) {
? [].concat(options.includeFiles)
: [];

return function (root) {
if (
ignoreFiles.length &&
root.source.input.file &&
isFileInArray(root.source.input.file, ignoreFiles)
) {
return;
}
if (
includeFiles.length &&
root.source.input.file &&
!isFileInArray(root.source.input.file, includeFiles)
) {
return;
}
return {
postcssPlugin: 'postcss-prefix-selector',
Root(root, { result }) {
const isFileIgnored =
ignoreFiles.length &&
root.source.input.file &&
isFileInArray(root.source.input.file, ignoreFiles);
const isFileNotIncluded =
includeFiles.length &&
root.source.input.file &&
!isFileInArray(root.source.input.file, includeFiles);

if (isFileIgnored || isFileNotIncluded) {
// TODO: 'skip' should be a symbol or constant
// NOTE: check how 'messages' works, is there some performance penalty?
result.messages.push('skip');
}
},
RootExit(root, { result }) {
// TODO: other plugins might be sending messages. Need to add/remove only our constant.
result.messages.pop();
},
Rule(rule, { result }) {
// NOTE: other plugins might be sending messages.
if (result.messages[0] === 'skip') {
return;
}

// NOTE: other plugins might be sending messages.
if (result.messages.length > 1) {
console.warn('more than one skip message found!');
}

root.walkRules((rule) => {
const keyframeRules = [
'keyframes',
'-webkit-keyframes',
Expand All @@ -44,13 +60,13 @@ module.exports = function postcssPrefixSelector(options) {
prefix,
selector,
prefixWithSpace + selector,
root.source.input.file
rule.source.input.file
);
}

return prefixWithSpace + selector;
});
});
},
};
};

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

16 changes: 8 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,16 @@ it('should prefix a selector. Use ".hello .world"', () => {
assert.equal(out, expected);
});

// it('should prefix postcss nested selectors', () => {
// const out = postcss()
// .use(postcssNested)
// .use(prefixer({ prefix: '.stuff' }))
// .process(getFixtureContents('nested-selectors.postcss')).css;
it('should prefix postcss nested selectors', () => {
const out = postcss()
.use(postcssNested)
.use(prefixer({ prefix: '.stuff' }))
.process(getFixtureContents('nested-selectors.postcss')).css;

// const expected = getFixtureContents('nested-selectors.expected.css');
const expected = getFixtureContents('nested-selectors.expected.css');

// assert.equal(out, expected);
// });
assert.equal(out, expected);
});

function getFixtureContents(name) {
return fs.readFileSync(`test/fixtures/${name}`, 'utf8').trim();
Expand Down