index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var sourceMappingURL = require('source-map-url')
  2. function InlineManifestPlugin (options) {
  3. this.options = extend({
  4. name: 'webpackManifest'
  5. }, options || {})
  6. }
  7. InlineManifestPlugin.prototype.apply = function (compiler) {
  8. var me = this
  9. compiler.plugin('compilation', function (compilation) {
  10. compilation.plugin('html-webpack-plugin-before-html-generation', function (htmlPluginData, callback) {
  11. var name = me.options.name
  12. // HtmlWebpackPlugin use the 'manifest' name as HTML5's app cache manifest
  13. // so we can't use the same name
  14. if (name === 'manifest') {
  15. throw new Error('[InlineManifestWebpackPlugin]: name can\'t be "manifest".')
  16. }
  17. var webpackManifest = []
  18. var assets = htmlPluginData.assets
  19. var manifestPath = (compilation.chunks.filter(function (chunk) {
  20. return chunk.name === 'manifest'
  21. })[0] || {files: []}).files[0]
  22. if (manifestPath) {
  23. webpackManifest.push('<script>')
  24. webpackManifest.push(sourceMappingURL.removeFrom(compilation.assets[manifestPath].source()))
  25. webpackManifest.push('</script>')
  26. var manifestIndex = assets.js.indexOf(assets.publicPath + manifestPath)
  27. if (manifestIndex >= 0) {
  28. assets.js.splice(manifestIndex, 1)
  29. delete assets.chunks.manifest
  30. }
  31. }
  32. assets[name] = webpackManifest.join('')
  33. callback(null, htmlPluginData)
  34. })
  35. })
  36. }
  37. function extend (base) {
  38. var i = 1
  39. var len = arguments.length
  40. for (; i < len; i++) {
  41. var obj = arguments[i]
  42. for (var key in obj) {
  43. if (obj.hasOwnProperty(key)) {
  44. base[key] = obj[key]
  45. }
  46. }
  47. }
  48. return base
  49. }
  50. module.exports = InlineManifestPlugin