Advertisement
giordanocardillo

webpack.config

Feb 1st, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const path = require('path')
  2. const webpack = require('webpack')
  3. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  4. const CleanWebpackPlugin = require('clean-webpack-plugin')
  5. const noop = require('noop-webpack-plugin')
  6.  
  7. process.env.NODE_ENV = process.env.NODE_ENV || 'development'
  8.  
  9. module.exports = (env) => {
  10.   const isProduction = env === 'production'
  11.  
  12.   const clean = new CleanWebpackPlugin([path.resolve(__dirname, 'public', 'generated')])
  13.  
  14.   const extractText = new ExtractTextPlugin({
  15.     filename: 'bundle.min.css',
  16.   })
  17.  
  18.   const uglifyJS = isProduction ? new webpack.optimize.UglifyJsPlugin({
  19.     comments: false,
  20.     mangle: true,
  21.     sourceMap: true,
  22.   }) : noop()
  23.  
  24.   const providePlugin = new webpack.ProvidePlugin({
  25.     $: 'jquery',
  26.     jQuery: 'jquery',
  27.     Raphael: 'raphael',
  28.     'window.$': 'jquery',
  29.     'window.jQuery': 'jquery',
  30.     'window.Raphael': 'raphael',
  31.   })
  32.  
  33.   return {
  34.     context: path.resolve(__dirname, 'client'),
  35.     entry: ['babel-polyfill', './entry.js'],
  36.     output: {
  37.       filename: 'bundle.min.js',
  38.       path: path.resolve(__dirname, 'public', 'generated'),
  39.     },
  40.     resolve: {
  41.       extensions: ['.js'],
  42.       alias: {
  43.         Templates: path.resolve(__dirname, 'client', 'templates'),
  44.       },
  45.     },
  46.     module: {
  47.       loaders: [
  48.         {
  49.           test: require.resolve('jquery'),
  50.           use: [{
  51.             loader: 'expose-loader',
  52.             options: 'jQuery',
  53.           }, {
  54.             loader: 'expose-loader',
  55.             options: '$',
  56.           }],
  57.         },
  58.         {
  59.           test: /\.js$/,
  60.           exclude: /(node_modules|public)/,
  61.           loader: 'babel-loader',
  62.         }, {
  63.           test: /\.(hbs|handlebars)$/,
  64.           exclude: /(node_modules|public)/,
  65.           loader: 'handlebars-loader',
  66.         },
  67.         {
  68.           test: /\.(ttf|eot|svg|woff2?)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
  69.           loader: 'file-loader',
  70.           options: {
  71.             name: '[name].[ext]',
  72.             publicPath: '',
  73.             outputPath: 'fonts/',
  74.           },
  75.         }, {
  76.           test: /\.(png|jpg|gif)$/,
  77.           loader: 'file-loader',
  78.           options: {
  79.             name: '[name].[ext]',
  80.             outputPath: 'images/',
  81.             publicPath: '',
  82.           },
  83.         },
  84.         {
  85.           test: /\.(less)$/,
  86.           loader: extractText.extract({
  87.             publicPath: '/',
  88.             fallback: 'style-loader',
  89.             use: [
  90.               {
  91.                 loader: 'css-loader',
  92.                 options: {
  93.                   sourceMap: true,
  94.                   minimize: true,
  95.                 },
  96.               },
  97.               {
  98.                 loader: 'less-loader',
  99.                 options: {
  100.                   sourceMap: true,
  101.                   minimize: true,
  102.                 },
  103.               },
  104.             ],
  105.           }),
  106.         },
  107.       ],
  108.     },
  109.     plugins: [
  110.       clean,
  111.       uglifyJS,
  112.       extractText,
  113.       providePlugin,
  114.     ],
  115.     devtool: isProduction ? 'source-map' : 'inline-source-map',
  116.     devServer: {
  117.       contentBase: path.join(__dirname, 'public'),
  118.       publicPath: '/generated/',
  119.       historyApiFallback: true,
  120.       port: 3000,
  121.     },
  122.   }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement