| [9af201e] | 1 | import postcssBrowserComments from 'postcss-browser-comments';
|
|---|
| 2 | import Module from 'module';
|
|---|
| 3 | import path from 'path';
|
|---|
| 4 | import { URL } from 'url';
|
|---|
| 5 | import fs from 'fs';
|
|---|
| 6 | import postcss from 'postcss';
|
|---|
| 7 |
|
|---|
| 8 | const assign = (...objects) => Object.assign(...objects);
|
|---|
| 9 | const create = (...objects) => assign(Object.create(null), ...objects);
|
|---|
| 10 |
|
|---|
| 11 | const currentURL = import.meta.url;
|
|---|
| 12 | const currentFilename = new URL(currentURL).pathname;
|
|---|
| 13 | const currentDirname = path.dirname(currentFilename); // get resolved filenames for normalize.css
|
|---|
| 14 |
|
|---|
| 15 | const normalizeCSS = resolve('@csstools/normalize.css');
|
|---|
| 16 | const normalizeDir = path.dirname(normalizeCSS);
|
|---|
| 17 | const normalizeOpinionatedCSS = path.join(normalizeDir, 'opinionated.css'); // get resolved filenames for sanitize.css
|
|---|
| 18 |
|
|---|
| 19 | const sanitizeCSS = resolve('sanitize.css');
|
|---|
| 20 | const sanitizeDir = path.dirname(sanitizeCSS);
|
|---|
| 21 | const sanitizeAssetsCSS = path.join(sanitizeDir, 'assets.css');
|
|---|
| 22 | const sanitizeFormsCSS = path.join(sanitizeDir, 'forms.css');
|
|---|
| 23 | const sanitizeReduceMotionCSS = path.join(sanitizeDir, 'reduce-motion.css');
|
|---|
| 24 | const sanitizeTypographyCSS = path.join(sanitizeDir, 'typography.css');
|
|---|
| 25 | const sanitizeSystemUiCSS = path.join(sanitizeDir, 'system-ui.css');
|
|---|
| 26 | const sanitizeUiMonospace = path.join(sanitizeDir, 'ui-monospace.css'); // export a hashmap of css library filenames
|
|---|
| 27 |
|
|---|
| 28 | const 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 |
|
|---|
| 40 | const 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 |
|
|---|
| 56 | function 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 |
|
|---|
| 64 | const cache$1 = create();
|
|---|
| 65 | async 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 |
|
|---|
| 76 | const cache = create(null);
|
|---|
| 77 | var parse = ((filename, transformer) => readFile(filename).then( // cache the parsed css root
|
|---|
| 78 | css => cache[css] = cache[css] || postcss.parse(css, {
|
|---|
| 79 | from: filename
|
|---|
| 80 | })).then( // clone the cached root
|
|---|
| 81 | root => root.clone()).then( // transform the cloned root
|
|---|
| 82 | clone => Promise.resolve(transformer(clone)).then( // resolve the cloned root
|
|---|
| 83 | () => clone)));
|
|---|
| 84 |
|
|---|
| 85 | var 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 | });
|
|---|
| 110 | const cssExtRegExp$1 = /\.css\b/g;
|
|---|
| 111 |
|
|---|
| 112 | const 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 |
|
|---|
| 168 | const cssExtRegExp = /\.css\b/g;
|
|---|
| 169 | const importRegExp = /^import(?:-(normalize|sanitize))?$/;
|
|---|
| 170 | const paramsRegExp = /^\s*(?:url\((?:"(.+)"|'(.+)')\)|"(.+)"|'(.+)')[\W\w]*$/;
|
|---|
| 171 |
|
|---|
| 172 | const 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 |
|
|---|
| 188 | plugin.postcss = true;
|
|---|
| 189 |
|
|---|
| 190 | export { plugin as default };
|
|---|