You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
12 KiB

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