Add loader tests and cleanup redundant code.

This commit is contained in:
Kees Kluskens 2015-09-05 21:36:37 +02:00 committed by Jan Nicklas
parent 55fcb6d939
commit 989a09f97a
3 changed files with 52 additions and 10 deletions

View File

@ -31,12 +31,6 @@ function HtmlWebpackPlugin(options) {
if(this.options.template.indexOf('!') === -1) {
this.options.template = require.resolve('./loader.js') + '!' + path.resolve(this.options.template);
}
// Resolve template path
this.options.template = this.options.template.replace(
/(\!)([^\/\\][^\!\?]+|[^\/\\!?])($|\?.+$)/,
function(match, prefix, filepath, postfix) {
return prefix + path.resolve(filepath) + postfix;
});
}
HtmlWebpackPlugin.prototype.apply = function(compiler) {
@ -185,10 +179,6 @@ HtmlWebpackPlugin.prototype.evaluateCompilationResult = function(compilation, co
if(!compilationResult) {
return Promise.reject('The child compilation didn\'t provide a result');
}
if(typeof source === 'string') {
// If the compilation result is already evaluted return it
return Promise.resolve(source);
}
// Strip the leading 'var '
var source = compilationResult.source().substr(3);
// Evaluate code and cast to string

View File

@ -39,6 +39,7 @@
"jshint": "^2.8.0",
"rimraf": "^2.4.1",
"style-loader": "^0.12.3",
"underscore-template-loader": "^0.5.1",
"url-loader": "^0.5.6",
"webpack": "^1.10.1"
},

View File

@ -74,6 +74,40 @@ describe('HtmlWebpackPlugin', function() {
}, ['<script src="util_bundle.js"', '<script src="app_bundle.js"'], null, done);
});
it('allows you to use a custom loader', function(done) {
testHtmlPlugin({
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
inject: false,
template: 'underscore-template-loader!' + path.join(__dirname, 'fixtures/underscore.html')
})]
},
['<script src="app_bundle.js', 'Some unique text'], null, done);
});
it('works when using html-loader', function(done) {
testHtmlPlugin({
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
inject: true,
template: 'html-loader!' + path.join(__dirname, 'fixtures/plain.html')
})]
},
['<script src="app_bundle.js"'], null, done);
});
it('allows you to specify your own HTML template file', function(done) {
testHtmlPlugin({
entry: {
@ -175,6 +209,23 @@ describe('HtmlWebpackPlugin', function() {
}, ['<script src="app_bundle.js"'], null, done);
});
it('allows you to disable injection', function (done) {
testHtmlPlugin({
entry: {
util: path.join(__dirname, 'fixtures/util.js'),
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
inject: false,
template: path.join(__dirname, 'fixtures/plain.html')
})]
}, ['<body>\n</body>'], null, done);
});
it('allows you to specify your own HTML template function', function(done) {
testHtmlPlugin({
entry: {app: path.join(__dirname, 'fixtures/index.js')},