Improve caching
This commit is contained in:
parent
cbce36cb58
commit
4cfef8a12c
|
|
@ -10,14 +10,15 @@ module.exports = {
|
|||
module: {
|
||||
loaders: [
|
||||
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
|
||||
{ test: /\.png$/, loader: 'file-loader' }
|
||||
{ test: /\.png$/, loader: 'file-loader' },
|
||||
{ test: /\.html$/, loader: 'html-loader' },
|
||||
]
|
||||
},
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin({
|
||||
filename: 'html-loader.html',
|
||||
favicon: 'favicon.ico',
|
||||
template: 'html!./template.html'
|
||||
template: 'template.html'
|
||||
}),
|
||||
new ExtractTextPlugin('styles.css')
|
||||
]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
require('./main.css');
|
||||
var h1 = document.createElement('h1');
|
||||
h1.innerHTML = 'Hello world';
|
||||
document.body.appendChild(h1);
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 53 KiB |
|
|
@ -0,0 +1,3 @@
|
|||
body {
|
||||
background: snow;
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
<h2>Partial</h2>
|
||||
<img src="logo.png">
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// Webpack require:
|
||||
var partial = require('./partial.html');
|
||||
|
||||
// Export a function / promise / or a string:
|
||||
module.exports = '<html><head></head><body>' + new Date() + partial + '</body></html>';
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
var HtmlWebpackPlugin = require('../..');
|
||||
var ExtractTextPlugin = require('extract-text-webpack-plugin');
|
||||
module.exports = {
|
||||
entry: './example.js',
|
||||
output: {
|
||||
path: __dirname + '/dist',
|
||||
publicPath: '',
|
||||
filename: 'bundle.js'
|
||||
},
|
||||
module: {
|
||||
loaders: [
|
||||
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
|
||||
{ test: /\.png$/, loader: 'file-loader' },
|
||||
{ test: /\.html$/, loader: 'html-loader' }
|
||||
]
|
||||
},
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin({
|
||||
template: 'template.js'
|
||||
}),
|
||||
new ExtractTextPlugin('styles.css')
|
||||
]
|
||||
};
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
require('./main.css');
|
||||
var h1 = document.createElement('h1');
|
||||
h1.innerHTML = 'Hello world!';
|
||||
document.body.appendChild(h1);
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 53 KiB |
|
|
@ -0,0 +1,3 @@
|
|||
body {
|
||||
background: snow;
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
<h2>Partial</h2>
|
||||
<img src="logo.png">
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Example template</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<%= require('html!./partial.html') %>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
var HtmlWebpackPlugin = require('../..');
|
||||
var ExtractTextPlugin = require('extract-text-webpack-plugin');
|
||||
module.exports = {
|
||||
entry: './example.js',
|
||||
output: {
|
||||
path: __dirname + '/dist',
|
||||
publicPath: '',
|
||||
filename: 'bundle.js'
|
||||
},
|
||||
module: {
|
||||
loaders: [
|
||||
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
|
||||
{ test: /\.png$/, loader: 'file-loader' }
|
||||
]
|
||||
},
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin({
|
||||
template: 'template.html'
|
||||
}),
|
||||
new ExtractTextPlugin('styles.css')
|
||||
]
|
||||
};
|
||||
29
index.js
29
index.js
|
|
@ -23,6 +23,7 @@ function HtmlWebpackPlugin(options) {
|
|||
compile: true,
|
||||
favicon: false,
|
||||
minify: false,
|
||||
cache: true,
|
||||
chunks: 'all',
|
||||
excludeChunks: [],
|
||||
title: 'Webpack App'
|
||||
|
|
@ -84,7 +85,10 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
|
|||
}
|
||||
// Once everything is compiled evaluate the html factory
|
||||
// and replace it with its content
|
||||
return self.evaluateCompilationResult(compilation, resultAsset);
|
||||
if (self.built || !self.options.cache) {
|
||||
self.evaluatedCompilationResult = self.evaluateCompilationResult(compilation, resultAsset);
|
||||
}
|
||||
return self.evaluatedCompilationResult;
|
||||
})
|
||||
// Execute the template
|
||||
.then(function(compilationResult) {
|
||||
|
|
@ -153,15 +157,6 @@ HtmlWebpackPlugin.prototype.compileTemplate = function(template, outputFilename,
|
|||
new webpack.DefinePlugin({ HTML_WEBPACK_PLUGIN : 'true' })
|
||||
);
|
||||
|
||||
// Create a subCache (copied from https://github.com/SanderSpies/extract-text-webpack-plugin/blob/master/loader.js)
|
||||
childCompiler.plugin('compilation', function(compilation) {
|
||||
if(compilation.cache) {
|
||||
if(!compilation.cache[compilerName]) {
|
||||
compilation.cache[compilerName] = {};
|
||||
}
|
||||
compilation.cache = compilation.cache[compilerName];
|
||||
}
|
||||
});
|
||||
// Compile and return a promise
|
||||
return new Promise(function (resolve, reject) {
|
||||
childCompiler.runAsChild(function(err, entries, childCompilation) {
|
||||
|
|
@ -173,10 +168,12 @@ HtmlWebpackPlugin.prototype.compileTemplate = function(template, outputFilename,
|
|||
|
||||
reject('Child compilation failed:\n' + errorDetails);
|
||||
} else {
|
||||
resolve(compilation.assets[outputFilename]);
|
||||
this.built = this.hash !== entries[0].hash;
|
||||
this.hash = entries[0].hash;
|
||||
resolve(childCompilation.assets[outputFilename]);
|
||||
}
|
||||
});
|
||||
});
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -187,6 +184,7 @@ HtmlWebpackPlugin.prototype.evaluateCompilationResult = function(compilation, co
|
|||
if(!compilationResult) {
|
||||
return Promise.reject('The child compilation didn\'t provide a result');
|
||||
}
|
||||
|
||||
var source = compilationResult.source();
|
||||
// The LibraryTemplatePlugin stores the template result in a local variable.
|
||||
// To extract the result during the evaluation this part has to be removed.
|
||||
|
|
@ -247,6 +245,9 @@ HtmlWebpackPlugin.prototype.executeTemplate = function(templateFunction, chunks,
|
|||
*/
|
||||
HtmlWebpackPlugin.prototype.postProcessHtml = function(html, assets) {
|
||||
var self = this;
|
||||
if (typeof html !== 'string') {
|
||||
return Promise.reject('Expected html to be a string but got ' + JSON.stringify(html));
|
||||
}
|
||||
return Promise.resolve()
|
||||
// Inject
|
||||
.then(function() {
|
||||
|
|
@ -296,7 +297,7 @@ HtmlWebpackPlugin.prototype.addFileToAssets = function(filename, compilation) {
|
|||
* Helper to sort chunks
|
||||
*/
|
||||
HtmlWebpackPlugin.prototype.sortChunks = function(chunks, sortMode) {
|
||||
// Sort mode auto by default:
|
||||
// Sort mode auto by default:
|
||||
if (typeof sortMode === 'undefined' || sortMode === 'auto') {
|
||||
return chunks.sort(function orderEntryLast(a, b) {
|
||||
if (a.entry !== b.entry) {
|
||||
|
|
|
|||
|
|
@ -14,6 +14,10 @@ module.exports = function (source) {
|
|||
if (allLoadersButThisOne.length > 0) {
|
||||
return source;
|
||||
}
|
||||
// Skip .js files
|
||||
if (/\.js$/.test(this.request)) {
|
||||
return source;
|
||||
}
|
||||
// Use underscore for a minimalistic loader
|
||||
var options = loaderUtils.parseQuery(this.query);
|
||||
var template = _.template(source, options);
|
||||
|
|
|
|||
Loading…
Reference in New Issue