index.js 18 KB

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