/* * Integration and unit tests for all features but caching */ /* eslint-env jasmine */ 'use strict'; // Workaround for css-loader issue // https://github.com/webpack/css-loader/issues/144 if (!global.Promise) { require('es6-promise').polyfill(); } var path = require('path'); var fs = require('fs'); var webpack = require('webpack'); var rimraf = require('rimraf'); var _ = require('lodash'); var CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin'); var ExtractTextPlugin = require('extract-text-webpack-plugin'); var extractTextPluginMajorVersion = require('extract-text-webpack-plugin/package.json').version.split('.')[0]; var HtmlWebpackPlugin = require('../index.js'); if (Number(extractTextPluginMajorVersion) > 1) { var extractOriginal = ExtractTextPlugin.extract; ExtractTextPlugin.extract = function (fallback, use) { return extractOriginal({ fallback: fallback, use: use }); }; } var OUTPUT_DIR = path.join(__dirname, '../dist'); jasmine.getEnv().defaultTimeoutInterval = 30000; function testHtmlPlugin (webpackConfig, expectedResults, outputFile, done, expectErrors, expectWarnings) { outputFile = outputFile || 'index.html'; webpack(webpackConfig, function (err, stats) { expect(err).toBeFalsy(); var compilationErrors = (stats.compilation.errors || []).join('\n'); if (expectErrors) { expect(compilationErrors).not.toBe(''); } else { expect(compilationErrors).toBe(''); } var compilationWarnings = (stats.compilation.warnings || []).join('\n'); if (expectWarnings) { expect(compilationWarnings).not.toBe(''); } else { expect(compilationWarnings).toBe(''); } if (outputFile instanceof RegExp) { var matches = Object.keys(stats.compilation.assets).filter(function (item) { return outputFile.test(item); }); expect(matches.length).toBe(1); outputFile = matches[0]; } expect(outputFile.indexOf('[hash]') === -1).toBe(true); var outputFileExists = fs.existsSync(path.join(OUTPUT_DIR, outputFile)); expect(outputFileExists).toBe(true); if (!outputFileExists) { return done(); } var htmlContent = fs.readFileSync(path.join(OUTPUT_DIR, outputFile)).toString(); var chunksInfo; for (var i = 0; i < expectedResults.length; i++) { var expectedResult = expectedResults[i]; if (expectedResult instanceof RegExp) { expect(htmlContent).toMatch(expectedResult); } else if (typeof expectedResult === 'object') { if (expectedResult.type === 'chunkhash') { if (!chunksInfo) { chunksInfo = getChunksInfoFromStats(stats); } var chunkhash = chunksInfo[expectedResult.chunkName].hash; expect(htmlContent).toContain(expectedResult.containStr.replace('%chunkhash%', chunkhash)); } } else { expect(htmlContent).toContain(expectedResult.replace('%hash%', stats.hash)); } } done(); }); } function getChunksInfoFromStats (stats) { var chunks = stats.compilation.getStats().toJson().chunks; var chunksInfo = {}; for (var i = 0; i < chunks.length; i++) { var chunk = chunks[i]; var chunkName = chunk.names[0]; if (chunkName) { chunksInfo[chunkName] = chunk; } } return chunksInfo; } describe('HtmlWebpackPlugin', function () { beforeEach(function (done) { rimraf(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()] }, [/
[\s]*