extract-text-webpack-plugin
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,1 +0,2 @@ | ||
require("./style2.css"); | ||
require("./style2.css"); | ||
require(["./dep2"]); |
@@ -13,6 +13,8 @@ var ExtractTextPlugin = require("../"); | ||
{ test: /\.css$/, loaders: [ | ||
plugin.loader({remove:true, extract: false}), | ||
"style-loader", | ||
plugin.loader, | ||
plugin.loader(), | ||
"css-loader" | ||
]} | ||
]}, | ||
{ test: /\.png$/, loader: "file-loader" } | ||
] | ||
@@ -19,0 +21,0 @@ }, |
94
index.js
@@ -6,51 +6,85 @@ /* | ||
var RawSource = require("webpack/lib/RawSource"); | ||
var Template = require("webpack/lib/Template"); | ||
var async = require("async"); | ||
var nextId = 0; | ||
function ExtractTextPlugin(filename, includeChunks) { | ||
function ExtractTextPlugin(id, filename, options) { | ||
if(typeof filename !== "string") { | ||
options = filename; | ||
filename = id; | ||
id = ++nextId; | ||
} | ||
if(!options) options = {}; | ||
this.filename = filename; | ||
this.includeChunks = includeChunks || false; | ||
this.id = ++nextId; | ||
this.loader = ExtractTextPlugin.loader + "?" + this.id | ||
this.options = options; | ||
this.id = id; | ||
} | ||
module.exports = ExtractTextPlugin; | ||
ExtractTextPlugin.loader = require.resolve("./loader"); | ||
ExtractTextPlugin.loader = function(options) { | ||
return require.resolve("./loader") + "?" + JSON.stringify(options); | ||
}; | ||
ExtractTextPlugin.prototype.loader = function(options) { | ||
options = JSON.parse(JSON.stringify(options || {})); | ||
options.id = this.id; | ||
return ExtractTextPlugin.loader(options); | ||
}; | ||
ExtractTextPlugin.prototype.apply = function(compiler) { | ||
var options = this.options; | ||
compiler.plugin("compilation", function(compilation) { | ||
compilation.plugin("normal-module-loader", function(loaderContext, module) { | ||
loaderContext[__dirname] = function(text) { | ||
module.meta[__dirname] = text; | ||
loaderContext[__dirname] = function(text, opt) { | ||
module.meta[__dirname] = { | ||
text: text, | ||
options: opt | ||
}; | ||
return options.allChunks || module.meta[__dirname + "/extract"]; | ||
}; | ||
loaderContext[__dirname + "?" + this.id] = function(text) { | ||
module.meta[__dirname + "?" + this.id] = text; | ||
}.bind(this); | ||
}.bind(this)); | ||
var filename = this.filename; | ||
var includeChunks = this.includeChunks; | ||
var id = this.id; | ||
compilation.plugin("after-optimize-chunks", function(chunks) { | ||
chunks.forEach(function(chunk) { | ||
if(chunk.initial) { | ||
var text = []; | ||
chunk.modules.forEach(function(module) { | ||
if(module.meta[__dirname]) { | ||
text.push(module.meta[__dirname]); | ||
module._source = new RawSource("// text extracted by extract-text-webpack-plugin\n" + | ||
"module.exports=\"\";"); | ||
compilation.plugin("optimize-tree", function(chunks, modules, callback) { | ||
var texts = {}; | ||
async.forEach(chunks, function(chunk, callback) { | ||
var shouldExtract = !!(options.allChunks || chunk.initial); | ||
var text = []; | ||
async.forEach(chunk.modules, function(module, callback) { | ||
var meta = module.meta[__dirname]; | ||
if(meta) { | ||
var wasExtracted = typeof meta.text === "string"; | ||
if(shouldExtract !== wasExtracted) { | ||
module.meta[__dirname + "/extract"] = shouldExtract | ||
compilation.buildModule(module, function(err) { | ||
if(err) return callback(err); | ||
meta = module.meta[__dirname]; | ||
if(typeof meta.text !== "string") return callback(new Error(module.identifier() + " doesn't export text")); | ||
text.push(meta.text); | ||
callback(); | ||
}); | ||
} else { | ||
text.push(meta.text); | ||
callback(); | ||
} | ||
if(module.meta[__dirname + "?" + id]) { | ||
text.push(module.meta[__dirname + "?" + id]); | ||
module._source = new RawSource("// text extracted by extract-text-webpack-plugin\n" + | ||
"module.exports=\"\";"); | ||
} | ||
}); | ||
var file = filename.replace(/\[name\]/g, chunk.name); | ||
text = text.join(""); | ||
} else callback(); | ||
}, function(err) { | ||
if(err) return callback(err); | ||
if(text.length > 0) { | ||
var file = filename.replace(Template.REGEXP_NAME, chunk.name); | ||
texts[file] = (texts[file] || []).concat(text); | ||
} | ||
callback(); | ||
}.bind(this)); | ||
}.bind(this), function(err) { | ||
if(err) return callback(err); | ||
Object.keys(texts).forEach(function(file) { | ||
var text = texts[file].join(""); | ||
this.assets[file] = new RawSource(text); | ||
} | ||
}, this); | ||
}.bind(this)); | ||
callback(); | ||
}.bind(this)); | ||
}); | ||
}.bind(this)); | ||
}; |
@@ -5,7 +5,62 @@ /* | ||
*/ | ||
var loaderUtils = require("loader-utils"); | ||
var NodeTemplatePlugin = require("webpack/lib/node/NodeTemplatePlugin"); | ||
var NodeTargetPlugin = require("webpack/lib/node/NodeTargetPlugin"); | ||
var LibraryTemplatePlugin = require("webpack/lib/LibraryTemplatePlugin"); | ||
var SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin"); | ||
var LimitChunkCountPlugin = require("webpack/lib/optimize/LimitChunkCountPlugin"); | ||
module.exports = function(source) { | ||
this.cacheable && this.cacheable(); | ||
var text = this.exec(source, this.request); | ||
this[__dirname + this.query](text); | ||
return source; | ||
}; | ||
module.exports.pitch = function(request, preReq, data) { | ||
this.cacheable && this.cacheable(); | ||
var query = loaderUtils.parseQuery(this.query); | ||
if(this[__dirname](null, query)) { | ||
if(query.remove) { | ||
var resultSource = "// removed by extract-text-webpack-plugin"; | ||
} else { | ||
var resultSource = "// text extracted by extract-text-webpack-plugin\n" + | ||
"module.exports=\"\";"; | ||
} | ||
if(query.extract !== false) { | ||
var childFilename = __dirname + request; | ||
var outputOptions = { | ||
filename: childFilename, | ||
publicPath: this._compilation.outputOptions.publicPath | ||
}; | ||
var childCompiler = this._compilation.createChildCompiler("extract-text-webpack-plugin", outputOptions); | ||
childCompiler.apply(new NodeTemplatePlugin(outputOptions)); | ||
childCompiler.apply(new LibraryTemplatePlugin(null, "commonjs2")); | ||
childCompiler.apply(new NodeTargetPlugin()); | ||
childCompiler.apply(new SingleEntryPlugin(this.context, "!!" + request)); | ||
childCompiler.apply(new LimitChunkCountPlugin({ maxChunks: 1 })); | ||
var subCache = "subcache " + __dirname + " " + request; | ||
childCompiler.plugin("compilation", function(compilation) { | ||
if(compilation.cache) { | ||
if(!compilation.cache[subCache]) | ||
compilation.cache[subCache] = {}; | ||
compilation.cache = compilation.cache[subCache]; | ||
} | ||
}); | ||
var source; | ||
childCompiler.plugin("after-compile", function(compilation, callback) { | ||
source = compilation.assets[childFilename].source(); | ||
delete compilation.assets[childFilename]; | ||
callback(); | ||
}.bind(this)) | ||
var callback = this.async(); | ||
childCompiler.runAsChild(function(err, entries, compilation) { | ||
if(err) return callback(err); | ||
var text = this.exec(source, request); | ||
this[__dirname](text, query); | ||
callback(null, resultSource); | ||
}.bind(this)); | ||
} else { | ||
this[__dirname]("", query); | ||
return resultSource; | ||
} | ||
} | ||
}; |
{ | ||
"name": "extract-text-webpack-plugin", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": "Tobias Koppers @sokra", | ||
@@ -9,2 +9,9 @@ "description": "Extract text from bundle into a file.", | ||
}, | ||
"dependencies": { | ||
"async": "0.2.x", | ||
"loader-utils": "0.2.x" | ||
}, | ||
"devDependencies": { | ||
"file-loader": "*" | ||
}, | ||
"homepage": "http://github.com/webpack/extract-text-webpack-plugin", | ||
@@ -11,0 +18,0 @@ "repository": { |
@@ -12,3 +12,3 @@ # extract text plugin for webpack | ||
"style-loader", | ||
ExtractTextPlugin.loader, | ||
ExtractTextPlugin.loader(), | ||
"css-loader" | ||
@@ -27,9 +27,10 @@ ]} | ||
``` javascript | ||
new ExtractTextPlugin(filename: string, includeChunks: boolean) | ||
new ExtractTextPlugin([id: string], filename: string, [options]) | ||
``` | ||
* `filename` the filename of the result file. May contain `[name]`, `[hash]` or `[id]`. | ||
* `includeChunks` if false (default) only texts from entry chunks is extracted. If true texts from all children is merged into the file for the entry chunk. (TODO) | ||
* `id` TODO | ||
* `filename` the filename of the result file. May contain `[name]`. | ||
* `options` TODO | ||
There is also a `loader` property on the instance. You should use this if you have more than one ExtractTextPlugin. | ||
There is also a `loader` function on the instance. You should use this if you have more than one ExtractTextPlugin. | ||
@@ -36,0 +37,0 @@ ## License |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
7667
14
176
37
3
1
1
+ Addedasync@0.2.x
+ Addedloader-utils@0.2.x