Welcome to mirror list, hosted at ThFree Co, Russian Federation.

getPostcssResult.js « lib « stylelint « node_modules « assets - github.com/fourtyone11/origin-hugo-theme.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b6f6d6d7eb73bb252898643ce665d3ee6bce897c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';

const dynamicRequire = require('./dynamicRequire');
const fs = require('fs');
const LazyResult = require('postcss/lib/lazy-result');
const postcss = require('postcss');
/** @type {import('postcss-syntax')} */
let autoSyntax = null;

/** @typedef {import('stylelint').StylelintInternalApi} StylelintInternalApi */
/** @typedef {{parse: any, stringify: any}} Syntax */

const postcssProcessor = postcss();

/**
 * @param {StylelintInternalApi} stylelint
 * @param {import('stylelint').GetPostcssOptions} options
 *
 * @returns {Promise<import('postcss').Result>}
 */
module.exports = function(stylelint, options = {}) {
	const cached = options.filePath ? stylelint._postcssResultCache.get(options.filePath) : undefined;

	if (cached) return Promise.resolve(cached);

	/** @type {Promise<string> | undefined} */
	let getCode;

	if (options.code !== undefined) {
		getCode = Promise.resolve(options.code);
	} else if (options.filePath) {
		getCode = readFile(options.filePath);
	}

	if (!getCode) {
		throw new Error('code or filePath required');
	}

	return getCode
		.then((code) => {
			const customSyntax = stylelint._options.customSyntax;
			/** @type {Syntax | null} */
			let syntax = null;

			if (customSyntax) {
				try {
					// TODO TYPES determine which type has customSyntax
					const useCustomSyntax = /** @type {any} */ dynamicRequire(customSyntax);

					/*
					 * PostCSS allows for syntaxes that only contain a parser, however,
					 * it then expects the syntax to be set as the `parser` option rather than `syntax`.
					 */
					if (!useCustomSyntax.parse) {
						syntax = {
							parse: useCustomSyntax,
							stringify: postcss.stringify,
						};
					} else {
						syntax = useCustomSyntax;
					}
				} catch (e) {
					throw new Error(`Cannot resolve custom syntax module ${customSyntax}`);
				}
			} else if (stylelint._options.syntax === 'css') {
				syntax = cssSyntax(stylelint);
			} else if (stylelint._options.syntax) {
				/** @type {{[k: string]: string}} */
				const syntaxes = {
					'css-in-js': 'postcss-jsx',
					html: 'postcss-html',
					less: 'postcss-less',
					markdown: 'postcss-markdown',
					sass: 'postcss-sass',
					scss: 'postcss-scss',
					sugarss: 'sugarss',
				};

				const syntaxFromOptions = syntaxes[stylelint._options.syntax];

				if (!syntaxFromOptions) {
					throw new Error(
						'You must use a valid syntax option, either: css, css-in-js, html, less, markdown, sass, scss, or sugarss',
					);
				}

				syntax = dynamicRequire(syntaxFromOptions);
			} else if (
				!(options.codeProcessors && options.codeProcessors.length) ||
				(options.filePath && /\.(scss|sass|less)$/.test(options.filePath))
			) {
				if (!autoSyntax) {
					autoSyntax = require('postcss-syntax');
				}

				syntax = autoSyntax({
					css: cssSyntax(stylelint),
				});
			}

			const postcssOptions = {
				from: options.filePath,
				syntax,
			};

			const source = options.code ? options.codeFilename : options.filePath;
			let preProcessedCode = code;

			if (options.codeProcessors && options.codeProcessors.length) {
				if (stylelint._options.fix) {
					// eslint-disable-next-line no-console
					console.warn(
						'Autofix is incompatible with processors and will be disabled. Are you sure you need a processor?',
					);
					stylelint._options.fix = false;
				}

				options.codeProcessors.forEach((codeProcessor) => {
					preProcessedCode = codeProcessor(preProcessedCode, source);
				});
			}

			const result = new LazyResult(postcssProcessor, preProcessedCode, postcssOptions);

			return result;
		})
		.then((postcssResult) => {
			if (options.filePath) {
				stylelint._postcssResultCache.set(options.filePath, postcssResult);
			}

			return postcssResult;
		});
};

/**
 * @param {string} filePath
 * @returns {Promise<string>}
 */
function readFile(filePath) {
	return new Promise((resolve, reject) => {
		fs.readFile(filePath, 'utf8', (err, content) => {
			if (err) {
				return reject(err);
			}

			resolve(content);
		});
	});
}

/**
 * @param {StylelintInternalApi} stylelint
 * @returns {Syntax}
 */
function cssSyntax(stylelint) {
	return {
		parse: stylelint._options.fix ? dynamicRequire('postcss-safe-parser') : postcss.parse,
		stringify: postcss.stringify,
	};
}