Skip to content

Commit

Permalink
Merge branch 'master' into mbrandau-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrandau committed Jul 20, 2021
2 parents 45e5a53 + 6c7f2df commit 6ad769c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -35,7 +35,7 @@ postcss([ classNameShortener({
// Setting the callback option is mandatory
callback: map => {
console.log(JSON.stringify(map));

// You can return a promise
return new Promise(((resolve, reject) => {
fs.writeFile('map.json', JSON.stringify(map), err => {
Expand All @@ -44,6 +44,8 @@ postcss([ classNameShortener({
});
}))
}
// Optionally disable shorting of class names in dev environment
disable: process.env.NODE_ENV === 'development'
}) ])
```

Expand All @@ -54,5 +56,5 @@ The `map` object will look like this:
}
```

[css-shortener](https://github.com/mbrandau/css-shortener) lets you import the `map` object and replace the classes in HTML code.
[css-shortener](https://github.com/mbrandau/css-shortener) lets you import the `map` object and replace the classes in HTML code.
See [PostCSS] docs for examples for your environment.
19 changes: 15 additions & 4 deletions index.js
Expand Up @@ -12,20 +12,31 @@ module.exports = postcss.plugin(
'A callback is required to return the mapped class names'
);

// If the disable flag is false, skip shortening of class names
// and return empty map.
if (typeof opts.disable === 'boolean' && opts.disable === true) {
return () => {
// Return empty map
if (opts.callback) return opts.callback({});
return undefined;
};
}

const shortener = opts.cssShortener || new CssShortener();
const processor = parser(selectors => {
selectors.walkClasses(classNode => {
const processor = parser((selectors) => {
selectors.walkClasses((classNode) => {
classNode.value = shortener.getNewClassName(classNode.value);
});
});

return function (root) {
root.walkRules(ruleNode => {
root.walkRules((ruleNode) => {
return processor.process(ruleNode);
});

// Return mapped class names to callback if present
if (opts.callback) return opts.callback(shortener.getMap());
return undefined;
};
});
}
);
38 changes: 22 additions & 16 deletions index.test.js
Expand Up @@ -2,15 +2,16 @@ const postcss = require('postcss'),
CssShortener = require('css-shortener'),
plugin = require('./');

function callback(expectedMap) {
function buildExpectMapCallback(expectedMap) {
return function (map) {
expect(map).toEqual(expectedMap);
};
}

function run(input, expectedOutput, options) {
return postcss([plugin(options)]).process(input)
.then(result => {
return postcss([plugin(options)])
.process(input)
.then((result) => {
expect(result.css).toEqual(expectedOutput);
expect(result.warnings().length).toBe(0);
return result;
Expand All @@ -19,22 +20,27 @@ function run(input, expectedOutput, options) {

describe('tests', () => {
it('replaces class name', () => {
return run('.test{ }', '.a{ }', { callback: callback({ test: 'a' }) });
return run('.test{ }', '.a{ }', {
callback: buildExpectMapCallback({ test: 'a' })
});
});
it('does nothing when disabled', () => {
return run('.test{ }', '.test{ }', {
disable: true,
callback: buildExpectMapCallback({})
});
});
it('throws error when callback is not present', () => {
expect(
() => postcss([plugin()]).process('.test{}')
).toThrowError(
expect(() => postcss([plugin()]).process('.test{}')).toThrowError(
/^A callback is required to return the mapped class names$/
);
});
it('uses custom CssShortener',
() => {
const shortener = new CssShortener();
return run('.test{ }', '.a{ }', { cssShortener: shortener })
.then(() => {
expect(shortener.getMap()).toEqual({ test: 'a' });
});
}
);
it('uses custom CssShortener', () => {
const shortener = new CssShortener();
return run('.test{ }', '.a{ }', { cssShortener: shortener }).then(
() => {
expect(shortener.getMap()).toEqual({ test: 'a' });
}
);
});
});

0 comments on commit 6ad769c

Please sign in to comment.