123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
- var postcss = _interopDefault(require('postcss'));
- var postcssBrowserComments = _interopDefault(require('postcss-browser-comments'));
- var Module = _interopDefault(require('module'));
- var path = _interopDefault(require('path'));
- var fs = _interopDefault(require('fs'));
- const assign = (...objects) => Object.assign(...objects);
- const create = (...objects) => assign(Object.create(null), ...objects);
- const currentFilename = __filename;
- const currentDirname = path.dirname(currentFilename);
- const normalizeCSS = resolve('@csstools/normalize.css');
- const normalizeOpinionatedCSS = resolve('@csstools/normalize.css/opinionated.css');
- const sanitizeCSS = resolve('sanitize.css');
- const sanitizeFormsCSS = resolve('sanitize.css/forms.css');
- const sanitizePageCSS = resolve('sanitize.css/page.css');
- const sanitizeTypographyCSS = resolve('sanitize.css/typography.css');
- const parsableFilenames = create({
- [normalizeCSS]: true,
- [normalizeOpinionatedCSS]: true,
- [sanitizeCSS]: true,
- [sanitizeFormsCSS]: true,
- [sanitizePageCSS]: true,
- [sanitizeTypographyCSS]: true
- });
- const resolvedFilenamesById = create({
- 'normalize': [normalizeCSS],
- 'normalize/opinionated': [normalizeOpinionatedCSS],
- 'normalize/*': [normalizeOpinionatedCSS],
- 'sanitize': [sanitizeCSS],
- 'sanitize/forms': [sanitizeCSS, sanitizeFormsCSS],
- 'sanitize/page': [sanitizeCSS, sanitizePageCSS],
- 'sanitize/typography': [sanitizeCSS, sanitizeTypographyCSS],
- 'sanitize/*': [sanitizeCSS, sanitizeFormsCSS, sanitizePageCSS, sanitizeTypographyCSS]
- });
- function resolve(id) {
- return resolve[id] = resolve[id] || Module._resolveFilename(id, {
- id: currentFilename,
- filename: currentFilename,
- paths: Module._nodeModulePaths(currentDirname)
- });
- }
- const cache = create();
- async function readFile(filename) {
- filename = path.resolve(filename);
- cache[filename] = cache[filename] || create();
- return new Promise((resolve, reject) => fs.stat(filename, (statsError, {
- mtime
- }) => statsError ? reject(statsError) : mtime === cache[filename].mtime ? resolve(cache[filename].data) : fs.readFile(filename, 'utf8', (readFileError, data) => readFileError ? reject(readFileError) : resolve((cache[filename] = {
- data,
- mtime
- }).data))));
- }
- const cache$1 = create(null);
- var parse = ((filename, transformer) => readFile(filename).then(
- css => cache$1[css] = cache$1[css] || postcss.parse(css, {
- from: filename
- })).then(
- root => root.clone()).then(
- clone => Promise.resolve(transformer(clone)).then(
- () => clone)));
- var postcssImportNormalize = (commentsTransformer => opts => {
- opts = create(opts);
- return create({
- load(filename, importOptions) {
- return filename in parsableFilenames
- ? parse(filename, commentsTransformer).then(root => root.toResult({
- to: filename,
- map: true
- }).css) : typeof opts.load === 'function'
- ? opts.load.call(null, filename, importOptions)
- : readFile(filename);
- },
- resolve(id, basedir, importOptions) {
-
- const cssId = id.replace(cssExtRegExp, '');
- return cssId in resolvedFilenamesById
- ? resolvedFilenamesById[cssId] : typeof opts.resolve === 'function'
- ? opts.resolve.call(null, id, basedir, importOptions)
- : id;
- }
- });
- });
- const cssExtRegExp = /\.css\b/g;
- const postcssPlugin = (commentsTransformer, opts) => root => {
- const promises = [];
- const insertedFilenames = {};
- root.walkAtRules(importRegExp, atrule => {
-
- const name = atrule.name.match(importRegExp)[1];
- const url = (atrule.params.match(paramsRegExp) || []).slice(1).find(part => part) || name;
- if (url) {
-
- const cssId = url.replace(cssExtRegExp$1, '');
- if (cssId in resolvedFilenamesById) {
-
- promises.push(Promise.all(resolvedFilenamesById[cssId].filter(
- filename => insertedFilenames[filename] = opts.allowDuplicates || !(filename in insertedFilenames)).map(
- filename => parse(filename, commentsTransformer))).then(roots => {
- if (roots.length) {
-
- const nodes = roots.reduce((all, root) => all.concat(root.nodes), []);
- atrule.replaceWith(...nodes);
- }
- }));
- }
- }
- });
- return Promise.all([].concat(
- promises,
- Promise.all([].concat(opts.forceImport || []).reduce(
- (all, id) => {
- if (id === true) {
- all.push(...resolvedFilenamesById.normalize);
- } else if (typeof id === 'string') {
- const cssId = id.replace(cssExtRegExp$1, '');
- if (cssId in resolvedFilenamesById) {
- all.push(...resolvedFilenamesById[cssId]);
- }
- }
- return all;
- }, []).filter(
- filename => insertedFilenames[filename] = opts.allowDuplicates || !(filename in insertedFilenames)).map(
- filename => parse(filename, commentsTransformer))).then(roots => {
- if (roots.length) {
-
- const nodes = roots.reduce((all, root) => all.concat(root.nodes), []);
- root.prepend(...nodes);
- }
- })));
- };
- const cssExtRegExp$1 = /\.css\b/g;
- const importRegExp = /^import(?:-(normalize|sanitize))?$/;
- const paramsRegExp = /^\s*(?:url\((?:"(.+)"|'(.+)')\)|"(.+)"|'(.+)')[\W\w]*$/;
- var index = postcss.plugin('postcss-normalize', opts => {
- opts = create(opts);
- const commentsTransformer = postcssBrowserComments(opts);
- const normalizeTransformer = postcssPlugin(commentsTransformer, opts);
- const postcssImportConfig = postcssImportNormalize(commentsTransformer);
- return assign(normalizeTransformer, {
- postcssImport: postcssImportConfig
- });
- });
- module.exports = index;
|