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