25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

152 satır
5.1 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.pitch = pitch;
  6. exports.default = function () {};
  7. var _module = require('module');
  8. var _module2 = _interopRequireDefault(_module);
  9. var _loaderUtils = require('loader-utils');
  10. var _loaderUtils2 = _interopRequireDefault(_loaderUtils);
  11. var _NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
  12. var _NodeTemplatePlugin2 = _interopRequireDefault(_NodeTemplatePlugin);
  13. var _NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
  14. var _NodeTargetPlugin2 = _interopRequireDefault(_NodeTargetPlugin);
  15. var _LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
  16. var _LibraryTemplatePlugin2 = _interopRequireDefault(_LibraryTemplatePlugin);
  17. var _SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
  18. var _SingleEntryPlugin2 = _interopRequireDefault(_SingleEntryPlugin);
  19. var _LimitChunkCountPlugin = require('webpack/lib/optimize/LimitChunkCountPlugin');
  20. var _LimitChunkCountPlugin2 = _interopRequireDefault(_LimitChunkCountPlugin);
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. const MODULE_TYPE = 'css/mini-extract';
  23. const pluginName = 'mini-css-extract-plugin';
  24. const exec = (loaderContext, code, filename) => {
  25. const module = new _module2.default(filename, loaderContext);
  26. module.paths = _module2.default._nodeModulePaths(loaderContext.context); // eslint-disable-line no-underscore-dangle
  27. module.filename = filename;
  28. module._compile(code, filename); // eslint-disable-line no-underscore-dangle
  29. return module.exports;
  30. };
  31. const findModuleById = (modules, id) => {
  32. for (const module of modules) {
  33. if (module.id === id) {
  34. return module;
  35. }
  36. }
  37. return null;
  38. };
  39. function pitch(request) {
  40. const query = _loaderUtils2.default.getOptions(this) || {};
  41. const loaders = this.loaders.slice(this.loaderIndex + 1);
  42. this.addDependency(this.resourcePath);
  43. const childFilename = '*'; // eslint-disable-line no-path-concat
  44. const publicPath = typeof query.publicPath === 'string' ? query.publicPath : this._compilation.outputOptions.publicPath;
  45. const outputOptions = {
  46. filename: childFilename,
  47. publicPath
  48. };
  49. const childCompiler = this._compilation.createChildCompiler(`${pluginName} ${request}`, outputOptions);
  50. new _NodeTemplatePlugin2.default(outputOptions).apply(childCompiler);
  51. new _LibraryTemplatePlugin2.default(null, 'commonjs2').apply(childCompiler);
  52. new _NodeTargetPlugin2.default().apply(childCompiler);
  53. new _SingleEntryPlugin2.default(this.context, `!!${request}`, pluginName).apply(childCompiler);
  54. new _LimitChunkCountPlugin2.default({ maxChunks: 1 }).apply(childCompiler);
  55. // We set loaderContext[MODULE_TYPE] = false to indicate we already in
  56. // a child compiler so we don't spawn another child compilers from there.
  57. childCompiler.hooks.thisCompilation.tap(`${pluginName} loader`, compilation => {
  58. compilation.hooks.normalModuleLoader.tap(`${pluginName} loader`, (loaderContext, module) => {
  59. loaderContext.emitFile = this.emitFile;
  60. loaderContext[MODULE_TYPE] = false; // eslint-disable-line no-param-reassign
  61. if (module.request === request) {
  62. // eslint-disable-next-line no-param-reassign
  63. module.loaders = loaders.map(loader => {
  64. return {
  65. loader: loader.path,
  66. options: loader.options,
  67. ident: loader.ident
  68. };
  69. });
  70. }
  71. });
  72. });
  73. let source;
  74. childCompiler.hooks.afterCompile.tap(pluginName, compilation => {
  75. source = compilation.assets[childFilename] && compilation.assets[childFilename].source();
  76. // Remove all chunk assets
  77. compilation.chunks.forEach(chunk => {
  78. chunk.files.forEach(file => {
  79. delete compilation.assets[file]; // eslint-disable-line no-param-reassign
  80. });
  81. });
  82. });
  83. const callback = this.async();
  84. childCompiler.runAsChild((err, entries, compilation) => {
  85. if (err) return callback(err);
  86. if (compilation.errors.length > 0) {
  87. return callback(compilation.errors[0]);
  88. }
  89. compilation.fileDependencies.forEach(dep => {
  90. this.addDependency(dep);
  91. }, this);
  92. compilation.contextDependencies.forEach(dep => {
  93. this.addContextDependency(dep);
  94. }, this);
  95. if (!source) {
  96. return callback(new Error("Didn't get a result from child compiler"));
  97. }
  98. let text;
  99. let locals;
  100. try {
  101. text = exec(this, source, request);
  102. locals = text && text.locals;
  103. if (!Array.isArray(text)) {
  104. text = [[null, text]];
  105. } else {
  106. text = text.map(line => {
  107. const module = findModuleById(compilation.modules, line[0]);
  108. return {
  109. identifier: module.identifier(),
  110. content: line[1],
  111. media: line[2],
  112. sourceMap: line[3]
  113. };
  114. });
  115. }
  116. this[MODULE_TYPE](text);
  117. } catch (e) {
  118. return callback(e);
  119. }
  120. let resultSource = `// extracted by ${pluginName}`;
  121. if (locals && typeof resultSource !== 'undefined') {
  122. resultSource += `\nmodule.exports = ${JSON.stringify(locals)};`;
  123. }
  124. return callback(null, resultSource);
  125. });
  126. }