source: frontend/node_modules/postcss-normalize/index.mjs

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[9af201e]1import postcssBrowserComments from 'postcss-browser-comments';
2import Module from 'module';
3import path from 'path';
4import { URL } from 'url';
5import fs from 'fs';
6import postcss from 'postcss';
7
8const assign = (...objects) => Object.assign(...objects);
9const create = (...objects) => assign(Object.create(null), ...objects);
10
11const currentURL = import.meta.url;
12const currentFilename = new URL(currentURL).pathname;
13const currentDirname = path.dirname(currentFilename); // get resolved filenames for normalize.css
14
15const normalizeCSS = resolve('@csstools/normalize.css');
16const normalizeDir = path.dirname(normalizeCSS);
17const normalizeOpinionatedCSS = path.join(normalizeDir, 'opinionated.css'); // get resolved filenames for sanitize.css
18
19const sanitizeCSS = resolve('sanitize.css');
20const sanitizeDir = path.dirname(sanitizeCSS);
21const sanitizeAssetsCSS = path.join(sanitizeDir, 'assets.css');
22const sanitizeFormsCSS = path.join(sanitizeDir, 'forms.css');
23const sanitizeReduceMotionCSS = path.join(sanitizeDir, 'reduce-motion.css');
24const sanitizeTypographyCSS = path.join(sanitizeDir, 'typography.css');
25const sanitizeSystemUiCSS = path.join(sanitizeDir, 'system-ui.css');
26const sanitizeUiMonospace = path.join(sanitizeDir, 'ui-monospace.css'); // export a hashmap of css library filenames
27
28const parsableFilenames = create({
29 [normalizeCSS]: true,
30 [normalizeOpinionatedCSS]: true,
31 [sanitizeCSS]: true,
32 [sanitizeAssetsCSS]: true,
33 [sanitizeFormsCSS]: true,
34 [sanitizeReduceMotionCSS]: true,
35 [sanitizeTypographyCSS]: true,
36 [sanitizeSystemUiCSS]: true,
37 [sanitizeUiMonospace]: true
38}); // export a hashmap of css library filenames by id
39
40const resolvedFilenamesById = create({
41 'normalize': [normalizeCSS],
42 'normalize/opinionated': [normalizeOpinionatedCSS],
43 'normalize/*': [normalizeOpinionatedCSS],
44 'sanitize': [sanitizeCSS],
45 'sanitize/assets': [sanitizeAssetsCSS],
46 'sanitize/forms': [sanitizeCSS, sanitizeFormsCSS],
47 'sanitize/page': [sanitizeAssetsCSS],
48 // deprecated; remaining for v10.0.0 compatibility
49 'sanitize/reduce-motion': [sanitizeCSS, sanitizeReduceMotionCSS],
50 'sanitize/system-ui': [sanitizeCSS, sanitizeSystemUiCSS],
51 'sanitize/typography': [sanitizeCSS, sanitizeTypographyCSS],
52 'sanitize/ui-monospace': [sanitizeCSS, sanitizeUiMonospace],
53 'sanitize/*': [sanitizeCSS, sanitizeFormsCSS]
54}); // get the resolved filename of a package/module
55
56function resolve(id) {
57 return resolve[id] = resolve[id] || Module._resolveFilename(id, {
58 id: currentFilename,
59 filename: currentFilename,
60 paths: Module._nodeModulePaths(currentDirname)
61 });
62}
63
64const cache$1 = create();
65async function readFile(filename) {
66 filename = path.resolve(filename);
67 cache$1[filename] = cache$1[filename] || create();
68 return new Promise((resolve, reject) => fs.stat(filename, (statsError, {
69 mtime
70 }) => statsError ? reject(statsError) : mtime === cache$1[filename].mtime ? resolve(cache$1[filename].data) : fs.readFile(filename, 'utf8', (readFileError, data) => readFileError ? reject(readFileError) : resolve((cache$1[filename] = {
71 data,
72 mtime
73 }).data))));
74}
75
76const cache = create(null);
77var parse = ((filename, transformer) => readFile(filename).then( // cache the parsed css root
78css => cache[css] = cache[css] || postcss.parse(css, {
79 from: filename
80})).then( // clone the cached root
81root => root.clone()).then( // transform the cloned root
82clone => Promise.resolve(transformer(clone)).then( // resolve the cloned root
83() => clone)));
84
85var postcssImportNormalize = (commentsTransformer => opts => {
86 opts = create(opts); // return an postcss-import configuration
87
88 return create({
89 load(filename, importOptions) {
90 return filename in parsableFilenames // parse the file (the file and css are conservatively cached)
91 ? parse(filename, commentsTransformer).then(root => root.toResult({
92 to: filename,
93 map: true
94 }).css) : typeof opts.load === 'function' // otherwise, use the override loader
95 ? opts.load.call(null, filename, importOptions) // otherwise, return the (conservatively cached) contents of the file
96 : readFile(filename);
97 },
98
99 resolve(id, basedir, importOptions) {
100 // get the css id by removing css extensions
101 const cssId = id.replace(cssExtRegExp$1, '');
102 return cssId in resolvedFilenamesById // return the known resolved path for the css id
103 ? resolvedFilenamesById[cssId] : typeof opts.resolve === 'function' // otherwise, use the override resolver
104 ? opts.resolve.call(null, id, basedir, importOptions) // otherwise, return the id to be resolved by postcss-import
105 : id;
106 }
107
108 });
109});
110const cssExtRegExp$1 = /\.css\b/g;
111
112const postcssPlugin = (commentsTransformer, opts) => root => {
113 const promises = [];
114 const insertedFilenames = {}; // use @import insertion point
115
116 root.walkAtRules(importRegExp, atrule => {
117 // get name as a fallback value for the library (e.g. @import-normalize is like @import "normalize.css")
118 const name = atrule.name.match(importRegExp)[1]; // get url from "library", 'library', url("library"), url('library'), or the fallback value
119
120 const url = (atrule.params.match(paramsRegExp) || []).slice(1).find(part => part) || name;
121
122 if (url) {
123 // get the css id by removing css extensions
124 const cssId = url.replace(cssExtRegExp, '');
125
126 if (cssId in resolvedFilenamesById) {
127 // promise the library import is replaced with its contents
128 promises.push(Promise.all(resolvedFilenamesById[cssId].filter( // ignore filenames that have already been inserted
129 filename => insertedFilenames[filename] = opts.allowDuplicates || !(filename in insertedFilenames)).map( // parse the file (the file and css are conservatively cached)
130 filename => parse(filename, commentsTransformer))).then(roots => {
131 if (roots.length) {
132 // combine all the library nodes returned by the parsed files
133 const nodes = roots.reduce((all, root) => all.concat(root.nodes), []); // replace the import with all the library nodes
134
135 atrule.replaceWith(...nodes);
136 }
137 }));
138 }
139 }
140 });
141 return Promise.all([].concat( // promise the library imports are replaced with their contents
142 promises, // promise certain libraries are prepended
143 Promise.all([].concat(opts.forceImport || []).reduce( // filter the id to be a known id or boolean true
144 (all, id) => {
145 if (id === true) {
146 all.push(...resolvedFilenamesById.normalize);
147 } else if (typeof id === 'string') {
148 const cssId = id.replace(cssExtRegExp, '');
149
150 if (cssId in resolvedFilenamesById) {
151 all.push(...resolvedFilenamesById[cssId]);
152 }
153 }
154
155 return all;
156 }, []).filter( // ignore filenames that have already been inserted
157 filename => insertedFilenames[filename] = opts.allowDuplicates || !(filename in insertedFilenames)).map( // parse the file (the file and css are conservatively cached)
158 filename => parse(filename, commentsTransformer))).then(roots => {
159 if (roots.length) {
160 // combine all the library nodes returned by the parsed files
161 const nodes = roots.reduce((all, root) => all.concat(root.nodes), []); // prepend the stylesheet with all the library nodes
162
163 root.prepend(...nodes);
164 }
165 })));
166};
167
168const cssExtRegExp = /\.css\b/g;
169const importRegExp = /^import(?:-(normalize|sanitize))?$/;
170const paramsRegExp = /^\s*(?:url\((?:"(.+)"|'(.+)')\)|"(.+)"|'(.+)')[\W\w]*$/;
171
172const plugin = opts => {
173 opts = create(opts);
174 const commentsTransformer = postcssBrowserComments(opts).Once;
175 const normalizeTransformer = postcssPlugin(commentsTransformer, opts);
176 const postcssImportConfig = postcssImportNormalize(commentsTransformer);
177 return {
178 postcssPlugin: 'postcss-normalize',
179
180 Once(root) {
181 return normalizeTransformer(root);
182 },
183
184 postcssImport: postcssImportConfig
185 };
186};
187
188plugin.postcss = true;
189
190export { plugin as default };
Note: See TracBrowser for help on using the repository browser.