index.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _webpack = _interopRequireWildcard(require("webpack"));
  7. var _schemaUtils = _interopRequireDefault(require("schema-utils"));
  8. var _CssModuleFactory = _interopRequireDefault(require("./CssModuleFactory"));
  9. var _CssDependencyTemplate = _interopRequireDefault(require("./CssDependencyTemplate"));
  10. var _CssDependency = _interopRequireDefault(require("./CssDependency"));
  11. var _pluginOptions = _interopRequireDefault(require("./plugin-options.json"));
  12. var _utils = require("./utils");
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  15. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  16. /* eslint-disable class-methods-use-this */
  17. // webpack 5 exposes the sources property to ensure the right version of webpack-sources is used
  18. const {
  19. ConcatSource,
  20. SourceMapSource,
  21. OriginalSource
  22. } = // eslint-disable-next-line global-require
  23. _webpack.default.sources || require('webpack-sources');
  24. const {
  25. Template,
  26. util: {
  27. createHash
  28. }
  29. } = _webpack.default;
  30. const isWebpack4 = _webpack.version[0] === '4';
  31. const pluginName = 'mini-css-extract-plugin';
  32. const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
  33. const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
  34. const REGEXP_NAME = /\[name\]/i;
  35. const REGEXP_PLACEHOLDERS = /\[(name|id|chunkhash)\]/g;
  36. const DEFAULT_FILENAME = '[name].css';
  37. class MiniCssExtractPlugin {
  38. constructor(options = {}) {
  39. (0, _schemaUtils.default)(_pluginOptions.default, options, 'Mini CSS Extract Plugin');
  40. this.options = Object.assign({
  41. filename: DEFAULT_FILENAME,
  42. moduleFilename: () => this.options.filename || DEFAULT_FILENAME,
  43. ignoreOrder: false
  44. }, options);
  45. if (!this.options.chunkFilename) {
  46. const {
  47. filename
  48. } = this.options; // Anything changing depending on chunk is fine
  49. if (filename.match(REGEXP_PLACEHOLDERS)) {
  50. this.options.chunkFilename = filename;
  51. } else {
  52. // Elsewise prefix '[id].' in front of the basename to make it changing
  53. this.options.chunkFilename = filename.replace(/(^|\/)([^/]*(?:\?|$))/, '$1[id].$2');
  54. }
  55. }
  56. if (!isWebpack4 && 'hmr' in this.options) {
  57. throw new Error("The 'hmr' option doesn't exist for the mini-css-extract-plugin when using webpack 5 (it's automatically determined)");
  58. }
  59. }
  60. /** @param {import("webpack").Compiler} compiler */
  61. apply(compiler) {
  62. if (!isWebpack4) {
  63. const {
  64. splitChunks
  65. } = compiler.options.optimization;
  66. if (splitChunks) {
  67. if (splitChunks.defaultSizeTypes.includes('...')) {
  68. splitChunks.defaultSizeTypes.push(_utils.MODULE_TYPE);
  69. }
  70. }
  71. }
  72. compiler.hooks.thisCompilation.tap(pluginName, compilation => {
  73. compilation.dependencyFactories.set(_CssDependency.default, new _CssModuleFactory.default());
  74. compilation.dependencyTemplates.set(_CssDependency.default, new _CssDependencyTemplate.default());
  75. if (isWebpack4) {
  76. compilation.mainTemplate.hooks.renderManifest.tap(pluginName, (result, {
  77. chunk
  78. }) => {
  79. const {
  80. chunkGraph
  81. } = compilation;
  82. const renderedModules = Array.from(this.getChunkModules(chunk, chunkGraph)).filter(module => module.type === _utils.MODULE_TYPE);
  83. const filenameTemplate = chunk.filenameTemplate || (({
  84. chunk: chunkData
  85. }) => this.options.moduleFilename(chunkData));
  86. if (renderedModules.length > 0) {
  87. result.push({
  88. render: () => this.renderContentAsset(compilation, chunk, renderedModules, compilation.runtimeTemplate.requestShortener),
  89. filenameTemplate,
  90. pathOptions: {
  91. chunk,
  92. contentHashType: _utils.MODULE_TYPE
  93. },
  94. identifier: `${pluginName}.${chunk.id}`,
  95. hash: chunk.contentHash[_utils.MODULE_TYPE]
  96. });
  97. }
  98. });
  99. compilation.chunkTemplate.hooks.renderManifest.tap(pluginName, (result, {
  100. chunk
  101. }) => {
  102. const {
  103. chunkGraph
  104. } = compilation;
  105. const renderedModules = Array.from(this.getChunkModules(chunk, chunkGraph)).filter(module => module.type === _utils.MODULE_TYPE);
  106. const filenameTemplate = chunk.filenameTemplate || this.options.chunkFilename;
  107. if (renderedModules.length > 0) {
  108. result.push({
  109. render: () => this.renderContentAsset(compilation, chunk, renderedModules, compilation.runtimeTemplate.requestShortener),
  110. filenameTemplate,
  111. pathOptions: {
  112. chunk,
  113. contentHashType: _utils.MODULE_TYPE
  114. },
  115. identifier: `${pluginName}.${chunk.id}`,
  116. hash: chunk.contentHash[_utils.MODULE_TYPE]
  117. });
  118. }
  119. });
  120. } else {
  121. compilation.hooks.renderManifest.tap(pluginName, (result, {
  122. chunk
  123. }) => {
  124. const {
  125. chunkGraph
  126. } = compilation; // We don't need hot update chunks for css
  127. // We will use the real asset instead to update
  128. if (chunk instanceof _webpack.default.HotUpdateChunk) return;
  129. const renderedModules = Array.from(this.getChunkModules(chunk, chunkGraph)).filter(module => module.type === _utils.MODULE_TYPE);
  130. const filenameTemplate = chunk.canBeInitial() ? ({
  131. chunk: chunkData
  132. }) => this.options.moduleFilename(chunkData) : this.options.chunkFilename;
  133. if (renderedModules.length > 0) {
  134. result.push({
  135. render: () => this.renderContentAsset(compilation, chunk, renderedModules, compilation.runtimeTemplate.requestShortener),
  136. filenameTemplate,
  137. pathOptions: {
  138. chunk,
  139. contentHashType: _utils.MODULE_TYPE
  140. },
  141. identifier: `${pluginName}.${chunk.id}`,
  142. hash: chunk.contentHash[_utils.MODULE_TYPE]
  143. });
  144. }
  145. });
  146. }
  147. /*
  148. * For webpack 5 this will be unneeded once the logic uses a RuntimeModule
  149. * as the content of runtime modules is hashed and added to the chunk hash automatically
  150. * */
  151. if (isWebpack4) {
  152. compilation.mainTemplate.hooks.hashForChunk.tap(pluginName, (hash, chunk) => {
  153. const {
  154. chunkFilename
  155. } = this.options;
  156. if (REGEXP_CHUNKHASH.test(chunkFilename)) {
  157. hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
  158. }
  159. if (REGEXP_CONTENTHASH.test(chunkFilename)) {
  160. hash.update(JSON.stringify(chunk.getChunkMaps(true).contentHash[_utils.MODULE_TYPE] || {}));
  161. }
  162. if (REGEXP_NAME.test(chunkFilename)) {
  163. hash.update(JSON.stringify(chunk.getChunkMaps(true).name));
  164. }
  165. });
  166. }
  167. compilation.hooks.contentHash.tap(pluginName, chunk => {
  168. const {
  169. outputOptions,
  170. chunkGraph
  171. } = compilation;
  172. const {
  173. hashFunction,
  174. hashDigest,
  175. hashDigestLength
  176. } = outputOptions;
  177. const hash = createHash(hashFunction);
  178. for (const m of this.getChunkModules(chunk, chunkGraph)) {
  179. if (m.type === _utils.MODULE_TYPE) {
  180. m.updateHash(hash, {
  181. chunkGraph
  182. });
  183. }
  184. }
  185. const {
  186. contentHash
  187. } = chunk;
  188. contentHash[_utils.MODULE_TYPE] = hash.digest(hashDigest).substring(0, hashDigestLength);
  189. });
  190. const {
  191. mainTemplate
  192. } = compilation;
  193. if (isWebpack4) {
  194. mainTemplate.hooks.localVars.tap(pluginName, (source, chunk) => {
  195. const chunkMap = this.getCssChunkObject(chunk, compilation);
  196. if (Object.keys(chunkMap).length > 0) {
  197. return Template.asString([source, '', '// object to store loaded CSS chunks', 'var installedCssChunks = {', Template.indent(chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(',\n')), '};']);
  198. }
  199. return source;
  200. });
  201. mainTemplate.hooks.requireEnsure.tap(pluginName, (source, chunk, hash) => {
  202. const chunkMap = this.getCssChunkObject(chunk, compilation);
  203. if (Object.keys(chunkMap).length > 0) {
  204. const chunkMaps = chunk.getChunkMaps();
  205. const {
  206. crossOriginLoading
  207. } = mainTemplate.outputOptions;
  208. const linkHrefPath = mainTemplate.getAssetPath(JSON.stringify(this.options.chunkFilename), {
  209. hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
  210. hashWithLength: length => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
  211. chunk: {
  212. id: '" + chunkId + "',
  213. hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
  214. hashWithLength(length) {
  215. const shortChunkHashMap = Object.create(null);
  216. for (const chunkId of Object.keys(chunkMaps.hash)) {
  217. if (typeof chunkMaps.hash[chunkId] === 'string') {
  218. shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substring(0, length);
  219. }
  220. }
  221. return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
  222. },
  223. contentHash: {
  224. [_utils.MODULE_TYPE]: `" + ${JSON.stringify(chunkMaps.contentHash[_utils.MODULE_TYPE])}[chunkId] + "`
  225. },
  226. contentHashWithLength: {
  227. [_utils.MODULE_TYPE]: length => {
  228. const shortContentHashMap = {};
  229. const contentHash = chunkMaps.contentHash[_utils.MODULE_TYPE];
  230. for (const chunkId of Object.keys(contentHash)) {
  231. if (typeof contentHash[chunkId] === 'string') {
  232. shortContentHashMap[chunkId] = contentHash[chunkId].substring(0, length);
  233. }
  234. }
  235. return `" + ${JSON.stringify(shortContentHashMap)}[chunkId] + "`;
  236. }
  237. },
  238. name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`
  239. },
  240. contentHashType: _utils.MODULE_TYPE
  241. });
  242. return Template.asString([source, '', `// ${pluginName} CSS loading`, `var cssChunks = ${JSON.stringify(chunkMap)};`, 'if(installedCssChunks[chunkId]) promises.push(installedCssChunks[chunkId]);', 'else if(installedCssChunks[chunkId] !== 0 && cssChunks[chunkId]) {', Template.indent(['promises.push(installedCssChunks[chunkId] = new Promise(function(resolve, reject) {', Template.indent([`var href = ${linkHrefPath};`, `var fullhref = ${mainTemplate.requireFn}.p + href;`, 'var existingLinkTags = document.getElementsByTagName("link");', 'for(var i = 0; i < existingLinkTags.length; i++) {', Template.indent(['var tag = existingLinkTags[i];', 'var dataHref = tag.getAttribute("data-href") || tag.getAttribute("href");', 'if(tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref)) return resolve();']), '}', 'var existingStyleTags = document.getElementsByTagName("style");', 'for(var i = 0; i < existingStyleTags.length; i++) {', Template.indent(['var tag = existingStyleTags[i];', 'var dataHref = tag.getAttribute("data-href");', 'if(dataHref === href || dataHref === fullhref) return resolve();']), '}', 'var linkTag = document.createElement("link");', 'linkTag.rel = "stylesheet";', 'linkTag.type = "text/css";', 'linkTag.onload = resolve;', 'linkTag.onerror = function(event) {', Template.indent(['var request = event && event.target && event.target.src || fullhref;', 'var err = new Error("Loading CSS chunk " + chunkId + " failed.\\n(" + request + ")");', 'err.code = "CSS_CHUNK_LOAD_FAILED";', 'err.request = request;', 'delete installedCssChunks[chunkId]', 'linkTag.parentNode.removeChild(linkTag)', 'reject(err);']), '};', 'linkTag.href = fullhref;', crossOriginLoading ? Template.asString([`if (linkTag.href.indexOf(window.location.origin + '/') !== 0) {`, Template.indent(`linkTag.crossOrigin = ${JSON.stringify(crossOriginLoading)};`), '}']) : '', 'var head = document.getElementsByTagName("head")[0];', 'head.appendChild(linkTag);']), '}).then(function() {', Template.indent(['installedCssChunks[chunkId] = 0;']), '}));']), '}']);
  243. }
  244. return source;
  245. });
  246. } else {
  247. const enabledChunks = new WeakSet();
  248. const handler = (chunk, set) => {
  249. if (enabledChunks.has(chunk)) return;
  250. enabledChunks.add(chunk); // eslint-disable-next-line global-require
  251. const CssLoadingRuntimeModule = require('./CssLoadingRuntimeModule');
  252. set.add(_webpack.default.RuntimeGlobals.publicPath);
  253. compilation.addRuntimeModule(chunk, new _webpack.default.runtime.GetChunkFilenameRuntimeModule(_utils.MODULE_TYPE, 'mini-css', `${_webpack.default.RuntimeGlobals.require}.miniCssF`, referencedChunk => referencedChunk.canBeInitial() ? ({
  254. chunk: chunkData
  255. }) => this.options.moduleFilename(chunkData) : this.options.chunkFilename, true));
  256. compilation.addRuntimeModule(chunk, new CssLoadingRuntimeModule(set));
  257. };
  258. compilation.hooks.runtimeRequirementInTree.for(_webpack.default.RuntimeGlobals.ensureChunkHandlers).tap(pluginName, handler);
  259. compilation.hooks.runtimeRequirementInTree.for(_webpack.default.RuntimeGlobals.hmrDownloadUpdateHandlers).tap(pluginName, handler);
  260. }
  261. });
  262. }
  263. getChunkModules(chunk, chunkGraph) {
  264. return typeof chunkGraph !== 'undefined' ? chunkGraph.getOrderedChunkModulesIterable(chunk, _utils.compareModulesByIdentifier) : chunk.modulesIterable;
  265. }
  266. getCssChunkObject(mainChunk, compilation) {
  267. const obj = {};
  268. const {
  269. chunkGraph
  270. } = compilation;
  271. for (const chunk of mainChunk.getAllAsyncChunks()) {
  272. for (const module of this.getChunkModules(chunk, chunkGraph)) {
  273. if (module.type === _utils.MODULE_TYPE) {
  274. obj[chunk.id] = 1;
  275. break;
  276. }
  277. }
  278. }
  279. return obj;
  280. }
  281. renderContentAsset(compilation, chunk, modules, requestShortener) {
  282. let usedModules;
  283. const [chunkGroup] = chunk.groupsIterable;
  284. const moduleIndexFunctionName = typeof compilation.chunkGraph !== 'undefined' ? 'getModulePostOrderIndex' : 'getModuleIndex2';
  285. if (typeof chunkGroup[moduleIndexFunctionName] === 'function') {
  286. // Store dependencies for modules
  287. const moduleDependencies = new Map(modules.map(m => [m, new Set()]));
  288. const moduleDependenciesReasons = new Map(modules.map(m => [m, new Map()])); // Get ordered list of modules per chunk group
  289. // This loop also gathers dependencies from the ordered lists
  290. // Lists are in reverse order to allow to use Array.pop()
  291. const modulesByChunkGroup = Array.from(chunk.groupsIterable, cg => {
  292. const sortedModules = modules.map(m => {
  293. return {
  294. module: m,
  295. index: cg[moduleIndexFunctionName](m)
  296. };
  297. }) // eslint-disable-next-line no-undefined
  298. .filter(item => item.index !== undefined).sort((a, b) => b.index - a.index).map(item => item.module);
  299. for (let i = 0; i < sortedModules.length; i++) {
  300. const set = moduleDependencies.get(sortedModules[i]);
  301. const reasons = moduleDependenciesReasons.get(sortedModules[i]);
  302. for (let j = i + 1; j < sortedModules.length; j++) {
  303. const module = sortedModules[j];
  304. set.add(module);
  305. const reason = reasons.get(module) || new Set();
  306. reason.add(cg);
  307. reasons.set(module, reason);
  308. }
  309. }
  310. return sortedModules;
  311. }); // set with already included modules in correct order
  312. usedModules = new Set();
  313. const unusedModulesFilter = m => !usedModules.has(m);
  314. while (usedModules.size < modules.length) {
  315. let success = false;
  316. let bestMatch;
  317. let bestMatchDeps; // get first module where dependencies are fulfilled
  318. for (const list of modulesByChunkGroup) {
  319. // skip and remove already added modules
  320. while (list.length > 0 && usedModules.has(list[list.length - 1])) {
  321. list.pop();
  322. } // skip empty lists
  323. if (list.length !== 0) {
  324. const module = list[list.length - 1];
  325. const deps = moduleDependencies.get(module); // determine dependencies that are not yet included
  326. const failedDeps = Array.from(deps).filter(unusedModulesFilter); // store best match for fallback behavior
  327. if (!bestMatchDeps || bestMatchDeps.length > failedDeps.length) {
  328. bestMatch = list;
  329. bestMatchDeps = failedDeps;
  330. }
  331. if (failedDeps.length === 0) {
  332. // use this module and remove it from list
  333. usedModules.add(list.pop());
  334. success = true;
  335. break;
  336. }
  337. }
  338. }
  339. if (!success) {
  340. // no module found => there is a conflict
  341. // use list with fewest failed deps
  342. // and emit a warning
  343. const fallbackModule = bestMatch.pop();
  344. if (!this.options.ignoreOrder) {
  345. const reasons = moduleDependenciesReasons.get(fallbackModule);
  346. compilation.warnings.push(new Error([`chunk ${chunk.name || chunk.id} [${pluginName}]`, 'Conflicting order. Following module has been added:', ` * ${fallbackModule.readableIdentifier(requestShortener)}`, 'despite it was not able to fulfill desired ordering with these modules:', ...bestMatchDeps.map(m => {
  347. const goodReasonsMap = moduleDependenciesReasons.get(m);
  348. const goodReasons = goodReasonsMap && goodReasonsMap.get(fallbackModule);
  349. const failedChunkGroups = Array.from(reasons.get(m), cg => cg.name).join(', ');
  350. const goodChunkGroups = goodReasons && Array.from(goodReasons, cg => cg.name).join(', ');
  351. return [` * ${m.readableIdentifier(requestShortener)}`, ` - couldn't fulfill desired order of chunk group(s) ${failedChunkGroups}`, goodChunkGroups && ` - while fulfilling desired order of chunk group(s) ${goodChunkGroups}`].filter(Boolean).join('\n');
  352. })].join('\n')));
  353. }
  354. usedModules.add(fallbackModule);
  355. }
  356. }
  357. } else {
  358. // fallback for older webpack versions
  359. // (to avoid a breaking change)
  360. // TODO remove this in next major version
  361. // and increase minimum webpack version to 4.12.0
  362. modules.sort((a, b) => a.index2 - b.index2);
  363. usedModules = modules;
  364. }
  365. const source = new ConcatSource();
  366. const externalsSource = new ConcatSource();
  367. for (const m of usedModules) {
  368. if (/^@import url/.test(m.content)) {
  369. // HACK for IE
  370. // http://stackoverflow.com/a/14676665/1458162
  371. let {
  372. content
  373. } = m;
  374. if (m.media) {
  375. // insert media into the @import
  376. // this is rar
  377. // TODO improve this and parse the CSS to support multiple medias
  378. content = content.replace(/;|\s*$/, m.media);
  379. }
  380. externalsSource.add(content);
  381. externalsSource.add('\n');
  382. } else {
  383. if (m.media) {
  384. source.add(`@media ${m.media} {\n`);
  385. }
  386. if (m.sourceMap) {
  387. source.add(new SourceMapSource(m.content, m.readableIdentifier(requestShortener), m.sourceMap));
  388. } else {
  389. source.add(new OriginalSource(m.content, m.readableIdentifier(requestShortener)));
  390. }
  391. source.add('\n');
  392. if (m.media) {
  393. source.add('}\n');
  394. }
  395. }
  396. }
  397. return new ConcatSource(externalsSource, source);
  398. }
  399. }
  400. MiniCssExtractPlugin.loader = require.resolve('./loader');
  401. var _default = MiniCssExtractPlugin;
  402. exports.default = _default;