normalizeConfig.js 15 KB

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