Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
allow to pass options and overwride publicPath
Browse files Browse the repository at this point in the history
fixes #27
  • Loading branch information
sokra committed Oct 13, 2014
1 parent 8d0b88f commit a95ce1e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
7 changes: 5 additions & 2 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ module.exports = {
loaders: [
{ test: /\.css$/, loader: ExtractTextPlugin.extract(
"style-loader",
"css-loader?sourceMap"
"css-loader?sourceMap",
{
publicPath: "../"
}
)},
{ test: /\.png$/, loader: "file-loader" }
]
},
plugins: [
new ExtractTextPlugin("[name].css?[hash]-[chunkhash]-[name]", {
new ExtractTextPlugin("css/[name].css?[hash]-[chunkhash]-[name]", {
disable: false,
allChunks: true
}),
Expand Down
24 changes: 16 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,29 @@ function ExtractTextPlugin(id, filename, options) {
}
module.exports = ExtractTextPlugin;

function mergeOptions(a, b) {
if(!b) return a;
Object.keys(b).forEach(function(key) {
a[key] = b[key];
});
return a;
};

ExtractTextPlugin.loader = function(options) {
return require.resolve("./loader") + (options ? "?" + JSON.stringify(options) : "");
};

ExtractTextPlugin.extract = function(before, loader) {
if(loader) {
ExtractTextPlugin.extract = function(before, loader, options) {
if(typeof loader === "string") {
return [
ExtractTextPlugin.loader({omit: before.split("!").length, extract: true, remove: true}),
ExtractTextPlugin.loader(mergeOptions({omit: before.split("!").length, extract: true, remove: true}, options)),
before,
loader
].join("!");
} else {
loader = before;
return [
ExtractTextPlugin.loader({remove: true}),
ExtractTextPlugin.loader(mergeOptions({remove: true}, options)),
loader
].join("!");
}
Expand All @@ -64,17 +72,17 @@ ExtractTextPlugin.prototype.loader = function(options) {
return ExtractTextPlugin.loader(options);
};

ExtractTextPlugin.prototype.extract = function(before, loader) {
if(loader) {
ExtractTextPlugin.prototype.extract = function(before, loader, options) {
if(typeof loader === "string") {
return [
this.loader({omit: before.split("!").length, extract: true, remove: true}),
this.loader(mergeOptions({omit: before.split("!").length, extract: true, remove: true}, options)),
before,
loader
].join("!");
} else {
loader = before;
return [
this.loader({remove: true}),
this.loader(mergeOptions({remove: true}, options)),
loader
].join("!");
}
Expand Down
3 changes: 2 additions & 1 deletion loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ module.exports.pitch = function(request, preReq, data) {

if(query.extract !== false) {
var childFilename = __dirname + " " + request;
var publicPath = typeof query.publicPath === "string" ? query.publicPath : this._compilation.outputOptions.publicPath
var outputOptions = {
filename: childFilename,
publicPath: this._compilation.outputOptions.publicPath
publicPath: publicPath
};
var childCompiler = this._compilation.createChildCompiler("extract-text-webpack-plugin", outputOptions);
childCompiler.apply(new NodeTemplatePlugin(outputOptions));
Expand Down

1 comment on commit a95ce1e

@necolas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't forget to add to README too :)

Please sign in to comment.