UNPKG

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