index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _postcss = require('postcss');
  6. var _postcss2 = _interopRequireDefault(_postcss);
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. let charset = 'charset';
  9. exports.default = _postcss2.default.plugin('postcss-normalize-' + charset, (opts = {}) => {
  10. return css => {
  11. let charsetRule;
  12. let nonAsciiNode;
  13. let nonAscii = /[^\x00-\x7F]/;
  14. css.walk(node => {
  15. if (node.type === 'atrule' && node.name === charset) {
  16. if (!charsetRule) {
  17. charsetRule = node;
  18. }
  19. node.remove();
  20. } else if (!nonAsciiNode && node.parent === css && nonAscii.test(node)) {
  21. nonAsciiNode = node;
  22. }
  23. });
  24. if (nonAsciiNode) {
  25. if (!charsetRule && opts.add !== false) {
  26. charsetRule = _postcss2.default.atRule({
  27. name: charset,
  28. params: '"utf-8"'
  29. });
  30. }
  31. if (charsetRule) {
  32. charsetRule.source = nonAsciiNode.source;
  33. css.prepend(charsetRule);
  34. }
  35. }
  36. };
  37. });
  38. module.exports = exports['default'];