util.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. exports.testWebpack5 = exports.genMatchResource = exports.stringifyRequest = exports.getOptions = exports.resolveTemplateTSOptions = exports.needHMR = void 0;
  26. const querystring_1 = __importDefault(require("querystring"));
  27. const path = __importStar(require("path"));
  28. function needHMR(vueLoaderOptions, compilerOptions) {
  29. var _a;
  30. const isServer = (_a = vueLoaderOptions.isServerBuild) !== null && _a !== void 0 ? _a : compilerOptions.target === 'node';
  31. const isProduction = compilerOptions.mode === 'production' ||
  32. process.env.NODE_ENV === 'production';
  33. return !isServer && !isProduction && vueLoaderOptions.hotReload !== false;
  34. }
  35. exports.needHMR = needHMR;
  36. function resolveTemplateTSOptions(descriptor, options) {
  37. var _a, _b, _c;
  38. if (options.enableTsInTemplate === false)
  39. return null;
  40. const lang = ((_a = descriptor.script) === null || _a === void 0 ? void 0 : _a.lang) || ((_b = descriptor.scriptSetup) === null || _b === void 0 ? void 0 : _b.lang);
  41. const isTS = !!(lang && /tsx?$/.test(lang));
  42. let expressionPlugins = ((_c = options === null || options === void 0 ? void 0 : options.compilerOptions) === null || _c === void 0 ? void 0 : _c.expressionPlugins) || [];
  43. if (isTS && !expressionPlugins.includes('typescript')) {
  44. expressionPlugins = [...expressionPlugins, 'typescript'];
  45. }
  46. return {
  47. isTS,
  48. expressionPlugins,
  49. };
  50. }
  51. exports.resolveTemplateTSOptions = resolveTemplateTSOptions;
  52. // loader utils removed getOptions in 3.x, but it's not available on webpack 4
  53. // loader context
  54. function getOptions(loaderContext) {
  55. const query = loaderContext.query;
  56. if (typeof query === 'string' && query !== '') {
  57. return parseQuery(query);
  58. }
  59. if (!query || typeof query !== 'object') {
  60. // Not object-like queries are not supported.
  61. return {};
  62. }
  63. return query;
  64. }
  65. exports.getOptions = getOptions;
  66. const specialValues = {
  67. null: null,
  68. true: true,
  69. false: false,
  70. };
  71. function parseQuery(query) {
  72. if (query.substr(0, 1) !== '?') {
  73. throw new Error("A valid query string passed to parseQuery should begin with '?'");
  74. }
  75. query = query.substr(1);
  76. if (!query) {
  77. return {};
  78. }
  79. if (query.substr(0, 1) === '{' && query.substr(-1) === '}') {
  80. return JSON.parse(query);
  81. }
  82. const queryArgs = query.split(/[,&]/g);
  83. const result = Object.create(null);
  84. queryArgs.forEach((arg) => {
  85. const idx = arg.indexOf('=');
  86. if (idx >= 0) {
  87. let name = arg.substr(0, idx);
  88. let value = decodeURIComponent(arg.substr(idx + 1));
  89. // eslint-disable-next-line no-prototype-builtins
  90. if (specialValues.hasOwnProperty(value)) {
  91. value = specialValues[value];
  92. }
  93. if (name.substr(-2) === '[]') {
  94. name = decodeURIComponent(name.substr(0, name.length - 2));
  95. if (!Array.isArray(result[name])) {
  96. result[name] = [];
  97. }
  98. result[name].push(value);
  99. }
  100. else {
  101. name = decodeURIComponent(name);
  102. result[name] = value;
  103. }
  104. }
  105. else {
  106. if (arg.substr(0, 1) === '-') {
  107. result[decodeURIComponent(arg.substr(1))] = false;
  108. }
  109. else if (arg.substr(0, 1) === '+') {
  110. result[decodeURIComponent(arg.substr(1))] = true;
  111. }
  112. else {
  113. result[decodeURIComponent(arg)] = true;
  114. }
  115. }
  116. });
  117. return result;
  118. }
  119. const matchRelativePath = /^\.\.?[/\\]/;
  120. function isAbsolutePath(str) {
  121. return path.posix.isAbsolute(str) || path.win32.isAbsolute(str);
  122. }
  123. function isRelativePath(str) {
  124. return matchRelativePath.test(str);
  125. }
  126. function stringifyRequest(loaderContext, request) {
  127. const splitted = request.split('!');
  128. const context = loaderContext.context ||
  129. // @ts-ignore
  130. (loaderContext.options && loaderContext.options.context);
  131. return JSON.stringify(splitted
  132. .map((part) => {
  133. // First, separate singlePath from query, because the query might contain paths again
  134. const splittedPart = part.match(/^(.*?)(\?.*)/);
  135. const query = splittedPart ? splittedPart[2] : '';
  136. let singlePath = splittedPart ? splittedPart[1] : part;
  137. if (isAbsolutePath(singlePath) && context) {
  138. singlePath = path.relative(context, singlePath);
  139. if (isAbsolutePath(singlePath)) {
  140. // If singlePath still matches an absolute path, singlePath was on a different drive than context.
  141. // In this case, we leave the path platform-specific without replacing any separators.
  142. // @see https://github.com/webpack/loader-utils/pull/14
  143. return singlePath + query;
  144. }
  145. if (isRelativePath(singlePath) === false) {
  146. // Ensure that the relative path starts at least with ./ otherwise it would be a request into the modules directory (like node_modules).
  147. singlePath = './' + singlePath;
  148. }
  149. }
  150. return singlePath.replace(/\\/g, '/') + query;
  151. })
  152. .join('!'));
  153. }
  154. exports.stringifyRequest = stringifyRequest;
  155. function genMatchResource(context, resourcePath, resourceQuery, lang) {
  156. resourceQuery = resourceQuery || '';
  157. const loaders = [];
  158. const parsedQuery = querystring_1.default.parse(resourceQuery.slice(1));
  159. // process non-external resources
  160. if ('vue' in parsedQuery && !('external' in parsedQuery)) {
  161. const currentRequest = context.loaders
  162. .slice(context.loaderIndex)
  163. .map((obj) => obj.request);
  164. loaders.push(...currentRequest);
  165. }
  166. const loaderString = loaders.join('!');
  167. return `${resourcePath}${lang ? `.${lang}` : ''}${resourceQuery}!=!${loaderString ? `${loaderString}!` : ''}${resourcePath}${resourceQuery}`;
  168. }
  169. exports.genMatchResource = genMatchResource;
  170. const testWebpack5 = (compiler) => {
  171. var _a;
  172. if (!compiler) {
  173. return false;
  174. }
  175. const webpackVersion = (_a = compiler === null || compiler === void 0 ? void 0 : compiler.webpack) === null || _a === void 0 ? void 0 : _a.version;
  176. return Boolean(webpackVersion && Number(webpackVersion.split('.')[0]) > 4);
  177. };
  178. exports.testWebpack5 = testWebpack5;