版博士V2.0程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

208 řádky
7.4 KiB

  1. import require$$0, { createRequire } from 'module';
  2. import require$$1 from 'path';
  3. function windi(strings) {
  4. var values = [];
  5. for (var _i = 1; _i < arguments.length; _i++) {
  6. values[_i - 1] = arguments[_i];
  7. }
  8. // windi template literal
  9. return strings.reduce(function (query, queryPart, i) {
  10. var valueExists = i < values.length;
  11. var text = query + queryPart;
  12. return valueExists ? text + values[i] : text;
  13. }, '');
  14. }
  15. var lib = {exports: {}};
  16. (function (module, exports) {
  17. Object.defineProperty(exports, "__esModule", {
  18. value: true
  19. });
  20. exports.addHook = addHook;
  21. var _module = _interopRequireDefault(require$$0);
  22. var _path = _interopRequireDefault(require$$1);
  23. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  24. /* (c) 2015 Ari Porad (@ariporad) <http://ariporad.com>. License: ariporad.mit-license.org */
  25. const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/; // Guard against poorly mocked module constructors.
  26. const Module = module.constructor.length > 1 ? module.constructor : _module.default;
  27. const HOOK_RETURNED_NOTHING_ERROR_MESSAGE = '[Pirates] A hook returned a non-string, or nothing at all! This is a' + ' violation of intergalactic law!\n' + '--------------------\n' + 'If you have no idea what this means or what Pirates is, let me explain: ' + 'Pirates is a module that makes is easy to implement require hooks. One of' + " the require hooks you're using uses it. One of these require hooks" + " didn't return anything from it's handler, so we don't know what to" + ' do. You might want to debug this.';
  28. /**
  29. * @param {string} filename The filename to check.
  30. * @param {string[]} exts The extensions to hook. Should start with '.' (ex. ['.js']).
  31. * @param {Matcher|null} matcher A matcher function, will be called with path to a file. Should return truthy if the file should be hooked, falsy otherwise.
  32. * @param {boolean} ignoreNodeModules Auto-ignore node_modules. Independent of any matcher.
  33. */
  34. function shouldCompile(filename, exts, matcher, ignoreNodeModules) {
  35. if (typeof filename !== 'string') {
  36. return false;
  37. }
  38. if (exts.indexOf(_path.default.extname(filename)) === -1) {
  39. return false;
  40. }
  41. const resolvedFilename = _path.default.resolve(filename);
  42. if (ignoreNodeModules && nodeModulesRegex.test(resolvedFilename)) {
  43. return false;
  44. }
  45. if (matcher && typeof matcher === 'function') {
  46. return !!matcher(resolvedFilename);
  47. }
  48. return true;
  49. }
  50. /**
  51. * @callback Hook The hook. Accepts the code of the module and the filename.
  52. * @param {string} code
  53. * @param {string} filename
  54. * @returns {string}
  55. */
  56. /**
  57. * @callback Matcher A matcher function, will be called with path to a file.
  58. *
  59. * Should return truthy if the file should be hooked, falsy otherwise.
  60. * @param {string} path
  61. * @returns {boolean}
  62. */
  63. /**
  64. * @callback RevertFunction Reverts the hook when called.
  65. * @returns {void}
  66. */
  67. /**
  68. * @typedef {object} Options
  69. * @property {Matcher|null} [matcher=null] A matcher function, will be called with path to a file.
  70. *
  71. * Should return truthy if the file should be hooked, falsy otherwise.
  72. *
  73. * @property {string[]} [extensions=['.js']] The extensions to hook. Should start with '.' (ex. ['.js']).
  74. * @property {string[]} [exts=['.js']] The extensions to hook. Should start with '.' (ex. ['.js']).
  75. *
  76. * @property {string[]} [extension=['.js']] The extensions to hook. Should start with '.' (ex. ['.js']).
  77. * @property {string[]} [ext=['.js']] The extensions to hook. Should start with '.' (ex. ['.js']).
  78. *
  79. * @property {boolean} [ignoreNodeModules=true] Auto-ignore node_modules. Independent of any matcher.
  80. */
  81. /**
  82. * Add a require hook.
  83. *
  84. * @param {Hook} hook The hook. Accepts the code of the module and the filename. Required.
  85. * @param {Options} [opts] Options
  86. * @returns {RevertFunction} The `revert` function. Reverts the hook when called.
  87. */
  88. function addHook(hook, opts = {}) {
  89. let reverted = false;
  90. const loaders = [];
  91. const oldLoaders = [];
  92. let exts; // We need to do this to fix #15. Basically, if you use a non-standard extension (ie. .jsx), then
  93. // We modify the .js loader, then use the modified .js loader for as the base for .jsx.
  94. // This prevents that.
  95. const originalJSLoader = Module._extensions['.js'];
  96. const matcher = opts.matcher || null;
  97. const ignoreNodeModules = opts.ignoreNodeModules !== false;
  98. exts = opts.extensions || opts.exts || opts.extension || opts.ext || ['.js'];
  99. if (!Array.isArray(exts)) {
  100. exts = [exts];
  101. }
  102. exts.forEach(ext => {
  103. if (typeof ext !== 'string') {
  104. throw new TypeError(`Invalid Extension: ${ext}`);
  105. }
  106. const oldLoader = Module._extensions[ext] || originalJSLoader;
  107. oldLoaders[ext] = Module._extensions[ext];
  108. loaders[ext] = Module._extensions[ext] = function newLoader(mod, filename) {
  109. let compile;
  110. if (!reverted) {
  111. if (shouldCompile(filename, exts, matcher, ignoreNodeModules)) {
  112. compile = mod._compile;
  113. mod._compile = function _compile(code) {
  114. // reset the compile immediately as otherwise we end up having the
  115. // compile function being changed even though this loader might be reverted
  116. // Not reverting it here leads to long useless compile chains when doing
  117. // addHook -> revert -> addHook -> revert -> ...
  118. // The compile function is also anyway created new when the loader is called a second time.
  119. mod._compile = compile;
  120. const newCode = hook(code, filename);
  121. if (typeof newCode !== 'string') {
  122. throw new Error(HOOK_RETURNED_NOTHING_ERROR_MESSAGE);
  123. }
  124. return mod._compile(newCode, filename);
  125. };
  126. }
  127. }
  128. oldLoader(mod, filename);
  129. };
  130. });
  131. return function revert() {
  132. if (reverted) return;
  133. reverted = true;
  134. exts.forEach(ext => {
  135. // if the current loader for the extension is our loader then unregister it and set the oldLoader again
  136. // if not we can not do anything as we cannot remove a loader from within the loader-chain
  137. if (Module._extensions[ext] === loaders[ext]) {
  138. Module._extensions[ext] = oldLoaders[ext];
  139. }
  140. });
  141. };
  142. }
  143. }(lib, lib.exports));
  144. function convert(code) {
  145. var map = {
  146. '@tailwindcss\\/typography': 'windicss/plugin/typography',
  147. '@tailwindcss\\/forms': 'windicss/plugin/forms',
  148. '@tailwindcss\\/aspect-ratio': 'windicss/plugin/aspect-ratio',
  149. '@tailwindcss\\/line-clamp': 'windicss/plugin/line-clamp',
  150. 'tailwindcss\\/plugin': 'windicss/plugin',
  151. 'tailwindcss\\/colors': 'windicss/colors',
  152. 'tailwindcss\\/resolveConfig': 'windicss/resolveConfig',
  153. 'tailwindcss\\/defaultConfig': 'windicss/defaultConfig',
  154. 'tailwindcss\\/defaultTheme': 'windicss/defaultTheme',
  155. };
  156. for (var _i = 0, _a = Object.entries(map); _i < _a.length; _i++) {
  157. var _b = _a[_i], key = _b[0], value = _b[1];
  158. code = code.replace(new RegExp(key, 'g'), value);
  159. }
  160. return code;
  161. }
  162. function transform(path) {
  163. var require = createRequire(import.meta.url);
  164. var matcher = function (filename) { return !/\/windicss\//.test(filename); };
  165. var revert = lib.exports.addHook(function (code) { return convert(code); }, { exts: ['.js'], matcher: matcher, ignoreNodeModules: false });
  166. var mod = require(path);
  167. revert();
  168. return mod;
  169. }
  170. function defineConfig(config) {
  171. return config;
  172. }
  173. export { convert, defineConfig, transform, windi };