loader.js 5.3 KB

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