index.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.pluginSymbol = exports.pluginName = void 0;
  6. var _schemaUtils = require("schema-utils");
  7. var _pluginOptions = _interopRequireDefault(require("./plugin-options.json"));
  8. var _utils = require("./utils");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /* eslint-disable class-methods-use-this */
  11. const pluginName = 'mini-css-extract-plugin';
  12. exports.pluginName = pluginName;
  13. const pluginSymbol = Symbol(pluginName);
  14. exports.pluginSymbol = pluginSymbol;
  15. const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
  16. const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
  17. const REGEXP_NAME = /\[name\]/i;
  18. const DEFAULT_FILENAME = '[name].css';
  19. const TYPES = new Set([_utils.MODULE_TYPE]);
  20. const CODE_GENERATION_RESULT = {
  21. sources: new Map(),
  22. runtimeRequirements: new Set()
  23. };
  24. /**
  25. * @type WeakMap<webpack, CssModule>
  26. */
  27. const cssModuleCache = new WeakMap();
  28. /**
  29. * @type WeakMap<webpack, CssDependency>
  30. */
  31. const cssDependencyCache = new WeakMap();
  32. const registered = new WeakSet();
  33. class MiniCssExtractPlugin {
  34. static getCssModule(webpack) {
  35. /**
  36. * Prevent creation of multiple CssModule classes to allow other integrations to get the current CssModule.
  37. */
  38. if (cssModuleCache.has(webpack)) {
  39. return cssModuleCache.get(webpack);
  40. }
  41. class CssModule extends webpack.Module {
  42. constructor({
  43. context,
  44. identifier,
  45. identifierIndex,
  46. content,
  47. media,
  48. sourceMap,
  49. assets,
  50. assetsInfo
  51. }) {
  52. super(_utils.MODULE_TYPE, context);
  53. this.id = '';
  54. this._context = context;
  55. this._identifier = identifier;
  56. this._identifierIndex = identifierIndex;
  57. this.content = content;
  58. this.media = media;
  59. this.sourceMap = sourceMap;
  60. this.assets = assets;
  61. this.assetsInfo = assetsInfo;
  62. this._needBuild = true;
  63. } // no source() so webpack 4 doesn't do add stuff to the bundle
  64. size() {
  65. return this.content.length;
  66. }
  67. identifier() {
  68. return `css|${this._identifier}|${this._identifierIndex}`;
  69. }
  70. readableIdentifier(requestShortener) {
  71. return `css ${requestShortener.shorten(this._identifier)}${this._identifierIndex ? ` (${this._identifierIndex})` : ''}`;
  72. } // eslint-disable-next-line class-methods-use-this
  73. getSourceTypes() {
  74. return TYPES;
  75. } // eslint-disable-next-line class-methods-use-this
  76. codeGeneration() {
  77. return CODE_GENERATION_RESULT;
  78. }
  79. nameForCondition() {
  80. const resource = this._identifier.split('!').pop();
  81. const idx = resource.indexOf('?');
  82. if (idx >= 0) {
  83. return resource.substring(0, idx);
  84. }
  85. return resource;
  86. }
  87. updateCacheModule(module) {
  88. if (this.content !== module.content || this.media !== module.media || this.sourceMap !== module.sourceMap || this.assets !== module.assets || this.assetsInfo !== module.assetsInfo) {
  89. this._needBuild = true;
  90. this.content = module.content;
  91. this.media = module.media;
  92. this.sourceMap = module.sourceMap;
  93. this.assets = module.assets;
  94. this.assetsInfo = module.assetsInfo;
  95. }
  96. } // eslint-disable-next-line class-methods-use-this
  97. needRebuild() {
  98. return this._needBuild;
  99. } // eslint-disable-next-line class-methods-use-this
  100. needBuild(context, callback) {
  101. callback(null, this._needBuild);
  102. }
  103. build(options, compilation, resolver, fileSystem, callback) {
  104. this.buildInfo = {
  105. assets: this.assets,
  106. assetsInfo: this.assetsInfo,
  107. cacheable: true,
  108. hash: this._computeHash(compilation.outputOptions.hashFunction)
  109. };
  110. this.buildMeta = {};
  111. this._needBuild = false;
  112. callback();
  113. }
  114. _computeHash(hashFunction) {
  115. const hash = webpack.util.createHash(hashFunction);
  116. hash.update(this.content);
  117. hash.update(this.media || '');
  118. hash.update(this.sourceMap || '');
  119. return hash.digest('hex');
  120. }
  121. updateHash(hash, context) {
  122. super.updateHash(hash, context);
  123. hash.update(this.buildInfo.hash);
  124. }
  125. serialize(context) {
  126. const {
  127. write
  128. } = context;
  129. write(this._context);
  130. write(this._identifier);
  131. write(this._identifierIndex);
  132. write(this.content);
  133. write(this.media);
  134. write(this.sourceMap);
  135. write(this.assets);
  136. write(this.assetsInfo);
  137. write(this._needBuild);
  138. super.serialize(context);
  139. }
  140. deserialize(context) {
  141. this._needBuild = context.read();
  142. super.deserialize(context);
  143. }
  144. }
  145. cssModuleCache.set(webpack, CssModule);
  146. if (webpack.util && webpack.util.serialization && webpack.util.serialization.register) {
  147. webpack.util.serialization.register(CssModule, 'mini-css-extract-plugin/dist/CssModule', null, {
  148. serialize(instance, context) {
  149. instance.serialize(context);
  150. },
  151. deserialize(context) {
  152. const {
  153. read
  154. } = context;
  155. const contextModule = read();
  156. const identifier = read();
  157. const identifierIndex = read();
  158. const content = read();
  159. const media = read();
  160. const sourceMap = read();
  161. const assets = read();
  162. const assetsInfo = read();
  163. const dep = new CssModule({
  164. context: contextModule,
  165. identifier,
  166. identifierIndex,
  167. content,
  168. media,
  169. sourceMap,
  170. assets,
  171. assetsInfo
  172. });
  173. dep.deserialize(context);
  174. return dep;
  175. }
  176. });
  177. }
  178. return CssModule;
  179. }
  180. static getCssDependency(webpack) {
  181. /**
  182. * Prevent creation of multiple CssDependency classes to allow other integrations to get the current CssDependency.
  183. */
  184. if (cssDependencyCache.has(webpack)) {
  185. return cssDependencyCache.get(webpack);
  186. } // eslint-disable-next-line no-shadow
  187. class CssDependency extends webpack.Dependency {
  188. constructor({
  189. identifier,
  190. content,
  191. media,
  192. sourceMap
  193. }, context, identifierIndex) {
  194. super();
  195. this.identifier = identifier;
  196. this.identifierIndex = identifierIndex;
  197. this.content = content;
  198. this.media = media;
  199. this.sourceMap = sourceMap;
  200. this.context = context; // eslint-disable-next-line no-undefined
  201. this.assets = undefined; // eslint-disable-next-line no-undefined
  202. this.assetsInfo = undefined;
  203. }
  204. getResourceIdentifier() {
  205. return `css-module-${this.identifier}-${this.identifierIndex}`;
  206. } // eslint-disable-next-line class-methods-use-this
  207. getModuleEvaluationSideEffectsState() {
  208. return webpack.ModuleGraphConnection.TRANSITIVE_ONLY;
  209. }
  210. serialize(context) {
  211. const {
  212. write
  213. } = context;
  214. write(this.identifier);
  215. write(this.content);
  216. write(this.media);
  217. write(this.sourceMap);
  218. write(this.context);
  219. write(this.identifierIndex);
  220. write(this.assets);
  221. write(this.assetsInfo);
  222. super.serialize(context);
  223. }
  224. deserialize(context) {
  225. super.deserialize(context);
  226. }
  227. }
  228. cssDependencyCache.set(webpack, CssDependency);
  229. if (webpack.util && webpack.util.serialization && webpack.util.serialization.register) {
  230. webpack.util.serialization.register(CssDependency, 'mini-css-extract-plugin/dist/CssDependency', null, {
  231. serialize(instance, context) {
  232. instance.serialize(context);
  233. },
  234. deserialize(context) {
  235. const {
  236. read
  237. } = context;
  238. const dep = new CssDependency({
  239. identifier: read(),
  240. content: read(),
  241. media: read(),
  242. sourceMap: read()
  243. }, read(), read());
  244. const assets = read();
  245. const assetsInfo = read();
  246. dep.assets = assets;
  247. dep.assetsInfo = assetsInfo;
  248. dep.deserialize(context);
  249. return dep;
  250. }
  251. });
  252. }
  253. return CssDependency;
  254. }
  255. constructor(options = {}) {
  256. (0, _schemaUtils.validate)(_pluginOptions.default, options, {
  257. name: 'Mini CSS Extract Plugin',
  258. baseDataPath: 'options'
  259. });
  260. this._sortedModulesCache = new WeakMap();
  261. this.options = Object.assign({
  262. filename: DEFAULT_FILENAME,
  263. ignoreOrder: false,
  264. experimentalUseImportModule: false
  265. }, options);
  266. this.runtimeOptions = {
  267. insert: options.insert,
  268. linkType: // Todo in next major release set default to "false"
  269. options.linkType === true || typeof options.linkType === 'undefined' ? 'text/css' : options.linkType,
  270. attributes: options.attributes
  271. };
  272. if (!this.options.chunkFilename) {
  273. const {
  274. filename
  275. } = this.options;
  276. if (typeof filename !== 'function') {
  277. const hasName = filename.includes('[name]');
  278. const hasId = filename.includes('[id]');
  279. const hasChunkHash = filename.includes('[chunkhash]');
  280. const hasContentHash = filename.includes('[contenthash]'); // Anything changing depending on chunk is fine
  281. if (hasChunkHash || hasContentHash || hasName || hasId) {
  282. this.options.chunkFilename = filename;
  283. } else {
  284. // Otherwise prefix "[id]." in front of the basename to make it changing
  285. this.options.chunkFilename = filename.replace(/(^|\/)([^/]*(?:\?|$))/, '$1[id].$2');
  286. }
  287. } else {
  288. this.options.chunkFilename = '[id].css';
  289. }
  290. }
  291. }
  292. /** @param {import("webpack").Compiler} compiler */
  293. apply(compiler) {
  294. const webpack = compiler.webpack ? compiler.webpack : // eslint-disable-next-line global-require
  295. require('webpack');
  296. if (this.options.experimentalUseImportModule) {
  297. if (!compiler.options.experiments) {
  298. throw new Error('experimentalUseImportModule is only support for webpack >= 5.33.2');
  299. }
  300. if (typeof compiler.options.experiments.executeModule === 'undefined') {
  301. // eslint-disable-next-line no-param-reassign
  302. compiler.options.experiments.executeModule = true;
  303. }
  304. } // TODO bug in webpack, remove it after it will be fixed
  305. // webpack tries to `require` loader firstly when serializer doesn't found
  306. if (webpack.util && webpack.util.serialization && webpack.util.serialization.registerLoader && !registered.has(webpack)) {
  307. registered.add(webpack);
  308. webpack.util.serialization.registerLoader(/^mini-css-extract-plugin\//, _utils.trueFn);
  309. }
  310. const isWebpack4 = compiler.webpack ? false : typeof compiler.resolvers !== 'undefined';
  311. if (!isWebpack4) {
  312. const {
  313. splitChunks
  314. } = compiler.options.optimization;
  315. if (splitChunks) {
  316. if (splitChunks.defaultSizeTypes.includes('...')) {
  317. splitChunks.defaultSizeTypes.push(_utils.MODULE_TYPE);
  318. }
  319. }
  320. }
  321. const CssModule = MiniCssExtractPlugin.getCssModule(webpack);
  322. const CssDependency = MiniCssExtractPlugin.getCssDependency(webpack);
  323. const NormalModule = compiler.webpack && compiler.webpack.NormalModule ? compiler.webpack.NormalModule : // eslint-disable-next-line global-require
  324. require('webpack/lib/NormalModule');
  325. compiler.hooks.compilation.tap(pluginName, compilation => {
  326. const normalModuleHook = typeof NormalModule.getCompilationHooks !== 'undefined' ? NormalModule.getCompilationHooks(compilation).loader : compilation.hooks.normalModuleLoader;
  327. normalModuleHook.tap(pluginName, loaderContext => {
  328. // eslint-disable-next-line no-param-reassign
  329. loaderContext[pluginSymbol] = {
  330. experimentalUseImportModule: this.options.experimentalUseImportModule
  331. };
  332. });
  333. });
  334. compiler.hooks.thisCompilation.tap(pluginName, compilation => {
  335. class CssModuleFactory {
  336. // eslint-disable-next-line class-methods-use-this
  337. create({
  338. dependencies: [dependency]
  339. }, callback) {
  340. callback(null, new CssModule(dependency));
  341. }
  342. }
  343. compilation.dependencyFactories.set(CssDependency, new CssModuleFactory());
  344. class CssDependencyTemplate {
  345. // eslint-disable-next-line class-methods-use-this
  346. apply() {}
  347. }
  348. compilation.dependencyTemplates.set(CssDependency, new CssDependencyTemplate());
  349. if (isWebpack4) {
  350. compilation.mainTemplate.hooks.renderManifest.tap(pluginName, (result, {
  351. chunk
  352. }) => {
  353. const {
  354. chunkGraph
  355. } = compilation;
  356. const renderedModules = Array.from(this.getChunkModules(chunk, chunkGraph)).filter(module => module.type === _utils.MODULE_TYPE);
  357. const filenameTemplate = chunk.filenameTemplate || this.options.filename;
  358. if (renderedModules.length > 0) {
  359. result.push({
  360. render: () => this.renderContentAsset(compiler, compilation, chunk, renderedModules, compilation.runtimeTemplate.requestShortener),
  361. filenameTemplate,
  362. pathOptions: {
  363. chunk,
  364. contentHashType: _utils.MODULE_TYPE
  365. },
  366. identifier: `${pluginName}.${chunk.id}`,
  367. hash: chunk.contentHash[_utils.MODULE_TYPE]
  368. });
  369. }
  370. });
  371. compilation.chunkTemplate.hooks.renderManifest.tap(pluginName, (result, {
  372. chunk
  373. }) => {
  374. const {
  375. chunkGraph
  376. } = compilation;
  377. const renderedModules = Array.from(this.getChunkModules(chunk, chunkGraph)).filter(module => module.type === _utils.MODULE_TYPE);
  378. const filenameTemplate = chunk.filenameTemplate || this.options.chunkFilename;
  379. if (renderedModules.length > 0) {
  380. result.push({
  381. render: () => this.renderContentAsset(compiler, compilation, chunk, renderedModules, compilation.runtimeTemplate.requestShortener),
  382. filenameTemplate,
  383. pathOptions: {
  384. chunk,
  385. contentHashType: _utils.MODULE_TYPE
  386. },
  387. identifier: `${pluginName}.${chunk.id}`,
  388. hash: chunk.contentHash[_utils.MODULE_TYPE]
  389. });
  390. }
  391. });
  392. } else {
  393. compilation.hooks.renderManifest.tap(pluginName, (result, {
  394. chunk
  395. }) => {
  396. const {
  397. chunkGraph
  398. } = compilation;
  399. const {
  400. HotUpdateChunk
  401. } = webpack; // We don't need hot update chunks for css
  402. // We will use the real asset instead to update
  403. if (chunk instanceof HotUpdateChunk) {
  404. return;
  405. }
  406. const renderedModules = Array.from(this.getChunkModules(chunk, chunkGraph)).filter(module => module.type === _utils.MODULE_TYPE);
  407. const filenameTemplate = chunk.canBeInitial() ? this.options.filename : this.options.chunkFilename;
  408. if (renderedModules.length > 0) {
  409. result.push({
  410. render: () => this.renderContentAsset(compiler, compilation, chunk, renderedModules, compilation.runtimeTemplate.requestShortener),
  411. filenameTemplate,
  412. pathOptions: {
  413. chunk,
  414. contentHashType: _utils.MODULE_TYPE
  415. },
  416. identifier: `${pluginName}.${chunk.id}`,
  417. hash: chunk.contentHash[_utils.MODULE_TYPE]
  418. });
  419. }
  420. });
  421. }
  422. /*
  423. * For webpack 5 this will be unneeded once the logic uses a RuntimeModule
  424. * as the content of runtime modules is hashed and added to the chunk hash automatically
  425. * */
  426. if (isWebpack4) {
  427. compilation.mainTemplate.hooks.hashForChunk.tap(pluginName, (hash, chunk) => {
  428. const {
  429. chunkFilename
  430. } = this.options;
  431. if (REGEXP_CHUNKHASH.test(chunkFilename)) {
  432. hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
  433. }
  434. if (REGEXP_CONTENTHASH.test(chunkFilename)) {
  435. hash.update(JSON.stringify(chunk.getChunkMaps(true).contentHash[_utils.MODULE_TYPE] || {}));
  436. }
  437. if (REGEXP_NAME.test(chunkFilename)) {
  438. hash.update(JSON.stringify(chunk.getChunkMaps(true).name));
  439. }
  440. });
  441. }
  442. compilation.hooks.contentHash.tap(pluginName, chunk => {
  443. const {
  444. outputOptions,
  445. chunkGraph
  446. } = compilation;
  447. const modules = isWebpack4 ? Array.from(this.getChunkModules(chunk, chunkGraph)).filter(module => module.type === _utils.MODULE_TYPE) : this.sortModules(compilation, chunk, chunkGraph.getChunkModulesIterableBySourceType(chunk, _utils.MODULE_TYPE), compilation.runtimeTemplate.requestShortener);
  448. if (modules) {
  449. const {
  450. hashFunction,
  451. hashDigest,
  452. hashDigestLength
  453. } = outputOptions;
  454. const createHash = compiler.webpack ? compiler.webpack.util.createHash : webpack.util.createHash;
  455. const hash = createHash(hashFunction);
  456. if (isWebpack4) {
  457. for (const m of modules) {
  458. m.updateHash(hash);
  459. }
  460. } else {
  461. for (const m of modules) {
  462. hash.update(chunkGraph.getModuleHash(m, chunk.runtime));
  463. }
  464. } // eslint-disable-next-line no-param-reassign
  465. chunk.contentHash[_utils.MODULE_TYPE] = hash.digest(hashDigest).substring(0, hashDigestLength);
  466. }
  467. });
  468. const {
  469. Template
  470. } = webpack;
  471. const {
  472. mainTemplate
  473. } = compilation;
  474. if (isWebpack4) {
  475. mainTemplate.hooks.localVars.tap(pluginName, (source, chunk) => {
  476. const chunkMap = this.getCssChunkObject(chunk, compilation);
  477. if (Object.keys(chunkMap).length > 0) {
  478. return Template.asString([source, '', '// object to store loaded CSS chunks', 'var installedCssChunks = {', Template.indent(chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(',\n')), '};']);
  479. }
  480. return source;
  481. });
  482. mainTemplate.hooks.requireEnsure.tap(pluginName, (source, chunk, hash) => {
  483. const chunkMap = this.getCssChunkObject(chunk, compilation);
  484. if (Object.keys(chunkMap).length > 0) {
  485. const chunkMaps = chunk.getChunkMaps();
  486. const {
  487. crossOriginLoading
  488. } = mainTemplate.outputOptions;
  489. const linkHrefPath = mainTemplate.getAssetPath(JSON.stringify(this.options.chunkFilename), {
  490. hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
  491. hashWithLength: length => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
  492. chunk: {
  493. id: '" + chunkId + "',
  494. hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
  495. hashWithLength(length) {
  496. const shortChunkHashMap = Object.create(null);
  497. for (const chunkId of Object.keys(chunkMaps.hash)) {
  498. if (typeof chunkMaps.hash[chunkId] === 'string') {
  499. shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substring(0, length);
  500. }
  501. }
  502. return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
  503. },
  504. contentHash: {
  505. [_utils.MODULE_TYPE]: `" + ${JSON.stringify(chunkMaps.contentHash[_utils.MODULE_TYPE])}[chunkId] + "`
  506. },
  507. contentHashWithLength: {
  508. [_utils.MODULE_TYPE]: length => {
  509. const shortContentHashMap = {};
  510. const contentHash = chunkMaps.contentHash[_utils.MODULE_TYPE];
  511. for (const chunkId of Object.keys(contentHash)) {
  512. if (typeof contentHash[chunkId] === 'string') {
  513. shortContentHashMap[chunkId] = contentHash[chunkId].substring(0, length);
  514. }
  515. }
  516. return `" + ${JSON.stringify(shortContentHashMap)}[chunkId] + "`;
  517. }
  518. },
  519. name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`
  520. },
  521. contentHashType: _utils.MODULE_TYPE
  522. });
  523. return Template.asString([source, '', `// ${pluginName} CSS loading`, `var cssChunks = ${JSON.stringify(chunkMap)};`, 'if(installedCssChunks[chunkId]) promises.push(installedCssChunks[chunkId]);', 'else if(installedCssChunks[chunkId] !== 0 && cssChunks[chunkId]) {', Template.indent(['promises.push(installedCssChunks[chunkId] = new Promise(function(resolve, reject) {', Template.indent([`var href = ${linkHrefPath};`, `var fullhref = ${mainTemplate.requireFn}.p + href;`, 'var existingLinkTags = document.getElementsByTagName("link");', 'for(var i = 0; i < existingLinkTags.length; i++) {', Template.indent(['var tag = existingLinkTags[i];', 'var dataHref = tag.getAttribute("data-href") || tag.getAttribute("href");', 'if(tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref)) return resolve();']), '}', 'var existingStyleTags = document.getElementsByTagName("style");', 'for(var i = 0; i < existingStyleTags.length; i++) {', Template.indent(['var tag = existingStyleTags[i];', 'var dataHref = tag.getAttribute("data-href");', 'if(dataHref === href || dataHref === fullhref) return resolve();']), '}', 'var linkTag = document.createElement("link");', this.runtimeOptions.attributes ? Template.asString(Object.entries(this.runtimeOptions.attributes).map(entry => {
  524. const [key, value] = entry;
  525. return `linkTag.setAttribute(${JSON.stringify(key)}, ${JSON.stringify(value)});`;
  526. })) : '', 'linkTag.rel = "stylesheet";', this.runtimeOptions.linkType ? `linkTag.type = ${JSON.stringify(this.runtimeOptions.linkType)};` : '', 'var onLinkComplete = function (event) {', Template.indent(['// 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;', 'delete installedCssChunks[chunkId]', '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(['document.head.appendChild(linkTag);'])]), '}).then(function() {', Template.indent(['installedCssChunks[chunkId] = 0;']), '}));']), '}']);
  527. }
  528. return source;
  529. });
  530. } else {
  531. const {
  532. RuntimeGlobals,
  533. runtime
  534. } = webpack; // eslint-disable-next-line no-shadow
  535. const getCssChunkObject = (mainChunk, compilation) => {
  536. const obj = {};
  537. const {
  538. chunkGraph
  539. } = compilation;
  540. for (const chunk of mainChunk.getAllAsyncChunks()) {
  541. const modules = chunkGraph.getOrderedChunkModulesIterable(chunk, _utils.compareModulesByIdentifier);
  542. for (const module of modules) {
  543. if (module.type === _utils.MODULE_TYPE) {
  544. obj[chunk.id] = 1;
  545. break;
  546. }
  547. }
  548. }
  549. return obj;
  550. };
  551. const {
  552. RuntimeModule
  553. } = webpack;
  554. class CssLoadingRuntimeModule extends RuntimeModule {
  555. constructor(runtimeRequirements, runtimeOptions) {
  556. super('css loading', 10);
  557. this.runtimeRequirements = runtimeRequirements;
  558. this.runtimeOptions = runtimeOptions;
  559. }
  560. generate() {
  561. const {
  562. chunk,
  563. runtimeRequirements
  564. } = this;
  565. const {
  566. runtimeTemplate,
  567. outputOptions: {
  568. crossOriginLoading
  569. }
  570. } = this.compilation;
  571. const chunkMap = getCssChunkObject(chunk, this.compilation);
  572. const withLoading = runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers) && Object.keys(chunkMap).length > 0;
  573. const withHmr = runtimeRequirements.has(RuntimeGlobals.hmrDownloadUpdateHandlers);
  574. if (!withLoading && !withHmr) {
  575. return null;
  576. }
  577. return Template.asString([`var createStylesheet = ${runtimeTemplate.basicFunction('chunkId, fullhref, resolve, reject', ['var linkTag = document.createElement("link");', this.runtimeOptions.attributes ? Template.asString(Object.entries(this.runtimeOptions.attributes).map(entry => {
  578. const [key, value] = entry;
  579. return `linkTag.setAttribute(${JSON.stringify(key)}, ${JSON.stringify(value)});`;
  580. })) : '', '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;', '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(['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, resolve, reject);'])});`)}`, withLoading ? Template.asString(['// object to store loaded CSS chunks', 'var installedCssChunks = {', Template.indent(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, ${runtimeTemplate.basicFunction('', ['tag.as = "style";', 'tag.rel = "preload";', 'resolve();'])}, reject);`, 'oldTags.push(oldTag);', 'newTags.push(tag);'])}));`])});`])}`]) : '// no hmr']);
  581. }
  582. }
  583. const enabledChunks = new WeakSet();
  584. const handler = (chunk, set) => {
  585. if (enabledChunks.has(chunk)) {
  586. return;
  587. }
  588. enabledChunks.add(chunk);
  589. if (typeof this.options.chunkFilename === 'string' && /\[(full)?hash(:\d+)?\]/.test(this.options.chunkFilename)) {
  590. set.add(RuntimeGlobals.getFullHash);
  591. }
  592. set.add(RuntimeGlobals.publicPath);
  593. compilation.addRuntimeModule(chunk, new runtime.GetChunkFilenameRuntimeModule(_utils.MODULE_TYPE, 'mini-css', `${RuntimeGlobals.require}.miniCssF`, referencedChunk => {
  594. if (!referencedChunk.contentHash[_utils.MODULE_TYPE]) {
  595. return false;
  596. }
  597. return referencedChunk.canBeInitial() ? this.options.filename : this.options.chunkFilename;
  598. }, true));
  599. compilation.addRuntimeModule(chunk, new CssLoadingRuntimeModule(set, this.runtimeOptions));
  600. };
  601. compilation.hooks.runtimeRequirementInTree.for(RuntimeGlobals.ensureChunkHandlers).tap(pluginName, handler);
  602. compilation.hooks.runtimeRequirementInTree.for(RuntimeGlobals.hmrDownloadUpdateHandlers).tap(pluginName, handler);
  603. }
  604. });
  605. }
  606. getChunkModules(chunk, chunkGraph) {
  607. return typeof chunkGraph !== 'undefined' ? chunkGraph.getOrderedChunkModulesIterable(chunk, _utils.compareModulesByIdentifier) : chunk.modulesIterable;
  608. }
  609. getCssChunkObject(mainChunk, compilation) {
  610. const obj = {};
  611. const {
  612. chunkGraph
  613. } = compilation;
  614. for (const chunk of mainChunk.getAllAsyncChunks()) {
  615. for (const module of this.getChunkModules(chunk, chunkGraph)) {
  616. if (module.type === _utils.MODULE_TYPE) {
  617. obj[chunk.id] = 1;
  618. break;
  619. }
  620. }
  621. }
  622. return obj;
  623. }
  624. sortModules(compilation, chunk, modules, requestShortener) {
  625. let usedModules = this._sortedModulesCache.get(chunk);
  626. if (usedModules || !modules) {
  627. return usedModules;
  628. }
  629. const modulesList = [...modules];
  630. const [chunkGroup] = chunk.groupsIterable;
  631. const moduleIndexFunctionName = typeof compilation.chunkGraph !== 'undefined' ? 'getModulePostOrderIndex' : 'getModuleIndex2';
  632. if (typeof chunkGroup[moduleIndexFunctionName] === 'function') {
  633. // Store dependencies for modules
  634. const moduleDependencies = new Map(modulesList.map(m => [m, new Set()]));
  635. const moduleDependenciesReasons = new Map(modulesList.map(m => [m, new Map()])); // Get ordered list of modules per chunk group
  636. // This loop also gathers dependencies from the ordered lists
  637. // Lists are in reverse order to allow to use Array.pop()
  638. const modulesByChunkGroup = Array.from(chunk.groupsIterable, cg => {
  639. const sortedModules = modulesList.map(m => {
  640. return {
  641. module: m,
  642. index: cg[moduleIndexFunctionName](m)
  643. };
  644. }) // eslint-disable-next-line no-undefined
  645. .filter(item => item.index !== undefined).sort((a, b) => b.index - a.index).map(item => item.module);
  646. for (let i = 0; i < sortedModules.length; i++) {
  647. const set = moduleDependencies.get(sortedModules[i]);
  648. const reasons = moduleDependenciesReasons.get(sortedModules[i]);
  649. for (let j = i + 1; j < sortedModules.length; j++) {
  650. const module = sortedModules[j];
  651. set.add(module);
  652. const reason = reasons.get(module) || new Set();
  653. reason.add(cg);
  654. reasons.set(module, reason);
  655. }
  656. }
  657. return sortedModules;
  658. }); // set with already included modules in correct order
  659. usedModules = new Set();
  660. const unusedModulesFilter = m => !usedModules.has(m);
  661. while (usedModules.size < modulesList.length) {
  662. let success = false;
  663. let bestMatch;
  664. let bestMatchDeps; // get first module where dependencies are fulfilled
  665. for (const list of modulesByChunkGroup) {
  666. // skip and remove already added modules
  667. while (list.length > 0 && usedModules.has(list[list.length - 1])) {
  668. list.pop();
  669. } // skip empty lists
  670. if (list.length !== 0) {
  671. const module = list[list.length - 1];
  672. const deps = moduleDependencies.get(module); // determine dependencies that are not yet included
  673. const failedDeps = Array.from(deps).filter(unusedModulesFilter); // store best match for fallback behavior
  674. if (!bestMatchDeps || bestMatchDeps.length > failedDeps.length) {
  675. bestMatch = list;
  676. bestMatchDeps = failedDeps;
  677. }
  678. if (failedDeps.length === 0) {
  679. // use this module and remove it from list
  680. usedModules.add(list.pop());
  681. success = true;
  682. break;
  683. }
  684. }
  685. }
  686. if (!success) {
  687. // no module found => there is a conflict
  688. // use list with fewest failed deps
  689. // and emit a warning
  690. const fallbackModule = bestMatch.pop();
  691. if (!this.options.ignoreOrder) {
  692. const reasons = moduleDependenciesReasons.get(fallbackModule);
  693. compilation.warnings.push(new Error([`chunk ${chunk.name || chunk.id} [${pluginName}]`, 'Conflicting order. Following module has been added:', ` * ${fallbackModule.readableIdentifier(requestShortener)}`, 'despite it was not able to fulfill desired ordering with these modules:', ...bestMatchDeps.map(m => {
  694. const goodReasonsMap = moduleDependenciesReasons.get(m);
  695. const goodReasons = goodReasonsMap && goodReasonsMap.get(fallbackModule);
  696. const failedChunkGroups = Array.from(reasons.get(m), cg => cg.name).join(', ');
  697. const goodChunkGroups = goodReasons && Array.from(goodReasons, cg => cg.name).join(', ');
  698. 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');
  699. })].join('\n')));
  700. }
  701. usedModules.add(fallbackModule);
  702. }
  703. }
  704. } else {
  705. // fallback for older webpack versions
  706. // (to avoid a breaking change)
  707. // TODO remove this in next major version
  708. // and increase minimum webpack version to 4.12.0
  709. modulesList.sort((a, b) => a.index2 - b.index2);
  710. usedModules = modulesList;
  711. }
  712. this._sortedModulesCache.set(chunk, usedModules);
  713. return usedModules;
  714. }
  715. renderContentAsset(compiler, compilation, chunk, modules, requestShortener) {
  716. const usedModules = this.sortModules(compilation, chunk, modules, requestShortener); // TODO remove after drop webpack v4
  717. const {
  718. ConcatSource,
  719. SourceMapSource,
  720. RawSource
  721. } = compiler.webpack ? compiler.webpack.sources : // eslint-disable-next-line global-require
  722. require('webpack-sources');
  723. const source = new ConcatSource();
  724. const externalsSource = new ConcatSource();
  725. for (const m of usedModules) {
  726. let content = m.content.toString();
  727. if (/^@import url/.test(content)) {
  728. // HACK for IE
  729. // http://stackoverflow.com/a/14676665/1458162
  730. if (m.media) {
  731. // insert media into the @import
  732. // this is rar
  733. // TODO improve this and parse the CSS to support multiple medias
  734. content = content.replace(/;|\s*$/, m.media);
  735. }
  736. externalsSource.add(content);
  737. externalsSource.add('\n');
  738. } else {
  739. if (m.media) {
  740. source.add(`@media ${m.media} {\n`);
  741. }
  742. if (m.sourceMap) {
  743. source.add(new SourceMapSource(content, m.readableIdentifier(requestShortener), m.sourceMap.toString()));
  744. } else {
  745. source.add(new RawSource(content, m.readableIdentifier(requestShortener)));
  746. }
  747. source.add('\n');
  748. if (m.media) {
  749. source.add('}\n');
  750. }
  751. }
  752. }
  753. return new ConcatSource(externalsSource, source);
  754. }
  755. }
  756. MiniCssExtractPlugin.loader = require.resolve('./loader');
  757. var _default = MiniCssExtractPlugin;
  758. exports.default = _default;