html-webpack-plugin/spec/HtmlWebpackPluginSpec.js

140 lines
4.3 KiB
JavaScript

var path = require('path');
var fs = require('fs');
var webpack = require('webpack');
var rm_rf = require('rimraf');
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');
webpack(webpackConfig, function(err, stats) {
expect(err).toBeFalsy();
expect(stats.hasErrors()).toBe(false);
var htmlContent = fs.readFileSync(outputHtmlFile).toString();
for (var i = 0; i < expectedResults.length; i++) {
var expectedResult = expectedResults[i];
if (expectedResult instanceof RegExp) {
expect(htmlContent).toMatch(expectedResult);
} else {
expect(htmlContent).toContain(expectedResult);
}
}
done();
});
}
describe('HtmlWebpackPlugin', function() {
beforeEach(function(done) {
rm_rf(OUTPUT_DIR, done);
});
it('generates a default index.html file for a single entry point', function(done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [new HtmlWebpackPlugin()]
}, ['<script src="index_bundle.js"'], done);
});
it('generates a default index.html file with multiple entry points', 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()]
}, ['<script src="util_bundle.js"', '<script src="app_bundle.js"'], done);
});
it('allows you to specify your own HTML template', function(done) {
testHtmlPlugin({
entry: {
app: path.join(__dirname, 'fixtures', 'index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin({template: path.join(__dirname, 'fixtures', 'test.html')})]
},
['<script src="app_bundle.js"', 'Some unique text'], done);
});
it('works with source maps', function(done) {
testHtmlPlugin({
devtool: 'sourcemap',
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [new HtmlWebpackPlugin()]
}, ['<script src="index_bundle.js"'], done);
});
it('handles hashes in bundle filenames', function(done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle_[hash].js'
},
plugins: [new HtmlWebpackPlugin()]
}, [/<script src="index_bundle_[0-9a-f]+\.js"/], done);
});
it('prepends the webpack public path to script src', function(done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js',
publicPath: 'http://cdn.example.com/assets/'
},
plugins: [new HtmlWebpackPlugin()]
}, ['<script src="http://cdn.example.com/assets/index_bundle.js"'], done);
});
it('handles subdirectories in the webpack output bundles', function(done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: path.join('assets', 'index_bundle.js')
},
plugins: [new HtmlWebpackPlugin()]
}, ['<script src="assets/index_bundle.js"'], done);
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: path.join('assets', 'index_bundle.js'),
publicPath: 'http://cdn.example.com/'
},
plugins: [new HtmlWebpackPlugin()]
}, ['<script src="http://cdn.example.com/assets/index_bundle.js"'], done);
});
it('allows you to configure the title of the generated HTML page', function(done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [new HtmlWebpackPlugin({title: 'My Cool App'})]
}, ['<title>My Cool App</title>'], done);
});
});