UNPKG

2.83 kBJavaScriptView Raw
1const webpack = require( 'webpack' )
2const HtmlWebpackPlugin = require( 'html-webpack-plugin' )
3const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
4const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
5const merge = require( 'webpack-merge' )
6const path = require( 'path' )
7const baseConfig = require( './base' )
8const cwd = process.cwd()
9
10const _ = {
11 cwd: function( filepath ) {
12 return path.resolve( cwd, filepath )
13 },
14 project: function ( filepath ) {
15 return path.resolve( __dirname, '../../', filepath )
16 }
17}
18
19module.exports = function( options ) {
20 const outputPath = options.dist
21 const template = options.template
22 const resolveFallback = typeof options.resolveFallback === 'string'
23 ? [ options.resolveFallback ]
24 : ( options.resolveFallback || [] )
25
26 const fallback = [].push.apply(
27 [ _.cwd( 'play/node_modules' ) ],
28 resolveFallback
29 )
30
31 const config = merge.smart( baseConfig, {
32 entry: {
33 app: _.project( 'lib/entries/app.js' ),
34 preview: [ _.project( 'lib/entries/preview.js' ) ]
35 },
36 module: {
37 loaders: [
38 {
39 test: /\.html$/,
40 loader: 'raw-loader',
41 }
42 ]
43 },
44 resolve: {
45 root: [
46 _.project( 'node_modules' ),
47 _.cwd( 'node_modules' )
48 ],
49 fallback: fallback,
50 packageMains: [ 'play:main', 'jsnext:main', 'browser', 'main' ],
51 },
52 resolveLoader: {
53 alias: {
54 text: 'raw-loader'
55 },
56 root: [
57 _.project( 'node_modules' ),
58 ],
59 },
60 output: {
61 path: outputPath,
62 filename: '[name]-[hash:8].js',
63 publicPath: './'
64 },
65 plugins: [
66 new HtmlWebpackPlugin( {
67 filename: 'index.html',
68 chunks: [ 'app' ],
69 template: _.project( 'lib/index.html' ),
70 } ),
71 new HtmlWebpackPlugin( {
72 filename: 'preview.html',
73 chunks: [ 'preview' ],
74 template: template,
75 } ),
76 new webpack.ProvidePlugin( {
77 play: require.resolve( 'regular-play' ),
78 } ),
79 new webpack.DefinePlugin( {
80 __PLAY_ROOT__: JSON.stringify( _.cwd( 'play' ) ),
81 __DEFAULT_LAYOUT__: JSON.stringify( options.mobile ? 'mobile' : 'desktop' ),
82 } ),
83 new FriendlyErrorsWebpackPlugin( {
84 clearConsole: false,
85 } ),
86 new CaseSensitivePathsPlugin(),
87 ]
88 } )
89
90 if ( options.mode === 'development' ) {
91 config.plugins.push( new webpack.HotModuleReplacementPlugin() )
92 config.entry.preview.push( require.resolve( 'webpack-hot-middleware/client' ) + '?reload=true' )
93 }
94
95 if ( options.mode === 'production' ) {
96 config.plugins.push( new webpack.optimize.UglifyJsPlugin( {
97 sourceMap: false,
98 compressor: {
99 warnings: false,
100 conditionals: true,
101 unused: true,
102 comparisons: true,
103 sequences: true,
104 dead_code: true,
105 evaluate: true,
106 if_return: true,
107 join_vars: true,
108 negate_iife: false
109 },
110 output: {
111 comments: false
112 }
113 } ) )
114 }
115
116 return config
117}