index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _fs = require('fs');
  6. var _fs2 = _interopRequireDefault(_fs);
  7. var _path = require('path');
  8. var _path2 = _interopRequireDefault(_path);
  9. var _webpack = require('webpack');
  10. var _webpack2 = _interopRequireDefault(_webpack);
  11. var _webpackSources = require('webpack-sources');
  12. var _webpackSources2 = _interopRequireDefault(_webpackSources);
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. const { ConcatSource, SourceMapSource, OriginalSource } = _webpackSources2.default;
  15. const { Template, util: { createHash } } = _webpack2.default;
  16. const NS = _path2.default.dirname(_fs2.default.realpathSync(__filename));
  17. const pluginName = 'mini-css-extract-plugin';
  18. const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
  19. const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
  20. const REGEXP_NAME = /\[name\]/i;
  21. class CssDependency extends _webpack2.default.Dependency {
  22. constructor({ identifier, content, media, sourceMap }, context, identifierIndex) {
  23. super();
  24. this.identifier = identifier;
  25. this.identifierIndex = identifierIndex;
  26. this.content = content;
  27. this.media = media;
  28. this.sourceMap = sourceMap;
  29. this.context = context;
  30. }
  31. getResourceIdentifier() {
  32. return `css-module-${this.identifier}-${this.identifierIndex}`;
  33. }
  34. }
  35. class CssDependencyTemplate {
  36. apply() {}
  37. }
  38. class CssModule extends _webpack2.default.Module {
  39. constructor(dependency) {
  40. super(NS, dependency.context);
  41. this._identifier = dependency.identifier;
  42. this._identifierIndex = dependency.identifierIndex;
  43. this.content = dependency.content;
  44. this.media = dependency.media;
  45. this.sourceMap = dependency.sourceMap;
  46. }
  47. // no source() so webpack doesn't do add stuff to the bundle
  48. size() {
  49. return this.content.length;
  50. }
  51. identifier() {
  52. return `css ${this._identifier} ${this._identifierIndex}`;
  53. }
  54. readableIdentifier(requestShortener) {
  55. return `css ${requestShortener.shorten(this._identifier)}${this._identifierIndex ? ` (${this._identifierIndex})` : ''}`;
  56. }
  57. nameForCondition() {
  58. const resource = this._identifier.split('!').pop();
  59. const idx = resource.indexOf('?');
  60. if (idx >= 0) return resource.substring(0, idx);
  61. return resource;
  62. }
  63. updateCacheModule(module) {
  64. this.content = module.content;
  65. this.media = module.media;
  66. this.sourceMap = module.sourceMap;
  67. }
  68. needRebuild() {
  69. return true;
  70. }
  71. build(options, compilation, resolver, fileSystem, callback) {
  72. this.buildInfo = {};
  73. this.buildMeta = {};
  74. callback();
  75. }
  76. updateHash(hash) {
  77. super.updateHash(hash);
  78. hash.update(this.content);
  79. hash.update(this.media || '');
  80. hash.update(JSON.stringify(this.sourceMap || ''));
  81. }
  82. }
  83. class CssModuleFactory {
  84. create({ dependencies: [dependency] }, callback) {
  85. callback(null, new CssModule(dependency));
  86. }
  87. }
  88. class MiniCssExtractPlugin {
  89. constructor(options) {
  90. this.options = Object.assign({
  91. filename: '[name].css'
  92. }, options);
  93. if (!this.options.chunkFilename) {
  94. const { filename } = this.options;
  95. const hasName = filename.includes('[name]');
  96. const hasId = filename.includes('[id]');
  97. const hasChunkHash = filename.includes('[chunkhash]');
  98. // Anything changing depending on chunk is fine
  99. if (hasChunkHash || hasName || hasId) {
  100. this.options.chunkFilename = filename;
  101. } else {
  102. // Elsewise prefix '[id].' in front of the basename to make it changing
  103. this.options.chunkFilename = filename.replace(/(^|\/)([^/]*(?:\?|$))/, '$1[id].$2');
  104. }
  105. }
  106. }
  107. apply(compiler) {
  108. compiler.hooks.thisCompilation.tap(pluginName, compilation => {
  109. compilation.hooks.normalModuleLoader.tap(pluginName, (lc, m) => {
  110. const loaderContext = lc;
  111. const module = m;
  112. loaderContext[NS] = content => {
  113. if (!Array.isArray(content) && content != null) {
  114. throw new Error(`Exported value was not extracted as an array: ${JSON.stringify(content)}`);
  115. }
  116. const identifierCountMap = new Map();
  117. for (const line of content) {
  118. const count = identifierCountMap.get(line.identifier) || 0;
  119. module.addDependency(new CssDependency(line, m.context, count));
  120. identifierCountMap.set(line.identifier, count + 1);
  121. }
  122. };
  123. });
  124. compilation.dependencyFactories.set(CssDependency, new CssModuleFactory());
  125. compilation.dependencyTemplates.set(CssDependency, new CssDependencyTemplate());
  126. compilation.mainTemplate.hooks.renderManifest.tap(pluginName, (result, { chunk }) => {
  127. const renderedModules = Array.from(chunk.modulesIterable).filter(module => module.type === NS);
  128. if (renderedModules.length > 0) {
  129. result.push({
  130. render: () => this.renderContentAsset(chunk, renderedModules, compilation.runtimeTemplate.requestShortener),
  131. filenameTemplate: this.options.filename,
  132. pathOptions: {
  133. chunk,
  134. contentHashType: NS
  135. },
  136. identifier: `${pluginName}.${chunk.id}`,
  137. hash: chunk.contentHash[NS]
  138. });
  139. }
  140. });
  141. compilation.chunkTemplate.hooks.renderManifest.tap(pluginName, (result, { chunk }) => {
  142. const renderedModules = Array.from(chunk.modulesIterable).filter(module => module.type === NS);
  143. if (renderedModules.length > 0) {
  144. result.push({
  145. render: () => this.renderContentAsset(chunk, renderedModules, compilation.runtimeTemplate.requestShortener),
  146. filenameTemplate: this.options.chunkFilename,
  147. pathOptions: {
  148. chunk,
  149. contentHashType: NS
  150. },
  151. identifier: `${pluginName}.${chunk.id}`,
  152. hash: chunk.contentHash[NS]
  153. });
  154. }
  155. });
  156. compilation.mainTemplate.hooks.hashForChunk.tap(pluginName, (hash, chunk) => {
  157. const { chunkFilename } = this.options;
  158. if (REGEXP_CHUNKHASH.test(chunkFilename)) {
  159. hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
  160. }
  161. if (REGEXP_CONTENTHASH.test(chunkFilename)) {
  162. hash.update(JSON.stringify(chunk.getChunkMaps(true).contentHash[NS] || {}));
  163. }
  164. if (REGEXP_NAME.test(chunkFilename)) {
  165. hash.update(JSON.stringify(chunk.getChunkMaps(true).name));
  166. }
  167. });
  168. compilation.hooks.contentHash.tap(pluginName, chunk => {
  169. const { outputOptions } = compilation;
  170. const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
  171. const hash = createHash(hashFunction);
  172. for (const m of chunk.modulesIterable) {
  173. if (m.type === NS) {
  174. m.updateHash(hash);
  175. }
  176. }
  177. const { contentHash } = chunk;
  178. contentHash[NS] = hash.digest(hashDigest).substring(0, hashDigestLength);
  179. });
  180. const { mainTemplate } = compilation;
  181. mainTemplate.hooks.localVars.tap(pluginName, (source, chunk) => {
  182. const chunkMap = this.getCssChunkObject(chunk);
  183. if (Object.keys(chunkMap).length > 0) {
  184. return Template.asString([source, '', '// object to store loaded CSS chunks', 'var installedCssChunks = {', Template.indent(chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(',\n')), '}']);
  185. }
  186. return source;
  187. });
  188. mainTemplate.hooks.requireEnsure.tap(pluginName, (source, chunk, hash) => {
  189. const chunkMap = this.getCssChunkObject(chunk);
  190. if (Object.keys(chunkMap).length > 0) {
  191. const chunkMaps = chunk.getChunkMaps();
  192. const linkHrefPath = mainTemplate.getAssetPath(JSON.stringify(this.options.chunkFilename), {
  193. hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
  194. hashWithLength: length => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
  195. chunk: {
  196. id: '" + chunkId + "',
  197. hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
  198. hashWithLength(length) {
  199. const shortChunkHashMap = Object.create(null);
  200. for (const chunkId of Object.keys(chunkMaps.hash)) {
  201. if (typeof chunkMaps.hash[chunkId] === 'string') {
  202. shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substring(0, length);
  203. }
  204. }
  205. return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
  206. },
  207. contentHash: {
  208. [NS]: `" + ${JSON.stringify(chunkMaps.contentHash[NS])}[chunkId] + "`
  209. },
  210. contentHashWithLength: {
  211. [NS]: length => {
  212. const shortContentHashMap = {};
  213. const contentHash = chunkMaps.contentHash[NS];
  214. for (const chunkId of Object.keys(contentHash)) {
  215. if (typeof contentHash[chunkId] === 'string') {
  216. shortContentHashMap[chunkId] = contentHash[chunkId].substring(0, length);
  217. }
  218. }
  219. return `" + ${JSON.stringify(shortContentHashMap)}[chunkId] + "`;
  220. }
  221. },
  222. name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`
  223. },
  224. contentHashType: NS
  225. });
  226. 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.request = request;', 'reject(err);']), '};', 'linkTag.href = fullhref;', 'var head = document.getElementsByTagName("head")[0];', 'head.appendChild(linkTag);']), '}).then(function() {', Template.indent(['installedCssChunks[chunkId] = 0;']), '}));']), '}']);
  227. }
  228. return source;
  229. });
  230. });
  231. }
  232. getCssChunkObject(mainChunk) {
  233. const obj = {};
  234. for (const chunk of mainChunk.getAllAsyncChunks()) {
  235. for (const module of chunk.modulesIterable) {
  236. if (module.type === NS) {
  237. obj[chunk.id] = 1;
  238. break;
  239. }
  240. }
  241. }
  242. return obj;
  243. }
  244. renderContentAsset(chunk, modules, requestShortener) {
  245. // get first chunk group and take ordr from this one
  246. // When a chunk is shared between multiple chunk groups
  247. // with different order this can lead to wrong order
  248. // but it's not possible to create a correct order in
  249. // this case. Don't share chunks if you don't like it.
  250. const [chunkGroup] = chunk.groupsIterable;
  251. if (typeof chunkGroup.getModuleIndex2 === 'function') {
  252. modules.sort((a, b) => chunkGroup.getModuleIndex2(a) - chunkGroup.getModuleIndex2(b));
  253. } else {
  254. // fallback for older webpack versions
  255. // (to avoid a breaking change)
  256. // TODO remove this in next mayor version
  257. // and increase minimum webpack version to 4.12.0
  258. modules.sort((a, b) => a.index2 - b.index2);
  259. }
  260. const source = new ConcatSource();
  261. const externalsSource = new ConcatSource();
  262. for (const m of modules) {
  263. if (/^@import url/.test(m.content)) {
  264. // HACK for IE
  265. // http://stackoverflow.com/a/14676665/1458162
  266. let { content } = m;
  267. if (m.media) {
  268. // insert media into the @import
  269. // this is rar
  270. // TODO improve this and parse the CSS to support multiple medias
  271. content = content.replace(/;|\s*$/, m.media);
  272. }
  273. externalsSource.add(content);
  274. externalsSource.add('\n');
  275. } else {
  276. if (m.media) {
  277. source.add(`@media ${m.media} {\n`);
  278. }
  279. if (m.sourceMap) {
  280. source.add(new SourceMapSource(m.content, m.readableIdentifier(requestShortener), m.sourceMap));
  281. } else {
  282. source.add(new OriginalSource(m.content, m.readableIdentifier(requestShortener)));
  283. }
  284. source.add('\n');
  285. if (m.media) {
  286. source.add('}\n');
  287. }
  288. }
  289. }
  290. return new ConcatSource(externalsSource, source);
  291. }
  292. }
  293. MiniCssExtractPlugin.loader = require.resolve('./loader');
  294. exports.default = MiniCssExtractPlugin;