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