index.js 16 KB

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