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