/*
  在主模板里加上代码 __webpack_require__ 导出为一个全局变量 reference_for_redevelopment
*/
'use strict'

const Template = require('webpack/lib/Template')

class ExportWebpackRequireMainTemplatePlugin {
  constructor(options) {
    this.options = options
  }
  apply (mainTemplate) {

    mainTemplate.hooks.beforeStartup.tap('ExportWebpackRequireMainTemplatePlugin', (source, chunk, hash) => {
      if (this.options.name) {
        return Template.asString([
          `window["${this.options.name}"] = __webpack_require__;`,
          source
        ])
      }
      return source
    })

  }
}
module.exports = ExportWebpackRequireMainTemplatePlugin
