-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(options): support htmlWebpackPlugin options & replace [name] in … (
#34) * feat(options): support htmlWebpackPlugin options & replace [name] in path * fix: add test case for html-webpack-plugin options
- Loading branch information
1 parent
08eeb6c
commit 78bdb91
Showing
11 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/> | ||
<title>title for a.html</title> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/> | ||
<title>title for b.html</title> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/> | ||
<title>title for c.html</title> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("Module A"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("Module B"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("Module C"); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const path = require('path'); | ||
const webpack = require('webpack'); | ||
const MultipageWebpackPlugin = require('../../src/plugin.js'); | ||
|
||
function resolve(dir) { | ||
return path.join(__dirname, dir); | ||
} | ||
|
||
let config = { | ||
context: __dirname, | ||
entry: { | ||
a: './src/a/a.js', | ||
b: './src/b/b.js', | ||
c: './src/c/c.js' | ||
}, | ||
output: { | ||
filename: '[name].chunk.js', | ||
path: resolve('./dist') | ||
}, | ||
plugins: [ | ||
new MultipageWebpackPlugin({ | ||
// replace [name] in template path | ||
htmlTemplatePath: resolve('./html-templates/[name].html'), | ||
// some other options in htmlWebpackPlugin | ||
htmlWebpackPluginOptions: { | ||
minify: { | ||
removeComments: true, | ||
collapseWhitespace: true, | ||
removeAttributeQuotes: true | ||
}, | ||
favicon: resolve('./static/favicon.ico') | ||
} | ||
}) | ||
] | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as path from 'path'; | ||
import Promise from 'bluebird'; | ||
import test from 'ava'; | ||
import { | ||
runWebpackCompilerMemoryFs, | ||
getEntryKeysFromStats, | ||
testFs | ||
} from './utils.js'; | ||
|
||
const simpleConfig = require('../examples/html-webpack-plugin-options/webpack.config.js'); | ||
const fs = testFs; // Use shared memoryfs instance | ||
|
||
const simpleExamplePath = path.resolve(__dirname, '../examples/html-webpack-plugin-options'); | ||
const webpackBuildPath = path.resolve(simpleExamplePath, './dist'); | ||
|
||
// Convienence to use async await with these common fs functions | ||
const readdir = Promise.promisify(fs.readdir, {context: fs}); | ||
const readFile = Promise.promisify(fs.readFile, {context: fs}); | ||
const fsReaddir = Promise.promisify(fs.readdir, {context: fs}); | ||
const fsReadFile = Promise.promisify(fs.readFile, {context: fs}); | ||
const fsStat = Promise.promisify(fs.stat, {context: fs}); | ||
const fsExists = Promise.promisify(fs.exists, {context: fs}); | ||
|
||
|
||
let webpackBuildStats = null; | ||
|
||
test.before('run webpack build first', async t => { | ||
webpackBuildStats = await runWebpackCompilerMemoryFs(simpleConfig); | ||
}); | ||
|
||
// Run | ||
test('it should run successfully', async t => { | ||
let {stats, warnings, errors} = webpackBuildStats; | ||
|
||
t.falsy(stats.hasWarnings() && errors.hasWarnings()); | ||
}); | ||
|
||
// Test some htmlWebpackPlugin options | ||
test('each template should use options of htmlWebpackPlugin correctly', async t => { | ||
const stats = webpackBuildStats.stats; | ||
let entries = await getEntryKeysFromStats(stats); | ||
|
||
// test minify option | ||
let allTemplatesMinified = true; | ||
// test favicon option | ||
let faviconAdded = true; | ||
|
||
for(let entryName of entries) { | ||
let templateContent = await readFile(path.join(webpackBuildPath, "templates", entryName, "index.html")); | ||
templateContent = templateContent.toString(); | ||
|
||
// whitespaces between tags have been collapsed | ||
if (/>\s</.test(templateContent)) { | ||
allTemplatesMinified = false; | ||
break; | ||
} | ||
|
||
// favicon has been added | ||
if (!/\<link rel=\"shortcut icon\" href=favicon\.ico\>/.test(templateContent)) { | ||
faviconAdded = false; | ||
break; | ||
} | ||
} | ||
|
||
t.true(allTemplatesMinified); | ||
t.true(faviconAdded); | ||
}); |