-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Due to indexhtml-webpack-plugin masking some errors, the previous html-plugin will be used, until unbroken-dome/indexhtml-webpack-plugin#11 is solved.
- Loading branch information
Showing
5 changed files
with
112 additions
and
24 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
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 |
---|---|---|
|
@@ -16,27 +16,28 @@ | |
"less" | ||
], | ||
"author": "Nihey Takizawa <[email protected]>", | ||
"license": "MIT", | ||
"license": "CC0-1.0", | ||
"dependencies": { | ||
"babel-core": "^6.3.13", | ||
"babel-loader": "^6.2.0", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babel-preset-stage-0": "^6.3.13", | ||
"cheerio": "^0.20.0", | ||
"config": "^1.16.0", | ||
"css-loader": "^0.23.0", | ||
"extract-text-webpack-plugin": "^1.0.1", | ||
"file-loader": "^0.9.0", | ||
"html-minifier": "^3.0.1", | ||
"img-loader": "^1.1.0", | ||
"json-loader": "^0.5.2", | ||
"less": "^2.5.1", | ||
"less-loader": "^2.2.0", | ||
"md5": "^2.1.0", | ||
"node-libs-browser": "^1.0.0", | ||
"style-loader": "^0.13.0", | ||
"webpack": "^1.10.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^1.6.0", | ||
"html-loader": "^0.4.3", | ||
"indexhtml-webpack-plugin": "^0.1.9" | ||
"eslint": "^1.6.0" | ||
} | ||
} |
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,80 @@ | ||
/* XXX This code is experimental, it may be subjected to great changes on the | ||
* near future. | ||
*/ | ||
var $ = require('cheerio'), | ||
md5 = require('md5'), | ||
htmlMinify = require('html-minifier').minify, | ||
fs = require('fs'), | ||
path = require('path'); | ||
|
||
function HtmlParserWebpackPlugin() { | ||
this.files = Array.prototype.slice.call(arguments); | ||
this.production = false; | ||
} | ||
|
||
HtmlParserWebpackPlugin.prototype.apply = function(compiler) { | ||
this.compiler = compiler; | ||
// XXX I could try to find a better way to check this. | ||
// Note: 'compiler.options.debug' options is not ovewritten by cli options | ||
// when it is on 'webpack.config.js'. | ||
this.production = process.argv.indexOf('--optimize-minimize') !== -1 || | ||
process.argv.indexOf('-p') !== -1; | ||
this.files.forEach(function(file) { | ||
compiler.plugin('emit', this.compile.bind(this, file)); | ||
}, this); | ||
}; | ||
|
||
/* Iterate through each element href and src tags and parse them */ | ||
HtmlParserWebpackPlugin.prototype.compile = function(file, compilation, callback) { | ||
var input = path.join(this.compiler.context, file); | ||
var html = $.load(fs.readFileSync(input)); | ||
html('[href],[src]').each(function(i, element) { | ||
this.parse(element, 'href', compilation); | ||
this.parse(element, 'src', compilation); | ||
}.bind(this)); | ||
|
||
var htmlSource = html.html(); | ||
if (this.production) { | ||
htmlSource = htmlMinify(htmlSource, { | ||
preserveLineBreaks: false, | ||
removeComments: true, | ||
removeCommentsFromCDATA: true, | ||
collapseWhitespace: true, | ||
}); | ||
} | ||
this.createFile(compilation, file, htmlSource); | ||
callback(); | ||
}; | ||
|
||
/* If the elementh with the tag includes a '!' prefix, it should be emitted */ | ||
HtmlParserWebpackPlugin.prototype.parse = function(element, attr, compilation) { | ||
var file = $(element).attr(attr); | ||
// If the element does not have a valid href or src, or if it does not | ||
// require bundling, don't do anything. | ||
if (!file || file[0] !== '!') { | ||
return; | ||
} | ||
$(element).attr(attr, this.emit(file.substring(1), compilation)); | ||
}; | ||
|
||
/* Create a file on the dist directory for the resource with a md5 name */ | ||
HtmlParserWebpackPlugin.prototype.emit = function(url, compilation) { | ||
var ext = url.split('.')[1]; | ||
var source = fs.readFileSync(path.join(this.compiler.context, url)); | ||
var filename = md5(source) + '.' + ext; | ||
this.createFile(compilation, filename, source); | ||
return filename; | ||
}; | ||
|
||
HtmlParserWebpackPlugin.prototype.createFile = function(compilation, name, source) { | ||
compilation.assets[name] = { | ||
source: function() { | ||
return source; | ||
}, | ||
size: function() { | ||
return source.length; | ||
}, | ||
}; | ||
}; | ||
|
||
module.exports = HtmlParserWebpackPlugin; |
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