index.js 45 KB

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