index.js 40 KB

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