summaryrefslogtreecommitdiff
path: root/client/web/webpack.config.js
blob: d9425c19865614258551213f3568ba8c3620de80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* eslint-disable */
const enableOfflinePlugin = false

const __DEV__ = process.env.NODE_ENV === 'development'
const __OFFLINE__ = enableOfflinePlugin && !__DEV__

const path = require('path')
const glob = require('glob')
const webpack = require('webpack')
const config = require('./shared.webpack.config.js')

const HtmlWebpackPlugin = require('html-webpack-plugin')
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin')

const CopyWebpackPlugin = require('copy-webpack-plugin')
const OfflinePlugin = require('offline-plugin')

const vendorConfig = require('./vendor.webpack.config.js')
const outputPath = path.join(__dirname, '/build/')


const addAssetHtmlFiles = Object.keys(vendorConfig.entry).map((name) => {
  const fileGlob = `${name}*.dll.js`
  const paths = glob.sync(path.join(vendorConfig.output.path, fileGlob))
  if (paths.length === 0) throw new Error(`Could not find ${fileGlob}!`)
  if (paths.length > 1) throw new Error(`Too many files for ${fileGlob}! You should clean and rebuild.`)
  return {
    filepath: require.resolve(paths[0]),
    includeSourcemap: false,
    outputPath: 'javascript/vendor',
    publicPath: '/javascript/vendor',
  }
})

const plugins = [
  ...Object.keys(vendorConfig.entry).map(name =>
    new webpack.DllReferencePlugin({
      context: process.cwd(),
      manifest: require(path.join(vendorConfig.output.path, `${name}-manifest.json`)),
    })),

  new webpack.DefinePlugin({
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
    __DEV__,
    __OFFLINE__,
  }),
  new HtmlWebpackPlugin({
    filename: 'index.html',
    template: 'web/templates/index.ejs',
  }),
  new AddAssetHtmlPlugin(addAssetHtmlFiles),

  new CopyWebpackPlugin([
    // Workaround for AddAssetHtmlPlugin not copying compressed .gz files
    { context: 'web/vendor/', from: '*.js.gz', to: 'javascript/vendor/' },
  ]),

  // Split out any remaining node modules
  new webpack.optimize.CommonsChunkPlugin({
    name: 'vendor/lib',
    minChunks: module => module.context && module.context.indexOf('node_modules/') !== -1,
  }),

  ...(__DEV__ ? [] : [
    ...config.productionPlugins,

    // Add any app-specific production plugins here.
  ])
]

// If offline plugin is enabled, it has to come last.
if (__OFFLINE__) plugins.push(new OfflinePlugin())

module.exports = {
  devServer: {
    contentBase: outputPath,
  },
  entry: {
    app: path.join(__dirname, '../index.web.js')
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        // TODO: Set up react-hot-loader during development.
        loaders: [ 'babel-loader?cacheDirectory=true' ],
      },
      ...config.loaders,
    ]
  },
  output: {
    path: outputPath,
    filename: 'javascript/[name]-[hash:16].js',
    publicPath: '/'
  },
  plugins: plugins,
  resolve: {
    alias: {
      'react-native': 'react-native-web'
    },
    extensions: [".web.js", ".js", ".json"]
  }
};