You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.5 KiB

4 years ago
  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. /* eslint-disable
  24. consistent-return,
  25. no-param-reassign
  26. */
  27. const NS = _path2.default.dirname(_fs2.default.realpathSync(__filename));
  28. const plugin = { name: 'ExtractTextPlugin' };
  29. exports.default = source => source;
  30. function pitch(request) {
  31. const query = _loaderUtils2.default.getOptions(this) || {};
  32. let loaders = this.loaders.slice(this.loaderIndex + 1);
  33. this.addDependency(this.resourcePath);
  34. // We already in child compiler, return empty bundle
  35. // eslint-disable-next-line no-undefined
  36. if (this[NS] === undefined) {
  37. 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');
  38. } else if (this[NS] === false) {
  39. return '';
  40. } else if (this[NS](null, query)) {
  41. if (query.omit) {
  42. this.loaderIndex += +query.omit + 1;
  43. request = request.split('!').slice(+query.omit).join('!');
  44. loaders = loaders.slice(+query.omit);
  45. }
  46. let resultSource;
  47. if (query.remove) {
  48. resultSource = '// removed by extract-text-webpack-plugin';
  49. } else {
  50. resultSource = undefined; // eslint-disable-line no-undefined
  51. }
  52. const childFilename = 'extract-text-webpack-plugin-output-filename'; // eslint-disable-line no-path-concat
  53. const publicPath = typeof query.publicPath === 'string' ? query.publicPath : this._compilation.outputOptions.publicPath;
  54. const outputOptions = {
  55. filename: childFilename,
  56. publicPath
  57. };
  58. const childCompiler = this._compilation.createChildCompiler(`extract-text-webpack-plugin ${NS} ${request}`, outputOptions);
  59. new _NodeTemplatePlugin2.default(outputOptions).apply(childCompiler);
  60. new _LibraryTemplatePlugin2.default(null, 'commonjs2').apply(childCompiler);
  61. new _NodeTargetPlugin2.default().apply(childCompiler);
  62. new _SingleEntryPlugin2.default(this.context, `!!${request}`).apply(childCompiler);
  63. new _LimitChunkCountPlugin2.default({ maxChunks: 1 }).apply(childCompiler);
  64. // We set loaderContext[NS] = false to indicate we already in
  65. // a child compiler so we don't spawn other child compilers from there.
  66. childCompiler.hooks.thisCompilation.tap(plugin, compilation => {
  67. compilation.hooks.normalModuleLoader.tap(plugin, (loaderContext, module) => {
  68. loaderContext[NS] = false;
  69. if (module.request === request) {
  70. module.loaders = loaders.map(loader => {
  71. return {
  72. loader: loader.path,
  73. options: loader.options
  74. };
  75. });
  76. }
  77. });
  78. });
  79. let source;
  80. childCompiler.hooks.afterCompile.tap(plugin, compilation => {
  81. source = compilation.assets[childFilename] && compilation.assets[childFilename].source();
  82. // Remove all chunk assets
  83. compilation.chunks.forEach(chunk => {
  84. chunk.files.forEach(file => {
  85. delete compilation.assets[file];
  86. });
  87. });
  88. });
  89. const callback = this.async();
  90. childCompiler.runAsChild((err, entries, compilation) => {
  91. if (err) return callback(err);
  92. if (compilation.errors.length > 0) {
  93. return callback(compilation.errors[0]);
  94. }
  95. compilation.fileDependencies.forEach(dep => {
  96. this.addDependency(dep);
  97. }, this);
  98. compilation.contextDependencies.forEach(dep => {
  99. this.addContextDependency(dep);
  100. }, this);
  101. if (!source) {
  102. return callback(new Error("Didn't get a result from child compiler"));
  103. }
  104. try {
  105. let text = this.exec(source, request);
  106. if (typeof text === 'string') {
  107. text = [[compilation.entries[0].identifier(), text]];
  108. } else {
  109. text.forEach(item => {
  110. const [id] = item;
  111. compilation.modules.forEach(module => {
  112. if (module.id === id) {
  113. item[0] = module.identifier();
  114. }
  115. });
  116. });
  117. }
  118. this[NS](text, query);
  119. // NOTE: converting this to ESM will require changes to renderExtractedChunk
  120. if (text.locals && typeof resultSource !== 'undefined') {
  121. resultSource += `\nmodule.exports = ${JSON.stringify(text.locals)};`;
  122. }
  123. } catch (e) {
  124. return callback(e);
  125. }
  126. if (resultSource) {
  127. callback(null, resultSource);
  128. } else {
  129. callback();
  130. }
  131. });
  132. }
  133. }