From c06f7ee0a40255214c477e13d9b956a7e07c2900 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 14 Oct 2023 15:05:55 +0800 Subject: postcss,(plugins): nanocss, postcss-preset-env --- .../postcss-normalize-charset/src/index.js | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 node_modules/postcss-normalize-charset/src/index.js (limited to 'node_modules/postcss-normalize-charset/src') diff --git a/node_modules/postcss-normalize-charset/src/index.js b/node_modules/postcss-normalize-charset/src/index.js new file mode 100644 index 00000000..eb8cde80 --- /dev/null +++ b/node_modules/postcss-normalize-charset/src/index.js @@ -0,0 +1,56 @@ +'use strict'; +const charset = 'charset'; +// eslint-disable-next-line no-control-regex +const nonAscii = /[^\x00-\x7F]/; + +/** + * @typedef {{add?: boolean}} Options + */ +/** + * @type {import('postcss').PluginCreator} + * @param {Options} opts + * @return {import('postcss').Plugin} + */ +function pluginCreator(opts = {}) { + return { + postcssPlugin: 'postcss-normalize-' + charset, + + OnceExit(css, { AtRule }) { + /** @type {import('postcss').AtRule | undefined} */ + let charsetRule; + /** @type {import('postcss').Node | undefined} */ + let nonAsciiNode; + + css.walk((node) => { + if (node.type === 'atrule' && node.name === charset) { + if (!charsetRule) { + charsetRule = node; + } + node.remove(); + } else if ( + !nonAsciiNode && + node.parent === css && + nonAscii.test(node.toString()) + ) { + nonAsciiNode = node; + } + }); + + if (nonAsciiNode) { + if (!charsetRule && opts.add !== false) { + charsetRule = new AtRule({ + name: charset, + params: '"utf-8"', + }); + } + if (charsetRule) { + charsetRule.source = nonAsciiNode.source; + css.prepend(charsetRule); + } + } + }, + }; +} + +pluginCreator.postcss = true; +module.exports = pluginCreator; -- cgit v1.2.3