index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function(thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function(resolve) { resolve(value); }); }
  4. return new(P || (P = Promise))(function(resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __generator = (this && this.__generator) || function(thisArg, body) {
  12. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] },
  13. f, y, t, g;
  14. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] =
  15. function() { return this; }), g;
  16. function verb(n) { return function(v) { return step([n, v]); }; }
  17. function step(op) {
  18. if (f) throw new TypeError("Generator is already executing.");
  19. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  20. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t
  21. .call(y, op[1])).done) return t;
  22. if (y = 0, t) op = [op[0] & 2, t.value];
  23. switch (op[0]) {
  24. case 0:
  25. case 1:
  26. t = op;
  27. break;
  28. case 4:
  29. _.label++;
  30. return { value: op[1], done: false };
  31. case 5:
  32. _.label++;
  33. y = op[1];
  34. op = [0];
  35. continue;
  36. case 7:
  37. op = _.ops.pop();
  38. _.trys.pop();
  39. continue;
  40. default:
  41. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  42. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  43. if (op[0] === 6 && _.label < t[1]) { _.label = t[1];
  44. t = op; break; }
  45. if (t && _.label < t[2]) { _.label = t[2];
  46. _.ops.push(op); break; }
  47. if (t[2]) _.ops.pop();
  48. _.trys.pop();
  49. continue;
  50. }
  51. op = body.call(thisArg, _);
  52. } catch (e) { op = [6, e];
  53. y = 0; } finally { f = t = 0; }
  54. if (op[0] & 5) throw op[1];
  55. return { value: op[0] ? op[1] : void 0, done: true };
  56. }
  57. };
  58. Object.defineProperty(exports, "__esModule", { value: true });
  59. exports.multipleEntryFilePlugin = void 0;
  60. function multipleEntryFilePlugin(pluginOptions) {
  61. var chunkName = pluginOptions.chunkName,
  62. entryPath = pluginOptions.entryPath,
  63. insertPlaceholder = pluginOptions.insertPlaceholder,
  64. crossorigin = pluginOptions.crossorigin,
  65. entryFileName = pluginOptions.entryFileName,
  66. _a = pluginOptions.injectTo,
  67. injectTo = _a === void 0 ? 'head-prepend' : _a;
  68. var filename = '';
  69. var base = '';
  70. var isDevServer = true;
  71. return {
  72. name: 'multiple-entries-plugin',
  73. // Only be executed in dev mode
  74. configureServer: function() {
  75. // isDevServer = true;
  76. // filename = entryPath;
  77. },
  78. // Record base path for build output file path
  79. configResolved: function(resolvedConfig) {
  80. base = resolvedConfig.base || '';
  81. },
  82. // Inject new entry
  83. options: function(options) {
  84. // is string
  85. if (typeof options.input === 'string') {
  86. options.input = [options.input, entryPath];
  87. }
  88. // is array
  89. else if (Array.isArray(options.input)) {
  90. options.input.push(entryPath);
  91. }
  92. // is object
  93. else if (typeof options.input === 'object') {
  94. options.input[chunkName] = entryPath;
  95. }
  96. // No entry specified, treat current entry as the only one.
  97. else {
  98. options.input = [entryPath];
  99. }
  100. },
  101. /**
  102. * override entryFileNames, append the new entry file name into origin.
  103. * By default, it will hit options.output.entryFileNames naming logic. output file name will be set to the same file name as the main entry.
  104. * If you need to specify the output file name, set pluginOptions.entryFileName in the same way as rollup.output.entryFileNames
  105. */
  106. outputOptions: function(options) {
  107. if (!entryFileName) {
  108. return;
  109. }
  110. if (Array.isArray(options)) {
  111. throw new TypeError('Array type output not supported');
  112. }
  113. var oldEntryFileNames = options.entryFileNames;
  114. options.entryFileNames = function(chunkInfo) {
  115. // Hit the entry we added
  116. if ('isEntry' in chunkInfo &&
  117. chunkInfo.isEntry === true &&
  118. chunkInfo.name === chunkName) {
  119. return entryFileName;
  120. }
  121. // No entry file name specified, use roll up default value
  122. else if (oldEntryFileNames === undefined) {
  123. return '[name].js';
  124. }
  125. // Entry is a function
  126. else if (typeof oldEntryFileNames === 'function') {
  127. return oldEntryFileNames(chunkInfo);
  128. }
  129. // Entry is a string
  130. else {
  131. return oldEntryFileNames;
  132. }
  133. };
  134. },
  135. // Record the file name of the injected module while generateBundle
  136. generateBundle: function(_, bundle) {
  137. Object.values(bundle).forEach(function(bun) {
  138. if ('isEntry' in bun && bun.isEntry === true && bun.name === chunkName) {
  139. filename = base + bun.fileName;
  140. }
  141. });
  142. },
  143. // Inject <script> tag for added entry file
  144. transformIndexHtml: function(html) {
  145. return __awaiter(this, void 0, void 0, function() {
  146. var injectScriptType, crossoriginAttr, tag, attrs;
  147. return __generator(this, function(_a) {
  148. // If there is a specified placeholder, use the placeholder first
  149. if (insertPlaceholder) {
  150. injectScriptType = isDevServer ? 'type="module" ' : '';
  151. crossoriginAttr = crossorigin ? "crossorigin=\"".concat(crossorigin, "\" ") : '';
  152. tag = "<script ".concat(injectScriptType).concat(crossoriginAttr, "src=\"").concat(filename, "\"></script>");
  153. return [2 /*return*/ , html.replace(insertPlaceholder, tag)];
  154. }
  155. // Inject <script> tag with vite HtmlTagDescriptor
  156. else {
  157. attrs = {
  158. src: filename,
  159. };
  160. if (crossorigin !== undefined) {
  161. attrs.crossorigin = crossorigin;
  162. }
  163. if (isDevServer) {
  164. attrs.type = 'module';
  165. }
  166. return [2 /*return*/ , {
  167. html: html,
  168. tags: [{ tag: 'script', attrs: attrs, injectTo: injectTo }],
  169. }];
  170. }
  171. return [2 /*return*/ ];
  172. });
  173. });
  174. },
  175. };
  176. }
  177. exports.multipleEntryFilePlugin = multipleEntryFilePlugin;