import * as path from 'path';
import webpack from 'webpack';

function createWebpackConfig(options) {
  return {
    context: path.dirname(options.entry),
    entry: `./${path.basename(options.entry).replace(/\.js$/, '')}`,
    watch: !!options.watch,

    module: {
      loaders: [
        { test: /\.json$/, loader: require.resolve('json-loader') },
        {
          test: /\.js$/,
          exclude: /node_modules/,
          loader: require.resolve('babel-loader'),
          query: {
            presets: require.resolve('babel-preset-extplug'),
          },
        },
      ],
    },

    output: {
      path: path.dirname(options.output),
      filename: path.basename(options.output),
      libraryTarget: 'amd',
    },

    externals: [
      'jquery',
      'underscore',
      'backbone',
      'plug-modules',
      'lang/Lang',
      (context, request, cb) => {
        if (/^plug\//.test(request)) {
          cb(null, `amd plug-modules!${request}`);
        } else if (/^extplug\//.test(request)) {
          cb(null, `amd ${request}`);
        } else {
          cb();
        }
      },
    ],

    plugins: [
      options.minify && new webpack.optimize.UglifyJsPlugin()
    ].filter(Boolean),
  };
}

export default function build({ entry, output, ...opts }) {
  const config = createWebpackConfig({
    entry,
    output,
    ...opts,
  });

  return new Promise((resolve, reject) => {
    webpack(config, (err, stats) => {
      if (err) {
        reject(err);
      } else {
        resolve(stats);
      }
    });
  });
}

@@ -23,16 +23,41 @@
 'path';%0A
+import pify from 'pify';%0A
 import w
@@ -1212,16 +1212,17 @@
 Plugin()
+,
 %0A    %5D.f
@@ -1393,184 +1393,85 @@
 %0A%0A  
-return new Promise((resolve, reject) =%3E %7B%0A    webpack(config, (err, stats) =%3E %7B%0A      if (err) %7B%0A        reject(err);%0A      %7D else %7B%0A        resolve(stats);%0A      %7D%0A    %7D);%0A  %7D
+const compiler = webpack(config);%0A  return pify(compiler.run.bind(compiler))(
 );%0A%7D

use pify to promisify webpack