Support writing HTML output to a configurable filename

This commit is contained in:
Charles Blaxland 2014-08-13 21:08:56 +10:00
parent 5285daf0df
commit 94250b9f10
3 changed files with 33 additions and 8 deletions

View File

@ -57,6 +57,8 @@ You can pass a hash of configuration options to `HtmlWebpackPlugin`.
Allowed values are as follows:
- `title`: The title to use for the generated HTML document.
- `filename`: The file to write the HTML to. Defaults to `index.html`.
You can specify a subdirectory here too (eg: `assets/admin.html`).
Here's an example webpack config illustrating how to use these options:
```javascript
@ -68,7 +70,8 @@ Here's an example webpack config illustrating how to use these options:
},
plugins: [
new HtmlWebpackPlugin({
title: 'My App'
title: 'My App',
filename 'assets/admin.html'
})
]
}

View File

@ -23,8 +23,8 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
var htmlTemplateContent = fs.readFileSync(templateFile, 'utf8');
var html = tmpl(htmlTemplateContent, templateParams);
var outputPath = path.join(compiler.options.output.path, 'index.html');
compiler.assets['index.html'] = {
var outputFilename = self.options.filename || 'index.html';
compiler.assets[outputFilename] = {
source: function() {
return html;
},

View File

@ -6,12 +6,12 @@ var HtmlWebpackPlugin = require('../index.js');
var OUTPUT_DIR = path.join(__dirname, '..', 'dist');
function testHtmlPlugin(webpackConfig, expectedResults, done) {
var outputHtmlFile = path.join(OUTPUT_DIR, 'index.html');
function testHtmlPlugin(webpackConfig, expectedResults, done, outputFile) {
outputFile = outputFile || 'index.html';
webpack(webpackConfig, function(err, stats) {
expect(err).toBeFalsy();
expect(stats.hasErrors()).toBe(false);
var htmlContent = fs.readFileSync(outputHtmlFile).toString();
var htmlContent = fs.readFileSync(path.join(OUTPUT_DIR, outputFile)).toString();
for (var i = 0; i < expectedResults.length; i++) {
var expectedResult = expectedResults[i];
if (expectedResult instanceof RegExp) {
@ -109,7 +109,7 @@ describe('HtmlWebpackPlugin', function() {
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: path.join('assets', 'index_bundle.js')
filename: 'assets/index_bundle.js'
},
plugins: [new HtmlWebpackPlugin()]
}, ['<script src="assets/index_bundle.js"'], done);
@ -120,7 +120,7 @@ describe('HtmlWebpackPlugin', function() {
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: path.join('assets', 'index_bundle.js'),
filename: 'assets/index_bundle.js',
publicPath: 'http://cdn.example.com/'
},
plugins: [new HtmlWebpackPlugin()]
@ -138,4 +138,26 @@ describe('HtmlWebpackPlugin', function() {
}, ['<title>My Cool App</title>'], done);
});
it('allows you to configure the output filename', function(done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [new HtmlWebpackPlugin({filename: 'test.html'})]
}, ['<script src="index_bundle.js"'], done, 'test.html');
});
it('allows you to configure the output filename with a path', function(done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [new HtmlWebpackPlugin({filename: 'assets/test.html'})]
}, ['<script src="index_bundle.js"'], done, 'assets/test.html');
});
});