normalizeConfig.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "normalizeConfig", {
  6. enumerable: true,
  7. get: function() {
  8. return normalizeConfig;
  9. }
  10. });
  11. const _featureFlags = require("../featureFlags");
  12. const _log = /*#__PURE__*/ _interop_require_wildcard(require("./log"));
  13. function _getRequireWildcardCache(nodeInterop) {
  14. if (typeof WeakMap !== "function") return null;
  15. var cacheBabelInterop = new WeakMap();
  16. var cacheNodeInterop = new WeakMap();
  17. return (_getRequireWildcardCache = function(nodeInterop) {
  18. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  19. })(nodeInterop);
  20. }
  21. function _interop_require_wildcard(obj, nodeInterop) {
  22. if (!nodeInterop && obj && obj.__esModule) {
  23. return obj;
  24. }
  25. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  26. return {
  27. default: obj
  28. };
  29. }
  30. var cache = _getRequireWildcardCache(nodeInterop);
  31. if (cache && cache.has(obj)) {
  32. return cache.get(obj);
  33. }
  34. var newObj = {};
  35. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  36. for(var key in obj){
  37. if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
  38. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  39. if (desc && (desc.get || desc.set)) {
  40. Object.defineProperty(newObj, key, desc);
  41. } else {
  42. newObj[key] = obj[key];
  43. }
  44. }
  45. }
  46. newObj.default = obj;
  47. if (cache) {
  48. cache.set(obj, newObj);
  49. }
  50. return newObj;
  51. }
  52. function normalizeConfig(config) {
  53. // Quick structure validation
  54. /**
  55. * type FilePath = string
  56. * type RawFile = { raw: string, extension?: string }
  57. * type ExtractorFn = (content: string) => Array<string>
  58. * type TransformerFn = (content: string) => string
  59. *
  60. * type Content =
  61. * | Array<FilePath | RawFile>
  62. * | {
  63. * files: Array<FilePath | RawFile>,
  64. * extract?: ExtractorFn | { [extension: string]: ExtractorFn }
  65. * transform?: TransformerFn | { [extension: string]: TransformerFn }
  66. * }
  67. */ let valid = (()=>{
  68. // `config.purge` should not exist anymore
  69. if (config.purge) {
  70. return false;
  71. }
  72. // `config.content` should exist
  73. if (!config.content) {
  74. return false;
  75. }
  76. // `config.content` should be an object or an array
  77. if (!Array.isArray(config.content) && !(typeof config.content === "object" && config.content !== null)) {
  78. return false;
  79. }
  80. // When `config.content` is an array, it should consist of FilePaths or RawFiles
  81. if (Array.isArray(config.content)) {
  82. return config.content.every((path)=>{
  83. // `path` can be a string
  84. if (typeof path === "string") return true;
  85. // `path` can be an object { raw: string, extension?: string }
  86. // `raw` must be a string
  87. if (typeof (path === null || path === void 0 ? void 0 : path.raw) !== "string") return false;
  88. // `extension` (if provided) should also be a string
  89. if ((path === null || path === void 0 ? void 0 : path.extension) && typeof (path === null || path === void 0 ? void 0 : path.extension) !== "string") {
  90. return false;
  91. }
  92. return true;
  93. });
  94. }
  95. // When `config.content` is an object
  96. if (typeof config.content === "object" && config.content !== null) {
  97. // Only `files`, `relative`, `extract`, and `transform` can exist in `config.content`
  98. if (Object.keys(config.content).some((key)=>![
  99. "files",
  100. "relative",
  101. "extract",
  102. "transform"
  103. ].includes(key))) {
  104. return false;
  105. }
  106. // `config.content.files` should exist of FilePaths or RawFiles
  107. if (Array.isArray(config.content.files)) {
  108. if (!config.content.files.every((path)=>{
  109. // `path` can be a string
  110. if (typeof path === "string") return true;
  111. // `path` can be an object { raw: string, extension?: string }
  112. // `raw` must be a string
  113. if (typeof (path === null || path === void 0 ? void 0 : path.raw) !== "string") return false;
  114. // `extension` (if provided) should also be a string
  115. if ((path === null || path === void 0 ? void 0 : path.extension) && typeof (path === null || path === void 0 ? void 0 : path.extension) !== "string") {
  116. return false;
  117. }
  118. return true;
  119. })) {
  120. return false;
  121. }
  122. // `config.content.extract` is optional, and can be a Function or a Record<String, Function>
  123. if (typeof config.content.extract === "object") {
  124. for (let value of Object.values(config.content.extract)){
  125. if (typeof value !== "function") {
  126. return false;
  127. }
  128. }
  129. } else if (!(config.content.extract === undefined || typeof config.content.extract === "function")) {
  130. return false;
  131. }
  132. // `config.content.transform` is optional, and can be a Function or a Record<String, Function>
  133. if (typeof config.content.transform === "object") {
  134. for (let value of Object.values(config.content.transform)){
  135. if (typeof value !== "function") {
  136. return false;
  137. }
  138. }
  139. } else if (!(config.content.transform === undefined || typeof config.content.transform === "function")) {
  140. return false;
  141. }
  142. // `config.content.relative` is optional and can be a boolean
  143. if (typeof config.content.relative !== "boolean" && typeof config.content.relative !== "undefined") {
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149. return false;
  150. })();
  151. if (!valid) {
  152. _log.default.warn("purge-deprecation", [
  153. "The `purge`/`content` options have changed in Tailwind CSS v3.0.",
  154. "Update your configuration file to eliminate this warning.",
  155. "https://tailwindcss.com/docs/upgrade-guide#configure-content-sources"
  156. ]);
  157. }
  158. // Normalize the `safelist`
  159. config.safelist = (()=>{
  160. var _purge_options;
  161. let { content , purge , safelist } = config;
  162. if (Array.isArray(safelist)) return safelist;
  163. if (Array.isArray(content === null || content === void 0 ? void 0 : content.safelist)) return content.safelist;
  164. if (Array.isArray(purge === null || purge === void 0 ? void 0 : purge.safelist)) return purge.safelist;
  165. if (Array.isArray(purge === null || purge === void 0 ? void 0 : (_purge_options = purge.options) === null || _purge_options === void 0 ? void 0 : _purge_options.safelist)) return purge.options.safelist;
  166. return [];
  167. })();
  168. // Normalize the `blocklist`
  169. config.blocklist = (()=>{
  170. let { blocklist } = config;
  171. if (Array.isArray(blocklist)) {
  172. if (blocklist.every((item)=>typeof item === "string")) {
  173. return blocklist;
  174. }
  175. _log.default.warn("blocklist-invalid", [
  176. "The `blocklist` option must be an array of strings.",
  177. "https://tailwindcss.com/docs/content-configuration#discarding-classes"
  178. ]);
  179. }
  180. return [];
  181. })();
  182. // Normalize prefix option
  183. if (typeof config.prefix === "function") {
  184. _log.default.warn("prefix-function", [
  185. "As of Tailwind CSS v3.0, `prefix` cannot be a function.",
  186. "Update `prefix` in your configuration to be a string to eliminate this warning.",
  187. "https://tailwindcss.com/docs/upgrade-guide#prefix-cannot-be-a-function"
  188. ]);
  189. config.prefix = "";
  190. } else {
  191. var _config_prefix;
  192. config.prefix = (_config_prefix = config.prefix) !== null && _config_prefix !== void 0 ? _config_prefix : "";
  193. }
  194. // Normalize the `content`
  195. config.content = {
  196. relative: (()=>{
  197. let { content } = config;
  198. if (content === null || content === void 0 ? void 0 : content.relative) {
  199. return content.relative;
  200. }
  201. return (0, _featureFlags.flagEnabled)(config, "relativeContentPathsByDefault");
  202. })(),
  203. files: (()=>{
  204. let { content , purge } = config;
  205. if (Array.isArray(purge)) return purge;
  206. if (Array.isArray(purge === null || purge === void 0 ? void 0 : purge.content)) return purge.content;
  207. if (Array.isArray(content)) return content;
  208. if (Array.isArray(content === null || content === void 0 ? void 0 : content.content)) return content.content;
  209. if (Array.isArray(content === null || content === void 0 ? void 0 : content.files)) return content.files;
  210. return [];
  211. })(),
  212. extract: (()=>{
  213. let extract = (()=>{
  214. var _config_purge, _config_content, _config_purge1, _config_purge_extract, _config_content1, _config_content_extract, _config_purge2, _config_purge_options, _config_content2, _config_content_options;
  215. if ((_config_purge = config.purge) === null || _config_purge === void 0 ? void 0 : _config_purge.extract) return config.purge.extract;
  216. if ((_config_content = config.content) === null || _config_content === void 0 ? void 0 : _config_content.extract) return config.content.extract;
  217. if ((_config_purge1 = config.purge) === null || _config_purge1 === void 0 ? void 0 : (_config_purge_extract = _config_purge1.extract) === null || _config_purge_extract === void 0 ? void 0 : _config_purge_extract.DEFAULT) return config.purge.extract.DEFAULT;
  218. if ((_config_content1 = config.content) === null || _config_content1 === void 0 ? void 0 : (_config_content_extract = _config_content1.extract) === null || _config_content_extract === void 0 ? void 0 : _config_content_extract.DEFAULT) return config.content.extract.DEFAULT;
  219. if ((_config_purge2 = config.purge) === null || _config_purge2 === void 0 ? void 0 : (_config_purge_options = _config_purge2.options) === null || _config_purge_options === void 0 ? void 0 : _config_purge_options.extractors) return config.purge.options.extractors;
  220. if ((_config_content2 = config.content) === null || _config_content2 === void 0 ? void 0 : (_config_content_options = _config_content2.options) === null || _config_content_options === void 0 ? void 0 : _config_content_options.extractors) return config.content.options.extractors;
  221. return {};
  222. })();
  223. let extractors = {};
  224. let defaultExtractor = (()=>{
  225. var _config_purge, _config_purge_options, _config_content, _config_content_options;
  226. if ((_config_purge = config.purge) === null || _config_purge === void 0 ? void 0 : (_config_purge_options = _config_purge.options) === null || _config_purge_options === void 0 ? void 0 : _config_purge_options.defaultExtractor) {
  227. return config.purge.options.defaultExtractor;
  228. }
  229. if ((_config_content = config.content) === null || _config_content === void 0 ? void 0 : (_config_content_options = _config_content.options) === null || _config_content_options === void 0 ? void 0 : _config_content_options.defaultExtractor) {
  230. return config.content.options.defaultExtractor;
  231. }
  232. return undefined;
  233. })();
  234. if (defaultExtractor !== undefined) {
  235. extractors.DEFAULT = defaultExtractor;
  236. }
  237. // Functions
  238. if (typeof extract === "function") {
  239. extractors.DEFAULT = extract;
  240. } else if (Array.isArray(extract)) {
  241. for (let { extensions , extractor } of extract !== null && extract !== void 0 ? extract : []){
  242. for (let extension of extensions){
  243. extractors[extension] = extractor;
  244. }
  245. }
  246. } else if (typeof extract === "object" && extract !== null) {
  247. Object.assign(extractors, extract);
  248. }
  249. return extractors;
  250. })(),
  251. transform: (()=>{
  252. let transform = (()=>{
  253. var _config_purge, _config_content, _config_purge1, _config_purge_transform, _config_content1, _config_content_transform;
  254. if ((_config_purge = config.purge) === null || _config_purge === void 0 ? void 0 : _config_purge.transform) return config.purge.transform;
  255. if ((_config_content = config.content) === null || _config_content === void 0 ? void 0 : _config_content.transform) return config.content.transform;
  256. if ((_config_purge1 = config.purge) === null || _config_purge1 === void 0 ? void 0 : (_config_purge_transform = _config_purge1.transform) === null || _config_purge_transform === void 0 ? void 0 : _config_purge_transform.DEFAULT) return config.purge.transform.DEFAULT;
  257. if ((_config_content1 = config.content) === null || _config_content1 === void 0 ? void 0 : (_config_content_transform = _config_content1.transform) === null || _config_content_transform === void 0 ? void 0 : _config_content_transform.DEFAULT) return config.content.transform.DEFAULT;
  258. return {};
  259. })();
  260. let transformers = {};
  261. if (typeof transform === "function") {
  262. transformers.DEFAULT = transform;
  263. } else if (typeof transform === "object" && transform !== null) {
  264. Object.assign(transformers, transform);
  265. }
  266. return transformers;
  267. })()
  268. };
  269. // Validate globs to prevent bogus globs.
  270. // E.g.: `./src/*.{html}` is invalid, the `{html}` should just be `html`
  271. for (let file of config.content.files){
  272. if (typeof file === "string" && /{([^,]*?)}/g.test(file)) {
  273. _log.default.warn("invalid-glob-braces", [
  274. `The glob pattern ${(0, _log.dim)(file)} in your Tailwind CSS configuration is invalid.`,
  275. `Update it to ${(0, _log.dim)(file.replace(/{([^,]*?)}/g, "$1"))} to silence this warning.`
  276. ]);
  277. break;
  278. }
  279. }
  280. return config;
  281. }