index.cjs.js 7.2 KB

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