index.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. "use strict";
  2. const path = require("path");
  3. const {
  4. validate
  5. } = require("schema-utils");
  6. const {
  7. SyncWaterfallHook
  8. } = require("tapable");
  9. const schema = require("./plugin-options.json");
  10. const {
  11. ABSOLUTE_PUBLIC_PATH,
  12. AUTO_PUBLIC_PATH,
  13. BASE_URI,
  14. MODULE_TYPE,
  15. SINGLE_DOT_PATH_SEGMENT,
  16. compareModulesByIdentifier,
  17. compileBooleanMatcher,
  18. getUndoPath,
  19. trueFn
  20. } = require("./utils");
  21. /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
  22. /** @typedef {import("webpack").Compiler} Compiler */
  23. /** @typedef {import("webpack").Compilation} Compilation */
  24. /** @typedef {import("webpack").ChunkGraph} ChunkGraph */
  25. /** @typedef {import("webpack").Chunk} Chunk */
  26. /** @typedef {import("webpack").ChunkGroup} ChunkGroup */
  27. /** @typedef {import("webpack").Module} Module */
  28. /** @typedef {import("webpack").Dependency} Dependency */
  29. /** @typedef {import("webpack").sources.Source} Source */
  30. /** @typedef {import("webpack").Configuration} Configuration */
  31. /** @typedef {import("webpack").WebpackError} WebpackError */
  32. /** @typedef {import("webpack").AssetInfo} AssetInfo */
  33. /** @typedef {import("./loader.js").Dependency} LoaderDependency */
  34. /** @typedef {NonNullable<Required<Configuration>['output']['filename']>} Filename */
  35. /** @typedef {NonNullable<Required<Configuration>['output']['chunkFilename']>} ChunkFilename */
  36. /**
  37. * @typedef {object} LoaderOptions
  38. * @property {string | ((resourcePath: string, rootContext: string) => string)=} publicPath public path
  39. * @property {boolean=} emit true when need to emit, otherwise false
  40. * @property {boolean=} esModule need to generate ES module syntax
  41. * @property {string=} layer a layer
  42. * @property {boolean=} defaultExport true when need to use default export, otherwise false
  43. */
  44. /**
  45. * @typedef {object} PluginOptions
  46. * @property {Filename=} filename filename
  47. * @property {ChunkFilename=} chunkFilename chunk filename
  48. * @property {boolean=} ignoreOrder true when need to ignore order, otherwise false
  49. * @property {string | ((linkTag: HTMLLinkElement) => void)=} insert link insert place or a custom insert function
  50. * @property {Record<string, string>=} attributes link attributes
  51. * @property {string | false | "text/css"=} linkType value of a link type attribute
  52. * @property {boolean=} runtime true when need to generate runtime code, otherwise false
  53. * @property {boolean=} experimentalUseImportModule true when need to use `experimentalUseImportModule` API, otherwise false
  54. */
  55. /**
  56. * @typedef {object} NormalizedPluginOptions
  57. * @property {Filename=} filename filename
  58. * @property {ChunkFilename=} chunkFilename chunk filename
  59. * @property {boolean} ignoreOrder true when need to ignore order, otherwise false
  60. * @property {string | ((linkTag: HTMLLinkElement) => void)=} insert a link insert place or a custom insert function
  61. * @property {Record<string, string>=} attributes link attributes
  62. * @property {string | false | "text/css"=} linkType value of a link type attribute
  63. * @property {boolean} runtime true when need to generate runtime code, otherwise false
  64. * @property {boolean=} experimentalUseImportModule true when need to use `experimentalUseImportModule` API, otherwise false
  65. */
  66. /**
  67. * @typedef {object} RuntimeOptions
  68. * @property {string | ((linkTag: HTMLLinkElement) => void)=} insert a link insert place or a custom insert function
  69. * @property {string | false | "text/css"} linkType value of a link type attribute
  70. * @property {Record<string, string>=} attributes link attributes
  71. */
  72. const pluginName = "mini-css-extract-plugin";
  73. const pluginSymbol = Symbol(pluginName);
  74. const DEFAULT_FILENAME = "[name].css";
  75. /**
  76. * @type {Set<string>}
  77. */
  78. const TYPES = new Set([MODULE_TYPE]);
  79. /**
  80. * @type {ReturnType<Module["codeGeneration"]>}
  81. */
  82. const CODE_GENERATION_RESULT = {
  83. sources: new Map(),
  84. runtimeRequirements: new Set()
  85. };
  86. // eslint-disable-next-line jsdoc/reject-any-type
  87. /** @typedef {{ context: string | null, identifier: string, identifierIndex: number, content: Buffer, sourceMap?: Buffer, media?: string, supports?: string, layer?: any, assetsInfo?: Map<string, AssetInfo>, assets?: { [key: string]: Source } }} CssModuleDependency */
  88. /** @typedef {Module & { content: Buffer, media?: string, sourceMap?: Buffer, supports?: string, layer?: string, assets?: { [key: string]: Source }, assetsInfo?: Map<string, AssetInfo> }} CssModule */
  89. /** @typedef {{ new (dependency: CssModuleDependency): CssModule }} CssModuleConstructor */
  90. /** @typedef {Dependency & CssModuleDependency} CssDependency */
  91. /** @typedef {Omit<LoaderDependency, "context">} CssDependencyOptions */
  92. /** @typedef {{ new (loaderDependency: CssDependencyOptions, context: string | null, identifierIndex: number): CssDependency }} CssDependencyConstructor */
  93. /**
  94. * @typedef {object} VarNames
  95. * @property {string} tag tag
  96. * @property {string} chunkId chunk id
  97. * @property {string} href href
  98. * @property {string} resolve resolve
  99. * @property {string} reject reject
  100. */
  101. /**
  102. * @typedef {object} MiniCssExtractPluginCompilationHooks
  103. * @property {import("tapable").SyncWaterfallHook<[string, VarNames], string>} beforeTagInsert before tag insert hook
  104. * @property {SyncWaterfallHook<[string, Chunk]>} linkPreload link preload hook
  105. * @property {SyncWaterfallHook<[string, Chunk]>} linkPrefetch link prefetch hook
  106. */
  107. /**
  108. * @type {WeakMap<Compiler["webpack"], CssModuleConstructor>}
  109. */
  110. const cssModuleCache = new WeakMap();
  111. /**
  112. * @type {WeakMap<Compiler["webpack"], CssDependencyConstructor>}
  113. */
  114. const cssDependencyCache = new WeakMap();
  115. /**
  116. * @type {WeakSet<Compiler["webpack"]>}
  117. */
  118. const registered = new WeakSet();
  119. /** @type {WeakMap<Compilation, MiniCssExtractPluginCompilationHooks>} */
  120. const compilationHooksMap = new WeakMap();
  121. class MiniCssExtractPlugin {
  122. /**
  123. * @param {Compiler["webpack"]} webpack webpack
  124. * @returns {CssModuleConstructor} CSS module constructor
  125. */
  126. static getCssModule(webpack) {
  127. /**
  128. * Prevent creation of multiple CssModule classes to allow other integrations to get the current CssModule.
  129. */
  130. if (cssModuleCache.has(webpack)) {
  131. return /** @type {CssModuleConstructor} */cssModuleCache.get(webpack);
  132. }
  133. class CssModule extends webpack.Module {
  134. /**
  135. * @param {CssModuleDependency} dependency css module dependency
  136. */
  137. constructor({
  138. context,
  139. identifier,
  140. identifierIndex,
  141. content,
  142. layer,
  143. supports,
  144. media,
  145. sourceMap,
  146. assets,
  147. assetsInfo
  148. }) {
  149. super(MODULE_TYPE, /** @type {string | undefined} */context);
  150. this.id = "";
  151. this._context = context;
  152. this._identifier = identifier;
  153. this._identifierIndex = identifierIndex;
  154. this.content = content;
  155. this.layer = layer;
  156. this.supports = supports;
  157. this.media = media;
  158. this.sourceMap = sourceMap;
  159. this.assets = assets;
  160. this.assetsInfo = assetsInfo;
  161. this._needBuild = true;
  162. }
  163. // no source() so webpack 4 doesn't do add stuff to the bundle
  164. size() {
  165. return this.content.length;
  166. }
  167. identifier() {
  168. return `css|${this._identifier}|${this._identifierIndex}|${this.layer || ""}|${this.supports || ""}|${this.media}}}`;
  169. }
  170. /**
  171. * @param {Parameters<Module["readableIdentifier"]>[0]} requestShortener request shortener
  172. * @returns {ReturnType<Module["readableIdentifier"]>} readable identifier
  173. */
  174. readableIdentifier(requestShortener) {
  175. return `css ${requestShortener.shorten(this._identifier)}${this._identifierIndex ? ` (${this._identifierIndex})` : ""}${this.layer ? ` (layer ${this.layer})` : ""}${this.supports ? ` (supports ${this.supports})` : ""}${this.media ? ` (media ${this.media})` : ""}`;
  176. }
  177. getSourceTypes() {
  178. return TYPES;
  179. }
  180. codeGeneration() {
  181. return CODE_GENERATION_RESULT;
  182. }
  183. nameForCondition() {
  184. const resource = /** @type {string} */
  185. this._identifier.split("!").pop();
  186. const idx = resource.indexOf("?");
  187. if (idx >= 0) {
  188. return resource.slice(0, Math.max(0, idx));
  189. }
  190. return resource;
  191. }
  192. /**
  193. * @param {Module} module a module
  194. */
  195. updateCacheModule(module) {
  196. if (!this.content.equals(/** @type {CssModule} */module.content) || this.layer !== /** @type {CssModule} */module.layer || this.supports !== /** @type {CssModule} */module.supports || this.media !== /** @type {CssModule} */module.media || (this.sourceMap ? !this.sourceMap.equals(/** @type {Uint8Array} * */
  197. /** @type {CssModule} */module.sourceMap) : false) || this.assets !== /** @type {CssModule} */module.assets || this.assetsInfo !== /** @type {CssModule} */module.assetsInfo) {
  198. this._needBuild = true;
  199. this.content = /** @type {CssModule} */module.content;
  200. this.layer = /** @type {CssModule} */module.layer;
  201. this.supports = /** @type {CssModule} */module.supports;
  202. this.media = /** @type {CssModule} */module.media;
  203. this.sourceMap = /** @type {CssModule} */module.sourceMap;
  204. this.assets = /** @type {CssModule} */module.assets;
  205. this.assetsInfo = /** @type {CssModule} */module.assetsInfo;
  206. }
  207. }
  208. needRebuild() {
  209. return this._needBuild;
  210. }
  211. /**
  212. * @param {Parameters<Module["needBuild"]>[0]} context context info
  213. * @param {Parameters<Module["needBuild"]>[1]} callback callback function, returns true, if the module needs a rebuild
  214. */
  215. needBuild(context, callback) {
  216. callback(undefined, this._needBuild);
  217. }
  218. /**
  219. * @param {Parameters<Module["build"]>[0]} options options
  220. * @param {Parameters<Module["build"]>[1]} compilation compilation
  221. * @param {Parameters<Module["build"]>[2]} resolver resolver
  222. * @param {Parameters<Module["build"]>[3]} fileSystem file system
  223. * @param {Parameters<Module["build"]>[4]} callback callback
  224. */
  225. build(options, compilation, resolver, fileSystem, callback) {
  226. this.buildInfo = {
  227. assets: this.assets,
  228. assetsInfo: this.assetsInfo,
  229. cacheable: true,
  230. hash: (/** @type {string} */
  231. this._computeHash(/** @type {string} */
  232. compilation.outputOptions.hashFunction))
  233. };
  234. this.buildMeta = {};
  235. this._needBuild = false;
  236. callback();
  237. }
  238. /**
  239. * @private
  240. * @param {string} hashFunction hash function
  241. * @returns {string | Buffer} hash digest
  242. */
  243. _computeHash(hashFunction) {
  244. const hash = webpack.util.createHash(hashFunction);
  245. hash.update(this.content);
  246. if (this.layer) {
  247. hash.update(this.layer);
  248. }
  249. hash.update(this.supports || "");
  250. hash.update(this.media || "");
  251. hash.update(this.sourceMap || "");
  252. return hash.digest("hex");
  253. }
  254. /**
  255. * @param {Parameters<Module["updateHash"]>[0]} hash hash
  256. * @param {Parameters<Module["updateHash"]>[1]} context context
  257. */
  258. updateHash(hash, context) {
  259. super.updateHash(hash, context);
  260. hash.update(/** @type {string} */
  261. /** @type {NonNullable<Module["buildInfo"]>} */
  262. this.buildInfo.hash);
  263. }
  264. /**
  265. * @param {Parameters<Module["serialize"]>[0]} context serializer context
  266. */
  267. serialize(context) {
  268. const {
  269. write
  270. } = context;
  271. write(this._context);
  272. write(this._identifier);
  273. write(this._identifierIndex);
  274. write(this.content);
  275. write(this.layer);
  276. write(this.supports);
  277. write(this.media);
  278. write(this.sourceMap);
  279. write(this.assets);
  280. write(this.assetsInfo);
  281. write(this._needBuild);
  282. super.serialize(context);
  283. }
  284. /**
  285. * @param {Parameters<Module["deserialize"]>[0]} context deserializer context
  286. */
  287. deserialize(context) {
  288. this._needBuild = context.read();
  289. super.deserialize(context);
  290. }
  291. }
  292. cssModuleCache.set(webpack, CssModule);
  293. webpack.util.serialization.register(CssModule, path.resolve(__dirname, "CssModule"), null, {
  294. serialize(instance, context) {
  295. instance.serialize(context);
  296. },
  297. deserialize(context) {
  298. const {
  299. read
  300. } = context;
  301. const contextModule = read();
  302. const identifier = read();
  303. const identifierIndex = read();
  304. const content = read();
  305. const layer = read();
  306. const supports = read();
  307. const media = read();
  308. const sourceMap = read();
  309. const assets = read();
  310. const assetsInfo = read();
  311. const dep = new CssModule({
  312. context: contextModule,
  313. identifier,
  314. identifierIndex,
  315. content,
  316. layer,
  317. supports,
  318. media,
  319. sourceMap,
  320. assets,
  321. assetsInfo
  322. });
  323. dep.deserialize(context);
  324. return dep;
  325. }
  326. });
  327. return CssModule;
  328. }
  329. /**
  330. * @param {Compiler["webpack"]} webpack webpack
  331. * @returns {CssDependencyConstructor} CSS dependency constructor
  332. */
  333. static getCssDependency(webpack) {
  334. /**
  335. * Prevent creation of multiple CssDependency classes to allow other integrations to get the current CssDependency.
  336. */
  337. if (cssDependencyCache.has(webpack)) {
  338. return /** @type {CssDependencyConstructor} */cssDependencyCache.get(webpack);
  339. }
  340. class CssDependency extends webpack.Dependency {
  341. /**
  342. * @param {CssDependencyOptions} loaderDependency loader dependency
  343. * @param {string | null} context context
  344. * @param {number} identifierIndex identifier index
  345. */
  346. constructor({
  347. identifier,
  348. content,
  349. layer,
  350. supports,
  351. media,
  352. sourceMap
  353. }, context, identifierIndex) {
  354. super();
  355. this.identifier = identifier;
  356. this.identifierIndex = identifierIndex;
  357. this.content = content;
  358. this.layer = layer;
  359. this.supports = supports;
  360. this.media = media;
  361. this.sourceMap = sourceMap;
  362. this.context = context;
  363. /** @type {{ [key: string]: Source } | undefined}} */
  364. this.assets = undefined;
  365. /** @type {Map<string, AssetInfo> | undefined} */
  366. this.assetsInfo = undefined;
  367. }
  368. /**
  369. * @returns {ReturnType<Dependency["getResourceIdentifier"]>} a resource identifier
  370. */
  371. getResourceIdentifier() {
  372. return `css-module-${this.identifier}-${this.identifierIndex}`;
  373. }
  374. /**
  375. * @returns {ReturnType<Dependency["getModuleEvaluationSideEffectsState"]>} side effect state
  376. */
  377. getModuleEvaluationSideEffectsState() {
  378. return webpack.ModuleGraphConnection.TRANSITIVE_ONLY;
  379. }
  380. /**
  381. * @param {Parameters<Dependency["serialize"]>[0]} context serializer context
  382. */
  383. serialize(context) {
  384. const {
  385. write
  386. } = context;
  387. write(this.identifier);
  388. write(this.content);
  389. write(this.layer);
  390. write(this.supports);
  391. write(this.media);
  392. write(this.sourceMap);
  393. write(this.context);
  394. write(this.identifierIndex);
  395. write(this.assets);
  396. write(this.assetsInfo);
  397. super.serialize(context);
  398. }
  399. /**
  400. * @param {Parameters<Dependency["deserialize"]>[0]} context deserializer context
  401. */
  402. deserialize(context) {
  403. super.deserialize(context);
  404. }
  405. }
  406. cssDependencyCache.set(webpack, CssDependency);
  407. webpack.util.serialization.register(CssDependency, path.resolve(__dirname, "CssDependency"), null, {
  408. serialize(instance, context) {
  409. instance.serialize(context);
  410. },
  411. deserialize(context) {
  412. const {
  413. read
  414. } = context;
  415. const dep = new CssDependency({
  416. identifier: read(),
  417. content: read(),
  418. layer: read(),
  419. supports: read(),
  420. media: read(),
  421. sourceMap: read()
  422. }, read(), read());
  423. const assets = read();
  424. const assetsInfo = read();
  425. dep.assets = assets;
  426. dep.assetsInfo = assetsInfo;
  427. dep.deserialize(context);
  428. return dep;
  429. }
  430. });
  431. return CssDependency;
  432. }
  433. /**
  434. * Returns all hooks for the given compilation
  435. * @param {Compilation} compilation the compilation
  436. * @returns {MiniCssExtractPluginCompilationHooks} hooks
  437. */
  438. static getCompilationHooks(compilation) {
  439. let hooks = compilationHooksMap.get(compilation);
  440. if (!hooks) {
  441. hooks = {
  442. beforeTagInsert: new SyncWaterfallHook(["source", "varNames"], "string"),
  443. linkPreload: new SyncWaterfallHook(["source", "chunk"]),
  444. linkPrefetch: new SyncWaterfallHook(["source", "chunk"])
  445. };
  446. compilationHooksMap.set(compilation, hooks);
  447. }
  448. return hooks;
  449. }
  450. /**
  451. * @param {PluginOptions=} options options
  452. */
  453. constructor(options = {}) {
  454. validate(/** @type {Schema} */schema, options, {
  455. baseDataPath: "options"
  456. });
  457. /**
  458. * @private
  459. * @type {WeakMap<Chunk, Set<CssModule>>}
  460. */
  461. this._sortedModulesCache = new WeakMap();
  462. /**
  463. * @private
  464. * @type {NormalizedPluginOptions}
  465. */
  466. this.options = {
  467. ignoreOrder: false,
  468. // TODO remove in the next major release
  469. experimentalUseImportModule: undefined,
  470. runtime: true,
  471. ...options
  472. };
  473. /**
  474. * @private
  475. * @type {RuntimeOptions}
  476. */
  477. this.runtimeOptions = {
  478. insert: options.insert,
  479. linkType:
  480. // Todo in next major release set default to "false"
  481. typeof options.linkType === "boolean" && /** @type {boolean} */options.linkType === true || typeof options.linkType === "undefined" ? "text/css" : options.linkType,
  482. attributes: options.attributes
  483. };
  484. }
  485. /**
  486. * @param {Compiler} compiler compiler
  487. */
  488. apply(compiler) {
  489. // Finally normalize filenames based on compiler options
  490. const normalizedFilename = this.options.filename || compiler.options.output.cssFilename || DEFAULT_FILENAME;
  491. let normalizedChunkFilename = this.options.chunkFilename || compiler.options.output.cssChunkFilename;
  492. if (!normalizedChunkFilename) {
  493. if (typeof normalizedFilename !== "function") {
  494. const hasName = /** @type {string} */normalizedFilename.includes("[name]");
  495. const hasId = /** @type {string} */normalizedFilename.includes("[id]");
  496. const hasChunkHash = /** @type {string} */
  497. normalizedFilename.includes("[chunkhash]");
  498. const hasContentHash = /** @type {string} */
  499. normalizedFilename.includes("[contenthash]");
  500. // Anything changing depending on chunk is fine
  501. if (hasChunkHash || hasContentHash || hasName || hasId) {
  502. normalizedChunkFilename = normalizedFilename;
  503. } else {
  504. // Otherwise prefix "[id]." in front of the basename to make it changing
  505. normalizedChunkFilename = /** @type {string} */
  506. normalizedFilename.replace(/(^|\/)([^/]*(?:\?|$))/, "$1[id].$2");
  507. }
  508. } else {
  509. normalizedChunkFilename = "[id].css";
  510. }
  511. }
  512. const {
  513. webpack
  514. } = compiler;
  515. if (this.options.experimentalUseImportModule && typeof (/** @type {Compiler["options"]["experiments"] & { executeModule?: boolean }} */
  516. compiler.options.experiments.executeModule) === "undefined") {
  517. /** @type {Compiler["options"]["experiments"] & { executeModule?: boolean }} */
  518. // @ts-expect-error TODO remove in the next major release
  519. compiler.options.experiments.executeModule = true;
  520. }
  521. // TODO bug in webpack, remove it after it will be fixed
  522. // webpack tries to `require` loader firstly when serializer doesn't found
  523. if (!registered.has(webpack)) {
  524. registered.add(webpack);
  525. webpack.util.serialization.registerLoader(/^mini-css-extract-plugin\//, trueFn);
  526. }
  527. const {
  528. splitChunks
  529. } = compiler.options.optimization;
  530. if (splitChunks && /** @type {string[]} */splitChunks.defaultSizeTypes.includes("...")) {
  531. /** @type {string[]} */
  532. splitChunks.defaultSizeTypes.push(MODULE_TYPE);
  533. }
  534. const CssModule = MiniCssExtractPlugin.getCssModule(webpack);
  535. const CssDependency = MiniCssExtractPlugin.getCssDependency(webpack);
  536. const {
  537. NormalModule
  538. } = compiler.webpack;
  539. compiler.hooks.compilation.tap(pluginName, compilation => {
  540. const {
  541. loader: normalModuleHook
  542. } = NormalModule.getCompilationHooks(compilation);
  543. normalModuleHook.tap(pluginName,
  544. /**
  545. * @param {object} loaderContext loader context
  546. */
  547. loaderContext => {
  548. /** @type {object & { [pluginSymbol]: { experimentalUseImportModule: boolean | undefined } }} */
  549. loaderContext[pluginSymbol] = {
  550. experimentalUseImportModule: this.options.experimentalUseImportModule
  551. };
  552. });
  553. });
  554. compiler.hooks.thisCompilation.tap(pluginName, compilation => {
  555. class CssModuleFactory {
  556. /**
  557. * @param {{ dependencies: Dependency[] }} dependencies
  558. * @param {(err?: null | Error, result?: CssModule) => void} callback
  559. */
  560. create({
  561. dependencies: [dependency]
  562. }, callback) {
  563. callback(undefined, new CssModule(/** @type {CssDependency} */dependency));
  564. }
  565. }
  566. compilation.dependencyFactories.set(CssDependency,
  567. // @ts-expect-error TODO fix in the next major release and fix using `CssModuleFactory extends webpack.ModuleFactory`
  568. new CssModuleFactory());
  569. class CssDependencyTemplate {
  570. apply() {}
  571. }
  572. compilation.dependencyTemplates.set(CssDependency, new CssDependencyTemplate());
  573. compilation.hooks.renderManifest.tap(pluginName,
  574. /**
  575. * @param {ReturnType<Compilation["getRenderManifest"]>} result result
  576. * @param {Parameters<Compilation["getRenderManifest"]>[0]} chunk chunk
  577. * @returns {ReturnType<Compilation["getRenderManifest"]>} a rendered manifest
  578. */
  579. (result, {
  580. chunk
  581. }) => {
  582. const {
  583. chunkGraph
  584. } = compilation;
  585. const {
  586. HotUpdateChunk
  587. } = webpack;
  588. // We don't need hot update chunks for css
  589. // We will use the real asset instead to update
  590. if (chunk instanceof HotUpdateChunk) {
  591. return result;
  592. }
  593. const renderedModules = /** @type {CssModule[]} */
  594. [...this.getChunkModules(chunk, chunkGraph)].filter(module => module.type === MODULE_TYPE);
  595. const filenameTemplate = /** @type {string} */
  596. chunk.canBeInitial() ? normalizedFilename : normalizedChunkFilename;
  597. if (renderedModules.length > 0) {
  598. result.push({
  599. render: () => this.renderContentAsset(compiler, compilation, chunk, renderedModules, compilation.runtimeTemplate.requestShortener, filenameTemplate, {
  600. contentHashType: MODULE_TYPE,
  601. chunk
  602. }),
  603. filenameTemplate,
  604. pathOptions: {
  605. chunk,
  606. contentHashType: MODULE_TYPE
  607. },
  608. identifier: `${pluginName}.${chunk.id}`,
  609. hash: chunk.contentHash[MODULE_TYPE]
  610. });
  611. }
  612. return result;
  613. });
  614. compilation.hooks.contentHash.tap(pluginName, chunk => {
  615. const {
  616. outputOptions,
  617. chunkGraph
  618. } = compilation;
  619. const modules = this.sortModules(compilation, chunk, /** @type {CssModule[]} */
  620. chunkGraph.getChunkModulesIterableBySourceType(chunk, MODULE_TYPE), compilation.runtimeTemplate.requestShortener);
  621. if (modules && modules.size > 0) {
  622. const {
  623. hashFunction,
  624. hashDigest,
  625. hashDigestLength
  626. } = outputOptions;
  627. const {
  628. createHash
  629. } = compiler.webpack.util;
  630. const hash = createHash(/** @type {string} */hashFunction);
  631. for (const m of modules) {
  632. hash.update(chunkGraph.getModuleHash(m, chunk.runtime));
  633. }
  634. chunk.contentHash[MODULE_TYPE] = /** @type {string} */
  635. hash.digest(hashDigest).slice(0, Math.max(0, /** @type {number} */hashDigestLength));
  636. }
  637. });
  638. // All the code below is dedicated to the runtime and can be skipped when the `runtime` option is `false`
  639. if (!this.options.runtime) {
  640. return;
  641. }
  642. const {
  643. Template,
  644. RuntimeGlobals,
  645. RuntimeModule,
  646. runtime
  647. } = webpack;
  648. /**
  649. * @param {Chunk} mainChunk
  650. * @param {Compilation} compilation
  651. * @returns {Record<string, number>}
  652. */
  653. const getCssChunkObject = (mainChunk, compilation) => {
  654. /** @type {Record<string, number>} */
  655. const obj = {};
  656. const {
  657. chunkGraph
  658. } = compilation;
  659. for (const chunk of mainChunk.getAllAsyncChunks()) {
  660. const modules = chunkGraph.getOrderedChunkModulesIterable(chunk, compareModulesByIdentifier);
  661. for (const module of modules) {
  662. if (module.type === MODULE_TYPE) {
  663. obj[(/** @type {string} */chunk.id)] = 1;
  664. break;
  665. }
  666. }
  667. }
  668. return obj;
  669. };
  670. /**
  671. * @param {Chunk} chunk chunk
  672. * @param {ChunkGraph} chunkGraph chunk graph
  673. * @returns {boolean} true, when the chunk has css
  674. */
  675. function chunkHasCss(chunk, chunkGraph) {
  676. // this function replace:
  677. // const chunkHasCss = require("webpack/lib/css/CssModulesPlugin").chunkHasCss;
  678. return Boolean(chunkGraph.getChunkModulesIterableBySourceType(chunk, "css/mini-extract"));
  679. }
  680. class CssLoadingRuntimeModule extends RuntimeModule {
  681. /**
  682. * @param {Set<string>} runtimeRequirements runtime Requirements
  683. * @param {RuntimeOptions} runtimeOptions runtime options
  684. */
  685. constructor(runtimeRequirements, runtimeOptions) {
  686. super("css loading", 10);
  687. this.runtimeRequirements = runtimeRequirements;
  688. this.runtimeOptions = runtimeOptions;
  689. }
  690. generate() {
  691. const {
  692. chunkGraph,
  693. chunk,
  694. runtimeRequirements
  695. } = this;
  696. const {
  697. runtimeTemplate,
  698. outputOptions: {
  699. crossOriginLoading
  700. }
  701. } = /** @type {Compilation} */this.compilation;
  702. const chunkMap = getCssChunkObject(/** @type {Chunk} */chunk, /** @type {Compilation} */this.compilation);
  703. const withLoading = runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers) && Object.keys(chunkMap).length > 0;
  704. const withHmr = runtimeRequirements.has(RuntimeGlobals.hmrDownloadUpdateHandlers);
  705. if (!withLoading && !withHmr) {
  706. return "";
  707. }
  708. const conditionMap = /** @type {ChunkGraph} */chunkGraph.getChunkConditionMap(/** @type {Chunk} */chunk, chunkHasCss);
  709. const hasCssMatcher = compileBooleanMatcher(conditionMap);
  710. const withPrefetch = runtimeRequirements.has(RuntimeGlobals.prefetchChunkHandlers);
  711. const withPreload = runtimeRequirements.has(RuntimeGlobals.preloadChunkHandlers);
  712. const {
  713. linkPreload,
  714. linkPrefetch
  715. } = MiniCssExtractPlugin.getCompilationHooks(compilation);
  716. return Template.asString(['if (typeof document === "undefined") return;', `var createStylesheet = ${runtimeTemplate.basicFunction("chunkId, fullhref, oldTag, resolve, reject", ['var linkTag = document.createElement("link");', this.runtimeOptions.attributes ? Template.asString(Object.entries(this.runtimeOptions.attributes).map(entry => {
  717. const [key, value] = entry;
  718. return `linkTag.setAttribute(${JSON.stringify(key)}, ${JSON.stringify(value)});`;
  719. })) : "", 'linkTag.rel = "stylesheet";', this.runtimeOptions.linkType ? `linkTag.type = ${JSON.stringify(this.runtimeOptions.linkType)};` : "", `if (${RuntimeGlobals.scriptNonce}) {`, Template.indent(`linkTag.nonce = ${RuntimeGlobals.scriptNonce};`), "}", `var onLinkComplete = ${runtimeTemplate.basicFunction("event", ["// avoid mem leaks.", "linkTag.onerror = linkTag.onload = null;", "if (event.type === 'load') {", Template.indent(["resolve();"]), "} else {", Template.indent(["var errorType = event && event.type;", "var realHref = event && event.target && event.target.href || fullhref;", 'var err = new Error("Loading CSS chunk " + chunkId + " failed.\\n(" + errorType + ": " + realHref + ")");', 'err.name = "ChunkLoadError";',
  720. // TODO remove `code` in the future major release to align with webpack
  721. 'err.code = "CSS_CHUNK_LOAD_FAILED";', "err.type = errorType;", "err.request = realHref;", "if (linkTag.parentNode) linkTag.parentNode.removeChild(linkTag)", "reject(err);"]), "}"])}`, "linkTag.onerror = linkTag.onload = onLinkComplete;", "linkTag.href = fullhref;", crossOriginLoading ? Template.asString(["if (linkTag.href.indexOf(window.location.origin + '/') !== 0) {", Template.indent(`linkTag.crossOrigin = ${JSON.stringify(crossOriginLoading)};`), "}"]) : "", MiniCssExtractPlugin.getCompilationHooks(compilation).beforeTagInsert.call("", {
  722. tag: "linkTag",
  723. chunkId: "chunkId",
  724. href: "fullhref",
  725. resolve: "resolve",
  726. reject: "reject"
  727. }) || "", typeof this.runtimeOptions.insert !== "undefined" ? typeof this.runtimeOptions.insert === "function" ? `(${this.runtimeOptions.insert.toString()})(linkTag)` : Template.asString([`var target = document.querySelector("${this.runtimeOptions.insert}");`, "target.parentNode.insertBefore(linkTag, target.nextSibling);"]) : Template.asString(["if (oldTag) {", Template.indent(["oldTag.parentNode.insertBefore(linkTag, oldTag.nextSibling);"]), "} else {", Template.indent(["document.head.appendChild(linkTag);"]), "}"]), "return linkTag;"])};`, `var findStylesheet = ${runtimeTemplate.basicFunction("href, fullhref", ['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 tag;']), "}", '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 tag;"]), "}"])};`, `var loadStylesheet = ${runtimeTemplate.basicFunction("chunkId", `return new Promise(${runtimeTemplate.basicFunction("resolve, reject", [`var href = ${RuntimeGlobals.require}.miniCssF(chunkId);`, `var fullhref = ${RuntimeGlobals.publicPath} + href;`, "if(findStylesheet(href, fullhref)) return resolve();", "createStylesheet(chunkId, fullhref, null, resolve, reject);"])});`)}`, withLoading ? Template.asString(["// object to store loaded CSS chunks", "var installedCssChunks = {", Template.indent(/** @type {string[]} */
  728. (/** @type {Chunk} */chunk.ids).map(id => `${JSON.stringify(id)}: 0`).join(",\n")), "};", "", `${RuntimeGlobals.ensureChunkHandlers}.miniCss = ${runtimeTemplate.basicFunction("chunkId, promises", [`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] = loadStylesheet(chunkId).then(${runtimeTemplate.basicFunction("", "installedCssChunks[chunkId] = 0;")}, ${runtimeTemplate.basicFunction("e", ["delete installedCssChunks[chunkId];", "throw e;"])}));`]), "}"])};`]) : "// no chunk loading", "", withHmr ? Template.asString(["var oldTags = [];", "var newTags = [];", `var applyHandler = ${runtimeTemplate.basicFunction("options", [`return { dispose: ${runtimeTemplate.basicFunction("", ["for(var i = 0; i < oldTags.length; i++) {", Template.indent(["var oldTag = oldTags[i];", "if(oldTag.parentNode) oldTag.parentNode.removeChild(oldTag);"]), "}", "oldTags.length = 0;"])}, apply: ${runtimeTemplate.basicFunction("", ['for(var i = 0; i < newTags.length; i++) newTags[i].rel = "stylesheet";', "newTags.length = 0;"])} };`])}`, `${RuntimeGlobals.hmrDownloadUpdateHandlers}.miniCss = ${runtimeTemplate.basicFunction("chunkIds, removedChunks, removedModules, promises, applyHandlers, updatedModulesList", ["applyHandlers.push(applyHandler);", `chunkIds.forEach(${runtimeTemplate.basicFunction("chunkId", [`var href = ${RuntimeGlobals.require}.miniCssF(chunkId);`, `var fullhref = ${RuntimeGlobals.publicPath} + href;`, "var oldTag = findStylesheet(href, fullhref);", "if(!oldTag) return;", `promises.push(new Promise(${runtimeTemplate.basicFunction("resolve, reject", [`var tag = createStylesheet(chunkId, fullhref, oldTag, ${runtimeTemplate.basicFunction("", ['tag.as = "style";', 'tag.rel = "preload";', "resolve();"])}, reject);`, "oldTags.push(oldTag);", "newTags.push(tag);"])}));`])});`])}`]) : "// no hmr", "", withPrefetch && withLoading && hasCssMatcher !== false ? `${RuntimeGlobals.prefetchChunkHandlers}.miniCss = ${runtimeTemplate.basicFunction("chunkId", [`if((!${RuntimeGlobals.hasOwnProperty}(installedCssChunks, chunkId) || installedCssChunks[chunkId] === undefined) && ${hasCssMatcher === true ? "true" : hasCssMatcher("chunkId")}) {`, Template.indent(["installedCssChunks[chunkId] = null;", linkPrefetch.call(Template.asString(["var link = document.createElement('link');", crossOriginLoading ? `link.crossOrigin = ${JSON.stringify(crossOriginLoading)};` : "", `if (${RuntimeGlobals.scriptNonce}) {`, Template.indent(`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`), "}", 'link.rel = "prefetch";', 'link.as = "style";', `link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.require}.miniCssF(chunkId);`]), /** @type {Chunk} */chunk), "document.head.appendChild(link);"]), "}"])};` : "// no prefetching", "", withPreload && withLoading && hasCssMatcher !== false ? `${RuntimeGlobals.preloadChunkHandlers}.miniCss = ${runtimeTemplate.basicFunction("chunkId", [`if((!${RuntimeGlobals.hasOwnProperty}(installedCssChunks, chunkId) || installedCssChunks[chunkId] === undefined) && ${hasCssMatcher === true ? "true" : hasCssMatcher("chunkId")}) {`, Template.indent(["installedCssChunks[chunkId] = null;", linkPreload.call(Template.asString(["var link = document.createElement('link');", "link.charset = 'utf-8';", `if (${RuntimeGlobals.scriptNonce}) {`, Template.indent(`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`), "}", 'link.rel = "preload";', 'link.as = "style";', `link.href = ${RuntimeGlobals.publicPath} + ${RuntimeGlobals.require}.miniCssF(chunkId);`, crossOriginLoading ? crossOriginLoading === "use-credentials" ? 'link.crossOrigin = "use-credentials";' : Template.asString(["if (link.href.indexOf(window.location.origin + '/') !== 0) {", Template.indent(`link.crossOrigin = ${JSON.stringify(crossOriginLoading)};`), "}"]) : ""]), /** @type {Chunk} */chunk), "document.head.appendChild(link);"]), "}"])};` : "// no preloaded"]);
  729. }
  730. }
  731. const enabledChunks = new WeakSet();
  732. /**
  733. * @param {Chunk} chunk chunk
  734. * @param {Set<string>} set set with runtime requirement
  735. */
  736. const handler = (chunk, set) => {
  737. if (enabledChunks.has(chunk)) {
  738. return;
  739. }
  740. enabledChunks.add(chunk);
  741. if (typeof normalizedChunkFilename === "string" && /\[(full)?hash(:\d+)?\]/.test(normalizedChunkFilename)) {
  742. set.add(RuntimeGlobals.getFullHash);
  743. }
  744. set.add(RuntimeGlobals.publicPath);
  745. compilation.addRuntimeModule(chunk, new runtime.GetChunkFilenameRuntimeModule(MODULE_TYPE, "mini-css", `${RuntimeGlobals.require}.miniCssF`,
  746. /**
  747. * @param {Chunk} referencedChunk a referenced chunk
  748. * @returns {ReturnType<import("webpack").runtime.GetChunkFilenameRuntimeModule["getFilenameForChunk"]>} a template value
  749. */
  750. referencedChunk => {
  751. if (!referencedChunk.contentHash[MODULE_TYPE]) {
  752. return false;
  753. }
  754. return referencedChunk.canBeInitial() ? (/** @type {Filename} */normalizedFilename) : (/** @type {ChunkFilename} */normalizedChunkFilename);
  755. }, set.has(RuntimeGlobals.hmrDownloadUpdateHandlers)));
  756. compilation.addRuntimeModule(chunk, new CssLoadingRuntimeModule(set, this.runtimeOptions));
  757. };
  758. compilation.hooks.runtimeRequirementInTree.for(RuntimeGlobals.ensureChunkHandlers).tap(pluginName, handler);
  759. compilation.hooks.runtimeRequirementInTree.for(RuntimeGlobals.hmrDownloadUpdateHandlers).tap(pluginName, handler);
  760. compilation.hooks.runtimeRequirementInTree.for(RuntimeGlobals.prefetchChunkHandlers).tap(pluginName, handler);
  761. compilation.hooks.runtimeRequirementInTree.for(RuntimeGlobals.preloadChunkHandlers).tap(pluginName, handler);
  762. });
  763. }
  764. /**
  765. * @private
  766. * @param {Chunk} chunk chunk
  767. * @param {ChunkGraph} chunkGraph chunk graph
  768. * @returns {Iterable<Module>} modules
  769. */
  770. getChunkModules(chunk, chunkGraph) {
  771. return typeof chunkGraph !== "undefined" ? chunkGraph.getOrderedChunkModulesIterable(chunk, compareModulesByIdentifier) : chunk.modulesIterable;
  772. }
  773. /**
  774. * @private
  775. * @param {Compilation} compilation compilation
  776. * @param {Chunk} chunk chunk
  777. * @param {CssModule[]} modules modules
  778. * @param {Compilation["requestShortener"]} requestShortener request shortener
  779. * @returns {Set<CssModule>} css modules
  780. */
  781. sortModules(compilation, chunk, modules, requestShortener) {
  782. let usedModules = this._sortedModulesCache.get(chunk);
  783. if (usedModules || !modules) {
  784. return /** @type {Set<CssModule>} */usedModules;
  785. }
  786. /** @type {CssModule[]} */
  787. const modulesList = [...modules];
  788. // Store dependencies for modules
  789. /** @type {Map<CssModule, Set<CssModule>>} */
  790. const moduleDependencies = new Map(modulesList.map(m => [m, (/** @type {Set<CssModule>} */
  791. new Set())]));
  792. /** @type {Map<CssModule, Map<CssModule, Set<ChunkGroup>>>} */
  793. const moduleDependenciesReasons = new Map(modulesList.map(m => [m, new Map()]));
  794. // Get ordered list of modules per chunk group
  795. // This loop also gathers dependencies from the ordered lists
  796. // Lists are in reverse order to allow to use Array.pop()
  797. /** @type {CssModule[][]} */
  798. const modulesByChunkGroup = Array.from(chunk.groupsIterable, chunkGroup => {
  799. const sortedModules = modulesList.map(module => ({
  800. module,
  801. index: chunkGroup.getModulePostOrderIndex(module)
  802. })).filter(item => item.index !== undefined).sort((a, b) => /** @type {number} */b.index - (/** @type {number} */a.index)).map(item => item.module);
  803. for (let i = 0; i < sortedModules.length; i++) {
  804. const set = moduleDependencies.get(sortedModules[i]);
  805. const reasons = /** @type {Map<CssModule, Set<ChunkGroup>>} */
  806. moduleDependenciesReasons.get(sortedModules[i]);
  807. for (let j = i + 1; j < sortedModules.length; j++) {
  808. const module = sortedModules[j];
  809. /** @type {Set<CssModule>} */
  810. set.add(module);
  811. const reason = reasons.get(module) || (/** @type {Set<ChunkGroup>} */new Set());
  812. reason.add(chunkGroup);
  813. reasons.set(module, reason);
  814. }
  815. }
  816. return sortedModules;
  817. });
  818. // set with already included modules in correct order
  819. usedModules = new Set();
  820. /**
  821. * @param {CssModule} m a css module
  822. * @returns {boolean} true when module unused, otherwise false
  823. */
  824. const unusedModulesFilter = m => !(/** @type {Set<CssModule>} */usedModules.has(m));
  825. while (usedModules.size < modulesList.length) {
  826. let success = false;
  827. let bestMatch;
  828. let bestMatchDeps;
  829. // get first module where dependencies are fulfilled
  830. for (const list of modulesByChunkGroup) {
  831. // skip and remove already added modules
  832. while (list.length > 0 && usedModules.has(list[list.length - 1])) {
  833. list.pop();
  834. }
  835. // skip empty lists
  836. if (list.length !== 0) {
  837. const module = list[list.length - 1];
  838. const deps = /** @type {Set<CssModule>} */
  839. moduleDependencies.get(module);
  840. // determine dependencies that are not yet included
  841. const failedDeps = [...deps].filter(unusedModulesFilter);
  842. // store best match for fallback behavior
  843. if (!bestMatchDeps || bestMatchDeps.length > failedDeps.length) {
  844. bestMatch = list;
  845. bestMatchDeps = failedDeps;
  846. }
  847. if (failedDeps.length === 0) {
  848. // use this module and remove it from list
  849. usedModules.add(/** @type {CssModule} */list.pop());
  850. success = true;
  851. break;
  852. }
  853. }
  854. }
  855. if (!success) {
  856. // no module found => there is a conflict
  857. // use list with fewest failed deps
  858. // and emit a warning
  859. const fallbackModule = /** @type {CssModule[]} */bestMatch.pop();
  860. if (!this.options.ignoreOrder) {
  861. const reasons = moduleDependenciesReasons.get(/** @type {CssModule} */fallbackModule);
  862. compilation.warnings.push(/** @type {WebpackError} */
  863. new Error([`chunk ${chunk.name || chunk.id} [${pluginName}]`, "Conflicting order. Following module has been added:", ` * ${ /** @type {CssModule} */fallbackModule.readableIdentifier(requestShortener)}`, "despite it was not able to fulfill desired ordering with these modules:", ... /** @type {CssModule[]} */bestMatchDeps.map(m => {
  864. const goodReasonsMap = moduleDependenciesReasons.get(m);
  865. const goodReasons = goodReasonsMap && goodReasonsMap.get(/** @type {CssModule} */fallbackModule);
  866. const failedChunkGroups = Array.from(/** @type {Set<ChunkGroup>} */
  867. /** @type {Map<CssModule, Set<ChunkGroup>>} */
  868. reasons.get(m), cg => cg.name).join(", ");
  869. const goodChunkGroups = goodReasons && Array.from(goodReasons, cg => cg.name).join(", ");
  870. 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");
  871. })].join("\n")));
  872. }
  873. usedModules.add(/** @type {CssModule} */fallbackModule);
  874. }
  875. }
  876. this._sortedModulesCache.set(chunk, usedModules);
  877. return usedModules;
  878. }
  879. /**
  880. * @private
  881. * @param {Compiler} compiler compiler
  882. * @param {Compilation} compilation compilation
  883. * @param {Chunk} chunk chunk
  884. * @param {CssModule[]} modules modules
  885. * @param {Compiler["requestShortener"]} requestShortener request shortener
  886. * @param {string} filenameTemplate filename template
  887. * @param {Parameters<Exclude<Required<Configuration>['output']['filename'], string | undefined>>[0]} pathData path data
  888. * @returns {Source} source
  889. */
  890. renderContentAsset(compiler, compilation, chunk, modules, requestShortener, filenameTemplate, pathData) {
  891. const usedModules = this.sortModules(compilation, chunk, modules, requestShortener);
  892. const {
  893. ConcatSource,
  894. SourceMapSource,
  895. RawSource
  896. } = compiler.webpack.sources;
  897. const source = new ConcatSource();
  898. const externalsSource = new ConcatSource();
  899. for (const module of usedModules) {
  900. let content = module.content.toString();
  901. const readableIdentifier = module.readableIdentifier(requestShortener);
  902. const startsWithAtRuleImport = content.startsWith("@import url");
  903. let header;
  904. if (compilation.outputOptions.pathinfo) {
  905. // From https://github.com/webpack/webpack/blob/29eff8a74ecc2f87517b627dee451c2af9ed3f3f/lib/ModuleInfoHeaderPlugin.js#L191-L194
  906. const reqStr = readableIdentifier.replace(/\*\//g, "*_/");
  907. const reqStrStar = "*".repeat(reqStr.length);
  908. const headerStr = `/*!****${reqStrStar}****!*\\\n !*** ${reqStr} ***!\n \\****${reqStrStar}****/\n`;
  909. header = new RawSource(headerStr);
  910. }
  911. if (startsWithAtRuleImport) {
  912. if (typeof header !== "undefined") {
  913. externalsSource.add(header);
  914. }
  915. // HACK for IE
  916. // http://stackoverflow.com/a/14676665/1458162
  917. if (module.media || module.supports || typeof module.layer === "string") {
  918. let atImportExtra = "";
  919. const needLayer = typeof module.layer === "string";
  920. if (needLayer) {
  921. atImportExtra += module.layer.length > 0 ? ` layer(${module.layer})` : " layer";
  922. }
  923. if (module.supports) {
  924. atImportExtra += ` supports(${module.supports})`;
  925. }
  926. if (module.media) {
  927. atImportExtra += ` ${module.media}`;
  928. }
  929. // insert media into the @import
  930. // this is rar
  931. // TODO improve this and parse the CSS to support multiple medias
  932. content = content.replace(/;|\s*$/, `${atImportExtra};`);
  933. }
  934. externalsSource.add(content);
  935. externalsSource.add("\n");
  936. } else {
  937. if (typeof header !== "undefined") {
  938. source.add(header);
  939. }
  940. if (module.supports) {
  941. source.add(`@supports (${module.supports}) {\n`);
  942. }
  943. if (module.media) {
  944. source.add(`@media ${module.media} {\n`);
  945. }
  946. const needLayer = typeof module.layer === "string";
  947. if (needLayer) {
  948. source.add(`@layer${module.layer.length > 0 ? ` ${module.layer}` : ""} {\n`);
  949. }
  950. const {
  951. path: filename
  952. } = compilation.getPathWithInfo(filenameTemplate, pathData);
  953. const undoPath = getUndoPath(filename, compiler.outputPath, false);
  954. // replacements
  955. content = content.replace(new RegExp(ABSOLUTE_PUBLIC_PATH, "g"), "");
  956. content = content.replace(new RegExp(SINGLE_DOT_PATH_SEGMENT, "g"), ".");
  957. content = content.replace(new RegExp(AUTO_PUBLIC_PATH, "g"), undoPath);
  958. const entryOptions = chunk.getEntryOptions();
  959. const baseUriReplacement = entryOptions && entryOptions.baseUri || undoPath;
  960. content = content.replace(new RegExp(BASE_URI, "g"), baseUriReplacement);
  961. if (module.sourceMap) {
  962. source.add(new SourceMapSource(content, readableIdentifier, module.sourceMap.toString()));
  963. } else {
  964. source.add(new RawSource(content));
  965. }
  966. source.add("\n");
  967. if (needLayer) {
  968. source.add("}\n");
  969. }
  970. if (module.media) {
  971. source.add("}\n");
  972. }
  973. if (module.supports) {
  974. source.add("}\n");
  975. }
  976. }
  977. }
  978. return new ConcatSource(externalsSource, source);
  979. }
  980. }
  981. MiniCssExtractPlugin.pluginName = pluginName;
  982. MiniCssExtractPlugin.pluginSymbol = pluginSymbol;
  983. MiniCssExtractPlugin.loader = require.resolve("./loader");
  984. module.exports = MiniCssExtractPlugin;