index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var fs = require('fs');
  6. var ConcatSource = require("webpack-sources").ConcatSource;
  7. var async = require("async");
  8. var ExtractedModule = require("./ExtractedModule");
  9. var Chunk = require("webpack/lib/Chunk");
  10. var OrderUndefinedError = require("./OrderUndefinedError");
  11. var loaderUtils = require("loader-utils");
  12. var schemaTester = require('./schema/validator');
  13. var loaderSchema = require('./schema/loader-schema');
  14. var pluginSchema = require('./schema/plugin-schema.json');
  15. var NS = fs.realpathSync(__dirname);
  16. var nextId = 0;
  17. function ExtractTextPluginCompilation() {
  18. this.modulesByIdentifier = {};
  19. }
  20. ExtractTextPlugin.prototype.mergeNonInitialChunks = function(chunk, intoChunk, checkedChunks) {
  21. if(!intoChunk) {
  22. checkedChunks = [];
  23. chunk.chunks.forEach(function(c) {
  24. if(c.isInitial()) return;
  25. this.mergeNonInitialChunks(c, chunk, checkedChunks);
  26. }, this);
  27. } else if(checkedChunks.indexOf(chunk) < 0) {
  28. checkedChunks.push(chunk);
  29. chunk.modules.slice().forEach(function(module) {
  30. intoChunk.addModule(module);
  31. module.addChunk(intoChunk);
  32. });
  33. chunk.chunks.forEach(function(c) {
  34. if(c.isInitial()) return;
  35. this.mergeNonInitialChunks(c, intoChunk, checkedChunks);
  36. }, this);
  37. }
  38. };
  39. ExtractTextPluginCompilation.prototype.addModule = function(identifier, originalModule, source, additionalInformation, sourceMap, prevModules) {
  40. var m;
  41. if(!this.modulesByIdentifier[identifier]) {
  42. m = this.modulesByIdentifier[identifier] = new ExtractedModule(identifier, originalModule, source, sourceMap, additionalInformation, prevModules);
  43. } else {
  44. m = this.modulesByIdentifier[identifier];
  45. m.addPrevModules(prevModules);
  46. if(originalModule.index2 < m.getOriginalModule().index2) {
  47. m.setOriginalModule(originalModule);
  48. }
  49. }
  50. return m;
  51. };
  52. ExtractTextPluginCompilation.prototype.addResultToChunk = function(identifier, result, originalModule, extractedChunk) {
  53. if(!Array.isArray(result)) {
  54. result = [[identifier, result]];
  55. }
  56. var counterMap = {};
  57. var prevModules = [];
  58. result.forEach(function(item) {
  59. var c = counterMap[item[0]];
  60. var module = this.addModule.call(this, item[0] + (c || ""), originalModule, item[1], item[2], item[3], prevModules.slice());
  61. extractedChunk.addModule(module);
  62. module.addChunk(extractedChunk);
  63. counterMap[item[0]] = (c || 0) + 1;
  64. prevModules.push(module);
  65. }, this);
  66. };
  67. ExtractTextPlugin.prototype.renderExtractedChunk = function(chunk) {
  68. var source = new ConcatSource();
  69. chunk.modules.forEach(function(module) {
  70. var moduleSource = module.source();
  71. source.add(this.applyAdditionalInformation(moduleSource, module.additionalInformation));
  72. }, this);
  73. return source;
  74. };
  75. function isInvalidOrder(a, b) {
  76. var bBeforeA = a.getPrevModules().indexOf(b) >= 0;
  77. var aBeforeB = b.getPrevModules().indexOf(a) >= 0;
  78. return aBeforeB && bBeforeA;
  79. }
  80. function getOrder(a, b) {
  81. var aOrder = a.getOrder();
  82. var bOrder = b.getOrder();
  83. if(aOrder < bOrder) return -1;
  84. if(aOrder > bOrder) return 1;
  85. var aIndex = a.getOriginalModule().index2;
  86. var bIndex = b.getOriginalModule().index2;
  87. if(aIndex < bIndex) return -1;
  88. if(aIndex > bIndex) return 1;
  89. var bBeforeA = a.getPrevModules().indexOf(b) >= 0;
  90. var aBeforeB = b.getPrevModules().indexOf(a) >= 0;
  91. if(aBeforeB && !bBeforeA) return -1;
  92. if(!aBeforeB && bBeforeA) return 1;
  93. var ai = a.identifier();
  94. var bi = b.identifier();
  95. if(ai < bi) return -1;
  96. if(ai > bi) return 1;
  97. return 0;
  98. }
  99. function ExtractTextPlugin(options) {
  100. if(arguments.length > 1) {
  101. throw new Error("Breaking change: ExtractTextPlugin now only takes a single argument. Either an options " +
  102. "object *or* the name of the result file.\n" +
  103. "Example: if your old code looked like this:\n" +
  104. " new ExtractTextPlugin('css/[name].css', { disable: false, allChunks: true })\n\n" +
  105. "You would change it to:\n" +
  106. " new ExtractTextPlugin({ filename: 'css/[name].css', disable: false, allChunks: true })\n\n" +
  107. "The available options are:\n" +
  108. " filename: string\n" +
  109. " allChunks: boolean\n" +
  110. " disable: boolean\n");
  111. }
  112. if(isString(options)) {
  113. options = { filename: options };
  114. } else {
  115. schemaTester(pluginSchema, options);
  116. }
  117. this.filename = options.filename;
  118. this.id = options.id != null ? options.id : ++nextId;
  119. this.options = {};
  120. mergeOptions(this.options, options);
  121. delete this.options.filename;
  122. delete this.options.id;
  123. }
  124. module.exports = ExtractTextPlugin;
  125. function getLoaderObject(loader) {
  126. if (isString(loader)) {
  127. return {loader: loader};
  128. }
  129. return loader;
  130. }
  131. function mergeOptions(a, b) {
  132. if(!b) return a;
  133. Object.keys(b).forEach(function(key) {
  134. a[key] = b[key];
  135. });
  136. return a;
  137. }
  138. function isString(a) {
  139. return typeof a === "string";
  140. }
  141. function isFunction(a) {
  142. return isType('Function', a);
  143. }
  144. function isType(type, obj) {
  145. return Object.prototype.toString.call(obj) === '[object ' + type + ']';
  146. }
  147. ExtractTextPlugin.loader = function(options) {
  148. return { loader: require.resolve("./loader"), options: options };
  149. };
  150. ExtractTextPlugin.prototype.applyAdditionalInformation = function(source, info) {
  151. if(info) {
  152. return new ConcatSource(
  153. "@media " + info[0] + " {",
  154. source,
  155. "}"
  156. );
  157. }
  158. return source;
  159. };
  160. ExtractTextPlugin.prototype.loader = function(options) {
  161. return ExtractTextPlugin.loader(mergeOptions({id: this.id}, options));
  162. };
  163. ExtractTextPlugin.prototype.extract = function(options) {
  164. if(arguments.length > 1) {
  165. throw new Error("Breaking change: extract now only takes a single argument. Either an options " +
  166. "object *or* the loader(s).\n" +
  167. "Example: if your old code looked like this:\n" +
  168. " ExtractTextPlugin.extract('style-loader', 'css-loader')\n\n" +
  169. "You would change it to:\n" +
  170. " ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' })\n\n" +
  171. "The available options are:\n" +
  172. " use: string | object | loader[]\n" +
  173. " fallback: string | object | loader[]\n" +
  174. " publicPath: string\n");
  175. }
  176. if(options.fallbackLoader) {
  177. console.warn('fallbackLoader option has been deprecated - replace with "fallback"');
  178. }
  179. if(options.loader) {
  180. console.warn('loader option has been deprecated - replace with "use"');
  181. }
  182. if(Array.isArray(options) || isString(options) || typeof options.options === "object" || typeof options.query === 'object') {
  183. options = { loader: options };
  184. } else {
  185. schemaTester(loaderSchema, options);
  186. }
  187. var loader = options.use ||  options.loader;
  188. var before = options.fallback || options.fallbackLoader || [];
  189. if(isString(loader)) {
  190. loader = loader.split("!");
  191. }
  192. if(isString(before)) {
  193. before = before.split("!");
  194. } else if(!Array.isArray(before)) {
  195. before = [before];
  196. }
  197. options = mergeOptions({omit: before.length, remove: true}, options);
  198. delete options.loader;
  199. delete options.use;
  200. delete options.fallback;
  201. delete options.fallbackLoader;
  202. return [this.loader(options)]
  203. .concat(before, loader)
  204. .map(getLoaderObject);
  205. }
  206. ExtractTextPlugin.extract = ExtractTextPlugin.prototype.extract.bind(ExtractTextPlugin);
  207. ExtractTextPlugin.prototype.apply = function(compiler) {
  208. var options = this.options;
  209. compiler.plugin("this-compilation", function(compilation) {
  210. var extractCompilation = new ExtractTextPluginCompilation();
  211. compilation.plugin("normal-module-loader", function(loaderContext, module) {
  212. loaderContext[NS] = function(content, opt) {
  213. if(options.disable)
  214. return false;
  215. if(!Array.isArray(content) && content != null)
  216. throw new Error("Exported value was not extracted as an array: " + JSON.stringify(content));
  217. module[NS] = {
  218. content: content,
  219. options: opt || {}
  220. };
  221. return options.allChunks || module[NS + "/extract"]; // eslint-disable-line no-path-concat
  222. };
  223. });
  224. var filename = this.filename;
  225. var id = this.id;
  226. var extractedChunks, entryChunks, initialChunks;
  227. compilation.plugin("optimize-tree", function(chunks, modules, callback) {
  228. extractedChunks = chunks.map(function() {
  229. return new Chunk();
  230. });
  231. chunks.forEach(function(chunk, i) {
  232. var extractedChunk = extractedChunks[i];
  233. extractedChunk.index = i;
  234. extractedChunk.originalChunk = chunk;
  235. extractedChunk.name = chunk.name;
  236. extractedChunk.entrypoints = chunk.entrypoints;
  237. chunk.chunks.forEach(function(c) {
  238. extractedChunk.addChunk(extractedChunks[chunks.indexOf(c)]);
  239. });
  240. chunk.parents.forEach(function(c) {
  241. extractedChunk.addParent(extractedChunks[chunks.indexOf(c)]);
  242. });
  243. });
  244. async.forEach(chunks, function(chunk, callback) {
  245. var extractedChunk = extractedChunks[chunks.indexOf(chunk)];
  246. var shouldExtract = !!(options.allChunks || chunk.isInitial());
  247. async.forEach(chunk.modules.slice(), function(module, callback) {
  248. var meta = module[NS];
  249. if(meta && (!meta.options.id || meta.options.id === id)) {
  250. var wasExtracted = Array.isArray(meta.content);
  251. if(shouldExtract !== wasExtracted) {
  252. module[NS + "/extract"] = shouldExtract; // eslint-disable-line no-path-concat
  253. compilation.rebuildModule(module, function(err) {
  254. if(err) {
  255. compilation.errors.push(err);
  256. return callback();
  257. }
  258. meta = module[NS];
  259. if(!Array.isArray(meta.content)) {
  260. err = new Error(module.identifier() + " doesn't export content");
  261. compilation.errors.push(err);
  262. return callback();
  263. }
  264. if(meta.content)
  265. extractCompilation.addResultToChunk(module.identifier(), meta.content, module, extractedChunk);
  266. callback();
  267. });
  268. } else {
  269. if(meta.content)
  270. extractCompilation.addResultToChunk(module.identifier(), meta.content, module, extractedChunk);
  271. callback();
  272. }
  273. } else callback();
  274. }, function(err) {
  275. if(err) return callback(err);
  276. callback();
  277. });
  278. }, function(err) {
  279. if(err) return callback(err);
  280. extractedChunks.forEach(function(extractedChunk) {
  281. if(extractedChunk.isInitial())
  282. this.mergeNonInitialChunks(extractedChunk);
  283. }, this);
  284. extractedChunks.forEach(function(extractedChunk) {
  285. if(!extractedChunk.isInitial()) {
  286. extractedChunk.modules.slice().forEach(function(module) {
  287. extractedChunk.removeModule(module);
  288. });
  289. }
  290. });
  291. compilation.applyPlugins("optimize-extracted-chunks", extractedChunks);
  292. callback();
  293. }.bind(this));
  294. }.bind(this));
  295. compilation.plugin("additional-assets", function(callback) {
  296. extractedChunks.forEach(function(extractedChunk) {
  297. if(extractedChunk.modules.length) {
  298. extractedChunk.modules.sort(function(a, b) {
  299. if(!options.ignoreOrder && isInvalidOrder(a, b)) {
  300. compilation.errors.push(new OrderUndefinedError(a.getOriginalModule()));
  301. compilation.errors.push(new OrderUndefinedError(b.getOriginalModule()));
  302. }
  303. return getOrder(a, b);
  304. });
  305. var chunk = extractedChunk.originalChunk;
  306. var source = this.renderExtractedChunk(extractedChunk);
  307. var getPath = (format) => compilation.getPath(format, {
  308. chunk: chunk
  309. }).replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function() {
  310. return loaderUtils.getHashDigest(source.source(), arguments[1], arguments[2], parseInt(arguments[3], 10));
  311. });
  312. var file = (isFunction(filename)) ? filename(getPath) : getPath(filename);
  313. compilation.assets[file] = source;
  314. chunk.files.push(file);
  315. }
  316. }, this);
  317. callback();
  318. }.bind(this));
  319. }.bind(this));
  320. };