// @flow

import path from 'path'
import webpack from 'webpack'
import ExtractTextPlugin from 'extract-text-webpack-plugin'
import HappyPack from 'happypack'
import ProgressBarPlugin from 'progress-bar-webpack-plugin'
import nodeExternals from 'webpack-node-externals'
import '../getenv'
import buildDir from '../buildDir'

const root = path.resolve(__dirname, '..')
const srcDir = path.resolve(root, 'src')
const globalCSS = path.join(srcDir, 'styles', 'global')

const config = {
  context: root,
  devtool: process.env.WEBPACK_DEVTOOL || 'source-map',
  entry: {
    prerender: './src/server',
  },
  target: 'node',
  node: {
    __dirname: false,
    __filename: false,
  },
  output: {
    path: buildDir,
    chunkFilename: '[name]_[chunkhash].js',
    filename: '[name].js',
    libraryTarget: 'commonjs2',
    publicPath: '/static/',
  },
  // ignore anything that throws warnings & doesn't affect the view
  externals: [
    nodeExternals({
      modulesDir: path.join(root, 'node_modules'),
    }),
    // (context: string, request: string, callback: (error?: ?Error, result?: ?string) => any): any => {
    //   const match = /^meteor\/(.*)$/.exec(request)
    //   if (match) {
    //     return callback(null, 'var Package.' + match[1].replace(/\//g, '.'))
    //   }
    //   callback()
    // },
  ],
  plugins: [
    new webpack.NoErrorsPlugin(),
    new ExtractTextPlugin('/static/[name].css'),
    new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
    new webpack.DefinePlugin({
      '__CLIENT__': false,
      '__PRODUCTION__': true,
      'Meteor.isClient': false,
      'Meteor.isCordova': false,
      'Meteor.isServer': true,
      'process.env.TARGET': JSON.stringify(process.env.TARGET),
      'process.env.NODE_ENV': JSON.stringify('production'),
      // uncomment this line to hard-disable full SSR
      // 'process.env.DISABLE_FULL_SSR': JSON.stringify('1'),
    }),
    new HappyPack({
      id: '1', // https://github.com/amireh/happypack/issues/88
      cache: false,
      loaders: ['babel'],
      threads: 4,
    }),
  ],
  module: {
    loaders: [
      { test: /\.json$/, loader: 'json-loader' },
      { test: /\.txt$/, loader: 'raw-loader' },
      { test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/, loader: 'url-loader?limit=10000' },
      { test: /\.(eot|ttf|wav|mp3)$/, loader: 'file-loader' },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract(
          'fake-style',
          'css?modules&importLoaders=1&localIdentName=[name]_[local]_[hash:base64:5]!postcss'
        ),
        include: srcDir,
        exclude: globalCSS,
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('fake-style', 'css'),
        include: globalCSS,
      },
      {
        test: /\.js$/,
        loader: 'happypack/loader',
        include: srcDir,
      },
    ],
  },
}

/* istanbul ignore next */
if (!process.env.CI) config.plugins.push(new ProgressBarPlugin())

export default config

@@ -1006,19 +1006,16 @@
  %7D),%0A   
- //
  (contex
@@ -1015,24 +1015,16 @@
 (context
-: string
 , reques
@@ -1028,16 +1028,8 @@
 uest
-: string
 , ca
@@ -1038,69 +1038,94 @@
 back
-: (error?: ?Error, result?: ?string) =%3E any): any =%3E %7B
+) =%3E %7B%0A      // require anything in build/meteor/bundle/programs/server/* via node
 %0A   
- //
    c
@@ -1142,17 +1142,16 @@
  = /
-%5E
 meteor%5C/
 (.*)
@@ -1150,12 +1150,36 @@
 or%5C/
-(
+bundle%5C/programs%5C/server%5C/
 .*
-)
 $/.e
@@ -1194,19 +1194,16 @@
 est)%0A   
- //
    if (m
@@ -1213,19 +1213,16 @@
 h) %7B%0A   
- //
      ret
@@ -1245,68 +1245,44 @@
 l, '
-var Package.' + match%5B1%5D.replace(/%5C//g, '.')
+commonjs ./' + match%5B0%5D
 )%0A   
- //
    %7D%0A   
  // 
@@ -1277,19 +1277,16 @@
    %7D%0A   
- //
    callb
@@ -1294,19 +1294,16 @@
 ck()%0A   
- //
  %7D,%0A  %5D,

make requires from meteor bundle webpack externals