loader.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.pitch = pitch;
  6. var _fs = require('fs');
  7. var _fs2 = _interopRequireDefault(_fs);
  8. var _path = require('path');
  9. var _path2 = _interopRequireDefault(_path);
  10. var _loaderUtils = require('loader-utils');
  11. var _loaderUtils2 = _interopRequireDefault(_loaderUtils);
  12. var _NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
  13. var _NodeTemplatePlugin2 = _interopRequireDefault(_NodeTemplatePlugin);
  14. var _NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
  15. var _NodeTargetPlugin2 = _interopRequireDefault(_NodeTargetPlugin);
  16. var _LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
  17. var _LibraryTemplatePlugin2 = _interopRequireDefault(_LibraryTemplatePlugin);
  18. var _SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
  19. var _SingleEntryPlugin2 = _interopRequireDefault(_SingleEntryPlugin);
  20. var _LimitChunkCountPlugin = require('webpack/lib/optimize/LimitChunkCountPlugin');
  21. var _LimitChunkCountPlugin2 = _interopRequireDefault(_LimitChunkCountPlugin);
  22. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  23. var NS = _path2.default.dirname(_fs2.default.realpathSync(__filename));
  24. exports.default = function (source) {
  25. return source;
  26. };
  27. function pitch(request) {
  28. var _this = this;
  29. var query = _loaderUtils2.default.getOptions(this) || {};
  30. var loaders = this.loaders.slice(this.loaderIndex + 1);
  31. this.addDependency(this.resourcePath);
  32. // We already in child compiler, return empty bundle
  33. if (this[NS] === undefined) {
  34. // eslint-disable-line no-undefined
  35. throw new Error('"extract-text-webpack-plugin" loader is used without the corresponding plugin, ' + 'refer to https://github.com/webpack/extract-text-webpack-plugin for the usage example');
  36. } else if (this[NS] === false) {
  37. return '';
  38. } else if (this[NS](null, query)) {
  39. if (query.omit) {
  40. this.loaderIndex += +query.omit + 1;
  41. request = request.split('!').slice(+query.omit).join('!');
  42. loaders = loaders.slice(+query.omit);
  43. }
  44. var resultSource = void 0;
  45. if (query.remove) {
  46. resultSource = '// removed by extract-text-webpack-plugin';
  47. } else {
  48. resultSource = undefined; // eslint-disable-line no-undefined
  49. }
  50. var childFilename = 'extract-text-webpack-plugin-output-filename'; // eslint-disable-line no-path-concat
  51. var publicPath = typeof query.publicPath === 'string' ? query.publicPath : this._compilation.outputOptions.publicPath;
  52. var outputOptions = {
  53. filename: childFilename,
  54. publicPath
  55. };
  56. var childCompiler = this._compilation.createChildCompiler(`extract-text-webpack-plugin ${NS} ${request}`, outputOptions);
  57. childCompiler.apply(new _NodeTemplatePlugin2.default(outputOptions));
  58. childCompiler.apply(new _LibraryTemplatePlugin2.default(null, 'commonjs2'));
  59. childCompiler.apply(new _NodeTargetPlugin2.default());
  60. childCompiler.apply(new _SingleEntryPlugin2.default(this.context, `!!${request}`));
  61. childCompiler.apply(new _LimitChunkCountPlugin2.default({ maxChunks: 1 }));
  62. // We set loaderContext[NS] = false to indicate we already in
  63. // a child compiler so we don't spawn another child compilers from there.
  64. childCompiler.plugin('this-compilation', function (compilation) {
  65. compilation.plugin('normal-module-loader', function (loaderContext, module) {
  66. loaderContext[NS] = false;
  67. if (module.request === request) {
  68. module.loaders = loaders.map(function (loader) {
  69. return {
  70. loader: loader.path,
  71. options: loader.options
  72. };
  73. });
  74. }
  75. });
  76. });
  77. var source = void 0;
  78. childCompiler.plugin('after-compile', function (compilation, callback) {
  79. source = compilation.assets[childFilename] && compilation.assets[childFilename].source();
  80. // Remove all chunk assets
  81. compilation.chunks.forEach(function (chunk) {
  82. chunk.files.forEach(function (file) {
  83. delete compilation.assets[file];
  84. });
  85. });
  86. callback();
  87. });
  88. var callback = this.async();
  89. childCompiler.runAsChild(function (err, entries, compilation) {
  90. if (err) return callback(err);
  91. if (compilation.errors.length > 0) {
  92. return callback(compilation.errors[0]);
  93. }
  94. compilation.fileDependencies.forEach(function (dep) {
  95. _this.addDependency(dep);
  96. }, _this);
  97. compilation.contextDependencies.forEach(function (dep) {
  98. _this.addContextDependency(dep);
  99. }, _this);
  100. if (!source) {
  101. return callback(new Error("Didn't get a result from child compiler"));
  102. }
  103. try {
  104. var text = _this.exec(source, request);
  105. if (typeof text === 'string') {
  106. text = [[compilation.entries[0].identifier(), text]];
  107. } else {
  108. text.forEach(function (item) {
  109. var id = item[0];
  110. compilation.modules.forEach(function (module) {
  111. if (module.id === id) {
  112. item[0] = module.identifier();
  113. }
  114. });
  115. });
  116. }
  117. _this[NS](text, query);
  118. if (text.locals && typeof resultSource !== 'undefined') {
  119. resultSource += `\nmodule.exports = ${JSON.stringify(text.locals)};`;
  120. }
  121. } catch (e) {
  122. return callback(e);
  123. }
  124. if (resultSource) {
  125. callback(null, resultSource);
  126. } else {
  127. callback();
  128. }
  129. });
  130. }
  131. }