|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var fs = require('fs'); |
| 4 | +var path = require('path'); |
| 5 | +var inlineSource = require('inline-source'); |
| 6 | + |
| 7 | +function readdir ( directory, callback ) { |
| 8 | + var result = []; |
| 9 | + if (fs.existsSync(directory)) { |
| 10 | + var files = fs.readdirSync(directory); |
| 11 | + files.forEach(function ( file ) { |
| 12 | + var filepath = path.join(directory, file); |
| 13 | + var stats = fs.statSync(filepath); |
| 14 | + if (stats.isFile()) { |
| 15 | + result.push(file); |
| 16 | + } |
| 17 | + if (stats.isDirectory()) { |
| 18 | + readdir(filepath).forEach(function ( filename ) { |
| 19 | + result.push(path.join(file, filename)); |
| 20 | + }); |
| 21 | + } |
| 22 | + }); |
| 23 | + } |
| 24 | + return result; |
| 25 | +} |
| 26 | + |
| 27 | +function inline ( file, option ) { |
| 28 | + fs.writeFileSync(file, inlineSource.sync(file, option)); |
| 29 | +} |
| 30 | + |
| 31 | +function HtmlInlineSourceWebpackPlugin ( configs ) { |
| 32 | + if (!(this instanceof HtmlInlineSourceWebpackPlugin)) { |
| 33 | + throw 'Cannot call HtmlInlineSourceWebpackPlugin as a function.'; |
| 34 | + } |
| 35 | + if (typeof configs !== 'object') { |
| 36 | + configs = {}; |
| 37 | + } |
| 38 | + if (configs instanceof Array) { |
| 39 | + this.configs = configs; |
| 40 | + } else { |
| 41 | + this.configs = [configs]; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +HtmlInlineSourceWebpackPlugin.prototype.apply = function ( compiler ) { |
| 46 | + var configs = this.configs; |
| 47 | + var length = configs.length; |
| 48 | + compiler.plugin('done', function ( stats ) { |
| 49 | + var compiler = stats.compilation.compiler; |
| 50 | + var outputPath = path.join(compiler.context, compiler.outputPath); |
| 51 | + var defaults = { |
| 52 | + compress : false, |
| 53 | + rootpath : outputPath, |
| 54 | + handlers : function ( source, context ) { |
| 55 | + if (source && source.fileContent && !source.content) { |
| 56 | + if (source.extension == 'css') { |
| 57 | + source.tag = 'style'; |
| 58 | + source.content = source.fileContent.replace(/url\(.*?\)/g, function ( match ) { |
| 59 | + var url = match.slice(4, -1); |
| 60 | + if (/^http(s?):\/\/|^data:image/.test(url)) { |
| 61 | + return match; |
| 62 | + } else { |
| 63 | + if (url.indexOf('?')) { |
| 64 | + url = url.split('?')[0]; |
| 65 | + } |
| 66 | + return 'url(' + path.join(path.relative(outputPath, source.filepath), url) + ')'; |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + if (source.extension == 'js') { |
| 71 | + source.content = source.fileContent.trim(); |
| 72 | + } |
| 73 | + } |
| 74 | + }, |
| 75 | + }; |
| 76 | + readdir(outputPath).forEach(function ( filename ) { |
| 77 | + if (/\.html$/.test(filename)) { |
| 78 | + for (var i = 0; i < length; i++) { |
| 79 | + var config = configs[i]; |
| 80 | + if (config.test instanceof RegExp) { |
| 81 | + if (config.test.test(filename)) { |
| 82 | + return inline( |
| 83 | + path.join(outputPath, filename), |
| 84 | + config.option || defaults |
| 85 | + ); |
| 86 | + } |
| 87 | + } else { |
| 88 | + return inline( |
| 89 | + path.join(outputPath, filename), |
| 90 | + config.option || defaults |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + }); |
| 96 | + }); |
| 97 | +}; |
| 98 | + |
| 99 | +module.exports = HtmlInlineSourceWebpackPlugin; |
0 commit comments