Skip to content

Commit fa62f4b

Browse files
committed
improved PR
1 parent b44e078 commit fa62f4b

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

example/webpack.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var fs = require('fs');
21
var webpack = require("webpack");
32
var ExtractTextPlugin = require("../");
43
module.exports = {
@@ -9,7 +8,7 @@ module.exports = {
98
output: {
109
filename: "[name].js?[hash]-[chunkhash]",
1110
chunkFilename: "[name].js?[hash]-[chunkhash]",
12-
path: fs.realpathSync(__dirname) + "/assets",
11+
path: __dirname + "/assets",
1312
publicPath: "/assets/"
1413
},
1514
module: {

index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var Chunk = require("webpack/lib/Chunk");
1010
var OrderUndefinedError = require("./OrderUndefinedError");
1111
var loaderUtils = require("loader-utils");
1212

13+
var NS = fs.realpathSync(__dirname);
14+
1315
var nextId = 0;
1416

1517
function ExtractTextPluginCompilation() {
@@ -206,16 +208,16 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
206208
compiler.plugin("this-compilation", function(compilation) {
207209
var extractCompilation = new ExtractTextPluginCompilation();
208210
compilation.plugin("normal-module-loader", function(loaderContext, module) {
209-
loaderContext[fs.realpathSync(__dirname)] = function(content, opt) {
211+
loaderContext[NS] = function(content, opt) {
210212
if(options.disable)
211213
return false;
212214
if(!Array.isArray(content) && content != null)
213215
throw new Error("Exported value was not extracted as an array: " + JSON.stringify(content));
214-
module.meta[fs.realpathSync(__dirname)] = {
216+
module.meta[NS] = {
215217
content: content,
216218
options: opt || {}
217219
};
218-
return options.allChunks || module.meta[fs.realpathSync(__dirname) + "/extract"]; // eslint-disable-line no-path-concat
220+
return options.allChunks || module.meta[NS + "/extract"]; // eslint-disable-line no-path-concat
219221
};
220222
});
221223
var filename = this.filename;
@@ -242,17 +244,17 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
242244
var extractedChunk = extractedChunks[chunks.indexOf(chunk)];
243245
var shouldExtract = !!(options.allChunks || chunk.isInitial());
244246
async.forEach(chunk.modules.slice(), function(module, callback) {
245-
var meta = module.meta && module.meta[fs.realpathSync(__dirname)];
247+
var meta = module.meta && module.meta[NS];
246248
if(meta && (!meta.options.id || meta.options.id === id)) {
247249
var wasExtracted = Array.isArray(meta.content);
248250
if(shouldExtract !== wasExtracted) {
249-
module.meta[fs.realpathSync(__dirname) + "/extract"] = shouldExtract; // eslint-disable-line no-path-concat
251+
module.meta[NS + "/extract"] = shouldExtract; // eslint-disable-line no-path-concat
250252
compilation.rebuildModule(module, function(err) {
251253
if(err) {
252254
compilation.errors.push(err);
253255
return callback();
254256
}
255-
meta = module.meta[fs.realpathSync(__dirname)];
257+
meta = module.meta[NS];
256258
if(!Array.isArray(meta.content)) {
257259
err = new Error(module.identifier() + " doesn't export content");
258260
compilation.errors.push(err);

loader.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,34 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
var fs = require("fs");
56
var loaderUtils = require("loader-utils");
67
var NodeTemplatePlugin = require("webpack/lib/node/NodeTemplatePlugin");
78
var NodeTargetPlugin = require("webpack/lib/node/NodeTargetPlugin");
89
var LibraryTemplatePlugin = require("webpack/lib/LibraryTemplatePlugin");
910
var SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin");
1011
var LimitChunkCountPlugin = require("webpack/lib/optimize/LimitChunkCountPlugin");
12+
13+
var NS = fs.realpathSync(__dirname);
14+
1115
module.exports = function(source) {
1216
if(this.cacheable) this.cacheable();
1317
return source;
1418
};
19+
1520
module.exports.pitch = function(request) {
1621
if(this.cacheable) this.cacheable();
1722
var query = loaderUtils.parseQuery(this.query);
1823
this.addDependency(this.resourcePath);
1924
// We already in child compiler, return empty bundle
20-
if(this[fs.realpathSync(__dirname)] === undefined) {
25+
if(this[NS] === undefined) {
2126
throw new Error(
2227
'"extract-text-webpack-plugin" loader is used without the corresponding plugin, ' +
2328
'refer to https://github.com/webpack/extract-text-webpack-plugin for the usage example'
2429
);
25-
} else if(this[fs.realpathSync(__dirname)] === false) {
30+
} else if(this[NS] === false) {
2631
return "";
27-
} else if(this[fs.realpathSync(__dirname)](null, query)) {
32+
} else if(this[NS](null, query)) {
2833
if(query.omit) {
2934
this.loaderIndex += +query.omit + 1;
3035
request = request.split("!").slice(+query.omit).join("!");
@@ -48,19 +53,19 @@ module.exports.pitch = function(request) {
4853
childCompiler.apply(new NodeTargetPlugin());
4954
childCompiler.apply(new SingleEntryPlugin(this.context, "!!" + request));
5055
childCompiler.apply(new LimitChunkCountPlugin({ maxChunks: 1 }));
51-
var subCache = "subcache " + fs.realpathSync(__dirname) + " " + request; // eslint-disable-line no-path-concat
56+
var subCache = "subcache " + NS + " " + request; // eslint-disable-line no-path-concat
5257
childCompiler.plugin("compilation", function(compilation) {
5358
if(compilation.cache) {
5459
if(!compilation.cache[subCache])
5560
compilation.cache[subCache] = {};
5661
compilation.cache = compilation.cache[subCache];
5762
}
5863
});
59-
// We set loaderContext[fs.realpathSync(__dirname)] = false to indicate we already in
64+
// We set loaderContext[NS] = false to indicate we already in
6065
// a child compiler so we don't spawn another child compilers from there.
6166
childCompiler.plugin("this-compilation", function(compilation) {
6267
compilation.plugin("normal-module-loader", function(loaderContext) {
63-
loaderContext[fs.realpathSync(__dirname)] = false;
68+
loaderContext[NS] = false;
6469
});
6570
});
6671
var source;
@@ -103,7 +108,7 @@ module.exports.pitch = function(request) {
103108
item[0] = module.identifier();
104109
});
105110
});
106-
this[fs.realpathSync(__dirname)](text, query);
111+
this[NS](text, query);
107112
if(text.locals && typeof resultSource !== "undefined") {
108113
resultSource += "\nmodule.exports = " + JSON.stringify(text.locals) + ";";
109114
}

test/TestCases.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ var webpack = require("webpack");
55
var should = require("should");
66
var ExtractTextPlugin = require("../");
77

8-
var cases = process.env.CASES ? process.env.CASES.split(",") : fs.readdirSync(path.join(fs.realpathSync(__dirname), "cases"));
8+
var cases = process.env.CASES ? process.env.CASES.split(",") : fs.readdirSync(path.join(__dirname, "cases"));
99

1010
describe("TestCases", function() {
1111
cases.forEach(function(testCase) {
1212
it(testCase, function(done) {
13-
var testDirectory = path.join(fs.realpathSync(__dirname), "cases", testCase);
14-
var outputDirectory = path.join(fs.realpathSync(__dirname), "js", testCase);
13+
var testDirectory = path.join(__dirname, "cases", testCase);
14+
var outputDirectory = path.join(__dirname, "js", testCase);
1515
var options = { entry: { test: "./index.js" } };
1616
var configFile = path.join(testDirectory, "webpack.config.js");
1717
if(fs.existsSync(configFile))

0 commit comments

Comments
 (0)