{"size": 2103, "ext": "js", "max_stars_repo_path": "api/hooks/webpack/index.js", "max_stars_repo_name": "mrdrewgurley/nopass-sails-react-webpack-seed", "max_stars_repo_stars_event_min_datetime": "2018-06-20T17:47:36.000Z", "max_stars_repo_stars_event_max_datetime": "2018-06-27T14:43:49.000Z", "max_issues_repo_path": "api/hooks/webpack/index.js", "max_issues_repo_name": "mrdrewgurley/nopass-sails-react-webpack-seed", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/hooks/webpack/index.js", "max_forks_repo_name": "mrdrewgurley/nopass-sails-react-webpack-seed", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 32.859375, "max_line_length": 110, "alphanum_fraction": 0.6219686163}
const webpack = require('webpack')

/**
 * Hooks.webpack
 * @module Hooks
 * @desc A hook to compile assets using Webhook.
 * @docs https://sailsjs.com/documentation/concepts/extending-sails/hooks
 */

module.exports = function defineWebpackHook (sails) {
  return {
    /**
     * Runs when a Sails app loads/lifts.
     *
     * @param {Function} done
     */
    initialize: function (done) {

      // In production mode, just compile the assets and continue lifting Sails.
      if (process.env.NODE_ENV === 'production') {
        return webpack(sails.config.webpack, done)
      }

      // Otherwise, create a compiler so that we can watch files.
      var compiler = webpack(sails.config.webpack)

      // Trigger the first compile, start watching files, and return
      // an instance we can use to stop the watcher when Sails lowers.
      var watcher = compiler.watch({
        // Wait a bit after a change is detected before recompiling,
        // in case other changes come in.
        aggregateTimeout: 300,
        // Ignore changes to files in node_modules, even if they're
        // imported by our scripts.
        ignored: /node_modules/
        // poll: true
        // ^^^^^ If native watching isn't working, uncomment the above.
      }, function (err, stats) {

        // If the compilation generates errors and we're not already logging all the output from
        // the watcher, log the errors to the console.
        if (stats.hasErrors() && sails.config.log.level !== 'silly' && sails.config.log.level !== 'verbose') {
          sails.log.verbose('Webpack encountered errors while compiling.  Full output:')
          sails.log.error(stats.toString())
        }

        // Otherwise output the result of the compilation.
        else {
          sails.log.verbose('Webpack compiled files and outputted:')
          sails.log.verbose(stats.toString())
        }
      })

      // When Sails lowers, stop watching files.
      sails.on('lower', function () {
        watcher && watcher.close()
      })

      // Continue lifting Sails.
      return done()
    }
  }
}
